Mercurial > repos > stevecassidy > nltktools
comparison g_pos.py @ 0:e991d4e60c17 draft
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
author | stevecassidy |
---|---|
date | Wed, 12 Oct 2016 22:17:53 -0400 |
parents | |
children | fb617586f4b2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e991d4e60c17 |
---|---|
1 import nltk | |
2 import argparse | |
3 import json | |
4 | |
5 def arguments(): | |
6 parser = argparse.ArgumentParser(description="tokenize a text") | |
7 parser.add_argument('--input', required=True, action="store", type=str, help="input text file") | |
8 parser.add_argument('--output', required=True, action="store", type=str, help="output file path") | |
9 args = parser.parse_args() | |
10 return args | |
11 | |
12 | |
13 def postag(in_file, out_file): | |
14 """Input: a text file with one token per line | |
15 Output: a version of the text with Part of Speech tags written as word/TAG | |
16 """ | |
17 text = unicode(open(in_file, 'r').read(), errors='ignore') | |
18 sentences = nltk.sent_tokenize(text) | |
19 output = open(out_file, 'w') | |
20 for sentence in sentences: | |
21 tokens = nltk.word_tokenize(sentence) | |
22 postags = nltk.pos_tag(tokens) | |
23 for postag in postags: | |
24 # print postag | |
25 output.write("%s/%s " % postag) | |
26 output.write('\n') | |
27 output.close() | |
28 | |
29 | |
30 if __name__ == '__main__': | |
31 args = arguments() | |
32 postag(args.input, args.output) | |
33 | |
34 |