-
Notifications
You must be signed in to change notification settings - Fork 85
/
generate_poem.py
183 lines (155 loc) · 6.44 KB
/
generate_poem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'''
================================================
## VOICEBOOK REPOSITORY ##
================================================
repository name: voicebook
repository version: 1.0
repository link: https://github.com/jim-schwoebel/voicebook
author: Jim Schwoebel
author contact: [email protected]
description: a book and repo to get you started programming voice applications in Python - 10 chapters and 200+ scripts.
license category: opensource
license: Apache 2.0 license
organization name: NeuroLex Laboratories, Inc.
location: Seattle, WA
website: https://neurolex.ai
release date: 2018-09-28
This code (voicebook) is hereby released under a Apache 2.0 license license.
For more information, check out the license terms below.
================================================
## LICENSE TERMS ##
================================================
Copyright 2018 NeuroLex Laboratories, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
================================================
## SERVICE STATEMENT ##
================================================
If you are using the code written for a larger project, we are
happy to consult with you and help you with deployment. Our team
has >10 world experts in Kafka distributed architectures, microservices
built on top of Node.js / Python / Docker, and applying machine learning to
model speech and text data.
We have helped a wide variety of enterprises - small businesses,
researchers, enterprises, and/or independent developers.
If you would like to work with us let us know @ [email protected].
================================================
## GENERATE_POEM.PY ##
================================================
Use natural language processing techniques to generate
a poem in the format ABQBA.
This was trained on my own poetry (59 poems) over the years.
Feel free to add in other texts as you see fit.
'''
import nltk, os, re, random
from collections import Counter
import numpy as np
from textblob import TextBlob
from nltk.corpus import genesis
def randselect(stringlist):
length=len(stringlist)-1
randnum=random.randint(0,length)
return stringlist[randnum]
# ask user if they'd like a random poem
randompoem=input('would you like a random poem? \n')
os.chdir(os.getcwd()+'/data')
poetry=open('poetry.txt', encoding="utf8").read()
tokens=poetry.split()
text=nltk.Text(tokens)
tags=nltk.pos_tag(text)
# get POS lists
verbs=list()
nouns=list()
adjectives=list()
adverbs=list()
vbed=list()
ned=list()
eadj=list()
nounend=list()
if randompoem in ['y','yes']:
for i in range(len(tags)):
if tags[i][1] in ['VB','VBD']:
verbs.append(tags[i][0])
if tags[i][0].endswith('ed')==True:
vbed.append(tags[i][0])
elif tags[i][1] in ['NN','NNS']:
nouns.append(tags[i][0])
if tags[i][0].endswith('ed')==True:
ned.append(tags[i][0])
elif tags[i][1] in ['JJ','JJS']:
adjectives.append(tags[i][0])
if tags[i][0].endswith('e')==True:
eadj.append(tags[i][0])
elif tags[i][1] in ['RB','RBS']:
adverbs.append(tags[i][0])
else:
pass
poemname=randselect(nouns)
#random selection of a noun
description='I feel %s and %s and %s toward %s'%(randselect(adjectives),randselect(adjectives),randselect(adjectives),poemname)
#I feel [adjective] and [adjective] and [adjective] towards [poemname].
tokens2=description.split()
text2=nltk.Text(tokens)
tags2=nltk.pos_tag(text)
name=poemname.title()
for i in range(len(tags)):
if tags[i][1] in ['NN','NNS']:
if tags[i][0].endswith(name[len(name)-1])==True:
nounend.append(tags[i][0])
#make a poem - funny
poem=open(name+'3.txt','w')
poem.write(name)
for i in range(5):
poem.write('\n\n')
#X5 stanzas
poem.write("I seek a %s %s \n"%(randselect(adjectives),name))
poem.write("The %s is %s \n"%(randselect(nouns),randselect(eadj)))
poem.write("Why is it %s to %s? \n"%(randselect(adverbs),randselect(verbs)))
poem.write("The %s is %s \n"%(randselect(nouns),randselect(eadj)))
poem.write("The %s is a %s %s \n"%(randselect(nouns),randselect(adjectives),randselect(nounend)))
poem.close()
elif randompoem in ['n','no']:
poemname=input('what is the name of the poem? (noun) \n')
description=input('what is the description? \n')
tokens2=description.split()
text2=nltk.Text(tokens)
tags2=nltk.pos_tag(text)
name=poemname.title()
for i in range(len(tags)):
if tags[i][1] in ['VB','VBD']:
verbs.append(tags[i][0])
if tags[i][0].endswith('ed')==True:
vbed.append(tags[i][0])
elif tags[i][1] in ['NN','NNS']:
nouns.append(tags[i][0])
if tags[i][0].endswith('ed')==True:
ned.append(tags[i][0])
if tags[i][0].endswith(name[len(name)-1])==True:
nounend.append(tags[i][0])
elif tags[i][1] in ['JJ','JJS']:
adjectives.append(tags[i][0])
if tags[i][0].endswith('e')==True:
eadj.append(tags[i][0])
elif tags[i][1] in ['RB','RBS']:
adverbs.append(tags[i][0])
else:
pass
#make a poem - funny
poem=open(name+'.txt','w')
poem.write(name)
for i in range(5):
poem.write('\n\n')
#X5 stanzas
poem.write("I seek a %s %s \n"%(randselect(adjectives),name))
poem.write("The %s is %s \n"%(randselect(nouns),randselect(eadj)))
poem.write("Why is it %s to %s? \n"%(randselect(adverbs),randselect(verbs)))
poem.write("The %s is %s \n"%(randselect(nouns),randselect(eadj)))
poem.write("The %s is a %s %s \n"%(randselect(nouns),randselect(adjectives),randselect(nounend)))
poem.close()