fenrir/src/fenrir-package/utils/word_utils.py

107 lines
2.9 KiB
Python
Raw Normal View History

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, currWord = getCurrentWord(currX,currY,currText)
2016-07-21 09:41:02 -04:00
wrappedLines = currText.split('\n')
if (currWord == ''):
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, currWord = getCurrentWord(x, y, currText)
if currWord == '':
2016-07-21 09:41:02 -04:00
return currX, currY, ''
return x, y, currWord
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
2016-07-22 08:33:36 -04:00
def getNextWord(currX,currY, currText):
if currText == '':
return -1, -1, ''
x = currX
y = currY
wrappedLines = currText.split('\n')
wordFound = False
currWord = ''
currLine = wrappedLines[y].replace("\t"," ")
while not wordFound:
xtmp = 0
if x + 1 >= len(currLine):
if y < len(wrappedLines):
y += 1
currLine = wrappedLines[y].replace("\t"," ")
else:
return currX, currY, ''
x = 0
else:
x += 1
xtmp = x
x = currLine[x:].find(" ")
if x == -1:
x = len(currLine)
continue
else:
if xtmp != 0:
2016-07-22 08:33:36 -04:00
xtmp += 1
x += xtmp
if x + 1 < len(currLine):
wordEnd = currLine[x + 1:].find(" ")
else:
wordEnd = -1
if wordEnd == -1:
wordEnd = len(currLine)
else:
wordEnd += x + 1
if wordEnd >= len(currLine) and y + 1 >= len(wrappedLines):
return currX, currY, ''
currWord = currLine[x:wordEnd]
wordFound = currWord.strip(" \t\n") != ''
if not wordFound:
x = wordEnd
return x, y, currWord