Finally ready to like add this now oh man this is a huge thing sort of

Added CC-Nanotrasen, an IRC bot made by Skibiliano and given to us by him under CC-BY-SA 3.0 licensing
WHAT DOES THIS MEAN? It means all servers running this SVN now have the option to use an easy to configure IRC bot capable of relaying adminhelps from ingame to the server/channel of their choice.
- Runs on python 2.6 scripts with psyco support
- Relaying can be toggled from config.txt (so you don't runtime with every adminhelp if you decide not to use it)
- Comes with a bunch of other useful and fun tools too
- Added a new global proc, send2irc(msg,msg2) YOU'LL NEVER GUESS WHAT IT DOES CONSIDERING WHAT I JUST MENTIONED
----msg and msg2 are just what text gets relayed to irc, separated by a |, for instance send2irc(hello, world) would come out as
"CC_NanoTrasen: Hello | World


git-svn-id: http://tgstation13.googlecode.com/svn/trunk@2783 316c924e-a436-60f5-8080-3fe189b3f50e
This commit is contained in:
quartz235@gmail.com
2011-12-23 17:39:47 +00:00
parent 240a9bfac5
commit 4472cecbd2
33 changed files with 5954 additions and 2 deletions

203
bot/Marakov_Chain.py Normal file
View File

@@ -0,0 +1,203 @@
import pickle
import random
import os
import sys
import time
import CORE_DATA
def merge(d1, d2, merger=lambda x,y:x+y):
#http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-dictionaries-as-a-single-expression
result = dict(d1)
for k,v in d2.iteritems():
if k in result:
result[k] = merger(result[k], v)
else:
result[k] = v
return result
full_data = {}
imported_data = {}
try:
tiedostot = os.listdir("Marakov")
except:
os.mkdir("Marakov")
tiedostot = os.listdir("Marakov")
else:
pass
listaus = []
for i in tiedostot:
if "marakov." not in i.lower():
pass
else:
listaus.append(i)
for i in listaus:
tiedosto = open("Marakov/"+i,"r")
old_size = len(full_data.keys())
if i != "Marakov.Cache":
imported_data = merge(imported_data,pickle.load(tiedosto))
print "Added contents of "+i+" (Import)"
print "Entries: "+str(len(imported_data))
else:
full_data = merge(full_data,pickle.load(tiedosto))
new_size = len(full_data.keys())
print "Added contents of "+i
print "Entries: "+str(new_size-old_size)
time.sleep(0.1)
def give_data(data):
state = False
for a,b in zip(data.split(" "),data.split(" ")[1:]):
a = a.lower().replace(",","").replace(".","").replace("?","").replace("!","").replace("(","").replace(")","").replace("[","").replace("]","").replace('"',"").replace("'","")
b = b.lower().replace(",","").replace(".","").replace("?","").replace("!","").replace("(","").replace(")","").replace("[","").replace("]","").replace('"',"").replace("'","")
if a not in [CORE_DATA.prefix+"marakov"]+CORE_DATA.SName:
state = True
if a[:7] == "http://" or a[:7] == "http:\\\\" or a[:4] == "www.":
pass
else:
try:
if b not in full_data[a]:
full_data[a].append(b)
except:
try:
if b not in imported_data[a]:
pass
except:
full_data[a] = []
full_data[a].append(b)
if state == True:
tiedosto = open("Marakov/Marakov.Cache","w")
pickle.dump(full_data,tiedosto)
tiedosto.close()
def form_sentence(argument=None):
length = 0
attempts = 0
while attempts < 20:
sentence = []
if argument != None:
a = argument
else:
try:
a = random.choice(full_data.keys())
except IndexError:
try:
b = random.choice(imported_data.keys())
except IndexError:
attempts = 999
return "No sentences formable at all"
sentence.append(a)
length = 0
attempts += 1
while length < 12 or sentence[-1].lower() in ["but","who","gets","im","most","is","it","if","then","after","over","every","of","on","or","as","the","wheather","whether","a","to","and","for"] and length < 24:
try:
b = random.choice(full_data[a])
except:
try:
b = random.choice(imported_data[a])
except IndexError:
break
except KeyError:
break
else:
sentence.append(b)
length += 1
a = b
else:
sentence.append(b)
length += 1
a = b
if len(sentence) > 5:
argument = None
return sentence
else:
pass
argument = None
return sentence
def remdata(arg):
try:
del(full_data[arg])
except:
print "There is no such data"
else:
tiedosto = open("Marakov/Marakov.Cache","w")
pickle.dump(full_data,tiedosto)
tiedosto.close()
def remobject(arg1,arg2):
try:
del(full_data[arg1][full_data[arg1].index(arg2)])
except ValueError:
print "No such object"
except KeyError:
print "No such data"
else:
tiedosto = open("Marakov/Marakov.Cache","w")
pickle.dump(full_data,tiedosto)
tiedosto.close()
def convert(filename_from,filename_to):
try:
tiedosto = open(filename_from,"r")
data = pickle.load(tiedosto)
tiedosto.close()
except:
try:
tiedosto.close()
except:
pass
print "Error!"
else:
for lista in data.keys():
try:
a = lista[-1]
except IndexError:
pass
else:
if lista[-1] in """",.?!'()[]{}""" and not lista.islower():
if lista[:-1].lower() in data.keys():
data[lista[:-1].lower()] += data[lista]
print "Added "+str(len(data[lista]))+" Objects from "+lista+" To "+lista[:-1].lower()
del(data[lista])
else:
data[lista[:-1].lower()] = data[lista]
print lista+" Is now "+lista[:-1].lower()
del(data[lista])
elif lista[-1] in """",.?!'()[]{}""" and lista.islower():
if lista[:-1] in data.keys():
data[lista[:-1]] += data[lista]
print "Added "+str(len(data[lista]))+" Objects from "+lista+" To "+lista[:-1]
del(data[lista])
else:
data[lista[:-1]] = data[lista]
print lista+" Is now "+lista[:-1]
del(data[lista])
elif not lista.islower():
if lista.lower() in data.keys():
data[lista.lower()] += data[lista]
print "Added "+str(len(data[lista]))+" Objects from "+lista+" To "+lista.lower()
del(data[lista])
else:
data[lista.lower()] = data[lista]
print lista+" Is now "+lista.lower()
del(data[lista])
for a in data.keys():
for b in data[a]:
if b.lower()[:7] == "http://" or b.lower()[:7] == "http:\\\\" or b.lower()[:4] == "www.":
data[a].pop(b)
else:
try:
if b[-1] in """",.?!'()[]{}""" and not b.islower() and not b.isdigit():
data[a].pop(data[a].index(b))
data[a].append(b[:-1].lower())
print a+" | "+b +" -> "+b[:-1].lower()
elif b[-1] in """",.?!'()[]{}""" and b.islower():
data[a].pop(data[a].index(b))
data[a].append(b[:-1].lower())
print a+" | "+b +" -> "+b[:-1]
elif not b.islower() and not b.isdigit():
data[a].pop(data[a].index(b))
data[a].append(b.lower())
print a+" | "+b +" -> "+b.lower()
except IndexError: #If it has no letters.. well.. yeah.
data[a].pop(data[a].index(b))
print "Removed a NULL object"
tiedosto = open(filename_to,"w")
pickle.dump(data,tiedosto)