def get_actor_and_actant (cls, root_node): idx = 0 actor = None actant = None while idx < len (root_node.children): child = root_node.children[idx] if child.type == PropositionTreeNodeType.linkage: if child.linkage.name == LanguageHelper.translate ("who") or child.linkage.name == LanguageHelper.translate ("what"): parent = child child = child.children[0] if child.type == PropositionTreeNodeType.concept: if child.side == PropositionTreeNodeSide.left: actor = child if root_node.concept.subroot != True: ContextProvider.set_actor_node (actor) elif child.side == PropositionTreeNodeSide.right: actant = child if actor != None and actant != None: break idx += 1 if actor == None: actor = ContextProvider.get_actor_node () return actor, actant
def replace_subtree (cls, root_node, side, is_new, cursor): actor, actant = PropositionTree.get_actor_and_actant (root_node) result_node = PropositionTreeNode () result_node.type = PropositionTreeNodeType.concept result_node.side = side result_node.concept = TreeNodeConcept () is_memobject = False error_text = "" if root_node.concept.name == LanguageHelper.translate ("to-have"): if actant.concept.name == LanguageHelper.translate ("name"): if actor.concept.name == LanguageHelper.translate ("module"): child1 = actant.children[0] if child1.type == PropositionTreeNodeType.linkage: if child1.linkage.name == LanguageHelper.translate ("which"): child2 = child1.children[0] if child2.type == PropositionTreeNodeType.concept: result_node.concept.type = TreeNodeConceptType.module result_node.concept.name = child2.concept.name result_node.text = result_node.concept.name is_memobject = True elif actor.concept.name == LanguageHelper.translate ("field"): child1 = actant.children[0] if child1.type == PropositionTreeNodeType.linkage: if child1.linkage.name == LanguageHelper.translate ("which"): child2 = child1.children[0] if child2.type == PropositionTreeNodeType.code_object: if is_new == True: result_node.concept.id = MemoryProvider.create_field (child2.text) is_memobject = True result_node.concept.type = TreeNodeConceptType.field result_node.concept.name = "$" + str (result_node.concept.id) result_node.text = result_node.concept.name ContextProvider.set_field_node (result_node) else: result_node.concept.id = MemoryProvider.get_field_id (child2.text) is_memobject = True result_node.concept.type = TreeNodeConceptType.field result_node.concept.name = "$" + str (result_node.concept.id) result_node.text = result_node.concept.name elif actor.concept.name == LanguageHelper.translate ("constant"): child1 = actant.children[0] if child1.type == PropositionTreeNodeType.linkage: if child1.linkage.name == LanguageHelper.translate ("which"): child2 = child1.children[0] if child2.type == PropositionTreeNodeType.code_object: if is_new == True: result_node.concept.id = MemoryProvider.create_constant (child2.text) is_memobject = True result_node.concept.type = TreeNodeConceptType.constant result_node.concept.name = "$" + str (result_node.concept.id) result_node.text = result_node.concept.name ContextProvider.set_constant_node (result_node) else: result_node.concept.id = MemoryProvider.get_constant_id (child2.text) is_memobject = True result_node.concept.type = TreeNodeConceptType.constant result_node.concept.name = "$" + str (result_node.concept.id) result_node.text = result_node.concept.name elif actor.concept.name == LanguageHelper.translate ("list"): child1 = actant.children[0] if child1.type == PropositionTreeNodeType.linkage: if child1.linkage.name == LanguageHelper.translate ("which"): child2 = child1.children[0] if child2.type == PropositionTreeNodeType.code_object: if is_new == True: result_node.concept.id = MemoryProvider.create_list (child2.text) is_memobject = True result_node.concept.type = TreeNodeConceptType.memlist result_node.concept.name = "$" + str (result_node.concept.id) result_node.text = result_node.concept.name ContextProvider.set_list_node (result_node) else: result_node.concept.id = MemoryProvider.get_list_id (child2.text) is_memobject = True result_node.concept.type = TreeNodeConceptType.memlist result_node.concept.name = "$" + str (result_node.concept.id) result_node.text = result_node.concept.name elif actor.concept.name == LanguageHelper.translate ("trigger"): child1 = actant.children[0] if child1.type == PropositionTreeNodeType.linkage: if child1.linkage.name == LanguageHelper.translate ("which"): child2 = child1.children[0] if child2.type == PropositionTreeNodeType.code_object: if is_new == True: result_node.concept.id = TriggerProvider.create_trigger (child2.text) result_node.concept.type = TreeNodeConceptType.trigger result_node.concept.name = "$" + str (result_node.concept.id) result_node.text = result_node.concept.name ContextProvider.set_trigger_node (result_node) elif actor.concept.name == LanguageHelper.translate ("condition"): child1 = actant.children[0] if child1.type == PropositionTreeNodeType.linkage: if child1.linkage.name == LanguageHelper.translate ("which"): child2 = child1.children[0] if child2.type == PropositionTreeNodeType.code_object: if is_new == True: result_node.concept.id = ConditionProvider.create_condition (child2.text) result_node.concept.type = TreeNodeConceptType.condition result_node.concept.name = "$" + str (result_node.concept.id) result_node.text = result_node.concept.name ContextProvider.set_condition_node (result_node) elif child2.type == PropositionTreeNodeType.concept: if is_new == True: result_node.concept.id = ConditionProvider.create_condition (child2.text) result_node.concept.type = TreeNodeConceptType.condition result_node.concept.name = "$" + str (result_node.concept.id) result_node.text = result_node.concept.name ContextProvider.set_condition_node (result_node) elif actor.concept.name == LanguageHelper.translate ("procedure"): child1 = actant.children[0] if child1.type == PropositionTreeNodeType.linkage: if child1.linkage.name == LanguageHelper.translate ("which"): child2 = child1.children[0] if child2.type == PropositionTreeNodeType.concept: database_triad = DatabaseTriad.read (cursor, actant.concept.id, child1.linkage.id, child2.concept.id) if database_triad == None: error_text = ErrorHelper.get_text (105) return None, error_text database_sequense1 = DatabaseSequence.read (cursor, 0, 0, database_triad.id) if database_sequense1 == None: error_text = ErrorHelper.get_text (105) return None, error_text database_triad = DatabaseTriad.read_by_id (cursor, database_sequense1.left_triad_id) if database_triad == None: error_text = ErrorHelper.get_text (105) return None, error_text if database_triad.left_concept_id == root_node.concept.id: database_sequense2 = DatabaseSequence.read (cursor, database_sequense1.proposition_id, 0, database_triad.id) if database_sequense2 == None: error_text = ErrorHelper.get_text (105) return None, error_text database_triad = DatabaseTriad.read_by_id (cursor, database_sequense2.left_triad_id) if database_triad == None: error_text = ErrorHelper.get_text (105) return None, error_text result_node.concept.id = database_triad.left_concept_id database_concept = DatabaseConcept.read_by_name (cursor, LanguageHelper.translate ("to-be")) if database_concept == None: error_text = ErrorHelper.get_text (104) return None, error_text database_triad1 = DatabaseTriad.read (cursor, result_node.concept.id, 0, database_concept.id) if database_triad1 == None: error_text = ErrorHelper.get_text (104) return None, error_text database_triad2 = DatabaseTriad.read (cursor, database_concept.id, 0, actor.concept.id) if database_triad2 == None: error_text = ErrorHelper.get_text (104) return None, error_text database_sequense3 = DatabaseSequence.read (cursor, 0, database_triad1.id, database_triad2.id) if database_sequense3 == None: error_text = ErrorHelper.get_text (104) return None, error_text else: error_text = ErrorHelper.get_text (105) return None, error_text elif actant.concept.name == LanguageHelper.translate ("class"): if actor.concept.name == LanguageHelper.translate ("trigger"): child1 = actant.children[0] if child1.type == PropositionTreeNodeType.linkage: if child1.linkage.name == LanguageHelper.translate ("which"): child2 = child1.children[0] if child2.type == PropositionTreeNodeType.string: result_node.concept.id = TriggerProvider.get_id_by_class (child2.text) result_node.concept.type = TreeNodeConceptType.trigger result_node.concept.name = "$" + str (result_node.concept.id) result_node.text = result_node.concept.name is_memobject = True if is_memobject != True: if result_node.concept.id != 0: database_concept = DatabaseConcept.read_by_id (cursor, result_node.concept.id) result_node.concept.type = database_concept.type result_node.concept.name = database_concept.name result_node.text = result_node.concept.name else: return None, error_text return result_node, error_text
class BasicTest(unittest.TestCase): def setUp(self): self.help = LanguageHelper(lexicon) # make sure that all the words in the lexicon are recognized def testContainment(self): for w in lexicon: self.assertTrue(w in self.help) def testFailures(self): self.assertFalse('cate' in self.help) # only allowed when capitalized self.assertFalse('fox' in self.help) # word is not there self.assertFalse('cofee' in self.help) # mis-spell word is not there self.assertFalse(' ' in self.help) # no spaces in a list def testSuggestions(self): self.assertEqual(self.help.getSuggestions('cofee'), ['coffee']) self.assertEqual(self.help.getSuggestions('Gate'), (['Cate', 'Date', 'Grate'])) self.assertEqual( self.help.getSuggestions('gate'), (['Cate', 'date', 'grate'])) #capitalization rules are tested here self.assertEqual(self.help.getSuggestions('blech'), []) self.assertEqual(self.help.getSuggestions('soften'), ['often', 'soften']) self.assertEqual(self.help.getSuggestions('entails'), (['entails', 'entrails'])) self.assertEqual(self.help.getSuggestions('dodge'), ['dodge']) self.assertEqual(self.help.getSuggestions('marred'), (['marred', 'married'])) self.assertEqual(self.help.getSuggestions('cater'), (['cater'])) self.assertEqual(self.help.getSuggestions('Cater'), (['Cate', 'Cater'])) self.assertEqual(self.help.getSuggestions('car'), (['car', 'care', 'cat'])) self.assertEqual(self.help.getSuggestions('Car'), (['Car', 'Care', 'Cat'])) self.assertEqual(self.help.getSuggestions('ablebodied'), (['able-bodied']))
def setUp(self): self.help = LanguageHelper(lexicon)