From 9a004f27cae4c97980d3bb6a9b55a5da143be980 Mon Sep 17 00:00:00 2001 From: chrys87 Date: Thu, 21 Jul 2016 11:35:16 +0200 Subject: [PATCH] add getPrevWord and fix some bugs --- src/fenrir-package/utils/word_utils.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/fenrir-package/utils/word_utils.py b/src/fenrir-package/utils/word_utils.py index 50919212..de4488c8 100644 --- a/src/fenrir-package/utils/word_utils.py +++ b/src/fenrir-package/utils/word_utils.py @@ -1,5 +1,13 @@ #!/bin/python +def getPrevWord(currX,currY, currText): + if currText == '': + return -1, -1, '' + x, y, word = getCurrentWord(currX,currY,currText) + if (x == currX) or (y == currY) and (word == ''): + return currX, currY, '' + return getCurrentWord(x - 2, y, currText) + def getCurrentWord(currX,currY, currText): if currText == '': return -1, -1, '' @@ -8,7 +16,15 @@ def getCurrentWord(currX,currY, currText): wrappedLines = currText.split('\n') wordFound = False currWord = '' - currLine = wrappedLines[y].replace("\t"," ") + if x <= 0: + if y != 0: + y -= 1 + currLine = wrappedLines[y].replace("\t"," ") + else: + return currX, currY, '' + x = len(currLine) - 1 + else: + currLine = wrappedLines[y].replace("\t"," ") while not wordFound: x = currLine[:x].rfind(" ") if x == -1: @@ -30,7 +46,7 @@ def getCurrentWord(currX,currY, currText): currLine = wrappedLines[y].replace("\t"," ") else: return currX, currY, '' - x = len(wrappedLines[y]) - 1 + x = len(currLine) - 1 else: x -= 1 return x, y, currWord