-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathner.py
139 lines (121 loc) · 3.75 KB
/
ner.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
''' ner '''
# -*-encoding:utf-8-*-
import settings
class NER():
''' 处理ner结果,返回时间、地名、组织
'''
def __init__(self, nlp):
self.nlp_result = nlp.ner_result
self.ner = {}
self.ner['time'] = self.taking_time()
self.ner['location'] = self.taking_location()
self.ner['organization'] = self.taking_organization()
self.ner['number'] = self.taking_number()
def taking_time(self):
''' 获取时间
'''
i = 0
state = False
only_number = True
time_fire = ""
result = []
while i < len(self.nlp_result):
if self.nlp_result[i][1] in ['DATE', 'TIME']:
time_fire += self.nlp_result[i][0]
if not state:
state = True
only_number = False
elif self.nlp_result[i][1] == 'NUMBER':
time_fire += self.nlp_result[i][0]
if not state:
state = True
elif self.nlp_result[i][1] == 'MISC':
time_fire += self.nlp_result[i][0]
else:
if state and not only_number:
result.append(time_fire)
time_fire = ""
state = False
only_number = True
i += 1
if state:
result.append(time_fire)
result = list(set(result))
return result
def taking_location(self):
''' 获取地点
'''
i = 0
state = False
location = ""
result = []
while i < len(self.nlp_result):
if self.nlp_result[i][1] in settings.LOC:
location += self.nlp_result[i][0]
if not state:
state = True
else:
if state:
result.append(location)
location = ""
state = False
i += 1
if state:
result.append(location)
result = list(set(result))
return result
def taking_organization(self):
''' 获取组织
'''
i = 0
state = False
organization = ""
result = []
while i < len(self.nlp_result):
if self.nlp_result[i][1] in settings.ORG:
organization += self.nlp_result[i][0]
if not state:
state = True
else:
if state:
result.append(organization)
organization = ""
state = False
i += 1
if state:
result.append(organization)
result = list(set(result))
return result
def taking_number(self):
''' 获取数目
'''
i = 0
state = False
having_time = False
number = ""
result = []
while i < len(self.nlp_result):
if self.nlp_result[i][1] in ['DATE', 'TIME']:
number += self.nlp_result[i][0]
if not state:
state = True
having_time = True
elif self.nlp_result[i][1] in ['NUMBER', 'PERCENT', 'MONEY']:
number += self.nlp_result[i][0]
if not state:
state = True
elif self.nlp_result[i][1] == 'MISC':
number += self.nlp_result[i][0]
else:
if state and not having_time:
result.append(number)
number = ""
state = False
having_time = False
i += 1
if state:
result.append(number)
result = list(set(result))
return result
if __name__ == '__main__':
pass