-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetnerformat.py
140 lines (120 loc) · 4.1 KB
/
getnerformat.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
#!/usr/bin/env python
# coding=utf-8
import os
indir = "./data/xmlParse/eng/"
fr = open("./eng_gold.tab")
fw = open("nerformat.txt","w")
n = 0
a = 0
b = 0
doc_loc_mention_dict = {}
for line in fr:
#print line
es = line.strip().split("\t")
mention = es[2]
mention_doc = es[3].split(":")[0]
mention_loc = es[3].split(":")[1]
start = int(mention_loc.split("-")[0])
mid = es[4]
mtype = es[5]
nam = es[6]
if 'NOM' == nam:
continue
if 'NIL' in mid:
continue
mention_loc_dict = doc_loc_mention_dict.setdefault(mention_doc,{})
mention_loc_dict.setdefault(start,mention+"\t" + mtype)
filelist = os.listdir(indir)
for file in filelist:
filepath = indir + file
print filepath
fr = open(filepath)
mention_loc = doc_loc_mention_dict[file.replace('.xml','')]
for line in fr:
line = line.strip()
start,text = line.split('\t')
start = int(start)
word = ''
mlen = 0
for i in range(len(text)):
if text[i].isalpha():
word += text[i]
else:
if mention_loc.has_key(start):
m = mention_loc[start].split('\t')[0]
mt = mention_loc[start].split('\t')[1]
if len(m) == len(word):
print '***********'
print mention_loc[start]
print "{0}\t{1}".format(word,'I-'+mt)
outline = "{0}\t{1}\n".format(word,'I-'+mt)
fw.write(outline)
print '***********'
start += len(word)
start += 1
word = ''
continue
if len(m) > mlen:
if mlen == 0:
print '***********'
print mention_loc[start]
print "{0}\t{1}".format(word,'B-'+mt)
outline = "{0}\t{1}\n".format(word,'B-'+mt)
fw.write(outline)
print '***********'
else:
print '***********'
print mention_loc[start]
print "{0}\t{1}".format(word,'I-'+mt)
outline = "{0}\t{1}\n".format(word,'I-'+mt)
fw.write(outline)
print '***********'
mlen += len(word)
word = ''
if mlen == len(m):
start += mlen
mlen = 0
else:
print text[i]
mlen += 1
continue
else:
print "{0}\t{1}".format(word,'O')
outline = "{0}\t{1}\n".format(word,'O')
fw.write(outline)
start += len(word)
if text[i] != ' ':
print "{0}\t{1}".format(text[i],'O')
outline = "{0}\t{1}\n".format(text[i],'O')
fw.write(outline)
start += 1
word = ''
fw.close()
'''
n += 1
start = int(mention_loc.split("-")[0])
end = int(mention_loc.split("-")[1])
filepath = indir + mention_doc + ".xml"
fr_doc = open(filepath)
for in_line in fr_doc:
in_start, text = in_line.split("\t")
in_start = int(in_start)
if start >= in_start and end <= in_start + len(text):
characters = list(text)
#print words
#print in_start
#print mention
m = ''.join(characters[start - in_start : end - in_start + 1])
if m != mention:
print(m)
print(mention)
print(line)
break
outline = mention_doc+":"+mention_loc + "\t" + text
fw.write(outline)
#print in_line
fw.close()
print(n)
print(a)
print(b)
'''