Ejemplo n.º 1
0
 def test_save(self):
     inputs = Inputs()
     subjects = [Subject("baba"), Subject("coko")]
     patterns = [
         PatternGoogle("why are"),
         PatternGoogle("Why are", "hasProperty", True)
     ]
     mmr = MultipleModuleReference(ModuleReferenceInterface("Module0"))
     mmr.add_reference(ModuleReferenceInterface("Module1"))
     msr = MultipleSubmoduleReference(
         SubmoduleReferenceInterface("Submodule0"))
     msr.add_reference(SubmoduleReferenceInterface("Submodule0"))
     ms0 = MultipleScore()
     ms0.add_score(1.0, ModuleReferenceInterface("Module0"),
                   SubmoduleReferenceInterface("Submodule0"))
     ms1 = MultipleScore()
     ms1.add_score(1.0, mmr, msr)
     ms1.add_score(0.5, ModuleReferenceInterface("Module1"),
                   SubmoduleReferenceInterface("Submodule2"))
     mp0 = MultiplePattern()
     mp0.add_pattern(patterns[0])
     mp1 = MultiplePattern()
     mp1.add_pattern(patterns[0])
     mp1.add_pattern(patterns[1])
     gfs = [
         GeneratedFact(
             "baba", "is", "you", "sometimes", False, ms0,
             MultipleSourceOccurrence.from_raw("baba is you", msr, 1), mp0),
         GeneratedFact(
             "coko", "is", "dead", "always", True, ms1,
             MultipleSourceOccurrence.from_raw("toto is always dead", msr,
                                               1), mp1)
     ]
     seeds = [
         Fact("baba", "is", "us", None, False),
         Fact("coko", "are", "missing", "coucou", True)
     ]
     objects = [Object("missing"), Object("you")]
     inputs = inputs.replace_seeds(seeds)
     inputs = inputs.replace_patterns(patterns)
     inputs = inputs.replace_subjects(subjects)
     inputs = inputs.replace_generated_facts(gfs)
     inputs = inputs.replace_objects(objects)
     inputs.save("temp.json")
     inputs_read = inputs.load("temp.json")
     self.assertEqual(len(inputs.get_generated_facts()),
                      len(inputs_read.get_generated_facts()))
     self.assertEqual(len(inputs.get_subjects()),
                      len(inputs_read.get_generated_facts()))
     self.assertEqual(len(inputs.get_patterns()),
                      len(inputs_read.get_patterns()))
     self.assertEqual(len(inputs.get_seeds()), len(inputs_read.get_seeds()))
     self.assertEqual(len(inputs.get_objects()),
                      len(inputs_read.get_objects()))
Ejemplo n.º 2
0
 def test_arent_short_suggestion_pattern(self):
     pattern = PatternGoogle("why aren't", "ARN'T", True)
     suggestion = ("why aren't lions fat", 1.0, pattern, "lion")
     new_gfs = self.openie_fact_generator.get_generated_facts([suggestion])
     self.assertEqual(len(new_gfs), 1)
     self.assertEqual(new_gfs[0].get_subject(), "lions")
     self.assertEqual(new_gfs[0].get_predicate(), "are")
     self.assertEqual(new_gfs[0].get_object(), "fat")
     self.assertTrue(new_gfs[0].is_negative())
Ejemplo n.º 3
0
 def test_ignore(self):
     suggestion = ("why do pandas eat bamboo", 1.0,
                   PatternGoogle("why do", "has_property", 1.0), "panda")
     new_gfs = self.openie_fact_generator.get_generated_facts([suggestion])
     inputs = self.empty_input.add_generated_facts(new_gfs)
     inputs = self.are_transformation.process(inputs)
     self.assertEqual(1, len(inputs.get_generated_facts()))
     self.assertEqual(inputs.get_generated_facts()[0].get_predicate(),
                      "eat")
Ejemplo n.º 4
0
 def test_property_no_relation(self):
     suggestion = ("why are pandas dead", 1.0, PatternGoogle("why are"),
                   "panda")
     new_gfs = self.openie_fact_generator.get_generated_facts([suggestion])
     inputs = self.empty_input.add_generated_facts(new_gfs)
     inputs = self.are_transformation.process(inputs)
     self.assertEqual(1, len(inputs.get_generated_facts()))
     self.assertEqual(inputs.get_generated_facts()[0].get_predicate(),
                      "has_property")
Ejemplo n.º 5
0
 def test_be_can_duplicate_pattern(self):
     generated_fact = GeneratedFact("test", "be", "can nothing", "", False,
                                    0.0, MultipleSourceOccurrence(),
                                    PatternGoogle("why can <SUBJ>"))
     inputs = self.empty_input.add_generated_facts([generated_fact])
     inputs = self.can_transformation.process(inputs)
     generated_facts = inputs.get_generated_facts()
     self.assertEqual(1, len(generated_facts))
     self.assertEqual("can be", generated_facts[0].get_predicate().get())
     self.assertEqual("nothing", generated_facts[0].get_object().get())
Ejemplo n.º 6
0
 def _test_process(self):
     inputs = self.empty_input.add_subjects([
         Subject("elephant")
     ]).add_patterns([PatternGoogle("why are <SUBJS>")])
     inputs = self.autocomplete.process(inputs)
     self.assertTrue(len(inputs.get_generated_facts()) > 16)
     trunk_facts = [
         x for x in inputs.get_generated_facts()
         if "trunk" in x.get_object().get()
     ]
     self.assertTrue(len(trunk_facts) > 0)
Ejemplo n.º 7
0
def read_pattern(pattern):
    if pattern["type"] == "NO_PATTERN":
        return None
    if pattern["type"] == "MultiplePattern":
        multiple_pattern = MultiplePattern()
        for pattern_temp in pattern["patterns"]:
            multiple_pattern.add_pattern(read_pattern(pattern_temp))
        return multiple_pattern
    if pattern["type"] == "PatternGoogle":
        return PatternGoogle(pattern["prefix"], pattern["relation"], pattern["negative"])
    raise UnknownSerializedObject("Unknown pattern type" + json.dumps(pattern))
Ejemplo n.º 8
0
 def test_vegetarian_negative_pattern(self):
     inputs = self.empty_input.add_subjects([
         Subject("vegetarian")
     ]).add_patterns([PatternGoogle("why don't <SUBJS>", negative=True)])
     inputs = self.autocomplete.process(inputs)
     self.assertTrue(len(inputs.get_generated_facts()) > 0)
     meat_facts = [
         x for x in inputs.get_generated_facts()
         if "meat" == x.get_object().get() and not x.is_negative()
     ]
     print(meat_facts)
     self.assertTrue(len(meat_facts) == 0)
Ejemplo n.º 9
0
 def test_vegetarian_positive_pattern(self):
     inputs = self.empty_input.add_subjects([
         Subject("vegetarian")
     ]).add_patterns([PatternGoogle("why do <SUBJS>")])
     inputs = self.autocomplete.process(inputs)
     predicate_cleaning = CleaningPredicateSubmodule(None)
     inputs = predicate_cleaning.process(inputs)
     self.assertTrue(len(inputs.get_generated_facts()) > 0)
     meat_facts = [
         x for x in inputs.get_generated_facts()
         if "meat" == x.get_object().get() and not x.is_negative()
     ]
     self.assertTrue(len(meat_facts) > 0)
Ejemplo n.º 10
0
 def _to_suggestion(self, question, subjects):
     # No pattern, rank 1 by default
     # We take the subject the closest to the beginning
     # and the longest in case of equality
     min_pos = len(question)
     length = 0
     subject_final = ""
     # Only check the third word for now
     for word in question.split(" ")[2:3]:
         if word in subjects:
             for subject in subjects[word]:
                 temp0 = question.find(subject[0])
                 temp1 = question.find(subject[1])
                 len_temp = len(subject[0])
                 if temp0 != -1 and temp0 < min_pos:
                     min_pos = temp0
                     subject_final = subject[0]
                     length = len_temp
                 elif temp0 != -1 and temp0 == min_pos and len_temp > length:
                     subject_final = subject[0]
                     length = len_temp
                 elif temp1 != -1 and temp1 < min_pos:
                     min_pos = temp1
                     subject_final = subject[0]
                     length = len_temp
                 elif temp1 != -1 and temp1 == min_pos and len_temp > length:
                     subject_final = subject[0]
                     length = len_temp
             if subject_final != "":
                 break
     pattern = None
     if question.startswith("why are ") and \
             not question.startswith("why are not"):
         pattern = PatternGoogle("why are <SUBJS>")
     elif question.startswith("why is ") and \
             not question.startswith("why is not"):
         pattern = PatternGoogle("why is <SUBJ>")
     elif question.startswith("why does ") and \
             not question.startswith("why does not"):
         pattern = PatternGoogle("why does <SUBJ>")
     elif question.startswith("why do ") and \
             not question.startswith("why do not"):
         pattern = PatternGoogle("why do <SUBJS>")
     elif question.startswith("why can "):
         pattern = PatternGoogle("why can <SUBJS>", "CAN")
     elif question.startswith("why can't "):
         pattern = PatternGoogle("why can't <SUBJS>", "CAN", True)
     return [question, 1, pattern, subject_final]
Ejemplo n.º 11
0
 def test_get_str(self):
     pattern = PatternGoogle("how are <SUBJS>")
     self.assertEqual(pattern.to_str_subject(Subject("perl oyster")),
                      "how are perl oysters")
 def process(self, input_interface):
     logging.info("Start the manual patterns for browsers submodule")
     new_patterns = [
         PatternGoogle("why aren't <SUBJS>",
                       relation="has_property",
                       negative=True),
         PatternGoogle("why are <SUBJS>", relation="has_property"),
         PatternGoogle("why is <SUBJ>", relation="has_property"),
         PatternGoogle("why isn't <SUBJ>",
                       relation="has_property",
                       negative=True),
         PatternGoogle("why do <SUBJS>"),
         PatternGoogle("why does <SUBJ>"),
         PatternGoogle("why don't <SUBJS>", negative=True),
         PatternGoogle("why doesn't <SUBJ>", negative=True),
         PatternGoogle("why can <SUBJS>", "CAN"),
         PatternGoogle("why can't <SUBJS>", "CAN", True),
         PatternGoogle("how are <SUBJS>", relation="has_property"),
         PatternGoogle("how is <SUBJ>", relation="has_property"),
         PatternGoogle("how do <SUBJS>"),
         PatternGoogle("how does <SUBJ>"),
         PatternGoogle("how can <SUBJS>", "CAN"),
         PatternGoogle("why <SUBJ>")
     ]
     return input_interface.add_patterns(new_patterns)