-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWordMarkovChain.py
323 lines (264 loc) · 10 KB
/
WordMarkovChain.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# -*- coding: utf-8 -*-
import random
import gzip
class RandomCollection:
def __init__(self):
self.total = 0
self.frequencies = {}
def add_occurrence(self, item, count = 1):
self.total += count
if item in self.frequencies:
self.frequencies[item] += count
else:
self.frequencies[item] = count
def remove_occurrence(self, item, count = 1):
if item in self.frequencies:
if self.frequencies[item] > count:
self.total -= count
self.frequencies[item] -= count
else:
self.total -= self.frequencies[item]
del self.frequencies[item]
return True
else:
return False
def remove_item(self, item):
if item in self.frequencies:
self.total -= self.frequencies[item]
del self.frequencies[item]
return True
else:
return False
def choose_one(self):
if self.total > 0:
chosen = random.randrange(0, self.total)
current = 0
for item in self.frequencies:
current += self.frequencies[item]
if current > chosen:
return item
else:
return None
def occurrences_of(self, item):
if item in self.frequencies:
return self.frequencies[item]
else:
return 0
def total_occurrences(self):
return self.total
def probability_of(self, item):
if item in self.frequencies:
return float(self.frequencies[item]) / float(self.total)
else:
return float(0)
def probabilities(self):
ret = {}
for item in self.frequencies:
ret[item] = self.probability_of(item)
return ret
def __iter__(self):
return self.frequencies.__iter__()
class Word:
def __init__(self, string):
self.string = string
self.transitions = RandomCollection()
def __str__(self):
return self.string.__str__()
def __unicode__(self):
return self.string.decode("utf-8")
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.string == other.string
return NotImplemented
def __ne__(self, other):
if isinstance(other, self.__class__):
return not self.__eq__(other)
return NotImplemented
def __hash__(self):
return hash(self.string)
def __iter__(self):
return self.transitions.__iter__()
def add_transition_to(self, word, count = 1):
self.transitions.add_occurrence(word, count)
def remove_transition_to(self, word, count = 1):
return self.transitions.remove_occurrence(word, count)
def remove_link_to(self, word):
return self.transitions.remove_item(word)
def add_occurrence_at_end(self, count = 1):
self.transitions.add_occurrence(False, count)
def remove_occurrence_at_end(self, count = 1):
return self.transitions.remove_occurrence(False, count)
def remove_all_occurrences_at_end(self):
return self.transitions.remove_item(False)
def generate_next_word(self):
return self.transitions.choose_one()
def probabilities(self):
return self.transitions.probabilities()
def transitions_to(self, word):
return self.transitions.occurrences_of(word)
class WordMarkovChain:
def __init__(self):
self.words = {}
self.start_words = RandomCollection()
self.randomness = 0.1
def remove_word(self, string):
if string in self.words:
word = self.words[string]
for other_word in self.words:
self.words[other_word].remove_link_to(word)
del self.words[string]
return True
else:
return False
def add_transition_between(self, string1, string2, count = 1):
word1 = self._add_word(string1)
word2 = self._add_word(string2)
word1.add_transition_to(word2, count)
def remove_transition_between(self, string1, string2, count = 1):
word1 = self._get_word(string1)
word2 = self._get_word(string2)
if word1 and word2:
return word1.remove_transition_to(word2, count)
else:
return False
def add_occurrence_at_start(self, string, count = 1):
word = self._add_word(string)
self.start_words.add_occurrence(word, count)
def remove_occurrence_at_start(self, string, count = 1):
word = self._get_word(string)
if word:
return self.start_words.remove_occurrence(word, count)
else:
return False
def add_occurrence_at_end(self, string, count = 1):
word = self._add_word(string)
word.add_occurrence_at_end(count)
def remove_occurrence_at_end(self, string, count = 1):
word = self._get_word(string)
if type(word) == Word:
return word.remove_occurrence_at_end(count)
else:
return False
def add_message(self, strings):
if len(strings) > 0:
word1 = self._add_word(strings.pop(0))
self.start_words.add_occurrence(word1)
for string in strings:
word2 = self._add_word(string)
word1.add_transition_to(word2)
word1 = word2
word1.add_occurrence_at_end()
def build_message(self, start = False):
# This list will contain the words of the generated message
message = []
if start:
# If a first word for the message was provided,
# retrieves this first word and adds it to the message
cur_word = self._get_word(start)
if not cur_word:
# If the word provided is not in the database, generate
# a random one
message.append(start)
cur_word = self._generate_first_word()
message.append(unicode(cur_word))
# What follows enforces at least a second word in the message
next_word = cur_word.generate_next_word()
while not next_word:
if random.random() < 0.05:
# This is to break infinite loops if the word has no
# non-empty successor
# (Could be improved by looking that up instead of
# relying in randomness)
next_word = self._generate_random_word()
else:
next_word = cur_word.generate_next_word()
cur_word = next_word
else:
# Generates a random first word for the message
cur_word = self._generate_first_word()
while cur_word:
# While message end has not been reached, keeps adding a word
# and generating a next one
if random.random() < self.randomness:
# With certain chance, generate a completely random word
cur_word = self._generate_random_word()
message.append(unicode(cur_word))
cur_word = cur_word.generate_next_word()
# Return the message as a space-joined string
return " ".join(message)
def set_randomness(self, p):
if 0 <= p <= 1:
self.randomness = p
else:
raise(ValueError, "Randomness should be a number between 0 and 1")
def probabilities_for(self, string):
word = self._get_word(string)
if word:
ret = {}
probabilities = word.probabilities()
for word in probabilities:
if word:
ret[str(word)] = probabilities[word]
else:
ret[word] = probabilities[word]
return ret
else:
return False
def export_chain(self, filename):
# f = gzip.open(filename, 'w')
f = open(filename, 'w')
for word in self.start_words:
occurrences = str(self.start_words.occurrences_of(word))
line = ["-", "start", word.string, occurrences]
f.write(" ".join(line) + "\n")
for word1 in self.words:
for word2 in self.words[word1]:
occurrences = str(self.words[word1].transitions_to(word2))
if word2:
line = [word1, word2.string, occurrences]
else:
line = ["-", "end", word1, occurrences]
f.write(" ".join(line) + "\n")
f.close()
def import_chain(self, filename, reverse = False):
# f = gzip.open(filename, 'r')
f = open(filename, 'r')
for line in f:
line = line.split()
if len(line) == 4:
if line[1] == "start":
if not reverse:
self.add_occurrence_at_start(line[2], int(line[3]))
else:
self.add_occurrence_at_end(line[2], int(line[3]))
elif line[1] == "end":
if not reverse:
self.add_occurrence_at_end(line[2], int(line[3]))
else:
self.add_occurrence_at_start(line[2], int(line[3]))
elif len(line) == 3:
if not reverse:
self.add_transition_between(line[0], line[1], int(line[2]))
else:
self.add_transition_between(line[1], line[0], int(line[2]))
f.close()
def _add_word(self, string):
if string in self.words:
return self.words[string]
else:
word = Word(string)
self.words[string] = word
return word
def _get_word(self, string):
if string in self.words:
return self.words[string]
else:
return False
def _generate_first_word(self):
return self.start_words.choose_one()
def _generate_random_word(self):
if self.words:
chosen = random.choice(self.words.keys())
return self.words[chosen]
else:
return False