-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStringTools.py
More file actions
277 lines (240 loc) · 7.73 KB
/
Copy pathStringTools.py
File metadata and controls
277 lines (240 loc) · 7.73 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import string
import re
import cgi
import htmllib
def escape(s):
return cgi.escape(s)
_htmlParser=htmllib.HTMLParser(None)
def unescape(s):
"""http://wiki.python.org/moin/EscapingHtml
Example:
>>> unescape("Norwegian<>Polish Dictionar TR'App")
>>> Norwegian<>Polish Dictionar TR'App
"""
_htmlParser = htmllib.HTMLParser(None)
_htmlParser.save_bgn()
_htmlParser.feed(s)
return _htmlParser.save_end()
def excelCsvLine(values):
"""
Excel Csv style line including newLine
"""
def escapeQuote(v):
return unicode(v).replace('"','""')
valuesAsText = [ (u'"%s"' % escapeQuote(v)) for v in values ]
return (u",".join(valuesAsText))+u"\n"
def dateStrConvert(s):
""" 'January 07, 2009' to 01/07/2009 """
fields=s.strip().replace(",","").split()
monthStr = fields[0]
day = int(fields[1])
year = int(fields[2])
months = ["January","February","March","April","May","June",
"July","August","September","October","November","December"]
month = months.index(monthStr)+1
return "%02d/%02d/%4d" % (month,day,year)
def punctuationToSpace(s):
for c in string.punctuation:
s= s.replace(c," ")
return s
_multiSpaceToSingleRe = re.compile(r'\s+', flags=re.UNICODE)
def multiSpaceToSingle(s):
return _multiSpaceToSingleRe.sub(' ',s)
def textInBetween(textStr,beforeTag,afterTag,allowEmptyAsNone=False):
start=findPast(textStr,beforeTag)
assert start >= 0
end=textStr.find(afterTag,start)
assert end >= 0
result = textStr[start:end]
if allowEmptyAsNone:
if not len(result):
return None
else:
assert len(result)
return result
def listOfTextInBetween(textStr,beforeTag,afterTag):
resultList = []
end=0
while 1:
start=findPast(textStr,beforeTag,end)
if start == -1:
return resultList
end=textStr.find(afterTag,start)
assert end >= 0
result = textStr[start:end]
assert len(result)
resultList.append(result)
def textInBetweenAfterSet(textStr,beforeTag,afterTagSet):
start=findPast(textStr,beforeTag)
assert start >= 0
end=len(textStr)
for afterTag in afterTagSet:
thisEnd=textStr.find(afterTag,start)
if thisEnd != -1:
end = min(end,thisEnd)
assert end != len(textStr)
assert end >= 0
result = textStr[start:end]
assert len(result)
return result
def commaTextToFloat(textStr):
return float(textStr.replace(",","."))
def commaTextToInt(textStr):
return int(textStr.replace(",",""))
def findPast(s, substr, start=0):
end = s.find(substr, start)
if end == -1:
return end
return end+len(substr)
def toAscii(str):
return ''.join([c for c in str if (ord(c) < 128) and (ord(c) > 31 or ord(c) == 9)])
def longestCommonSubstring(S1, S2):
M = [[0]*(1+len(S2)) for i in xrange(1+len(S1))]
longest, x_longest = 0, 0
for x in xrange(1,1+len(S1)):
for y in xrange(1,1+len(S2)):
if S1[x-1] == S2[y-1]:
M[x][y] = M[x-1][y-1] + 1
if M[x][y]>longest:
longest = M[x][y]
x_longest = x
else:
M[x][y] = 0
return S1[x_longest-longest: x_longest]
## {{{ http://code.activestate.com/recipes/576874/ (r1)
## Upper bound value is at most the length of the longer string.
''' Calculates the Levenshtein distance of 2 strings'''
## TODO use instead C# of http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
def editingDistance(s1, s2):
l1 = len(s1)
l2 = len(s2)
matrix = [range(l1 + 1)] * (l2 + 1)
for zz in range(l2 + 1):
matrix[zz] = range(zz,zz + l1 + 1)
for zz in range(0,l2):
for sz in range(0,l1):
if s1[sz] == s2[zz]:
matrix[zz+1][sz+1] = min(matrix[zz+1][sz] + 1, matrix[zz][sz+1] + 1, matrix[zz][sz])
else:
matrix[zz+1][sz+1] = min(matrix[zz+1][sz] + 1, matrix[zz][sz+1] + 1, matrix[zz][sz] + 1)
return matrix[l2][l1]
_reRankWordSplitter = re.compile("\s|\.|-|/|\\|,|:|;|_")
# deprecated
def rankOnSubstrMatches(subStrs, stringOrg):
stringOrgLen = len(stringOrg)
rank=0
while True:
lcs = longestCommonSubstring(subStrs,stringOrg)
if len(lcs) >= 3:
rank+=len(lcs)
subStrs = subStrs.replace(lcs,'')
stringOrg = stringOrg.replace(lcs,'')
else:
return rank / float(stringOrgLen)
# rank how s fits in text
# the smaller the number the better it fits, including negative numbers
# If no chars are matching (upper bound editDistance) then 0 is returned
# best used with all lower cased().
# and normalized spacing
def rankMe(s, text):
# complete match, best
if s == text:
return -100000
v = text.find(s)
# sub string on word boundary, next best
if v != -1 and(v==0 or text[v-1]==' '):
# penalize for extra chars
return -100000+len(text)-len(s)
textWords = set(_reRankWordSplitter.split(text))
sWords = _reRankWordSplitter.split(s)
nrWordsinText = 0
for w in sWords:
nrWordsinText += w in textWords
if nrWordsinText:
# rank for number of words matched
# penalize for larger text body
return (128*-nrWordsinText)+len(text)
v = editingDistance(s, text)
if v == max(len(s),len(text)):
return 0
return v
def commaTextToFloat(textStr):
return float(textStr.replace(",","."))
def commaTextToInt(textStr):
return int(textStr.replace(",",""))
def findPast(s, substr, start=0):
end = s.find(substr, start)
if end == -1:
return end
return end+len(substr)
def toAscii(str):
return ''.join([c for c in str if (ord(c) < 128) and (ord(c) > 31 or ord(c) == 9)])
def longestCommonSubstring(S1, S2):
M = [[0]*(1+len(S2)) for i in xrange(1+len(S1))]
longest, x_longest = 0, 0
for x in xrange(1,1+len(S1)):
for y in xrange(1,1+len(S2)):
if S1[x-1] == S2[y-1]:
M[x][y] = M[x-1][y-1] + 1
if M[x][y]>longest:
longest = M[x][y]
x_longest = x
else:
M[x][y] = 0
return S1[x_longest-longest: x_longest]
## {{{ http://code.activestate.com/recipes/576874/ (r1)
## Upper bound value is at most the length of the longer string.
''' Calculates the Levenshtein distance of 2 strings'''
def editingDistance(s1, s2):
l1 = len(s1)
l2 = len(s2)
matrix = [range(l1 + 1)] * (l2 + 1)
for zz in range(l2 + 1):
matrix[zz] = range(zz,zz + l1 + 1)
for zz in range(0,l2):
for sz in range(0,l1):
if s1[sz] == s2[zz]:
matrix[zz+1][sz+1] = min(matrix[zz+1][sz] + 1, matrix[zz][sz+1] + 1, matrix[zz][sz])
else:
matrix[zz+1][sz+1] = min(matrix[zz+1][sz] + 1, matrix[zz][sz+1] + 1, matrix[zz][sz] + 1)
return matrix[l2][l1]
_reRankWordSplitter = re.compile("\s|\.|-|/|\\|,|:|;|_")
# deprecated
def rankOnSubstrMatches(subStrs, stringOrg):
stringOrgLen = len(stringOrg)
rank=0
while True:
lcs = longestCommonSubstring(subStrs,stringOrg)
if len(lcs) >= 3:
rank+=len(lcs)
subStrs = subStrs.replace(lcs,'')
stringOrg = stringOrg.replace(lcs,'')
else:
return rank / float(stringOrgLen)
# rank how s fits in text
# the smaller the number the better it fits, including negative numbers
# If no chars are matching (upper bound editDistance) then 0 is returned
# best used with all lower cased().
# and normalized spacing
def rankMe(s, text):
# complete match, best
if s == text:
return -100000
v = text.find(s)
# sub string on word boundary, next best
if v != -1 and(v==0 or text[v-1]==' '):
# penalize for extra chars
return -100000+len(text)-len(s)
textWords = set(_reRankWordSplitter.split(text))
sWords = _reRankWordSplitter.split(s)
nrWordsinText = 0
for w in sWords:
nrWordsinText += w in textWords
if nrWordsinText:
# rank for number of words matched
# penalize for larger text body
return (128*-nrWordsinText)+len(text)
v = editingDistance(s, text)
if v == max(len(s),len(text)):
return 0
return v