summaryrefslogtreecommitdiffstats
path: root/markov.py
blob: d90f76a8a3aeade27c856e69bdae2a2c9e20ac55 (plain)
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
323
324
#!/usr/bin/env python
# Copyright (C) 2010-2011 sand <daniel@spatof.org>
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation and/or
#    other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products derived
#    from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# credits:
# http://code.autistici.org/trac/hmbot/browser
# http://www.eflorenzano.com/blog/post/writing-markov-chain-irc-bot-twisted-and-python/
# http://uswaretech.com/blog/2009/06/pseudo-random-text-markov-chains-python/

import random
import cPickle as pickle
import os
import re
from collections import defaultdict
from utils import *

class Markov(object):
    def __init__(self, n=2, max_words=25, brain_file='brain.new',
            sample_text='/Users/sand/Downloads/i_promes.txt', autosave=50):
        """autosave = 0 per disattivare"""

        self.markov = defaultdict(list)
        self.brain_file = brain_file
        self.sample_text = sample_text
        self.autosave = autosave
        self.counter = 0
        self.stopwords = set([x.strip() for x in open("stopwords_it.txt")])
        self.n = n
        self.max_words = max_words
        self.beginnings = []
        self.ngrams = {}

        if os.path.exists(self.brain_file):
            fd = open(self.brain_file, 'rb')
            self.markov = pickle.load(fd)
            fd.close()
        else:
            self.learn_from_file(self.sample_text)

    def tokenize(self, text):
        return text.split(" ")

    def dump_brain(self):
        fd = open(self.brain_file, 'wb')
        pickle.dump(self.markov, fd, pickle.HIGHEST_PROTOCOL)
        fd.close()

    def learn_from_file(self, filename):
        fd = open(filename, 'r')
        fd.seek(0)
        data = fd.read()
        data = unicodize(data)
        words = data.split()
        fd.close()
        if len(words) < 2:
            print "Poche parole nel file da imparare."
            raise Exception
        else:
            print "totale parole: %i" % (len(words))

        # avoid autosave
        old_autosave = self.autosave
        self.autosave = 0
        self.learn(words)
        self.autosave = old_autosave

        self.dump_brain()

    def learn(self, words):
        if len(words) < 2:
            return None

        chain = [None, None]
        for word in words:
            chain[0], chain[1] = chain[1], word
            if chain[0]:
                self.markov[chain[0]].append(chain[1])

        if self.autosave > 0:
            self.counter += 1
            if self.counter >= self.autosave:
                self.counter = 0
                self.dump_brain()

    # hmbot
    def createProbabilityHash(self, words):
        numWords = len(words)
        wordCount = {}
        for word in words:
            if wordCount.has_key(word):
                wordCount[word] += 1
            else:
                wordCount[word] = 1

        for word in wordCount.keys():
            wordCount[word] /= 1.0 * numWords
        return wordCount

        # mmm... XXX
        # calcolarlo in fase di gen()erazione ?
        # forse lento, ma almeno non perdo i valori originali

    def new_learn(self, text):
        tokens = self.tokenize(text)

        # PULIRE IL TESTO!

        if len(tokens) < self.n:
            return

        # fare probability hash per questi ?
        beginning = tuple(tokens[:self.n])
        self.beginnings.append(beginning)

        for i in range(len(tokens) - self.n):
            gram = tuple(tokens[i:i+self.n])
            next = tokens[i+self.n]

            if gram in self.ngrams:
                self.ngrams[gram].append(next)
            else:
                self.ngrams[gram] = [next]

    def gen(self, sample=None, max_words=20):
        message = []

        if sample:
            sample_words = sample.split()
            sample_words = filter (lambda x: x not in self.stopwords,
                    sample_words)
            random.shuffle(sample_words)
            for word in sample_words:
                if self.markov.has_key(word):
                    message.append(word)
                    break

        # non ci sono dati sample, ne prendo uno a caso
        if len(message) == 0:
            message.append(random.choice(self.markov.keys()))

        while len(message) < max_words:
            if self.markov.has_key(message[-1]):
                message.append(random.choice(self.markov[message[-1]]))
            else:
                message.append(random.choice(self.markov.keys()))

            # mi fermo se c'e' un punto nella frase
            if message[-1].endswith('.'):
                break

        return (' '.join(message) + '.').capitalize()


class NewMarkov(object):
    def __init__(self, n=2, max=100):
        self.n = n
        #self.ngrams = {}
        self.ngrams = defaultdict(list)
        self.max = max
        self.beginnings = []
        self.stopwords = set([x.strip() for x in open("stopwords_it.txt")])

    def tokenize(self, text):
        return text.split(" ")

    def concatenate(self, elements):
        return ' '.join(elements)

    def feed(self, text):
        tokens = self.tokenize(text)

        if len(tokens) < self.n:
            return

        beginning = tuple(tokens[:self.n])
        self.beginnings.append(beginning)

        for i in range(len(tokens) - self.n):
            gram = tuple(tokens[i:i+self.n])
            next = tokens[i+self.n]

            self.ngrams[gram].append(next)
            #if gram in self.ngrams:
            #    self.ngrams[gram].append(next)
            #else:
            #    self.ngrams[gram] = [next]

    def pulisci(self, text):
        for pattern, repl in [
                (r'(?<![:;=])[\(\)]', " "),
                (r'["-]', " "),
                (r'\s+:\s', ": "),
                (r'\s+,\s', ", "),
                (r'\s+\.\s', ". "),
                (r'\s+\.+(?=\s|$)', '...'),
                (r' +', " "),
                (r'\.+', ".")
                ]:
            text = re.sub(pattern, repl, text)
        return text

    def generate(self):
        from random import choice

        current = choice(self.beginnings)
        output = list(current)

        for i in range(self.max):
            if current in self.ngrams:
                possible_next = self.ngrams[current]
                next = choice(possible_next)
                output.append(next)
                current = tuple(output[-self.n:])
            else:
                break
        output_str = self.concatenate(output)
        if not output_str.endswith('.'):
            output_str += '.'

        return output_str

    def dump_brain(self):
        fd = open("brain_new.b", "wb")
        pickle.dump(self.ngrams, fd, pickle.HIGHEST_PROTOCOL)
        fd.close()

        fd = open("brain_new.n", "wb")
        pickle.dump(self.beginnings, fd, pickle.HIGHEST_PROTOCOL)
        fd.close()


def usage():
    print "%s: [-h] [-g \"sample text\"] [-i \"input file\"]" % ("markov.py")

if __name__ == '__main__':
    import getopt, sys
    import time

    # new
    p = NewMarkov(2)
    #fd = open('/Users/sand/Downloads/i_promes.txt', 'r')
    #for line in fd:
    #    for sentence in re.split("\.{1,3}(?:\n|\s+)", line):
    #        sentence = p.pulisci(sentence)
    #        p.feed(sentence)
    #fd.close()
    #fd = open('/Users/sand/Downloads/retro.log', 'r')


# NewMarkov
    #i = 0
    #fd = open('/tmp/porcoddio3.log', 'r')
    #print "Inizio alle: " + time.strftime("%H:%M %d-%m-%y")
    #for line in fd:
    #    p.feed(p.pulisci(line))
    #    i += 1
    #    if i >= 1000:
    #        print "1000"
    #        i = 0
    #fd.close()
    #print "Inizio il dump alle: " + time.strftime("%H:%M %d-%m-%y")
    #p.dump_brain()
    print "Inizio il load 1 del brain alle: " + time.strftime("%H:%M %d-%m-%y")
    fd = open("brain_new.b", "rb")
    p.ngrams = pickle.load(fd)
    fd.close()

    print "Inizio il load 2 del brain alle: " + time.strftime("%H:%M %d-%m-%y")
    fd = open("brain_new.n", "rb")
    p.beginnings = pickle.load(fd)
    fd.close()

    print "Inizio a generare tuo genero alle: " + time.strftime("%H:%M %d-%m-%y")
    for i in range(10):
        print p.generate()
        print "-"

    sys.exit()


    # end
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hri:g:", ["help", "random", "import=",
            "generate="])
    except getopt.GetoptError, err:
        print str(err)
        usage()
        sys.exit(2)

    m = Markov(autosave=0)

    for o, a in opts:
        if o in ("-g", "--generate"):
            print m.gen(a)
        elif o in ("-i", "--import"):
            m.learn_from_file(a)
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-r", "--random"):
            print m.gen()
        else:
            assert False, "unhandled option"