Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
import re
|
5 |
+
import dataclasses
|
6 |
+
|
7 |
+
import streamlit as st
|
8 |
+
|
9 |
+
from gamestate import persistent_game_state
|
10 |
+
|
11 |
+
st.markdown("""MAD LIBS
|
12 |
+
(dataset is from [a Microsoft EMNLP paper](https://www.microsoft.com/en-us/download/details.aspx?id=55593))
|
13 |
+
Fill in all the fields then click "Generate Story".
|
14 |
+
""")
|
15 |
+
|
16 |
+
|
17 |
+
with open('stories.json') as f:
|
18 |
+
stories = json.load(f)
|
19 |
+
|
20 |
+
|
21 |
+
@dataclasses.dataclass
|
22 |
+
class GameState:
|
23 |
+
story: str
|
24 |
+
game_number: int = 0
|
25 |
+
|
26 |
+
state = persistent_game_state(initial_state=GameState(random.choice(stories)))
|
27 |
+
|
28 |
+
|
29 |
+
if st.button("new story"):
|
30 |
+
state.story = random.choice(stories)
|
31 |
+
state.game_number += 1
|
32 |
+
|
33 |
+
pos = {
|
34 |
+
'cc': 'Coordinating conjunction',
|
35 |
+
'cd': 'Cardinal number',
|
36 |
+
'dt': 'Determiner',
|
37 |
+
'ex': 'Existential there',
|
38 |
+
'fw': 'Foreign word',
|
39 |
+
'in': 'Preposition or subordinating conjunction',
|
40 |
+
'jj': 'Adjective',
|
41 |
+
'jjr': 'Adjective, comparative',
|
42 |
+
'jjs': 'Adjective, superlative',
|
43 |
+
'ls': 'List item marker',
|
44 |
+
'md': 'Modal',
|
45 |
+
'nn': 'Noun, singular or mass',
|
46 |
+
'nns': 'Noun, plural',
|
47 |
+
'nnp': 'Proper noun, singular',
|
48 |
+
'nnps': 'Proper noun, plural',
|
49 |
+
'pdt': 'Predeterminer',
|
50 |
+
'pos': 'Possessive ending',
|
51 |
+
'prp': 'Personal pronoun',
|
52 |
+
'prp$': 'Possessive pronoun',
|
53 |
+
'rb': 'Adverb',
|
54 |
+
'rbr': 'Adverb, comparative',
|
55 |
+
'rbs': 'Adverb, superlative',
|
56 |
+
'rp': 'Particle',
|
57 |
+
'sym': 'Symbol',
|
58 |
+
'to': 'to',
|
59 |
+
'uh': 'Interjection',
|
60 |
+
'vb': 'Verb, base form',
|
61 |
+
'vbd': 'Verb, past tense',
|
62 |
+
'vbg': 'Verb, gerund or present participle',
|
63 |
+
'vbn': 'Verb, past participle',
|
64 |
+
'vbp': 'Verb, non-3rd person singular present',
|
65 |
+
'vbz': 'Verb, 3rd person singular present',
|
66 |
+
'wdt': 'Wh-determiner',
|
67 |
+
'wp': 'Wh-pronoun',
|
68 |
+
'wp$': 'Possessive wh-pronoun',
|
69 |
+
'wrb': 'Wh-adverb',
|
70 |
+
# others
|
71 |
+
'animal': 'Animal',
|
72 |
+
'body': 'Body part',
|
73 |
+
'body_plural': 'Body part, plural',
|
74 |
+
'food': 'Food',
|
75 |
+
'liquid': 'Liquid',
|
76 |
+
}
|
77 |
+
|
78 |
+
|
79 |
+
regex = "<.*?::(.*?)/>"
|
80 |
+
|
81 |
+
parts = re.split(regex, state.story)
|
82 |
+
|
83 |
+
outparts = []
|
84 |
+
|
85 |
+
for i, part in enumerate(parts):
|
86 |
+
if i % 2 == 1:
|
87 |
+
# remove ':'
|
88 |
+
part = part.strip(':')
|
89 |
+
# use two-part key so that new stories get new text boxes
|
90 |
+
answer = st.text_input(pos.get(part, part), key=(state.game_number, i))
|
91 |
+
|
92 |
+
outparts.append(f"**{answer}**" if answer else "")
|
93 |
+
else:
|
94 |
+
outparts.append(part)
|
95 |
+
|
96 |
+
if all(outparts) and st.button("generate madlib"):
|
97 |
+
st.markdown("".join(outparts))
|