Mercurial > repos > stevecassidy > nltktools
view g_read_sents.py @ 3:0df72a8ab095 draft default tip
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit f2432aaedd36ae7662873623d8861d0982dffdd2
author | stevecassidy |
---|---|
date | Mon, 20 Nov 2017 22:52:11 -0500 |
parents | a47980ef2b96 |
children |
line wrap: on
line source
import os import nltk from nltk.corpus import PlaintextCorpusReader import argparse nltk.download('punkt', quiet=True) def Parser(): the_parser = argparse.ArgumentParser(description="Segments the text input into separate sentences") the_parser.add_argument('--input', required=True, action="store", type=str, help="input text file") the_parser.add_argument('--output', required=True, action="store", type=str, help="output file path") args = the_parser.parse_args() return args def print_out(outp, sentences): with open(outp, 'w') as output: for sent in sentences: for tok in sent: output.write(tok) output.write(' ') output.write('\n') def find_nth(string, sub, n, offset): start = string.find(sub, offset) while start >= 0 and n > 1: start = string.find(sub, start + len(sub)) n -= 1 return start def count_occurences(lst, string): count = 0 for item in lst: if string in item: count += 1 return count def read_sents(inp, outp): corpus = PlaintextCorpusReader(os.path.dirname(inp), os.path.basename(inp)) sents = corpus.sents() print_out(outp, sents) if __name__ == '__main__': args = Parser() read_sents(args.input, args.output)