def test_returnsBeamTreeOfSize1For1ValidInterpretation( self, esperantifier): sentence_tokens = [ BeamToken("assign", {ReservedWord.PUT: 1}), BeamToken("7", {UnalphabeticTerminal.NUMBER: 1}), BeamToken("to", {ReservedWord.TO: 1}), BeamToken("small", {PartOfSpeech.ADJECTIVE: 1}), BeamToken("cat", {PartOfSpeech.NOUN: 1}), ] interpretations = esperantifier.try_interpreting(sentence_tokens) assert isinstance(interpretations, BeamTree) assert interpretations.number_of_leaves() == 1
def test_whenMidwayNodeHasInvalidInterpretationsItIsPruned( self, esperantifier): sentence_tokens = [ BeamToken("assign", {ReservedWord.PUT: 1}), BeamToken("7", {UnalphabeticTerminal.NUMBER: 1}), BeamToken("to", { ReservedWord.TO: 1, PartOfSpeech.V_IMP: 0.5 }), BeamToken("small", {PartOfSpeech.ADJECTIVE: 1}), BeamToken("cat", {PartOfSpeech.NOUN: 0.5}), ] interpretations = esperantifier.try_interpreting(sentence_tokens) remaining_interpretation = interpretations.get_next_interpretation() assert isinstance(interpretations, BeamTree) assert interpretations.number_of_leaves() == 1 assert len(remaining_interpretation) == len(sentence_tokens) assert remaining_interpretation[2].tag == ReservedWord.TO
def test_pruningOneBranchDoesNotAffectOtherSubtrees(self, esperantifier): """The bug that this checks: pruning [PUT, ADJECTIVE, TO] has pruned TO from the [PUT,NOUN] branch as well, resulting in: └- BeamTree- root (None: 1) └- BeamTreeNode- assign (ReservedWord.PUT: 1) ├- BeamTreeNode- love (PartOfSpeech.ADJECTIVE: 1) -- real mistake │ ├- BeamTreeNode- to (ReservedWord.TO: 1) │ │ └- BeamTreeNode- small (PartOfSpeech.ADJECTIVE: 1) │ │ └- BeamTreeNode- cat (PartOfSpeech.NOUN: 0.5) │ └- BeamTreeNode- to (PartOfSpeech.V_IMP: 0.5) │ └- BeamTreeNode- small (PartOfSpeech.ADJECTIVE: 1) │ └- BeamTreeNode- cat (PartOfSpeech.NOUN: 0.5) └- BeamTreeNode- love (PartOfSpeech.NOUN: 1) ├- BeamTreeNode- to (ReservedWord.TO: 1) -- pruned unnecessarily │ └- BeamTreeNode- small (PartOfSpeech.ADJECTIVE: 1) │ └- BeamTreeNode- cat (PartOfSpeech.NOUN: 0.5) └- BeamTreeNode- to (PartOfSpeech.V_IMP: 0.5) └- BeamTreeNode- small (PartOfSpeech.ADJECTIVE: 1) └- BeamTreeNode- cat (PartOfSpeech.NOUN: 0.5)""" sentence_tokens = [ BeamToken("assign", {ReservedWord.PUT: 1}), BeamToken("love", { PartOfSpeech.ADJECTIVE: 1, PartOfSpeech.NOUN: 1 }), BeamToken("to", { ReservedWord.TO: 1, PartOfSpeech.V_IMP: 0.5 }), BeamToken("small", {PartOfSpeech.ADJECTIVE: 1}), BeamToken("cat", {PartOfSpeech.NOUN: 0.5}), ] interpretations = esperantifier.try_interpreting(sentence_tokens) remaining_interpretation = interpretations.get_next_interpretation() assert isinstance(interpretations, BeamTree) assert interpretations.number_of_leaves() == 1 assert len(remaining_interpretation) == len(sentence_tokens) assert remaining_interpretation[2].tag == ReservedWord.TO
def test_whenWrongInterpretationIsAfter1RuleHasBeenFoldedPruningActionDoesNotMiss( self, esperantifier): sentence_tokens = [ BeamToken("assign", {ReservedWord.PUT: 1}), BeamToken("40", {UnalphabeticTerminal.NUMBER: 1}), BeamToken("+", {UnalphabeticTerminal.PLUS: 1}), BeamToken("1", {UnalphabeticTerminal.NUMBER: 1}), BeamToken("+", {UnalphabeticTerminal.PLUS: 1}), BeamToken("1", {UnalphabeticTerminal.NUMBER: 1}), BeamToken("to", { ReservedWord.TO: 1, PartOfSpeech.V_IMP: 0.5 }), BeamToken("small", {PartOfSpeech.ADJECTIVE: 1}), BeamToken("cat", {PartOfSpeech.NOUN: 0.5}), ] interpretations = esperantifier.try_interpreting(sentence_tokens) remaining_interpretation = interpretations.get_next_interpretation() assert isinstance(interpretations, BeamTree) assert interpretations.number_of_leaves() == 1 assert len(remaining_interpretation) == len(sentence_tokens) assert remaining_interpretation[6].tag == ReservedWord.TO
def test_listOfMultipleBeamTokensIsPrintedWithAsMuchLinesAsTheMostPossibleTagsPlusOne( self, kite_noun_token, love_noun_or_verb_token): token_list = [kite_noun_token, love_noun_or_verb_token] list_format = BeamToken.list_pretty_format(token_list) assert len( list_format.splitlines()) == len(love_noun_or_verb_token.tags) + 1
def token(self): return BeamToken.from_tuple(self._stack.pop())
def love_noun_or_verb_token(self): return BeamToken("love", { PartOfSpeech.NOUN: 0.5, PartOfSpeech.V_IMP: 0.125 })
def kite_noun_token(self): return BeamToken("kite", {PartOfSpeech.NOUN: 1})