-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentenceGenerator.py
More file actions
73 lines (62 loc) · 2.26 KB
/
sentenceGenerator.py
File metadata and controls
73 lines (62 loc) · 2.26 KB
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
# Generating sentences with a Context-Free Grammar
# Ian S. Woodley
from collections import OrderedDict
import random
class CFG(OrderedDict):
def __init__(self, *args):
super().__init__(map(lambda s: s.replace(' ', '').split('->'), args))
def __repr__(self):
return '\n'.join('{} -> {}'.format(k, v) for k, v in self.items())
def getProductions(self, symbol):
return self[symbol].split('|')
# Depth-first walk through tree, selecting random productions
def generateSentence(cfg, start='S'):
string = []
def dfs(root):
local_str = ''
prod = random.choice(cfg.getProductions(root))
for char in prod:
if char in cfg:
result = dfs(char)
if result:
string.append(result)
else:
local_str += char
return local_str
dfs(start)
return ' '.join(string[:-1]).capitalize() + string[-1]
if __name__ == "__main__":
# Example CFG found online
L = [
'S -> NP VP ENDP',
'NP -> DET ADJ_L NOUN',
'VP -> VERB | VERB ADV | VP CONJ VP',
'ADJ_L -> ADJ | ADJ_L ADJ',
'NOUN -> "butterflies" | "flowers" | "days" | "moons" | "waves" | "kisses" | "sighs" | "ideas" | "winds"',
'ADJ -> "painful" | "yellow" | "lonely" | "beautiful" | "colorless"',
'DET -> "the" | "some" | "many" | "these" | "those"',
'VERB -> "die" | "wither" | "sleep" | "wilt" | "disappear"',
'ADV -> "woefully" | "pointlessly" | "slowly" | "selflessly" | "graciously"',
'CONJ -> "and" | "but" | "or"',
'ENDP -> "." | "!" | ". . ."'
]
# Replacing variable names for simpler parsing
table = OrderedDict([
('NP', 'A'),
('VP', 'B'),
('ADJ_L', 'C'),
('NOUN', 'D'),
('ADJ', 'E'),
('DET', 'F'),
('VERB', 'G'),
('ADV', 'H'),
('CONJ', 'I'),
('ENDP', 'J')
])
for i in range(len(L)):
L[i] = L[i].replace('\"', '')
for key in table:
L[i] = L[i].replace(key, table[key])
cfg = CFG(*L)
for _ in range(100):
print(generateSentence(cfg))