-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmadlibs.py
32 lines (28 loc) · 937 Bytes
/
madlibs.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
import random
import nltk
from nltk.tag import map_tag
def main():
"""Selects a random paragraph from paragraphs.txt,
and uses NLTK position tagging (excluding adp, conj
det, and prt) to generate an interative madlibs
dialog
"""
exclude = ['ADP', 'CONJ', 'DET', 'PRT']
text = open("paragraphs.txt").read()
paragraphs = text.split("\n")
num = random.randint(0, len(paragraphs)-1)
paragraph = text.split("\n")[num]
words = paragraph.split(' ')
for x in range(5):
num = random.randint(0, len(words)-1)
word = [words[num]]
part = nltk.pos_tag(word)
simplePart = [(wor, map_tag('en-ptb', 'universal', tag))
for wor, tag in part][0][1]
if simplePart not in exclude:
print(simplePart)
word = input()
words[num] = "__%s__" % word
print(' '.join(words))
if __name__ == "__main__":
main()