def runIters(procNum): ITERATIONS = 100 guessesToWin = [] losses = 0 for i in range(ITERATIONS): guesser = Guesser() guesser.weightingTuple = weightTuple responder = Responder( getCellTuple(n2l(random.randint(1, 18)), random.randint(1, 18))) # print(f"Iteration {i}: {formatCell(responder.cell)}") turns = 0 gameOver = False while not gameOver: guess = guesser.getGuess() # print(f"Turn {turns} of iteration {i}: {formatCell(guess[0])}") if guess[1]: if responder.checkSolve(guess[0]): guessesToWin.append(turns) break else: losses += 1 break guesser.feedback(guess[0], responder.check(guess[0])) turns += 1 return (guessesToWin, losses, ITERATIONS)
def create_guess_game(): """ Define game logic""" your_name = input("Hello! What's your name?\n") guesser = Guesser() max_d = Guesser.get_max_difficulty() min_d = Guesser.get_min_difficulty() guess = False while not guess: difficulty = input( f"{your_name}, choose a difficulty between {max_d} and {min_d} ") guess = guesser.choose_challenge_level(difficulty) print( f'Well, {your_name} I am thinking of a number between {Guesser.min_range} and {Guesser.max_range}' ) run = True count = 0 while run: user_guess = input(f"{your_name}, take a guess: ") count += 1 run = guesser.run_game(user_guess, count) if guesser.status == -1: print("Your guess is too low\n") if guesser.status == 1: print("Your guess is too high\n") if guesser.status == 0: print( f"Good Job {your_name} You guessed my number in {count} guesses!") else: print( f"{your_name}, you've lost. My number was {guesser.guess_number}")
def __init__(self, beam=1000, max_guess=20, rare_treshold=10, capitalization=True): self._uni = FreqDist() self._bi = ConditionalFreqDist() self._tri = ConditionalFreqDist() self._wd = ConditionalFreqDist() self._l1 = 0.0 self._l2 = 0.0 self._l3 = 0.0 self._beam_size = beam self._use_capitalization = capitalization self._max_guess = max_guess self._treshold = rare_treshold self._unk = Guesser(10) self._analyzer = None self.cache = {}
from manualfeedbacker import ManualFeedbacker from guesser import Guesser print("Welcome, im a masterminded bot.") guesser = Guesser(ManualFeedbacker()) solutionString = guesser.solve() print("Well it seems like i've found the solution. It's: " + solutionString) print("Goodbye you fool! ;)")
#!/usr/bin/env python3 import random import sys from guesser import FitnessGuesser as Guesser from helpers import getCellTuple, n2l, formatCell guesser = Guesser() print(f"Board initialized; {len(guesser.board)} possibilities remain") while True: guess = guesser.getGuess() if guess[1]: print("Making solve attempt!") print(formatCell(guess[0])) break response = input(formatCell(guess[0]) + '? ') guesser.feedback(guess[0], response.upper() in ['Y', 'YES']) print(f"{len(guesser.board)} possibilities remain")
def setUpClass(cls): cls.guesser = Guesser()
class MorphProbModel(): UNK_PROB = -99 def __init__(self, beam=1000, max_guess=20, rare_treshold=10, capitalization=True): self._uni = FreqDist() self._bi = ConditionalFreqDist() self._tri = ConditionalFreqDist() self._wd = ConditionalFreqDist() self._l1 = 0.0 self._l2 = 0.0 self._l3 = 0.0 self._beam_size = beam self._use_capitalization = capitalization self._max_guess = max_guess self._treshold = rare_treshold self._unk = Guesser(10) self._analyzer = None self.cache = {} def set_analyzer(self, obj): self._analyzer = obj def train(self, data): C = False for sent in data: history = [('BOS', False), ('BOS', False)] for w, l, t in sent: # Ezt azért szedtem ki mert megeszik 4 giga memóriát ha marad # t = encode((w, l, t)) if self._use_capitalization and w[0].isupper(): C = True self._wd[w].inc(t) self._uni.inc((t, C)) self._bi[history[1]].inc((t, C)) self._tri[tuple(history)].inc((t, C)) history.append((t, C)) history.pop(0) C = False for word, fd in self._wd.iteritems(): for tag, count in fd.iteritems(): if count < self._treshold: self._unk.add_word(word.lower(), tag, count) self._unk.finalize() self._compute_lambda() def _compute_lambda(self): tl1 = 0.0 tl2 = 0.0 tl3 = 0.0 for history in self._tri.conditions(): (h1, h2) = history for tag in self._tri[history].samples(): if self._uni[tag] == 1: continue c3 = self._safe_div((self._tri[history][tag] - 1), (self._tri[history].N() - 1)) c2 = self._safe_div((self._bi[h2][tag] - 1), (self._bi[h2].N() - 1)) c1 = self._safe_div((self._uni[tag] - 1), (self._uni.N() - 1)) if (c1 > c3) and (c1 > c2): tl1 += self._tri[history][tag] elif (c2 > c3) and (c2 > c1): tl2 += self._tri[history][tag] elif (c3 > c2) and (c3 > c1): tl3 += self._tri[history][tag] elif (c3 == c2) and (c3 > c1): tl2 += float(self._tri[history][tag]) / 2.0 tl3 += float(self._tri[history][tag]) / 2.0 elif (c2 == c1) and (c1 > c3): tl1 += float(self._tri[history][tag]) / 2.0 tl2 += float(self._tri[history][tag]) / 2.0 else: pass self._l1 = tl1 / (tl1 + tl2 + tl3) self._l2 = tl2 / (tl1 + tl2 + tl3) self._l3 = tl3 / (tl1 + tl2 + tl3) def _safe_div(self, v1, v2): if v2 == 0: return -1 else: return float(v1) / float(v2) def _transition_prob(self, t, C, history): p_uni = self._uni.freq((t, C)) p_bi = self._bi[history[-1]].freq((t, C)) p_tri = self._tri[tuple(history[-2:])].freq((t, C)) p = self._l1 * p_uni + self._l2 * p_bi + self._l3 * p_tri if p == 0.0: return self.UNK_PROB return log(p, 2) def _known_lexical_prob(self, word, t, C): p = float(self._wd[word][t]) / float(self._uni[(t, C)]) return log(p, 2) def _analyze(self, word): tag_candidates = [] if word in self._wd.conditions(): tag_candidates = set(self._wd[word].samples()) else: analyses = map(itemgetter(1), self._analyzer.analyze(word)) guesses = self._unk.get_probs(word.lower()) guesses = map(itemgetter(0), sorted(guesses.iteritems(), reverse=True, key=itemgetter(1))[:self._max_guess]) tag_candidates = set(guesses) if analyses: tag_candidates &= set(analyses) if not tag_candidates: tag_candidates = set(guesses) return tag_candidates def _lexical_prob(self, word, t, C): if word in self._wd.conditions(): return self._known_lexical_prob(word, t, C) else: return self._unk.get_prob(word, t) def tag(self, sent, n=5): current_state = [(['BOS', 'BOS'], 0.0)] out = self._tagword(sent, current_state, n) return out def _tagword(self, sent, current_states, n=5): # A cache-sel elég gyors. Nem érdemes jobban vesződni vele. if sent == []: # yield ... return [(map(itemgetter(0), tag_seq[0][2:]), tag_seq[1]) for tag_seq in current_states[:n]] word = sent[0] sent = sent[1:] new_states = [] # Cache lookup sent_str = word + str(current_states) if sent_str in self.cache: return self._tagword(sent, self.cache[sent_str], n) C = False if self._use_capitalization and word[0].isupper(): C = True analyses = self._analyze(word) for (history, curr_sent_logprob) in current_states: logprobs = [] for t in analyses: p_t = self._transition_prob(t, C, history) p_l = self._lexical_prob(word, t, C) p = p_t + p_l logprobs.append(((t, C), p)) for (tag, logprob) in logprobs: new_states.append((history + [tag], curr_sent_logprob + logprob)) new_states.sort(reverse=True, key=itemgetter(1)) if len(new_states) > self._beam_size: new_states = new_states[:self._beam_size] # Cache store self.cache[sent_str] = new_states # yield new_states # self._tagword(sent, new_states, n) return self._tagword(sent, new_states, n)
from token import Token from guesser import Guesser #init guesser = Guesser() token = Token() for times in range(1): guesser.reset() token.getNewToken() token.printToken() guessCount = 0 while token.status == 'ALIVE': guess = guesser.makeGuess(token.state) print 'Guess: ' + guess token.guess (guess) token.printToken() guessCount += 1 print 'Total guesses: ' + str(guessCount) print '+'*60