-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
177 lines (151 loc) · 6.1 KB
/
utils.py
File metadata and controls
177 lines (151 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import re
import numpy as np
def readLinesFromFile( fileDirectory ) :
with open(fileDirectory) as file:
lines = file.readlines();
return lines;
def writeLinesToFile( fileDirectory , lines):
with open( fileDirectory ) as file:
file.writelines(lines)
def getCorruptedWordWithCorrectWordInSentence(sentence):
errors = re.findall(r'<ERR targ=[\?\'\-\w\s,]+>[\?\'\-\w\s,]+</ERR>', sentence, re.I)
corruptedWords = []
correctWords = []
for error in errors:
correctWords.append(getCorrectWordFromErrorTags(error))
corruptedWords.append(getWrongWordFromErrorTags(error))
return (corruptedWords, correctWords)
def getErrors(sentence):
return re.findall(r'<ERR targ=[\?\'\-\w\s,]+>[\?\'\-\w\s,]+</ERR>', sentence, re.I)
def getCorrections(sentence):
tokens = sentence.split(" ");
corrections = list();
for token in tokens:
if token.startswith("ERROR:"):
corrections.append(token)
return corrections;
#gets sentence with error tags and return
def escapeErrorsInSentence(sentence):
correctedSentence = sentence;
errors = re.findall(r'<ERR targ=[\?\'\-\w\s,]+>[\?\'\-\w\s,]+</ERR>', correctedSentence, re.I)
for error in errors:
correctedSentence = getCorrectedSentence(correctedSentence, error);
correctedSentence = correctedSentence.replace(".", " ");
correctedSentence = correctedSentence.replace(",", " ");
correctedSentence.lower();
return correctedSentence.strip();
def getCorrectedSentence(sentence, error):
sentenceParts = sentence.split(error);
if len(sentenceParts) == 2:
return sentenceParts[0] + getCorrectWordFromErrorTags(error) + sentenceParts[1];
elif len(sentenceParts) > 2:
correntSentence = sentenceParts[0] + getCorrectWordFromErrorTags(error) + sentenceParts[1];
for i in range(2, len(sentenceParts)):
correntSentence = correntSentence + error + sentenceParts[i];
return correntSentence;
else:
return sentenceParts[0] + getCorrectWordFromErrorTags(error) + sentenceParts[1];
def getEstimatedWordFromCorrectionTags(correctionString):
word = " ";
if correctionString.startswith("ERROR:"):
temp = correctionString.split("*");
word= temp[3];
return word;
# gets correct word from error tags
def getCorrectWordFromErrorTags(errorString):
matchObj = re.search(r'targ=[\?\'\-\w\s,]+>', errorString, re.I);
matchString = matchObj.group()
##HERE and THERE
temp = matchString[5:-1]
tempList = temp.split(" ");
if len(tempList) > 1:
temp2 = ""
for temp3 in tempList:
temp2 = temp2 + "-" + temp3
return temp2;
else:
return temp
def getWrongWordFromErrorTags(errorString):
matchObj = re.search(r'>[\?\'\-\w\s,]+<', errorString, re.I);
matchString = matchObj.group()
temp = matchString[1:-1]
tempList = temp.strip().split(" ");
if len(tempList) > 1:
temp2 = ""
for temp3 in tempList:
temp2 = temp2 + "-" + temp3
return temp2.strip();
else:
return temp.strip();
def editDistance(word1, word2):
word1 = word1.strip();
word1 = word1.lower();
word2 = word2.strip();
word2 = word2.lower();
lengthWord1 = len(word1);
lengthWord2 = len(word2);
#init. edit distance array
distanceTable = np.zeros((lengthWord1+1, lengthWord2+1));
for i in range(lengthWord1+1):
distanceTable[i,0] = i;
for i in range(lengthWord2+1):
distanceTable[0,i] = i;
#generating distances
for i in range (1, lengthWord1+1, 1):
for j in range(1, lengthWord2+1, 1):
if word1[i-1] == word2[j-1]:
distanceTable[i][j] = distanceTable[i-1][j-1]
else:
minDistance = distanceTable[i-1][j-1]
if minDistance > distanceTable[i][j-1]:
minDistance = distanceTable[i][j-1]
if minDistance > distanceTable[i-1][j]:
minDistance = distanceTable[i-1][j]
distanceTable[i][j] = minDistance + 1;
return distanceTable[lengthWord1][lengthWord2]
def getDeletedCharacterKey(corruptedWord, word):
deletedCharacter = "";
for i in range(len(word)):
if i == len(corruptedWord):
deletedCharacter = word[i];
break;
elif corruptedWord[i] != word[i]:
deletedCharacter = word[i];
break;
return deletedCharacter;
def getSubstitutedCharacterKey(corruptedWord, word):
wrongCharacter = "";
correctCharacter = "";
for i in range(len(corruptedWord)):
if corruptedWord[i] != word[i]:
wrongCharacter = corruptedWord[i];
correctCharacter = word[i];
break;
key = correctCharacter.strip() + " " + wrongCharacter.strip();
return key;
def getInsertedCharacterKey(corruptedWord, word):
insertedCharacter = "";
for i in range(len(corruptedWord)):
if i == len(word):
insertedCharacter = corruptedWord[i];
break;
elif corruptedWord[i] != word[i]:
insertedCharacter = corruptedWord[i];
break;
key = insertedCharacter;
return key;
def evaluateSentence(rawSentence, estimatedSentece):
errorsWithTags = getErrors(rawSentence);
correctionsWithTags = getCorrections(estimatedSentece);
errorCount = len(errorsWithTags);
trueEstimationCount = 0;
correctWords = list();
estimatedWords = list();
for errorWithTag in errorsWithTags:
correctWords.append(getCorrectWordFromErrorTags(errorWithTag).lower().strip());
for correctionWithTag in correctionsWithTags:
estimatedWords.append(getEstimatedWordFromCorrectionTags(correctionWithTag));
for word in estimatedWords:
if word in correctWords:
trueEstimationCount = trueEstimationCount + 1;
return (errorCount, trueEstimationCount);