-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlputils.py
349 lines (283 loc) · 8.92 KB
/
nlputils.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from nltk.parse.corenlp import CoreNLPParser
import functools
import string
import jieba.posseg as pseg
import jieba
import logging
jieba.setLogLevel(logging.INFO) # 关闭结巴分词的log信息
'''
中文词性对应表:
https://gist.github.com/luw2007/6016931#ictclas-%E6%B1%89%E8%AF%AD%E8%AF%8D%E6%80%A7%E6%A0%87%E6%B3%A8%E9%9B%86
Stanford parser server 相关
https://stanfordnlp.github.io/CoreNLP/corenlp-server.html
'''
url = 'http://10.134.171.239:9000/'
url_En = 'http://10.134.171.239:9000/'
url_Han = 'http://10.134.171.239:9001/'
def containHan(s):
'''包含汉字的返回TRUE'''
for c in s:
if '\u4e00' <= c <= '\u9fa5':
return True
return False
def parse_sentense(sentence):
return choose_function(sentence, parse_sentence_Han, parse_sentence_En)(sentence)
def choose_function(sentence, func_Han, func_En):
global url
if containHan(sentence):
url = url_Han
return func_Han
else:
url = url_En
return func_En
def parse_sentence_En(sentence):
sentence = sentence.strip()
if not sentence:
return [], [], []
parser = CoreNLPParser(url=url)
parse = next(parser.raw_parse(sentence))
return parse_S_or_IP(parse[0])
parse_sentence_Han = parse_sentence_En
def parse_S_or_IP(S):
subjects = []
verbs = []
objects = []
for i in S:
if i.label() == 'NP':
subjects = parse_np(i)
if i.label() == 'VP':
v, o = parse_vp(i)
verbs += v
objects += o
if i.label() in ['S', 'IP']:
s, v, o = parse_S_or_IP(i)
subjects += s
verbs += v
objects += o
if i.label() in string.punctuation:
break
return subjects, verbs, objects
def get_verbs_count_of_sentense(sentence):
return choose_function(sentence, get_verbs_count_of_sentense_Han, get_verbs_count_of_sentense_En)(sentence)
def get_verbs_count_of_sentense_Han(sentence):
sentence = sentence.strip()
if not sentence:
return [], [], [], []
pronoun = []
adverb = []
modal_verb = []
participle = []
words = pseg.cut(sentence)
for word, flag in words:
if flag == 'r':
pronoun.append(word)
elif flag == 'd':
adverb.append(word)
elif flag == 'u':
modal_verb.append(word)
else:
continue
# 中文没有 过去分词/现在分词
return pronoun, adverb, modal_verb, participle
def get_verbs_count_of_sentense_En(sentence):
pronoun = []
adverb = []
modal_verb = []
participle = []
sentence = sentence.strip()
if not sentence:
return [], [], [], []
parser = CoreNLPParser(url=url)
parse = next(parser.raw_parse(sentence))
quene = [parse[0]]
while len(quene) > 0:
s = quene.pop(0)
for i in s:
if not isinstance(i, str):
quene.append(i)
if s.label() in ['PRP', 'PRP$', 'WP', 'WP$']:
pronoun.append(s[0])
if s.label() in ['RB', 'RBR', 'RBS', 'WRB'] and s[0] != 'not':
adverb.append(s[0])
if s.label() in ['MD']:
modal_verb.append(s[0])
if s.label() in ['VBG', 'VBN']:
participle.append(s[0])
return pronoun, adverb, modal_verb, participle
def parse_np(np):
ret = []
last_label = ''
for i in np:
if i.label() in ['PRP', 'EX', 'DT', 'PN', 'NR'] or i.label().startswith('NN'):
if i.label().startswith('NN') and (last_label in ['DT'] or last_label.startswith('NN')):
ret[-1] += ' ' + i[0]
elif (i.label().startswith('NN') or i.label() == 'NR') and last_label == 'NR':
ret[-1] += i[0]
else:
ret.append(i[0])
if i.label() == 'NP':
ret += parse_np(i)
last_label = i.label()
return ret
def parse_vp(vp):
verbs = []
objects = []
for i in vp:
if i.label() == 'VP':
v, o = parse_vp(i)
verbs += v
objects += o
elif i.label() == 'NP':
objects += parse_np(i)
elif i.label().startswith('VB'):
verbs.append(i[0])
elif i.label() == 'VV':
verbs.append(i[0])
elif i.label() == 'VRD':
for j in i:
if j.label() == 'VV':
verbs.append(j[0])
elif i.label() in string.punctuation:
break
else:
pass
return verbs, objects
vb_tense_map = {
'VB': 'present',
'VBD': 'past',
'VBG': 'none', # 'ing',
'VBN': 'past',
'VBP': 'present',
'VBZ': 'present'
}
def parse_sentense_tense(sentence):
return choose_function(sentence, parse_sentense_tense_Han, parse_sentense_tense_En)(sentence)
def parse_sentense_tense_Han(sentence):
'''
中文没时态
'''
return 'present'
def parse_sentense_tense_En(sentence):
sentence = sentence.strip()
if not sentence:
return 'none'
parser = CoreNLPParser(url=url)
parse = next(parser.raw_parse(sentence))
quene = [parse[0]]
while len(quene) > 0:
s = quene.pop(0)
for i in s:
if not isinstance(i, str):
quene.append(i)
tag = s.label()
if tag in ('VBD', 'VBN'):
return vb_tense_map[tag]
if tag == 'MD':
return 'future'
if tag.startswith('VB'):
return vb_tense_map[tag]
return 'none'
def parse_word_tense(word):
return choose_function(word, parse_word_tense_Han, parse_word_tense_En)(word)
def parse_word_tense_Han(word):
'''
中文没时态
'''
return 'present'
def parse_word_tense_En(word):
parser = CoreNLPParser(url=url)
parse = next(parser.raw_parse(word))
while not isinstance(parse, str):
pre = parse.label()
parse = parse[0]
if pre.startswith('VB') and pre in vb_tense_map:
return vb_tense_map[pre]
return 'none'
def parse_word_type(word):
return choose_function(word, parse_word_type_Han, parse_word_type_En)(word)
def parse_word_type_Han(word):
words = pseg.cut(word)
for _, flag in words:
if flag.startswith('v'):
return 'verb'
elif flag.startswith('n'):
return 'noun'
elif flag.startswith('a'):
return 'adj'
else:
continue
return 'none'
def parse_word_type_En(word):
parser = CoreNLPParser(url=url)
parse = next(parser.raw_parse(word))
while not isinstance(parse, str):
if parse.label().startswith('VB'):
return 'verb'
if parse.label().startswith('NN'):
return 'noun'
if parse.label().startswith('JJ'):
return 'adj'
parse = parse[0]
return 'none'
if __name__ == "__main__":
test_Han = [
'I want to sleep.',
'妈妈你睡了吗?',
'∂∑´® ƒß∂f I don\'t know what I write',
''
]
for t in test_Han:
print(t, containHan(t))
url_En = 'http://localhost:9000'
url_Han = 'http://localhost:9001'
test = [
'I want a girl.',
'A girl shot an elephant.',
'You and I are a couple.',
'You and I have and see money',
'I shot an girl',
'You and I will have a baby!',
'This system ejects the ATM card',
'This is a boy',
'ATM is idle, displaying a Welcome message',
'This is a Welcome message'
]
for t in test:
print(t, parse_sentense(t), 'tense: ', parse_sentense_tense(t))
print('-'*80)
test_words = functools.reduce(lambda x, y: x + y, map(lambda x: x.split(), [
'I want a girl.',
'A girl shot an elephant.',
'You and I are a couple.',
'You and I have and see money',
'I shot an girl',
'You and I will have a baby!'
]))
for t in test_words:
print(t, f"type: {parse_word_type(t)}",
'tense: %s' % parse_word_tense(t))
print('-'*80)
test_count = [
'I can do this, however you cannot.',
'Happily, I have an A finally.',
'This gril is not that girl.',
'To be or not to be, it is question.',
'I would like to swimming rather than running.',
'ATM is idle, displaying a Welcome message',
'I wanted to sleep!'
]
for t in test_count:
print(t, get_verbs_count_of_sentense(t))
test_Han = [
'他来到了网易杭研大厦.',
'我是小明,她是小红.',
'芷若,这件事我在心中已想了很久。.',
'我爱你',
'我今天去上学',
'我打篮球',
'汽车在高速上奔驰',
'然而,芷若,我不能瞒你,要是我这一生再不能见到赵姑娘,我是宁可死了的好。',
'那日在大都,我见你到那小酒店去和她相会,便知你内心真正情爱之所系。'
]
for t in test_Han:
print(t, parse_sentense(t))