import string import random, bisect import sys from subprocess import Popen,PIPE import irclib ##Does this sentence parse >.9? def isValid(sentence): ##safety stuff f = sentence.find("\"") if(f != -1): return False f = sentence.find(",") if(f != -1): return False f = sentence.find("\'") if(f != -1): return False f = sentence.find(";") if(f != -1): return False f = sentence.find("exit") if(f != -1): return False f = sentence.find("|") if(f != -1): return False p = Popen(['./relex.sh', sentence],stdout=PIPE) #call relex while True: o = p.stdout.readline() if (o.find("Parse confidence:") > -1): print o if (o.find(".9") > -1) or (o.find(".8") > -1) or (o.find(".7") > -1): print "Hoo doggie!" return True else: return False if o == '' and p.poll() != None: break # the 'o' variable stores a line from the command's stdout # do anything u wish with the 'o' variable here # this loop will break once theres a blank output # from stdout and the subprocess have ended return False def insertWord(d, last, key): t = d.setdefault(last, [[],[]]) if key in t[1]: d[last][0][t[1].index(key)] += 1 else: d[last][0].append(1) d[last][1].append(key) def randWithDist(dist, items): s = random.randint(0, sum(dist)) for i in range(len(dist)): s -= dist[i] if s <= 0: return items[i] def giveSentence(): text = open("corpus.txt").read().split(" ") table = {} last = text[0] delim = ['.','!','?',';',':'] for word in text[1:]: if len(word) == 0: continue key = word.strip("\"\'!?.;:-*&^%$#@!`~\r\n") isend = word[-1] in delim insertWord(table, last, key) if isend: insertWord(table, key, word[-1]) last = word[-1] else: last = key out = open("output.txt","a") next = randWithDist(*table[random.choice(table.keys())]) for i in range(500): next = randWithDist(*table[next]) sentence = [next.capitalize()] while True: next = randWithDist(*table[next]) if next in delim: sentence = ' '.join(sentence) + next + "\n" break sentence.append(next) if isValid(sentence): out.write(sentence) out.close() print "This sentence is valid!:" print sentence return sentence else: print "This isn't valid!:" print sentence print "Sorry, I couldn't come up with anything new. The corpus is probably bad, give me some more time to read!" def addSentence(sent): if sent and sent[-1] not in [".","?","!"]: sent = sent + "." print sent if (isValid(sent)): #Remove this line to parse all input print "Adding to corpus" corpus = open("corpus.txt","a") corpus.write(sent) corpus.write(' ') corpus.close() # Public messages def handlePubMessage ( connection, event ): string = event.arguments() [ 0 ] addSentence(string) # Private messages def handlePrivMessage ( connection, event ): string = event.arguments() [ 0 ] addSentence(string) newsent = giveSentence() connection.privmsg ( event.source().split ( '!' ) [ 0 ], newsent ) # Connection information network = 'irc.freenode.net' port = 6667 channel = '#opencog' nick = 'linkovbot' name = 'Link Markov Robot' # Create an IRC object irc = irclib.IRC() # Create a server object, connect and join the channel server = irc.server() server.connect ( network, port, nick, ircname = name ) server.join ( channel ) irc.add_global_handler ( 'pubmsg', handlePubMessage ) irc.add_global_handler ( 'privmsg', handlePrivMessage ) # Jump into an infinite loop irc.process_forever()