forked from guillaumegenthial/tf_ner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_data_from_conll2003.py
60 lines (48 loc) · 2.19 KB
/
convert_data_from_conll2003.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
import argparse, os
from file_operations import read_lines, write_lines
from build_vocab import make_vocabs
from build_glove import make_embeddings
TEST_A_CONLL2003_FILE_NAME = 'test.txt'
TEST_B_CONLL2003_FILE_NAME = 'dev.txt'
TRAIN_CONLL2003_FILE_NAME = 'train.txt'
def reformat_file(filename):
lines = read_lines(filename)
sentence = []
words = []
labels = []
for line in lines[2:]:
if line == '':
words.append(' '.join(map(lambda token: token.split(' ')[0], sentence)))
labels.append(' '.join(map(lambda token: token.split(' ')[-1], sentence)))
sentence = []
else:
sentence.append(line)
# Add the last sentence
words.append(' '.join(map(lambda token: token.split(' ')[0], sentence)))
labels.append(' '.join(map(lambda token: token.split(' ')[-1], sentence)))
return words, labels
def convert(input_folder, output_folder):
# Change format
if not os.path.isdir(output_folder):
os.mkdir(output_folder)
test_a_filename = f'{input_folder}/{TEST_A_CONLL2003_FILE_NAME}'
words, tags = reformat_file(test_a_filename)
write_lines(f'{output_folder}/testa.words.txt', words)
write_lines(f'{output_folder}/testa.tags.txt', tags)
test_b_filename = f'{input_folder}/{TEST_B_CONLL2003_FILE_NAME}'
words, tags = reformat_file(test_b_filename if os.path.isfile(test_b_filename) else test_a_filename)
write_lines(f'{output_folder}/testb.words.txt', words)
write_lines(f'{output_folder}/testb.tags.txt', tags)
words, tags = reformat_file(f'{input_folder}/{TRAIN_CONLL2003_FILE_NAME}')
write_lines(f'{output_folder}/train.words.txt', words)
write_lines(f'{output_folder}/train.tags.txt', tags)
# Build vocabs
make_vocabs(output_folder)
# Build embeddings
make_embeddings(output_folder, '/home/dima/models/ArModel100w2v.txt')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--input_folder', type=str, default='/home/dima/tener/data/conll2003ru-bio-super-distinct')
parser.add_argument('--output_folder', type=str, default='data/conll2003ru')
args = parser.parse_args()
convert(args.input_folder, args.output_folder)