Track existing code
This commit is contained in:
commit
f1a295ab3a
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal 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
11
code_random_text.py
Normal 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
26
generate_poem.py
Normal 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
16
generate_random.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user