Mercurial > repos > stevecassidy > nltktools
annotate g_collocation.py @ 2:a47980ef2b96 draft
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
author | stevecassidy |
---|---|
date | Wed, 01 Nov 2017 01:19:55 -0400 |
parents | fb617586f4b2 |
children |
rev | line source |
---|---|
0
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
1 import nltk |
2
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
2 from nltk.collocations import BigramCollocationFinder, BigramAssocMeasures |
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
3 from nltk.collocations import TrigramCollocationFinder, TrigramAssocMeasures |
0
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
4 import argparse |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
5 |
2
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
6 nltk.download('punkt', quiet=True) |
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
7 |
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
8 |
0
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
9 def Parser(): |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
10 the_parser = argparse.ArgumentParser(description="Parse the sentence using Chart Parser and a supplied grammar") |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
11 the_parser.add_argument('--input', required=True, action="store", type=str, help="input text file") |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
12 the_parser.add_argument('--output', required=True, action="store", type=str, help="output file path") |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
13 the_parser.add_argument('--freq_filter', required=True, action="store", type=str, help="The minimum number of required occurrences in the corpus") |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
14 the_parser.add_argument('--results', required=True, action="store", type=str, help="The maximum number of collocations to show in the results") |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
15 the_parser.add_argument('--coll_type', required=True, action="store", type=str, help="Type of collocations to find") |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
16 the_parser.add_argument('--pos', required=True, action="store", type=str, help="Data input is a set of POS tags") |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
17 |
2
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
18 return the_parser.parse_args() |
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
19 |
0
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
20 |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
21 def collocation(inp, outp, freq_filter, results, coll_type, pos): |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
22 pos = bool(pos == 'true') |
1
fb617586f4b2
planemo upload commit a81826fe44f09a3710a35c183aa88b745aeec064-dirty
stevecassidy
parents:
0
diff
changeset
|
23 with open(inp, 'r') as fd: |
fb617586f4b2
planemo upload commit a81826fe44f09a3710a35c183aa88b745aeec064-dirty
stevecassidy
parents:
0
diff
changeset
|
24 i = fd.read() |
fb617586f4b2
planemo upload commit a81826fe44f09a3710a35c183aa88b745aeec064-dirty
stevecassidy
parents:
0
diff
changeset
|
25 |
0
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
26 all_words = [] |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
27 if pos: |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
28 text = i.split(' ')[:-1] |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
29 all_words = [x[0:x.index('/')] if x != '\n' else x for x in text] |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
30 all_words = [x.strip(' ').strip('\n') for x in all_words] |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
31 else: |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
32 sents = nltk.sent_tokenize(i) |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
33 for sent in sents: |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
34 all_words += nltk.word_tokenize(sent) |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
35 if coll_type == 'bigram': |
2
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
36 measures = BigramAssocMeasures() |
0
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
37 finder = BigramCollocationFinder.from_words(all_words) |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
38 else: |
2
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
39 measures = TrigramAssocMeasures() |
0
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
40 finder = TrigramCollocationFinder.from_words(all_words) |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
41 finder.apply_freq_filter(int(freq_filter)) |
2
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
42 # score the ngrams and get the first N |
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
43 colls = finder.score_ngrams(measures.pmi)[:int(results)] |
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
44 with open(outp, 'w') as output: |
1
fb617586f4b2
planemo upload commit a81826fe44f09a3710a35c183aa88b745aeec064-dirty
stevecassidy
parents:
0
diff
changeset
|
45 for coll in colls: |
2
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
46 (a, b), score = coll |
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
47 output.write("%s\t%s\n" % (a, b)) |
a47980ef2b96
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
1
diff
changeset
|
48 |
0
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
49 |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
50 if __name__ == '__main__': |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
51 args = Parser() |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
52 |
e991d4e60c17
planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff
changeset
|
53 collocation(args.input, args.output, args.freq_filter, args.results, args.coll_type, args.pos) |