-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
171 lines (140 loc) · 7.24 KB
/
main.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
import gc
import os
import glob
import json
from pathlib import Path
from collections import namedtuple
from collections import OrderedDict
from models.utils import ROOT_DIR
from models.utils import extend_maps, prepocess_data_for_lstmcrf, build_map, load_data_and_labels
from models.evaluate import crf_train_eval, crf_train_eval_tagged, bilstm_train_and_eval
def read_counter(path):
with open(path, 'r') as f:
data = json.load(f)
data = OrderedDict(sorted(data.items(), key=lambda x: int(x[0])))
return data
def split_data(sents, gold_labels, tag_labels, dev=False, train_ratio=0.7, dev_ratio=0.85):
if not dev:
split_index = int(len(sents) * train_ratio)
train_word_lists, train_tag_lists = sents[:split_index], tag_labels[:split_index]
test_word_lists, test_tag_lists = sents[split_index:], gold_labels[split_index:]
return train_word_lists, train_tag_lists, test_word_lists, test_tag_lists
else:
train_index = int(len(sents) * train_ratio)
dev_index = int(len(sents) * dev_ratio)
train_word_lists, train_tag_lists = sents[:train_index], tag_labels[:train_index]
dev_word_lists, dev_tag_lists = sents[train_index:dev_index], gold_labels[train_index:dev_index]
test_word_lists, test_tag_lists = sents[dev_index:], gold_labels[dev_index:]
return train_word_lists, train_tag_lists, dev_word_lists, dev_tag_lists, test_word_lists, test_tag_lists
def split_tagged_data(data, train_ratio=0.7):
split_index = int(len(data) * train_ratio)
train_data, test_data = data[:split_index], data[split_index:]
return train_data, test_data
def crf_pipeline(data_paths, gold_data_path, entity_level, low_frequency=None):
# read gold data
sents, gold_labels = load_data_and_labels(gold_data_path)
counter = read_counter(low_frequency)
for data_path in data_paths:
# read tagged data
tag_sents, tag_labels = load_data_and_labels(data_path)
train_word_lists, train_tag_lists, test_word_lists, test_tag_lists = split_data(tag_sents, gold_labels, tag_labels, dev=False)
data_path = Path(data_path)
print("Training and evaluating CRF model for data:", data_path.stem)
print('trian data: {}, test data: {}'.format(len(train_tag_lists), len(test_tag_lists)))
crf_pred = crf_train_eval(
(train_word_lists, train_tag_lists),
(test_word_lists, test_tag_lists),
entity_level=entity_level, counter=counter
)
print()
print()
del crf_pred
gc.collect()
def bi_lstm_crf_pipeline(data_path, gold_data_path, entity_level):
# read gold data
sents, gold_labels = load_data_and_labels(gold_data_path)
# read tagged data
tag_sents, tag_labels = load_data_and_labels(data_path)
train_word_lists, train_tag_lists, dev_word_lists, dev_tag_lists, test_word_lists, test_tag_lists = split_data(tag_sents, gold_labels, tag_labels, dev=True, train_ratio=0.7, dev_ratio=0.85)
word2id = build_map(train_word_lists)
tag2id = build_map(train_tag_lists)
# Add <start> and <end> if using CRF layer with Bi-LSTM (decoding)
crf_word2id, crf_tag2id = extend_maps(word2id, tag2id, for_crf=True)
# other data process
train_word_lists, train_tag_lists = prepocess_data_for_lstmcrf(
train_word_lists, train_tag_lists
)
dev_word_lists, dev_tag_lists = prepocess_data_for_lstmcrf(
dev_word_lists, dev_tag_lists
)
test_word_lists, test_tag_lists = prepocess_data_for_lstmcrf(
test_word_lists, test_tag_lists, test=True
)
print("Training and evaluating Bi-LSTM-CRF model for data:", data_path.stem)
print('trian data: {}, dev data: {}, test data: {}'.format(len(train_tag_lists), len(dev_tag_lists), len(test_tag_lists)))
lstmcrf_pred = bilstm_train_and_eval(
(train_word_lists, train_tag_lists),
(dev_word_lists, dev_tag_lists),
(test_word_lists, test_tag_lists),
crf_word2id, crf_tag2id, entity_level=entity_level
)
del lstmcrf_pred
gc.collect()
def main(data_paths, gold_data_path, entity_level=False, low_frequency=None):
"""CRF and Bi-LSTM-CRF pipelines"""
# CRF pipeline
crf_pipeline(data_paths, gold_data_path, entity_level, low_frequency)
# Bi-LSTM-CRF Pipeline
for data_path in data_paths:
data_path = Path(data_path)
bi_lstm_crf_pipeline(data_path, gold_data_path, entity_level)
def crf_tagged_pipeline(data_paths, gold_data_path, entity_level=False, low_frequency=None):
# read gold data
Sentence = namedtuple('Sentence', 'words tag_labels gold_labels')
sents, gold_labels = load_data_and_labels(gold_data_path)
counter = read_counter(low_frequency)
for data_path in data_paths:
# read tagged data
tag_sents, tag_labels = load_data_and_labels(data_path)
data = [Sentence(*pair) for pair in zip(tag_sents, tag_labels, gold_labels)]
train_data, test_data = split_tagged_data(data)
data_path = Path(data_path)
print("Training and evaluating CRF model for data tagged with:", data_path.stem)
print('trian data: {}, test data: {}'.format(len(train_data), len(test_data)))
crf_pred = crf_train_eval_tagged(train_data, test_data, entity_level=entity_level, counter=counter)
print()
print()
del crf_pred
gc.collect()
if __name__ == "__main__":
entity_level = True
data_dir = os.path.join(ROOT_DIR, 'data/corpora/output/*.bio')
data_paths = glob.glob(data_dir)
data_paths = sorted(data_paths, key=lambda x: len(x))
# path
bccwj_paths = [x for x in data_paths if 'bccwj' in x]
bccwj_gold = os.path.join(ROOT_DIR, 'data/corpora/output/bccwj.bio')
bccwj_counter = os.path.join(ROOT_DIR, 'data/corpora/output/bccwj_names_counter.json')
mainichi_paths = [x for x in data_paths if 'mainichi' in x]
mainichi_gold = os.path.join(ROOT_DIR, 'data/corpora/output/mainichi.bio')
mainichi_counter = os.path.join(ROOT_DIR, 'data/corpora/output/mainichi_names_counter.json')
### result 1 ###
# # bccwj
# main(bccwj_paths, bccwj_gold, entity_level=entity_level)
# # mainichi
# main(mainichi_paths, mainichi_gold, entity_level=entity_level)
### result 2 ###
# # bccwj: use dictionary as feature for CRF
# crf_tagged_pipeline(bccwj_paths, bccwj_gold, entity_level=entity_level)
# # mainichi: use dictionary as feature for CRF
# crf_tagged_pipeline(mainichi_paths, mainichi_gold, entity_level=entity_level)
### result 3 ###
# # bccwj: evaluate on low frequency company names
# main(bccwj_paths, bccwj_gold, entity_level=entity_level, low_frequency=bccwj_counter)
# # mainichi: evaluate on low frequency company names
# main(mainichi_paths, mainichi_gold, entity_level=entity_level, low_frequency=mainichi_counter)
### result 4 ###
# bccwj: evaluate on low frequency company names, use dictionary as feature for CRF
crf_tagged_pipeline(bccwj_paths, bccwj_gold, entity_level=entity_level, low_frequency=bccwj_counter)
# mainichi: evaluate on low frequency company names, use dictionary as feature for CRF
crf_tagged_pipeline(mainichi_paths, mainichi_gold, entity_level=entity_level, low_frequency=mainichi_counter)