def __init__(self, list_words=None): self.corpus = list_words self.words = {} # [String: Stochastic] # iterate corpus for index, current_word in enumerate(self.corpus): adjacent_word = None # None is equal to stop def get_adjacent_word(): try: # TODO: end of sentence return self.corpus[index + 1] except IndexError: return None # TODO: identify the beginning of a sentence adjacent_word = get_adjacent_word() stochastic_for_current_word = None if current_word in self.words: stochastic_for_current_word = self.words[current_word] else: stochastic_for_current_word = Stochastic() self.words[current_word] = stochastic_for_current_word stochastic_for_current_word.add_count(adjacent_word)
def __init__(self, words, order): self.corpus = re.findall(r"[\w']+|[.,!?;]", words) self.words = HashTable(32) self.markov_order = order # iterate corpus previous_element = None previous_element_list = None if len(self.corpus) < self.markov_order + 1 and self.markov_order > 0: print("invalid corpus") else: max_iteration = len(self.corpus) - self.markov_order + 1 for index in range(0, max_iteration): adjacent_index = index + self.markov_order current_element_list = self.corpus[index:adjacent_index] current_element = ' '.join(current_element_list) # check: at beginning of sentence, create/append current_word to self.words[None] if index == 0 or previous_element_list[0] in OrderedMarkovChain.end_punctuation: stochastic_for_beginning_of_sentence = None try: stochastic_for_beginning_of_sentence = self.words[None] except KeyError: # stochastic for None is not made, so make one stochastic_for_beginning_of_sentence = Stochastic() self.words[None] = stochastic_for_beginning_of_sentence stochastic_for_beginning_of_sentence.add_count(current_element) # grab adjacent word, can be None to represent end of the sentence def get_adjacent_word(): try: adjacent_word = self.corpus[adjacent_index] temp_list = current_element_list[1:] # current_element without the first index temp_element = ' '.join(temp_list) return adjacent_word except IndexError: # end of list_words, thus end of sentence return None adjacent_element = get_adjacent_word() # print(current_element, "->{}".format(adjacent_element)) # if current_word is an ending puncuation, then adjacent_word is None if current_element_list[-1] in OrderedMarkovChain.end_punctuation: adjacent_element = None # continue stochastic_for_current_word = None if current_element in self.words: stochastic_for_current_word = self.words[current_element] else: stochastic_for_current_word = Stochastic() self.words[current_element] = stochastic_for_current_word stochastic_for_current_word.add_count(adjacent_element) previous_element = current_element previous_element_list = current_element_list
def init_nature(self, naturelabel): if naturelabel == 'stochastic': self.nature = Stochastic(self.matchID) elif naturelabel == 'deterministic': self.nature = Deterministic(self.matchID) else: self.nature = Adversarial(self.matchID)
def __init__(self, N, eta, nature): self.N = N self.weights = [1] * self.N self.eta = eta self.T = 100 self.matchID = 0 self.h = [0] * self.N self.naturelabel = nature self.nature = Stochastic(self.matchID) self.learner_loss = [0] * self.T self.expert_loss = [[0 for i in range(self.N)] for j in range(self.T)] self.sum_learner = 0 self.sum_experts = [0] * self.N self.init_nature(self.naturelabel)
from minimax import Minimax from alpha_beta import AlphaBeta from stochastic import Stochastic if __name__ == '__main__': start = time.time() # Initialize game board gameboard = Board() # Initialize red agent idx = input('Please choose a type for RED:\n' '1. Stochastic\n' '2. Alpha Beta\n') if idx == '1': RED = Stochastic('red') elif idx == '2': RED = AlphaBeta('red') # Initialize blue agent idx = input('Please choose a type for BLUE:\n' '1. Stochastic\n' '2. Alpha Beta\n') if idx == '1': BLUE = Stochastic('blue') elif idx == '2': BLUE = AlphaBeta('blue') # Mark order or two agents _red_ = 1 _blue_ = 0
def stochastic_test(): test_stochastic = Stochastic('words_sample.txt') sentence = test_stochastic.create_sentence(10000) words_list = sentence.split(" ") sentence_histogram = test_stochastic.create_histogram(words_list) print(sentence_histogram)