move stuff out of the way
This commit is contained in:
79
related/bot.py
Executable file
79
related/bot.py
Executable file
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import logging
|
||||
|
||||
from sleekxmpp import ClientXMPP
|
||||
|
||||
try:
|
||||
from local_config import conf
|
||||
except ImportError:
|
||||
import sys
|
||||
sys.stderr.write('''
|
||||
%s: E: local_config.py isn't tracked because of included secrets and
|
||||
%s site specific configurations. Rename local_config.py.skel and
|
||||
%s adjust to you needs.
|
||||
'''[1:] % (
|
||||
sys.argv[0],
|
||||
' ' * len(sys.argv[0]),
|
||||
' ' * len(sys.argv[0])
|
||||
)
|
||||
)
|
||||
|
||||
sys.exit(-1)
|
||||
|
||||
import time
|
||||
t = -time.time()
|
||||
|
||||
class bot(ClientXMPP):
|
||||
def __init__(self, jid, password, room, nick):
|
||||
ClientXMPP.__init__(self, jid, password)
|
||||
|
||||
self.room = room
|
||||
self.nick = nick
|
||||
|
||||
self.add_event_handler('session_start', self.session_start)
|
||||
self.add_event_handler('groupchat_message', self.muc_message)
|
||||
|
||||
def session_start(self, event):
|
||||
self.get_roster()
|
||||
self.send_presence()
|
||||
|
||||
self.plugin['xep_0045'].joinMUC(
|
||||
self.room,
|
||||
self.nick,
|
||||
wait=True
|
||||
)
|
||||
|
||||
def muc_message(self, msg):
|
||||
print(msg['mucnick'])
|
||||
print(msg['body'])
|
||||
print((msg['from'], msg['from'].bare))
|
||||
|
||||
print(conf('room') == msg['from'].bare)
|
||||
|
||||
# don't talk to yourself
|
||||
if msg['mucnick'] == self.nick:
|
||||
return
|
||||
|
||||
self.send_message(
|
||||
mto=msg['from'].bare,
|
||||
mbody='got[%s]' % msg['body'],
|
||||
mtype='groupchat'
|
||||
)
|
||||
|
||||
if '__main__' == __name__:
|
||||
logging.basicConfig(
|
||||
level=logging.DEBUG,
|
||||
format='%(levelname)-8s %(message)s'
|
||||
)
|
||||
|
||||
xmpp = bot(
|
||||
jid=conf('jid'),
|
||||
password=conf('password'),
|
||||
room=conf('room'),
|
||||
nick=conf('bot_user')
|
||||
)
|
||||
|
||||
xmpp.connect()
|
||||
xmpp.register_plugin('xep_0045')
|
||||
xmpp.process(threaded=False)
|
||||
38
related/levenshtein_test.py
Executable file
38
related/levenshtein_test.py
Executable file
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from common import levenshtein
|
||||
|
||||
(a, b) = ('foo barbaz', 'foobar baz')
|
||||
(a, b) = ('sitting', 'kitten')
|
||||
(a, b) = ('Monte Kali (Heringen)', 'http://de.wikipedia.org/wiki/Monte_Kali_%28Heringen%29')
|
||||
|
||||
(matrix, ret) = levenshtein(a, b, return_table=True)
|
||||
|
||||
sep = ' '*0
|
||||
out = ''
|
||||
for B in b:
|
||||
out += sep + '%2s' % B
|
||||
print(sep + ' '*4 + out)
|
||||
|
||||
for i in range(len(matrix)):
|
||||
if 0 == i:
|
||||
out = ' '
|
||||
else:
|
||||
out = '%2s' % a[i-1]
|
||||
|
||||
for j in range(len(matrix[i])):
|
||||
if 0 == i or 0 == j:
|
||||
col = '30;42'
|
||||
elif i == j:
|
||||
col = '41'
|
||||
else:
|
||||
col = 0
|
||||
|
||||
if 0 != col:
|
||||
out += sep + '\x1b[%sm%2d\x1b[m' %(col, matrix[i][j])
|
||||
else:
|
||||
out += sep + '%2d' % matrix[i][j]
|
||||
|
||||
print(out)
|
||||
|
||||
print(ret)
|
||||
69
related/strsim.py
Executable file
69
related/strsim.py
Executable file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import re
|
||||
|
||||
def str_sim(a, b, do_print=False):
|
||||
a = a.lower()
|
||||
b = b.lower()
|
||||
|
||||
a_parts = re.split('[\W_]+', a)
|
||||
b_parts = re.split('[\W_]+', b)
|
||||
|
||||
# this is a "simple" way to declare out[a][b]
|
||||
out = list(map(list, [[0]*len(b_parts)]*len(a_parts)))
|
||||
|
||||
for i in range(0, len(a_parts)-1):
|
||||
for j in range(0, len(b_parts)-1):
|
||||
if a_parts[i] == b_parts[j]:
|
||||
out[i][j] += 1
|
||||
|
||||
if do_print:
|
||||
i = 0
|
||||
for j in range(0, len(b_parts)):
|
||||
print(' |'*i + ' '*2 + '.- ' + b_parts[j])
|
||||
i += 1
|
||||
print(' |'*i)
|
||||
|
||||
for i in range(0, len(a_parts)):
|
||||
print(' ' + str(out[i]) + ' ' + a_parts[i])
|
||||
|
||||
return out
|
||||
|
||||
def sum_array(array):
|
||||
_sum = 0
|
||||
for a in array:
|
||||
if list == type(a) or tuple == type(a) or hash == type(a):
|
||||
_sum += sum_array(a)
|
||||
elif int == type(a) or float == type(a):
|
||||
_sum += a
|
||||
return _sum
|
||||
|
||||
def wrapper_print(a, b, comment=''):
|
||||
ret = str_sim(a, b, do_print=True)
|
||||
|
||||
if '' != comment:
|
||||
comment = ' ^ ' + comment
|
||||
|
||||
print('[%2dx%2d::%2d]%s' %(len(ret), len(ret[0]), sum_array(ret), comment))
|
||||
|
||||
if '__main__' == __name__:
|
||||
pairs = (
|
||||
(
|
||||
'http://de.wikipedia.org/wiki/Monte_Kali_%28Heringen%29',
|
||||
'Monte Kali (Heringen)'
|
||||
),
|
||||
(
|
||||
'http://www.spiegel.de/politik/ausland/buddhisten-treffen-in-colombo-blitzender-moench-a-994447.html',
|
||||
'Buddhisten-Treffen in Colombo: Blitzender Mönch - SPIEGEL ONLINE'
|
||||
)
|
||||
)
|
||||
|
||||
wrapper_print('foo bar baz', 'foo bar boom')
|
||||
|
||||
for (url, title) in pairs:
|
||||
wrapper_print(title, url, comment='raw')
|
||||
url_no_proto = re.sub(r'https?://[^/]*/', '', url)
|
||||
wrapper_print(title, url_no_proto, comment='no proto/domain')
|
||||
url_no_proto_no_digits = re.sub(r'[0-9]*', '', url_no_proto)
|
||||
wrapper_print(title, url_no_proto_no_digits, comment='no proto/domain/[0-9]')
|
||||
Reference in New Issue
Block a user