-
Notifications
You must be signed in to change notification settings - Fork 41
/
gen.py
32 lines (27 loc) · 852 Bytes
/
gen.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
from parse import Parser
class Generator:
def __init__(self, name, db, rnd):
self.name = name
self.db = db
self.rnd = rnd
def _get_next_word(self, word_list):
candidate_words = self.db.get_word_count(word_list)
total_next_words = sum(candidate_words.values())
i = self.rnd.randint(total_next_words)
t=0
for w in candidate_words.keys():
t += candidate_words[w]
if (i <= t):
return w
assert False
def generate(self, word_separator):
depth = self.db.get_depth()
sentence = [Parser.SENTENCE_START_SYMBOL] * (depth - 1)
end_symbol = [Parser.SENTENCE_END_SYMBOL] * (depth - 1)
while True:
tail = sentence[(-depth+1):]
if tail == end_symbol:
break
word = self._get_next_word(tail)
sentence.append(word)
return word_separator.join(sentence[depth-1:][:1-depth])