def test_parses_noun_lower_response(self): """ Tests that although other nouns can be recognised, they have a lower response than objects in the game. """ real_obj_s = pre_process('pick up the rock') fake_obj_s = pre_process('pick up the phone') real_r = statement().parse(real_obj_s) fake_r = statement().parse(fake_obj_s) assert fake_r.response < real_r.response
def test_no_match(self): """ Tests None is returned if no words with the tags are found. """ parser = word_tagged(['CD', 'NN']) s = pre_process('go listening') assert parser.parse(s).is_failure()
def test_match(self): """ Tests the similar word is parsed. """ p = word_meaning('hi') s = pre_process('boat walk hello') assert p.parse(s) == SuccessParse(parsed='hello', response=1.0, remaining=[])
def test_min_word_length(self): s = pre_process('up') r1 = word_spelling('cup', min_word_length=0).parse(s).parsed self.assertTrue(r1, 'cup') r2 = word_spelling('cup', min_word_length=3).parse(s) self.assertTrue(r2.is_failure())
def test_matches_first_only(self): def condition(input_word: Word) -> Response: return 0.0 if input_word == 'a' else 1.0 s = pre_process('a b') assert predicate(condition, first_only=False).parse(s).parsed == 'b' assert predicate(condition, first_only=True).parse(s).is_failure()
def test_match(self): """ Tests the word is returned if it matches any of the tags. """ parser = word_tagged(['NN']) s = pre_process('go tree listening') assert parser.parse(s) == SuccessParse(parsed='tree', response=1.0, remaining=['listening'])
def test_no_replace_result(self): """ Tests the result of a parser is not replaced if it result is not None. """ parser = maybe(produce('a', 0.5)) s = pre_process('b c') assert parser.parse(s) == SuccessParse(parsed='a', response=0.5, remaining=['b', 'c'])
def test_uses_default_if_failure(self): s = pre_process('') p = failure() d = produce(parsed=2, response=0.0) r = defaulted(p, d).parse(s) self.assertEqual(r, SuccessParse(2, 0.0, ['']))
def test_no_parse(self): p1 = word_match('a') p2 = word_match('b') parser = self.strongest_parser([p1, p2]) s = pre_process('x y z') assert parser.parse(s).is_failure()
def test_go_into_directional(self): s = pre_process('go to the second room') r = statement().parse(s).parsed self.assertEqual( r, Move(Speed.FAST, Positional('room', 1, MoveDirection.FORWARDS), None))
def test_uses_success_parse(self): s = pre_process('') p = produce(parsed=1, response=0.7) d = produce(parsed=2, response=0.0) r = defaulted(p, d).parse(s) self.assertEqual(r, SuccessParse(1, 0.7, ['']))
def test_uses_default_if_partial(self): s = pre_process('') p = partial_parser(failure(), response=0.0, marker='M') d = produce(parsed=2, response=0.5) r = defaulted(p, d).parse(s) self.assertEqual(r, SuccessParse(2, 0.5, ['']))
def test_replaces_none_result(self): """ Tests that if a parser returns None, that the result is replaced with an empty result with zero response. """ parser = maybe(failure()) s = pre_process('a b') assert parser.parse(s) == SuccessParse(parsed=None, response=0.0, remaining=['a', 'b'])
def test_cartesian_product_of_parser_constructors2(self): """ Tests that every parser constructor is combined with every word to match on. """ s = pre_process('run whale') parser = strongest_word(['go', 'hi'], make_word_parsers=[word_spelling, word_meaning]) assert parser.parse(s).parsed == 'run'
def test_parses_noun(self): """ Tests that other nouns that haven't been explicitly listed can also be recognised. """ s = pre_process('pick up the phone') r = statement().parse(s).parsed self.assertEqual(r, PickUp('phone', ObjectRelativeDirection.VICINITY))
def test_cartesian_product_of_parser_constructors1(self): """ Tests that every parser constructor is combined with every word to match on. """ s = pre_process('orange hillo') parser = strongest_word(['blue', 'hello'], make_word_parsers=[word_spelling, word_meaning]) assert parser.parse(s).parsed == 'hello'
def test_prefers_success_to_partial_results(self): p1 = partial_parser(word_match('b'), response=1.0, marker='Type') p2 = word_match('a') parser = self.strongest_parser([p1, p2]) s = pre_process('a') assert parser.parse(s).parsed == 'a'
def test_corridor_does_not_crouch(self): """ Tests that 'corridor' does not mean crouch """ s = pre_process('go to the end of the corridor') assert statement().parse(s).parsed == Move(Speed.FAST, EndOf('corridor'), None)
def test_response_default(self): p1 = produce('x', 0.5) p2 = produce('y', 1.0) p = p1.then(append(p2)) s = pre_process('w') assert p.parse(s) == SuccessParse('x y', 0.5, ['w'])
def test_chooses_strongest_partial_from_unique(self): p1 = partial_parser(word_match('a'), 0.1, 'Type') p2 = partial_parser(word_match('b'), 0.8, 'Type') p3 = partial_parser(word_match('c'), 0.4, 'Type') parser = self.strongest_parser([p1, p2, p3]) s = pre_process('x') assert parser.parse(s).response == 0.8
def test_combine_responses(self): p1 = produce('x', 0.5) p2 = produce('y', 1.0) p = p1.then(append(p2, mix)) s = pre_process('w') assert p.parse(s) == SuccessParse('x y', 0.75, ['w'])
def test_chooses_first_strongest(self): p1 = produce('a', 0.8) p2 = produce('b', 0.8) p3 = produce('c', 0.4) parser = self.strongest_parser([p1, p2, p3]) s = pre_process('x') assert parser.parse(s) == SuccessParse(parsed='a', response=0.8, remaining=['x'])
def test_then(self): # Take a parser, keep the parse result, but set the response to zero. def f(parsed: str, response) -> Parser: return produce(parsed, 0.0) parser = produce('a', 1.0).then(f) s = pre_process('b') assert parser.parse(s) == SuccessParse(parsed='a', response=0.0, remaining=['b'])
def test_go_into_absolute(self): s = pre_process('go into lab 2') r = statement().parse(s).parsed move = Move(Speed.FAST, Absolute('lab 2'), None) expected = Composite( [move, ThroughDoor(ObjectRelativeDirection.VICINITY)]) self.assertEqual(r, expected)
def test_input_is_unchanged(self): """ Tests even if the input will be consumed by a parser, that by wrapping said parser in an `anywhere` parser none of the input text is consumed. """ s = pre_process('b a c') parser = non_consuming(word_match('a')) assert parser.parse(s) == SuccessParse(parsed='a', response=1.0, remaining=s)
def test_ignore_parsed_captures_state(self): letter = 'b' parser = produce('a', 0.8).ignore_parsed(letter) # Change the letter to check that the letter returned by parser is still a 'b' letter = 'c' s = pre_process('') assert parser.parse(s) == SuccessParse(parsed='b', response=0.8, remaining=[''])
def test_parse_no_spaces(self): p1 = word_match('hello') p2 = word_match('world') p3 = word_match('!') p = p1.then(append(p2)).then(append(p3, spaces=False)) s = pre_process('hello world ! c') assert p.parse(s) == SuccessParse('hello world!', 1, ['c'])
def test_move_into(self): s = pre_process('go into the third door on your left') r = statement().parse(s).parsed move = Move(Speed.FAST, Positional('door', 2, MoveDirection.LEFT), None) expected = Composite( [move, ThroughDoor(ObjectRelativeDirection.VICINITY)]) self.assertEqual(r, expected)
def test_parses_and_then(self): s = pre_process('go left and then pick up the rock') expected_actions = [ Move(Speed.FAST, Directional(MoveDirection.LEFT, Distance.MEDIUM), None), PickUp('rock', ObjectRelativeDirection.VICINITY) ] assert statement().parse(s).parsed == Composite(expected_actions)
def test_parses_then(self): s = pre_process('go left then go right') expected_actions = [ Move(Speed.FAST, Directional(MoveDirection.LEFT, Distance.MEDIUM), None), Move(Speed.FAST, Directional(MoveDirection.RIGHT, Distance.MEDIUM), None), ] assert statement().parse(s).parsed == Composite(expected_actions)