-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tagger.py
58 lines (38 loc) · 1.51 KB
/
test_tagger.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
#!/usr/bin/python3
"""A program to use a trained POS tagger based on Hidden Markov Model."""
import argparse
import pickle
import nltk
from nltk.corpus.reader import ConllCorpusReader
def tag_testset():
"""
Read options from user input, and tagger on test set.
"""
DISCR = 'Test a POS tagger given a test data.'
parser = argparse.ArgumentParser(description=DISCR)
parser.add_argument('-test_dir', type=str,
help='Directory of test file.', required=True)
parser.add_argument('-test_fileid', type=str,
help='Name of the test file.', required=True)
parser.add_argument('-POS_tagger', type=str,
help='POS Tagger object.', required=True)
parser.add_argument('-tagged_output', type=str,
help='Path to tagged words file.', required=True)
args = parser.parse_args()
# create a CoNLL corpus reader object
test_corpus = nltk.corpus.ConllCorpusReader(
args.test_dir, args.test_fileid, ['words'], tagset='universal', encoding='utf8'
)
# obtain tagger object from disk
with open(args.POS_tagger, "rb") as f:
POSTagger= pickle.load(f)
with open(args.tagged_output, 'w') as op_file:
for sent in test_corpus.sents():
predicted_tags = POSTagger.decode(sent)
for w, tag in zip(sent, predicted_tags):
op_file.write(w + '\t' + tag + '\n')
op_file.write('\n')
def main():
tag_testset()
if __name__ == '__main__':
main()