-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstandoff2crf.py
205 lines (172 loc) · 6.27 KB
/
standoff2crf.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
#! -*- coding:utf-8 -*-
import jieba
import jieba.posseg as pseg
import os
import random
import codecs
import pandas as pd
import csv
import copy
NAMES = ['token','pos','isknowledge','isoj','nertag','predict']
knowledge_dict = codecs.open('dict/算法知识词典.txt','r','utf-8').readlines()
knowledge_dict = [item.strip('\n') for item in knowledge_dict]
oj_dict = codecs.open('dict/oj.txt','r','utf-8').readlines()
oj_dict = [item.strip('\n') for item in oj_dict]
def extract_ann(filename):
'''
In [176]: extract_ann('reportb11936.ann')
Out[176]: {'0': ('7', 'OJ'), '15': ('19', 'KNOWLEDGE')}
'''
annmap = {}
with file(filename) as f:
for line in f:
_,info,_ = line.split('\t')
entity,start,end = info.split(' ')
annmap[int(start)] = (int(end),entity)
return annmap
def trans_txt(filename,tagmode=False):
'''
从txt文本转换为standoff格式,tagmode默认False代表有标记ann文件
'''
curr_pos = 0
if not tagmode:
annmap = extract_ann(filename.split('.')[0]+'.ann')
with file(filename) as f:
document = f.read()
#document = document.replace('\r\n','\n')
#document = document.replace('\t',' ')
words = pseg.cut(document)
result = []
last_tag = None
last_word = None
for word,flag in words:
if word == '\t':
word = ' '
if not tagmode:
if annmap.has_key(curr_pos):
nertag = 'B-'+annmap[curr_pos][1]
last_tag = (curr_pos,annmap[curr_pos][0],annmap[curr_pos][1])
elif last_tag and last_tag[0] < curr_pos < last_tag[1]:
nertag = 'I-' + last_tag[2]
else:
nertag = 'O'
else:
nertag='O'
if last_word == u'。' and word != '\n' and word!=last_word:
result.append('')
for item in knowledge_dict:
if item.find(word)>=0 and len(word)>1:
inKnowledgeDict = 'Y'
break;
else:
inKnowledgeDict = 'N'
for item in oj_dict:
if word.find(item)>=0 and len(word)>1:
inOjDict = 'Y'
break;
else:
inOjDict = 'N'
if not tagmode:
result.append('\t'.join([word,flag,inKnowledgeDict,inOjDict,nertag]))
else:
result.append('\t'.join([word,flag,inKnowledgeDict,inOjDict,nertag]))
curr_pos+=len(word)
last_word = word
output = '\n'.join(result)
output = output.replace('\r\n','\n')
with file(filename.split('.')[0]+'.crf','w') as f:
f.write(output.encode('utf8'))
return output
def gen_dataset(fold,outputfilename):
result = []
for filename in fold:
result.append(trans_txt(filename))
final = '\n'.join(result).encode('utf8')
print "处理了%d个文件,%d行" % (len(result),len(final))
with file(outputfilename,'w') as f:
final = final.replace('\r\n','\n')
f.write(final)
def cross_validation(data,num=5):
total = len(data)
sample_num = total/num
for i in range(num):
testset = random.sample(data,sample_num)
trainset = list(set(data).difference(set(testset)))
#trainset = random.sample(data,total-sample_num)
#testset = random.sample(data,sample_num)
yield (trainset,testset)
def test(data):
for index,(train,test) in enumerate(cross_validation(data,num=5)):
os.system('echo -e "new round\n\n" >> evaluate')
gen_dataset(train,'result/train'+str(index))
gen_dataset(test,'result/test'+str(index))
os.system("crf_learn -t -p8 -f 1 -c 4.0 template result/train%s result/model%s" % (index,index))
os.system("crf_test -m result/model%s result/test%s > result/result%s"%(index,index,index))
os.system("perl conlleval.pl -d '\t' < result/result%s 2>&1 | tee -a evaluate"%index)
def train(trainset):
pass
#def join_ne(x):
#if x.startswith('B'):
def get_ne(test_result,tagmode=False):
'''
从standoff格式的result中提取预测的实体
'''
NE = []
curtag = None
if not tagmode:
names = NAMES
else:
names = copy.copy(NAMES)
#names.remove('nertag')
df = pd.read_csv(test_result,sep='\t',header=None,quoting=csv.QUOTE_NONE,names=names)
predict = zip(df['token'],df['predict'])
for token,tag in predict:
if tag.startswith('B-'):
if curtag:
NE.append(curtag)
curtag = None
curtag = token
elif tag.startswith('I-'):
if curtag:
curtag += ' '
curtag += token
elif tag == 'O':
if curtag:
NE.append(curtag)
curtag = None
else:
print 'error'
output = '\n'.join(NE)
with file(test_result.split('.')[0]+'.ne','w') as f:
f.write(output)
return NE
def generate_ne(docset):
for doc in docset:
trans_txt(doc,tagmode=True)
testname = doc.split('.')[0]+'.crf'
resultname = doc.split('.')[0]+'.result'
os.system("crf_test -m result/model1 %s > %s"%(testname,resultname))
get_ne(resultname,tagmode=True)
if __name__ == '__main__':
#data = [os.path.join('test/data/txt',filename) for filename in os.listdir('test/data/txt') if filename.endswith('.txt')]
#generate_ne(data)
data = [os.path.join('test/data',filename) for filename in os.listdir('test/data') if filename.endswith('.txt')]
test(data)
#print trans_txt('test/reportb09877.txt')
#result = []
#for filename in os.listdir('test/data'):
#if filename.endswith('.txt'):
#result.append(trans_txt(os.path.join('test/data',filename)))
#final = '\n'.join(result).encode('utf8')
#final = final.replace('\r\n','\n')
#print "处理了%d个文件" % len(result)
#with file('train.ori','w') as f:
#final = final.replace('\r\n','\n')
#f.write(final)
#linenum = int(os.popen('wc -l train.ori').read().split(' ')[0])
#tail = linenum*0.2
#head = linenum - tail
#os.system("tail -%d train.ori > test.crf" % tail)
#os.system("head -%d train.ori > train.crf" % head)
#print "训练集%d行" % head
#print "测试集%d行" % tail