2016-07-20 08:53:00 -04:00
|
|
|
#!/bin/python
|
|
|
|
|
2016-07-21 05:35:16 -04:00
|
|
|
def getPrevWord(currX,currY, currText):
|
|
|
|
if currText == '':
|
|
|
|
return -1, -1, ''
|
|
|
|
x, y, word = getCurrentWord(currX,currY,currText)
|
2016-07-21 09:41:02 -04:00
|
|
|
wrappedLines = currText.split('\n')
|
|
|
|
if (word == ''):
|
2016-07-21 05:35:16 -04:00
|
|
|
return currX, currY, ''
|
2016-07-21 09:41:02 -04:00
|
|
|
while True:
|
|
|
|
if x < 2:
|
|
|
|
if y != 0:
|
|
|
|
y -= 1
|
|
|
|
else:
|
|
|
|
return currX, currY, ''
|
|
|
|
x = len(wrappedLines[y]) - 1
|
|
|
|
else:
|
|
|
|
x -= 1
|
|
|
|
if wrappedLines[y] != '':
|
|
|
|
break
|
|
|
|
x, y, word = getCurrentWord(x, y, currText)
|
|
|
|
if word == '':
|
|
|
|
return currX, currY, ''
|
|
|
|
return x, y, word
|
2016-07-21 05:35:16 -04:00
|
|
|
|
2016-07-20 10:03:22 -04:00
|
|
|
def getCurrentWord(currX,currY, currText):
|
2016-07-20 09:25:28 -04:00
|
|
|
if currText == '':
|
|
|
|
return -1, -1, ''
|
2016-07-20 10:11:05 -04:00
|
|
|
x = currX
|
|
|
|
y = currY
|
|
|
|
wrappedLines = currText.split('\n')
|
2016-07-20 08:53:00 -04:00
|
|
|
wordFound = False
|
|
|
|
currWord = ''
|
2016-07-21 09:41:02 -04:00
|
|
|
currLine = wrappedLines[y].replace("\t"," ")
|
|
|
|
if currLine[x] == ' ' and x > 1:
|
|
|
|
x = x - 2
|
2016-07-20 08:53:00 -04:00
|
|
|
while not wordFound:
|
|
|
|
x = currLine[:x].rfind(" ")
|
|
|
|
if x == -1:
|
|
|
|
x = 0
|
|
|
|
else:
|
|
|
|
x += 1
|
2016-07-21 09:41:02 -04:00
|
|
|
wordEnd = currLine[x + 1:].find(" ")
|
2016-07-20 08:53:00 -04:00
|
|
|
if wordEnd == -1:
|
2016-07-21 09:41:02 -04:00
|
|
|
wordEnd = len(currLine)
|
2016-07-20 08:53:00 -04:00
|
|
|
else:
|
|
|
|
wordEnd += x + 1
|
|
|
|
currWord = currLine[x:wordEnd]
|
|
|
|
wordFound = currWord.strip(" \t\n") != ''
|
|
|
|
if wordFound:
|
|
|
|
break
|
|
|
|
if x == 0:
|
|
|
|
if y != 0:
|
|
|
|
y -= 1
|
|
|
|
currLine = wrappedLines[y].replace("\t"," ")
|
|
|
|
else:
|
2016-07-20 10:03:22 -04:00
|
|
|
return currX, currY, ''
|
2016-07-21 09:41:02 -04:00
|
|
|
x = len(wrappedLines[y]) - 1
|
2016-07-20 08:53:00 -04:00
|
|
|
else:
|
|
|
|
x -= 1
|
2016-07-20 10:11:05 -04:00
|
|
|
return x, y, currWord
|
2016-07-21 09:41:02 -04:00
|
|
|
|
|
|
|
currText = " das ist ein test\ntest das\ntesttest\n\ntest"
|
|
|
|
currY = 4
|
|
|
|
currX = 3
|
|
|
|
currX, currY, word = getCurrentWord(currX,currY,currText)
|
|
|
|
#currX, currY, word = getPrevWord(currX,currY,currText)
|
|
|
|
print(currX, currY, word)
|