def strat_markov_chain(play): """You can copy and modify this strategy.""" ret_val = result.Result() for act in play.gen_acts(): for scene in act.gen_scenes(): ret_val.set_act_scene(act, scene) mc = MarkovChain.MarkovChain() actors_seen = [] while True: # Ask the markov chain for the most likely guess if len(actors_seen): most_probable = mc.most_probable(actors_seen[-1]) else: most_probable = tuple() # If multiple values choose one randomly if len(most_probable) > 1: my_choice = random.choice(most_probable) elif len(most_probable) == 1: my_choice = most_probable[0] else: # Not enough information my_choice = '' actual_actor = ret_val.guess(my_choice) if actual_actor is None: break else: # Record the transition if len(actors_seen): mc.add(actors_seen[-1], actual_actor) actors_seen.append(actual_actor) return ret_val
def MarkovTrumpReactiveTweetGen(inp): ret = "" chain = MarkovChain.MarkovChain(csvParser.ImportTrumpData()) if (inp == ''): ret = MarkovTweetGen(chain) else: ret = MarkovReactiveTweetGen(chain, inp) if (ret.endswith(',')): ret = ret[0:-1] if (not (ret.endswith(".")) and not (ret.endswith("!")) and not (ret.endswith("?"))): ret += '.' return ret
def __init__(self): self.memory = [] self.markov_graph = MarkovChain.MarkovChain() self.active_input_pattern_index = -1 self.prev_active_input_pattern_index = -1 self.temporal_groups = [] self.active_temporal_group_index = -1 # In the format (x1, y1, x2, y2) where (x1, y1) is the top left of the rectangle # and (x2, y2) is the bottom right of the rectangle. All rectangles are in the 4th # quadrant of the Euclidean plane where the negative y axis is positive. self.receptive_field_dimensions = None
def chainBuilder(self): parsedEpisodes = getEpisodes(self.episodes) for e in parsedEpisodes: castList = e.getCast() for c in castList: if c not in self.suppCharacters and c not in self.mainCharacters: self.suppCharacters.add(c) directChain = MarkovChain(2) #Change direction order here stageDirections = getAllStageDirs(self.episodes) for direction in stageDirections: directChain.addData(direction.getChainableSource()) self.chains["STAGEDIR"] = directChain for i in self.mainCharacters: self.characterChain(i) for m in self.suppCharacters: self.characterChain(m)
from MarkovChain import * m = MarkovChain() m.transition_table = [[0.9, 0.05, 0.05], [0.5, 0.1, 0.4], [0, 1, 0]] m.nodes = ["alive", "zombie", "dead"] for i in m.generator(): print(i)
num_nodes = len(G) num_items = len(G) k = 50 item_distributions = ['uniform', 'direct', 'inverse', 'ego'] for item_distribution in item_distributions: if item_distribution == 'ego': iterations = 10 else: iterations = 1 for iteration in xrange(iterations): print "Evaluating item distribution {}".format(item_distribution) __builtin__.mc = MarkovChain(num_nodes=num_nodes, num_items=num_items, item_distribution=item_distribution, G=G) print "Starting evaluation of methods" methods = [ random_nodes, highest_item_nodes, highest_closeness_centrality_nodes, highest_in_degree_centrality_nodes, highest_in_probability_nodes, highest_betweenness_centrality_nodes, smart_greedy_parallel ] for method in methods: print "Evaluating method {}".format(method.func_name) get_objective_evolution(method, k, iteration)
def setUp(self): self._c = MarkovChain.MarkovChain()
def characterChain(self, character): tempChain = MarkovChain(2) #Change line order here characterLines = getAllCharLines(self.episodes, character) for line in characterLines: tempChain.addData(line.getChainableSource()) self.chains[character] = tempChain
word1 = inp retSen = word1.capitalize() else: if (inp.endswith('s')): word1 = "are" else: word1 = "is" retSen = inp.capitalize() + ' ' + word1 for it in range(length - 1): word2 = random.choice(MC[word1]) # Check for custom end of tweet token if (word2 == "@b@"): break word1 = word2 retSen += " " + word1 if (not (retSen.endswith(".")) and not (retSen.endswith("!")) and not (retSen.endswith("?"))): retSen += '.' print(retSen) return retSen if (__name__ == "__main__"): #TweetCreator(input1) chain = MarkovChain.MarkovChain(csvParser.ImportTrumpData()) for it in range(10): print("Test #" + str(it) + ':') MarkovTrumpReactiveTweetGen("")
#! /c/Anaconda/python from MarkovChain import * mc = MarkovChain("./markov") # To generate the markov chain's language model, in case it's not present mc.generateDatabase("ThisThis string of Text. is This is a string of Text. a string of Text.It won't is a string of Text. This is a string of Text. It won't generate string of Text. an interesting string of Text.database though.") # To let the markov chain generate some text, execute print mc.generateString()
if not channel: channel = input("Channel: ") if not dict_name: print("No dict name specified. Setting it to channel name.") dict_name = channel username = "" auth_token = "" with open("creds.txt", 'r') as creds_file: username = creds_file.readline() auth_token = creds_file.readline() irc_bot = TwitchChat.TwitchChat(username, auth_token, channel) markov_chain = MarkovChain.MarkovChain(save_dir, dict_name) while True: msg = irc_bot.next_msg() if msg: markov_chain.take_message(msg) if markov_chain.iterations() % 5 == 1: print("Channel:", markov_chain.dict_name) print("Iterations:", markov_chain.iterations()) print("Time elapsed:", int(markov_chain.time_elapsed()), "seconds") print("Message:", markov_chain.make_message()) print() markov_chain.save_progress()