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
|
"""
Recreational plugin.
Message parser that can generate sentences based on what he has already seen
before.
"""
from plugin import BasePlugin
from random import choice
from re import split as rsplit
import pickle
import tabs
class Dico(object):
def __init__(self):
self.start_words = []
self.end_words = []
self.words = {}
def add_next(self, word, next):
w = self.words[word]
if next in w:
w[next] += 1
else:
w[next] = 1
def add_word(self, word):
if not word in self.words:
self.words[word] = {}
def select_next(self, word):
d = sorted(self.words[word], key=lambda w: self.words[word][w], reverse=True)
if not d:
return ''
nexts = d[:10]
for i in range(0, len(d) // 10):
nexts.append(choice(d[9:]))
return choice(nexts)
def create_sentence(self, length):
if not self.start_words:
return ''
current = choice(self.start_words)
i = 1
sent = current.capitalize()
while current and self.words[current] and i < length:
current = self.select_next(current)
sent += " " + current
i += 1
return sent
def save(self, fname):
file = open(fname, 'wb')
pickle.dump(self, file)
file.close
spaces = ' '
end_sentence = ['.', '?', '!']
def end_re():
r = '('
for i in end_sentence[:]:
end_sentence.append('%s ' % i)
i = '\%s'% i
r += '%s$|%s |' % (i, i)
r = r[:-1]
r += ')'
return r
end = end_re()
class Analyzer(object):
dico = None
def __init__(self):
pass
def parse(self, text):
text = text.replace('\n', '')
res = rsplit(end, text)
for i in res[:]:
if i == '':
continue
elif i in end_sentence:
continue
self.analyze(i)
def analyze(self, text):
prev = None
for word in rsplit('[%s]'%spaces, text):
if word in spaces: continue
word = word.lower()
self.dico.add_word(word)
if prev:
self.dico.add_next(prev, word)
else:
self.dico.start_words.append(word)
prev = word
class Plugin(BasePlugin):
def init(self):
self.add_event_handler('groupchat_message', self.on_groupchat_message)
self.add_tab_command(tabs.MucTab, 'random', self.command_random, '/random [n]\nRandom: Send a random message, if n is provided and is integer > 1, the message will have a maximum number of n words', None)
self.add_tab_command(tabs.MucTab, 'start', self.command_start, '/start\nStart: Start parsing the messages', None)
self.add_tab_command(tabs.MucTab, 'stop', self.command_stop, '/stop\nStop: Stop parsing the messages', None)
self.add_tab_command(tabs.MucTab, 'flush', self.command_flush, '/flush\nFlush: Flush the database', None)
self.add_tab_command(tabs.MucTab, 'save', self.command_save, '/save <filepath>\nSave: Save the database to a file', None)
self.add_tab_command(tabs.MucTab, 'load_db', self.command_load_db, '/load_db <filepath>\nLoad: Load the database from a file', None)
self.tabs = {}
self.analyzer = Analyzer()
def command_start(self, arg):
name = self.core.current_tab().get_name()
if not name in self.tabs:
self.tabs[name] = Dico()
self.core.information('Started analyzing in %s' % name, 'Info')
else:
self.core.information('Already started', 'Info')
def command_stop(self, arg):
name = self.core.current_tab().get_name()
if name in self.tabs:
del self.tabs[name]
self.core.information('Stopped analyzing in %s' % name, 'Info')
else:
self.core.information('Nothing to stop', 'Info')
def command_save(self, arg):
name = self.core.current_tab().get_name()
if name in self.tabs:
try:
self.tabs[name].save(arg)
except:
self.core.information('Could not save the file', 'Info')
else:
self.core.information('Nothing to save', 'Info')
def command_flush(self, arg):
name = self.core.current_tab().get_name()
if name in self.tabs:
del self.tabs[name]
self.tabs[name] = Dico()
self.core.information('Database flushed', 'Info')
else:
self.core.information('Nothing to flush', 'Info')
def command_load_db(self, arg):
name = self.core.current_tab().get_name()
try:
file = open(arg, 'rb')
self.tabs[name] = pickle.load(file)
file.close()
self.core.information('File loaded', 'Info')
except:
self.core.information('Could not load the file', 'Info')
def on_groupchat_message(self, message):
if not message['body']:
return
jid = message['from']
if jid.bare not in self.tabs or jid.resource == self.core.current_tab().own_nick:
return
jid = jid.bare
self.analyzer.dico = self.tabs[jid]
self.analyzer.parse(message['body'])
def command_random(self, arg):
name = self.core.current_tab().get_name()
try:
i = int(arg)
if i < 1:
i = 1
except:
i = 25
if name in self.tabs:
self.core.send_message(self.tabs[name].create_sentence(i))
|