From f1a295ab3a406aa2405320263b6d4b65103c83bc Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Sun, 7 Jun 2015 16:27:59 -0400 Subject: [PATCH] Track existing code --- .gitignore | 5 +++++ code_random_text.py | 11 +++++++++++ generate_poem.py | 26 ++++++++++++++++++++++++++ generate_random.py | 16 ++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 .gitignore create mode 100644 code_random_text.py create mode 100644 generate_poem.py create mode 100644 generate_random.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a75cc9d --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +[._]*.s[a-w][a-z] +[._]s[a-w][a-z] +*.pyc +lib +pyStatParser diff --git a/code_random_text.py b/code_random_text.py new file mode 100644 index 0000000..5b3d711 --- /dev/null +++ b/code_random_text.py @@ -0,0 +1,11 @@ +import nltk +# Natural Language Toolkit: code_random_text + +def generate_model(cfdist, word, num=15): + for i in range(num): + print word, + word = cfdist[word].max() + +text = nltk.corpus.genesis.words('english-kjv.txt') +bigrams = nltk.bigrams(text) +cfd = nltk.ConditionalFreqDist(bigrams) # [_bigram-condition] diff --git a/generate_poem.py b/generate_poem.py new file mode 100644 index 0000000..60f07bc --- /dev/null +++ b/generate_poem.py @@ -0,0 +1,26 @@ +import nltk +import random +from stat_parser import Parser + + +class PoemGenerator(): + def __init__(self, corpus): + self.sents = corpus.sents('austen-emma.txt') + self.bigrams = list(nltk.bigrams(corpus.words('austen-emma.txt'))) + self.cfd = nltk.ConditionalFreqDist(self.bigrams) + self.parser = Parser() + self.history = [] + + def generate_poem(self): + sent = random.choice(self.sents) + parsed = self.parser.parse(' '.join(sent)) + word = random.choice(self.bigrams)[0] + for i in range(15): + print word, + for gram in self.cfd[word].items(): + import ipdb; ipdb.set_trace() # BREAKPOINT + + +if __name__ == '__main__': + generator = PoemGenerator(nltk.corpus.gutenberg) + print generator.generate_poem() diff --git a/generate_random.py b/generate_random.py new file mode 100644 index 0000000..59e3b6d --- /dev/null +++ b/generate_random.py @@ -0,0 +1,16 @@ +import nltk +import random + +TEXT = nltk.corpus.genesis.words('english-kjv.txt') + + +def main(): + bigrams = nltk.bigrams(TEXT) + cfdist = nltk.ConditionalFreqDist(bigrams) + word = random.choice(bigrams)[0] + for i in range(15): + print word, + word = cfdist[word].max() + +if __name__ == '__main__': + main()