def __init__(self, size): # length of the console self._size = 20 # stores our Message objects in a list self._log = [] # initialize our log to the empty string for each entry for i in range(0, self._size): self._log.append(Message(["?" * 40])) # parsing for commands self.parser = Parser(self)
def train_test(self, model_type, compare=True): passages = [self.passage] scores = [] for mode in "train", "load": print("-- %sing %s" % (mode, model_type)) p = Parser(model_file="test_files/%s" % model_type, model_type=model_type) p.train(passages if mode == "train" else None) guess, ref = zip(*list(p.parse(passages))) print() self.assertSequenceEqual(ref, passages) score = evaluation.Scores.aggregate([evaluation.evaluate( g, r, verbose=False, units=False, errors=False) for g, r in zip(guess, ref)]) scores.append(score.average_f1()) if compare: self.assertEqual(*scores) print("-- average labeled f1: %.3f, %.3f" % tuple(scores))
def train_test(passages, *args, **kwargs): p = Parser(*args, **kwargs) p.train(passages) _, parsed = zip(*p.parse(passages)) return parsed
class Console: def __init__(self, size): # length of the console self._size = 20 # stores our Message objects in a list self._log = [] # initialize our log to the empty string for each entry for i in range(0, self._size): self._log.append(Message(["?" * 40])) # parsing for commands self.parser = Parser(self) """ Returns overall size of the console """ @property def size(self): return self._size @property def log(self): return self._log """ pushes a message onto the console """ def push(self, message): # if we are pushing a trivial string we wrap it around our Message class for ease if isinstance(message, str): # we have to try and parse this message self.parser.command(message) print("converted...") message = Message([message]) # iterate from the top, pushing each message up 1 for i in range(self._size - 1, 0, -1): self._log[i] = self._log[i - 1] # finally set the bottom equal to the message self._log[0] = message """ displays the console to the screen given a valid display """ def display(self, userInput, display, height, width): # console text font = pygame.font.Font('freesansbold.ttf', CONSOLE_FONT_SIZE) for i in range(0, self.size): currMessage = self._log[i] # we continue outputting, until the bitter end string, image = currMessage.pop(0) j = 0 # why python have no do while whatever # shift from previous text/images shift = 0 while string: currect = pygame.Rect(shift, height - (i + 2) * CONSOLE_FONT_SIZE, width, CONSOLE_FONT_SIZE) curtext = font.render(string, True, WHITE, BLACK) shift += len(string) * CONSOLE_FONT_SIZE / 1.9 # if we got a image if image: # need to scale it down to our font size display.blit(pygame.transform.scale( image, (CONSOLE_FONT_SIZE + 1, CONSOLE_FONT_SIZE + 1)), (shift, height - (i + 2) * CONSOLE_FONT_SIZE), special_flags=pygame.BLEND_RGBA_MULT) shift += CONSOLE_FONT_SIZE display.blit(curtext, currect) j += 1 string, image = currMessage.pop(j) # create a text surface object, # on which text is drawn on it. text = font.render(userInput, True, WHITE, BLACK) # text surface object textRect = text.get_rect() textRect.midleft = (0, height - CONSOLE_FONT_SIZE / 2) display.blit(text, textRect) # 'text console' output for text based aspect consolestart = height - ((self.size + 1) * CONSOLE_FONT_SIZE) pygame.draw.line(display, BLACK, (0, consolestart), (width, consolestart), CONSOLE_LINE_WIDTH)