workman161 / pointything

This URL has Read+Write access

pointything / markov.py
100644 142 lines (127 sloc) 3.585 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import re
import os
import sys
 
class Graph:
    SENTENCE_END = [".", "!", "?"]
    def __init__(self):
        self.nodes = {}
    
    def getWord(self, word):
        end = word[-1]
        word = re.sub('\W', '', word.lower())
        if word == "":
            return None
        if (not (word in self.nodes)):
            #print "New word %s"%word
            self.nodes[word] = Word(word)
        if end in Graph.SENTENCE_END:
            self.nodes[word].addNext(None)
        return self.nodes[word]
 
class Word:
    def __init__(self, word):
        self.word = word
        self.count = 0
        self.next = {}
        self.prev = {}
        self.score = 0
    
    def addNext(self, other):
        if other in self.next:
            self.next[other]+=1
        else:
            self.next[other] = 1
    
    def addPrev(self, other):
        if other in self.prev:
            self.prev[other]+=1
        else:
            self.prev[other] = 1
    
    def trimList(self, lst, limit=-1):
        if len(lst)==0:
            return lst
        total = 0
        minval = sys.maxint
        maxval = 0
        for i in lst:
            n = lst[i]
            if n>maxval:
                maxval = n
            if n<minval:
                minval = n
            total+=n
        mean = total/len(lst)
        if limit == -1:
            limit = mean/7
        #print "%s Min: %i Max: %i Mean: %i Limit: %i Total: %i Size: %i"%(self, minval, maxval, mean, limit, total, len(lst))
        ret = {}
        for i in lst:
            if lst[i]<=limit:
                continue
            ret[i] = lst[i]
        return ret
    
    def getPrev(self):
        return self.getRandom(self.prev)
    
    def getNext(self):
        return self.getRandom(self.next)
    
    def getRandom(self, lst):
        if len(lst) == 0:
            return None
        smaller = self.trimList(self.trimList(lst, 1))
        total = 0
        for i in smaller:
            total+=smaller[i]
        rand = random.randint(0, total)
        for i in smaller:
            rand-=lst[i]
            if rand<=0:
                if (not (i == None)):
                    i.score = lst[i]
                return i
        return None
    
    def __str__(self):
        return self.word
        #return self.word+("(%s)"%self.score)
 
def loadFile(f):
    line = f.readline()
    i = 0
    while (line != ""):
        i+=1
        line = line.strip()
        if i % 2000 == 0:
            print "\r%s"%i
        text = prog.search(line)
        if text != None:
            text = text.group(1)
            prev = None
            for word in text.split(' '):
                if word == "":
                    continue
                word = g.getWord(word)
                if word != None:
                    word.addPrev(prev)
                if prev != None:
                    prev.addNext(word)
                prev = word
        line = f.readline()
 
prog = re.compile("^\[.*?\] \[.*?\] <.*?>(.*)$")
g = Graph()
 
for log in os.listdir("/home/trever/logs/"):
    print "Loading %s"%log
    f = open("/home/trever/logs/"+log)
    loadFile(f)
 
print "Ready."
while True:
    word = sys.stdin.readline().strip()
    if word == "":
        continue
    word = g.getWord(word)
    next = word.getNext()
    prev = word.getPrev()
    ret = str(word)
    while prev != None:
        ret=str(prev)+" "+ret
        prev = prev.getPrev()
    while next != None:
        ret=ret+" "+str(next)
        next = next.getNext()
    
    print ret