-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtest.py
50 lines (41 loc) · 1.38 KB
/
test.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
def getProp(text, label):
res = {}
for index, lab in enumerate(label):
if len(lab.split('-')) != 2:
continue
state, key = lab.split('-')
if key in res:
#print(res[key])
temp = res[key]
temp.append([state, index])
#print("temp:", temp)
res[key] = temp
else:
#print(key, [state,index])
res[key] = [[state, index]]
ret = {}
#print("res:", res)
for key, value in enumerate(res):
#print("value 1:", res[value])
# 处理顺序错误
if res[value][0][0] != 'B' or res[value][-1][0] != 'E':
return {}
B = 0
E = 0
for val in res[value]:
if val[0] == 'B':
B += 1
if val[0] == 'E':
E += 1
# 个数错误
if B > 1 or E > 1:
return {}
ret[value] = ''.join(text[res[value][0][1]:res[value][-1][1] + 1])
return ret
text = ['小', '明', '是', '中', '国','人']
label = ['B-name', 'E-name', 'N', 'B-nation','I-nation','E-nation']
label2 = ['B-name', 'E-name', 'N', 'B-nation','E-nation','E-nation']
label3 = ['B-name', 'E-nation', 'N', 'B-nation','E-nation','E-nation']
print(getProp(text, label))
print(getProp(text, label2))
print(getProp(text, label3))