-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathutils.py
217 lines (206 loc) · 5.27 KB
/
utils.py
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
__author__ = 'mengjiang'
def CleanText(text):
text = text.replace('</',' ').replace('<',' ltlt ').replace('>',' gtgt ')
text = text.replace(' vs. ',' vs ').replace('etc.','etc')
text = text.replace("'s "," whosewhat ").replace("'S "," whosewhat ")
text = text.replace("s' ","s whosewhat ").replace("S' ","S whosewhat ")
words = text.split(' ')
text = ''
for word in words:
if word == '': continue
l = len(word)
if word.count('.') >= 2:
is_valid = True
for c in word:
if c == '.': continue
if c.isdigit():
is_valid = False
break
if is_valid:
word = word.replace('.','')
elif l > 1 and l <= 5 and word[l-1] == '.' and word[0].isalpha() and word[0].isupper():
word = word[0:l-1]
if word == '': continue
text += ' '+word
if text == '': return ''
return text[1:]
def SetFromFile(filename,IFLOWER=True):
ret = set()
fr = open(filename,'rb')
for line in fr:
word = line.strip('\r\n')
if IFLOWER: word = word.lower()
if word == '': continue
ret.add(word)
fr.close()
return ret
def EntityIndex(filenames):
index,n_index = [{}],1
for filename in filenames:
fr = open(filename,'rb')
for line in fr:
arr = line.strip('\r\n').split('\t')
if not len(arr) == 2: continue
entity,classname = arr[0],arr[1]
words = entity.split(' ')
n = len(words)
if n > n_index:
for i in range(n_index,n):
index.append({})
n_index = n
temp = index[n-1]
if n > 1:
for i in range(0,n-1):
word = words[i]
if not word in temp: temp[word] = {}
temp = temp[word]
word = words[n-1]
else:
word = words[0]
temp[word] = classname
fr.close()
return index
def PatternIndex(filename):
index,n_index = [{}],1
fr = open(filename,'rb')
for line in fr:
arr = line.strip('\r\n').split('\t')
if len(arr) < 2: continue
pattern,score = arr[0],float(arr[1])
sentence = XMLToSentence(pattern)
keys = SentenceToKeys(sentence)
n = len(keys)
if n > n_index:
for i in range(n_index,n):
index.append({})
n_index = n
temp = index[n-1]
if n > 1:
for i in range(0,n-1):
key = keys[i]
if not key in temp: temp[key] = {}
temp = temp[key]
key = keys[n-1]
else:
key = keys[0]
temp[key] = [pattern,score]
fr.close()
return index
def XMLToSentence(xml):
periodset = set(['.','?','!'])
sentence = []
while '<' in xml:
pos0 = xml.find('<')
pos1 = xml.find('>')
if pos0 > 0:
part = xml[0:pos0-1]
if not xml[pos0-1] == ' ':
part += xml[pos0-1]
for word in part.split(' '):
if word in periodset and len(sentence) > 0:
sentence.append(['PERIOD',word])
continue
sentence.append(['',word])
classname = xml[pos0+1:pos1]
xml = xml[pos1+1:]
pos0 = xml.find('<')
pos1 = xml.find('>')
if pos0 >= 0:
words = xml[0:pos0].split(' ')
sentence.append([classname,words])
if pos1+1 < len(xml) and xml[pos1+1] == ' ':
xml = xml[pos1+2:]
else:
xml = xml[pos1+1:]
if len(xml) > 0:
part = xml[1:]
if not xml[0] == ' ':
part = xml[0]+part
for word in part.split(' '):
if word in periodset and len(sentence) > 0:
sentence.append(['PERIOD',word])
continue
sentence.append(['',word])
return sentence
def XMLToSentences(xml):
periodset = set(['.','?','!'])
sentences = []
sentence = []
while '<' in xml:
pos0 = xml.find('<')
pos1 = xml.find('>')
if pos0 > 0:
part = xml[0:pos0-1]
if not xml[pos0-1] == ' ':
part += xml[pos0-1]
for word in part.split(' '):
if word in periodset and len(sentence) > 0:
sentence.append(['PERIOD',word])
sentences.append([elem for elem in sentence])
sentence = []
continue
sentence.append(['',word])
classname = xml[pos0+1:pos1]
xml = xml[pos1+1:]
pos0 = xml.find('<')
pos1 = xml.find('>')
if pos0 >= 0:
words = xml[0:pos0].split(' ')
sentence.append([classname,words])
if pos1+1 < len(xml) and xml[pos1+1] == ' ':
xml = xml[pos1+2:]
else:
xml = xml[pos1+1:]
if len(xml) > 0:
part = xml[1:]
if not xml[0] == ' ':
part = xml[0]+part
for word in part.split(' '):
if word in periodset and len(sentence) > 0:
sentence.append(['PERIOD',word])
sentences.append([elem for elem in sentence])
sentence = []
continue
sentence.append(['',word])
if len(sentence) > 0:
sentences.append([elem for elem in sentence])
return sentences
def SentenceToXML(sentence):
xml = ''
for [classname,words] in sentence:
if classname == '' or classname == 'PERIOD':
xml += ' '+words
else:
entity = ''
for word in words: entity += ' '+word
entity = entity[1:]
xml += ' <'+classname+'>'+entity+'</'+classname+'>'
if xml == '': return ''
return xml[1:]
def SentencesToXML(sentences):
xml = ''
for sentence in sentences:
xml += ' '+SentenceToXML(sentence)
if xml == '': return ''
return xml[1:]
def SentenceToDollar(sentence):
dollar = ''
for [classname,words] in sentence:
if classname == '' or classname == 'PERIOD':
dollar += ' '+words
else:
_classname = classname
if '.' in classname:
pos = classname.rfind('.')
_classname = classname[pos+1:]
dollar += ' $'+_classname
if dollar == '': return ''
return dollar[1:]
def SentenceToKeys(sentence):
keys = []
for [classname,words] in sentence:
if classname == '' or classname == 'PERIOD':
keys.append('\t'+words)
else:
keys.append(classname+'\t')
return keys