annotate g_read_sents.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
1
0
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
2 import os
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
3 import nltk
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
4 from nltk.corpus import PlaintextCorpusReader
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
5 import argparse
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
6
2
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
7 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
8
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
9
0
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
10 def Parser():
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
11 the_parser = argparse.ArgumentParser(description="Segments the text input into separate sentences")
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
12 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
13 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
14
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
15 args = the_parser.parse_args()
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
16 return args
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
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
19 def print_out(outp, sentences):
1
fb617586f4b2 planemo upload commit a81826fe44f09a3710a35c183aa88b745aeec064-dirty
stevecassidy
parents: 0
diff changeset
20 with open(outp, 'w') as output:
fb617586f4b2 planemo upload commit a81826fe44f09a3710a35c183aa88b745aeec064-dirty
stevecassidy
parents: 0
diff changeset
21 for sent in sentences:
2
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
22 for tok in sent:
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
23 output.write(tok)
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
24 output.write(' ')
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
25 output.write('\n')
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
26
0
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
27
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
28 def find_nth(string, sub, n, offset):
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
29 start = string.find(sub, offset)
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
30 while start >= 0 and n > 1:
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
31 start = string.find(sub, start + len(sub))
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
32 n -= 1
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
33 return start
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
34
2
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
35
0
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
36 def count_occurences(lst, string):
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
37 count = 0
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
38 for item in lst:
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
39 if string in item:
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
40 count += 1
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
41 return count
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
42
2
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
43
0
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
44 def read_sents(inp, outp):
2
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
45
0
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
46 corpus = PlaintextCorpusReader(os.path.dirname(inp), os.path.basename(inp))
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
47 sents = corpus.sents()
2
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
48 print_out(outp, sents)
a47980ef2b96 planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents: 1
diff changeset
49
0
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
50
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
51 if __name__ == '__main__':
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
52 args = Parser()
e991d4e60c17 planemo upload commit 0203cb3a0b40d9348674b2b098af805e2986abca-dirty
stevecassidy
parents:
diff changeset
53 read_sents(args.input, args.output)