def apply_markov_chains(self, author, twitter_posts): """ """ m = Markov() words = [] for twitter_post in twitter_posts: for word in twitter_post.split(): words.append(word.encode('utf-8')) m.words = words m.database() markov_twitter_post = m.generate_markov_twitter_post() TwitterPostMarkov.objects.get_or_create(author=author,content=markov_twitter_post) return
def __init__(self, file_name='./marco_db.pickle', chat_freq=0.1): """Load a Markov from file or create a new Markov as necessary. Initialize the response frequency and begin the autosave schedule. Keyword arguments: file_name -- The location at which to load/store the database. chat_freq -- The chance of spontaneously generating an output message upon receiving an input message. """ self.file_name = file_name if os.path.isfile(self.file_name): db_file = open(self.file_name, 'rb') self.markov = pickle.load(db_file) db_file.close() else: self.markov = Markov() self.chat_freq = chat_freq self.on() self.autosave(True)
def main(): text = "" option = int(raw_input("1. URL, 2. plain text:")) if option == 1: if DEBUG: url = "http://www.cs.drexel.edu/~jpopyack/Kasparov.html" else: url = raw_input("Enter the URL: ") text = summartUtil.getHtmlText(url) elif option == 2: filename = raw_input("Enter the filename: ") fin = file(filename) for line in fin: text += line + "\n" fin.close() else: print "Please enter the right option" sys.exit(2) MAXGEN = int(raw_input("Enter number of population: ")) sentences = summartUtil.getSentences(text) print "Analyzing text..." for s in sentences: analyze_sentence(s) # Generate graph with # dot -Tpng -o test.png test.dot outputDot("word.dot", word_dictionary) outputDot("grammer.dot", grammer_dictionary) markov = Markov(start_word, end_word, start_grammer, end_grammer) markov.set_original(text) markov.set_blackList(BLACK_LIST) print "Generating summary..." # print markov.GenerateSentence() population = markov.GenerateSummary(MAXGEN) print markov.select(population)
pass post['comments_count'] = comments_count # Note we do not need to go back to blog metadata CF as we have stored the values in posts CF post_summary_list.append(post) return (post_summary_list, next) if( __name__ == '__main__'): blog_post_counts = 20 total_comments = 200 total_upvotes = 300 total_downvotes = 100 textgen = Markov(open('alice_in_wonderland.txt')) blogs = ['blog1', 'blog2', 'blog3'] authors = ['Jim Ellis', 'Sammy Jenkins', 'Marla Singer'] emails = ['*****@*****.**', '*****@*****.**', '*****@*****.**'] passwd = 'passwd1234' print "Adding blog metadata" for i in range(len(blogs)): add_blog(blogs[i], authors[i], emails[i], passwd) post_ids = list() comment_ids = list() for i in range(blog_post_counts): title = textgen.generate_markov_text(randint(3,10)) text = textgen.generate_markov_text(randint(30, 500)) idx = randint(0,len(blogs)-1)
#! /usr/bin/env python3 # -*- coding: utf-8 -*- import sys import xml.sax.saxutils from Markov import Markov if __name__ == "__main__": chainpath = "archive/"+sys.argv[1]+"/json" corpuspath = "archive/"+sys.argv[1]+"/log" backlogpath = "archive/"+sys.argv[1]+"/past" m = Markov(corpusFile=corpuspath, chainsFile=chainpath, backlogFile=backlogpath, odds=[1, 2, 2, 3]) m.save_chains(chainpath) output = m.generate_with_checks(140) # we leave ! and ? since they make sense isolated output = [w.translate(str.maketrans('', '', '"()[]{}«»¡¿')) for w in output] output = m.linearize(output) output = xml.sax.saxutils.unescape(output) print(output) with open("archive/"+sys.argv[1]+"/past", 'a+') as f: f.write(output+"\n")
#! /usr/bin/env python3 # -*- coding: utf-8 -*- import sys from Markov import Markov if __name__ == "__main__": corpuspath = "archive/" + sys.argv[1] + "/log" chainpath = "archive/" + sys.argv[1] + "/json" m = Markov(corpusFile=corpuspath) m.save_chains(chainpath)
from Markov import Markov import numpy as np weather_today = Markov() weather_today.load_data(file_path='./weather.csv') print(weather_today.get_prob('cloudy', 'windy'))
class marco: def __init__(self, file_name='./marco_db.pickle', chat_freq=0.1): """Load a Markov from file or create a new Markov as necessary. Initialize the response frequency and begin the autosave schedule. Keyword arguments: file_name -- The location at which to load/store the database. chat_freq -- The chance of spontaneously generating an output message upon receiving an input message. """ self.file_name = file_name if os.path.isfile(self.file_name): db_file = open(self.file_name, 'rb') self.markov = pickle.load(db_file) db_file.close() else: self.markov = Markov() self.chat_freq = chat_freq self.on() self.autosave(True) def autosave(self, skip = False): """Save the database to a file and schedule the next autosave. Keyword arguments: skip -- If True, do not save the database to file. (default False) """ threading.Timer(5 * 60, self.autosave).start() if not skip: db_file = open(self.file_name, 'wb') pickle.dump(self.markov, db_file) db_file.close() def command(self, command, args = ''): """Execute the provided command and return the chat output of the command. Keyword arguments: command -- The name of the command to run as a string. args -- Any arguments to the command as a string. """ command = command.lower() if command == 'ping': return 'pong!' if command == 'output': return self.output() if command == 'help': if args: args = args.lower() if args[0] == '!': args = args[1:] elif args == 'ping': return '!ping responds "pong!"' elif args == 'help': return ( '!help provides help messages. use !help commandname ' 'for more information about a command.' ) elif args == 'off': return ( '!off silences the bot indefinitely. provide a number ' 'to silence the bot for that many minutes instead.' ) elif args == 'on': return '!on unsilences the bot immediately.' elif args == 'output': return ( '!output immediately produces a line of output from ' 'the bot.' ) elif args == 'unlearn': return ( '!unlearn reverses the learning process on the ' 'provided message, removing it from the database.' ) elif args == 'freq': return ( '!freq adjusts the spontaneous response rate ' 'percentage, which is 15% by default.' ) return ( 'I am a markov chain bot created by lemon. I understand the ' 'following commands: !ping !help !off !on !output !unlearn ' '!freq. Use !help commandname for more information about a ' 'command.' ) if command == 'off': if args: args = self.__strToNum(args) if args: self.off(args) return ( 'deactivating for ' + str(args) + ' minutes (use !on ' 'to reactivate immediately)' ) else: return ( '!off only takes a positive integer as an argument. ' '(no action taken)' ) else: self.off() return 'deactivating indefinitely (use !on to reactivate)' if command == 'on': self.on() return 'reactivating.'; if command == 'unlearn': self.markov.unparse(args) return 'Unlearned provided message.' if command == 'freq' or command == 'frequency': if args: args = self.__strToNum(args) if args and args < 100: self.set_chat_freq(args) return ( 'Response frequency changed to ' + str(args) + '%.' ) else: return ( '!freq requires a positive integer less than 100 as ' 'an argument. (no action taken)' ) else: return ( 'Current spontaneous response frequency is ' + str(self.chat_freq * 100) + '%.' ) def __strToNum(self, string): """Convert a string to a number. Return False if the conversion fails. Keyword arguments: string -- A string. """ try: return int(string) except ValueError: return False def off(self, time = 0): """Deactivate, temporarily disabling the spontaneous response feature. Keyword arguments: time -- The number of minutes to deactivate for (0 for until reactivated). (default 0) """ self.silent = True if time > 0: threading.Timer(time * 60, self.on).start() def on(self): """Immediately reactivate.""" self.silent = False def input(self, message): """Passes input to the Markov chain. May generate a spontaneous response. """ self.markov.parse(message) if random.random() <= self.chat_freq and not self.silent: return self.output() def output(self): """Generates an output message from the Markov chain.""" return self.markov.output() def set_chat_freq(self, rate): self.chat_freq = rate / 100