def test_whenDeepBranchIsPrunedParallelBranchesAreUnaffected(
         self, kite_noun_token, love_noun_or_verb_token):
     love_tree = BeamTree.from_tokens_list([love_noun_or_verb_token] +
                                           [kite_noun_token] * 2)
     pruned_tree = love_tree.prune([PartOfSpeech.NOUN] * 3)
     assert pruned_tree.tree_size() == 4
     assert pruned_tree.number_of_leaves() == 1
Exemple #2
0
 def try_interpreting(self, statement: list):
     interpretations_tree = BeamTree.from_tokens_list(statement)
     interpretation = interpretations_tree.get_next_interpretation()
     while interpretation is not None:
         try:
             self._ast.parse(esperantify_tokens(interpretation))
         except EsperantoLocatedSyntaxError as e:
             interpretations_tree = interpretations_tree.prune(
                 [t.tag for t in interpretation[:e.index + 1]])
             if not interpretations_tree.verify_integrity():
                 print("tree integrity ruined!")
                 print("last interpretation was: " + str(interpretation))
                 print("resulting tree:")
                 print(interpretations_tree.pretty_print())
                 return None
             interpretation = interpretations_tree.longest_legal_sub_interpretation(
                 interpretation)
             if interpretations_tree.tree_size() == 1:
                 print(e)
                 return None
         except EsperantoSyntaxError as e:
             print(e)
             return None
         interpretation = interpretations_tree.get_next_interpretation(
             interpretation)
     return interpretations_tree
 def test_beamTreeHasSubTreeForEachBeamTokenInterpretationWithItsProbability(
         self, love_noun_or_verb_token):
     subtrees = BeamTree.from_tokens_list([love_noun_or_verb_token
                                           ]).get_children()
     noun_location = 0 if subtrees[0].token.tag == PartOfSpeech.NOUN else 1
     assert len(subtrees) == 2
     assert subtrees[
         noun_location].probability == love_noun_or_verb_token.tags[
             PartOfSpeech.NOUN]
     assert subtrees[
         1 - noun_location].probability == love_noun_or_verb_token.tags[
             PartOfSpeech.V_IMP]
 def test_beamTreeOf2PossibleTagsEachTimeHasAsManyNodesAsBinaryTree(
         self, love_noun_or_verb_token):
     # number of nodes is 2^(lvl+1)-1 => 2^3-1 => 7
     assert BeamTree.from_tokens_list([love_noun_or_verb_token] *
                                      2).tree_size() == 7
 def test_beamTreeHasSizeEqualToTokenTagsPlusOneForRoot(
         self, kite_noun_token, love_noun_or_verb_token):
     assert BeamTree.from_tokens_list([kite_noun_token]).tree_size() == 2
     assert BeamTree.from_tokens_list([love_noun_or_verb_token
                                       ]).tree_size() == 3
 def test_GivenMultipleTagsTheTreeRootTagIsNoneWithProbability1(
         self, love_noun_or_verb_token):
     single_love_tree = BeamTree.from_tokens_list([love_noun_or_verb_token])
     assert single_love_tree.token.tag is None
     assert single_love_tree.probability == 1
 def empty_tree(self):
     return BeamTree([])
 def double_love_tree(self, love_noun_or_verb_token):
     return BeamTree.from_tokens_list([love_noun_or_verb_token] * 2)
 def test_canRapidlyConstructHugeBeamTrees(self, love_noun_or_verb_token):
     BeamTree.from_tokens_list([love_noun_or_verb_token] * 20)
 def test_gettingInterpretationOfEmptyTreeYieldsEmptyList(self):
     empty_tree = BeamTree([])
     next_of_none = empty_tree.get_next_interpretation()
     assert next_of_none == []
 def test_pruningWholeTreeReturnsRoot(self, kite_noun_token):
     assert BeamTree.from_tokens_list([kite_noun_token] * 2) \
                .prune([PartOfSpeech.NOUN]).tree_size() == 1
 def test_loneRootHasDepthOf1(self, double_love_tree):
     assert BeamTree([]).tree_depth() == 1