-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread_pos_json.py
99 lines (96 loc) · 2.87 KB
/
read_pos_json.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
# -*- coding: cp936 -*-
import json
pos_json='''
{
"doc id": 1,
"doc": {
"paras": [
{
"para id":0
},
{
"para id": 1,
"sentences": [
{
"sent id": 0,
"cont": "国内专家学者40余人参加研讨会。",
"words": [
{
"word id": 0,
"wd": "国内",
"pos": "NN",
"ne": "O"
},
{
"word id": 1,
"wd": "专家",
"pos": "NN",
"ne": "O"
},
{
"word id": 2,
"wd": "学者",
"pos": "NN",
"ne": "O"
},
{
"word id": 3,
"wd": "40余",
"pos": "CC",
"ne": "Number"
},
{
"word id": 4,
"wd": "人",
"pos": "NN",
"ne": "0"
},
{
"word id": 5,
"wd": "研讨会",
"pos": "NN",
"ne": "O"
}
,
{
"word id": 6,
"wd": "。",
"pos": "PU",
"ne": "O"
}
]
}
]
}
]
}
}'''
def read_pos_json(pjs):
try:
pjs=pjs.decode('utf8')
except:
try:
pjs=pjs.decode('gb18030')
except:
pass
js=json.loads(pjs)
wll=[]
try:
doc=js['doc']
paras=doc['paras']
for para in paras:
sens=para.get('sentences',None)
if sens!=None:
for sen in sens:
wds=sen.get('words',None)
if wds!=None:
wl=[(x['wd'],x['pos']) for x in wds]
if len(wl)!=0:
wll.append(wl)
except:
wll=[]
return wll
if __name__=='__main__':
wll=read_pos_json(pos_json)
print wll
print 'done'