Track existing code

This commit is contained in:
Tyler Hallada 2015-06-07 16:27:59 -04:00
commit f1a295ab3a
4 changed files with 58 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.pyc
lib
pyStatParser

11
code_random_text.py Normal file
View File

@ -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]

26
generate_poem.py Normal file
View File

@ -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()

16
generate_random.py Normal file
View File

@ -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()