-
Notifications
You must be signed in to change notification settings - Fork 5
/
tagging.py
194 lines (153 loc) · 5.72 KB
/
tagging.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
class Setence(object):
def __init__(self):
self.words = []
def addWord(self, w):
self.words.append(w)
def getWords(self):
return self.words
def setLine(self, line):
self.line = line
def getLine(self):
return self.line
class NE(object):
def __init__(self):
self.list = []
def setOne(self, one):
self.list.append(one)
def getOne(self):
return self.list
class One(object):
def set(self, ne, word, pos):
self.ne = ne
self.word = word
self.pos = pos
def getWord(self):
return self.word
def getPos(self):
return self.pos
class Tag(object):
def tagging(self, file):
with open(file, 'r') as f:
sentence_set = []
for line in f:
input = line.split(" ")
if input[0] == ';':
s = Setence()
s.setLine(line)
for word in input[1:]:
s.addWord(word)
else:
continue
sentence_set.append(s)
predict_set = []
count = 0
with open("predict/prediction.txt", 'r') as f:
start = True
predict = None
get = False
for line in f:
if start:
count += 1
predict = NE()
start = False
po = 1
index = 0
if len(line) == 1:
predict_set.append(predict)
start = True
continue
inputs = line.split("\t")
if len(inputs) == 1:
print(inputs)
ne = inputs[1]
if ne != 'O':
one = One()
if len(ne) == 1:
one.set(ne, inputs[0], int(inputs[2]))
else:
ne = ne.split("_")[1]
one.set(ne,inputs[0], int(inputs[2]))
predict.setOne(one)
get = True
else:
one = One()
one.set(ne, inputs[0], int(inputs[2]))
predict.setOne(one)
get = False
predict_set.append(predict)
print(count)
print("{} : {}".format(len(sentence_set), len(predict_set)))
fw = open("submit/result.txt", 'w')
for sentences, Ne in zip(sentence_set, predict_set):
fw.write(sentences.getLine())
str = sentences.getLine()[2:]
words = sentences.getWords()
line = "$"
if len(Ne.list) == 0:
fw.write(line+sentences.getLine()[2:])
fw.write("\n")
else:
start = True
word = ""
ner = ""
idx = 0
index = 0
position = []
ne_list = []
for i, ne in enumerate(Ne.list):
if ne.ne != "I" and ne.ne != 'O':
if start:
start = False
else:
ne_list.append((ner, tag))
for w_idx in range(idx, position[0]-1):
word += words[w_idx]+" "
replace = ""
for w_idx in position:
replace += words[w_idx-1]+" "
replace = replace.replace(ner, "<"+ner+":"+tag+">")
word += replace
idx = position[-1]
position = []
position.append(ne.pos)
ner = ne.word
tag = ne.ne
elif ne.ne != 'O': # I 태깅
if len(position) != 0:
if ne.pos != position[-1]:
position.append(ne.pos)
ner += " "+ne.word
else:
if ne.word == 'ㄴ':
if "지나" in ner:
ner = ner.replace('지나', "지난")
elif "하" in ner:
ner = ner.replace('하', "한")
elif ne.word == 'ㄹ':
if "오" in ner:
ner = ner.replace("오", "올")
else:
ner += ne.word
if len(position) != 0:
ne_list.append((ner, tag))
for w_idx in range(idx, position[0] - 1):
word += words[w_idx] + " "
replace = ""
for w_idx in position:
replace += words[w_idx - 1] + " "
# print("이전 : {} ner --> {}".format(replace, ner))
replace = replace.replace(ner, "<" + ner +":"+tag+">")
# print("이후 : {}".format(replace))
word += replace
idx = position[-1]
for w in words[idx:]:
word += w+" "
else:
word = str + " "
# $ + 태깅 결과
line += word[:-1]
#print(line)
fw.write(line)
for _w, _ne in ne_list:
fw.write(_w + "\t" + _ne +"\n")
fw.write("\n")