예제 #1
0
def _get_negated_query(question):
    relation = question.relation
    relation_name = relation.relation_name
    if relation_name.endswith('_negated'):
        negated_relation_name = relation_name.replace('_negated', '')
    else:
        negated_relation_name = f'{relation_name}_negated'
    negated_relation_class = name2relation(negated_relation_name)
    negated_relation = negated_relation_class(relation.entity1,
                                              relation.entity2)
    return negated_relation.as_prolog_query() + '.'
예제 #2
0
 def _memory2fact(self, memory):
     pattern = r'([a-zA-Z0_]+)\(([a-zA-Z_]+),([a-zA-Z_]+)\)'
     match = re.match(pattern, memory)
     if match is None:
         raise Exception(f'Invalid fact in memory file: {memory}')
     groups = match.groups()
     relation_name = groups[0]
     entity1 = groups[1]
     entity2 = groups[2]
     relation_class = name2relation(relation_name)
     relation = relation_class(entity1, entity2)
     fact = Fact(relation)
     return fact
예제 #3
0
 def _find_triple_match(self, threshold, triple):
     best_match = None
     for relation_name, sentences in self.relations:
         insert_entities_in_sentence = partial(
             self._insert_entities_in_sentence, triple)
         target_sentences = map(insert_entities_in_sentence, sentences)
         for target_sentence in target_sentences:
             distance = get_distance(self.nlp, triple.predicate_decsription,
                                     target_sentence)
             if distance < threshold and (best_match is None or
                                          distance < best_match.distance):
                 relation_class = name2relation(relation_name)
                 relation = relation_class(triple.entity1, triple.entity2)
                 best_match = Match(relation, distance)
     return best_match
예제 #4
0
def match_with_relation(threshold, matcher, sentence, relation_name, entity1,
                        entity2):
    """
    Check if a match can be found for a sentence.

    :type threshold: float
    :type matcher: app.match.Matcher
    :type sentence: str
    :type relation_name: str
    :type entity1: str
    :type entity2: str
    :rtype: (app.types.match.Match, bool, str)
    """
    relation_class = name2relation(relation_name)
    expected_relation = relation_class(entity1, entity2)
    try:
        matches = matcher.input2matches(threshold, sentence)
        if len(matches) == 0:
            match = None
            passed = False
            fail_reason = f'No matches found (threshold = {threshold}).'
        else:
            found_match = False
            for match in matches:
                found_match = found_match or match.relation == expected_relation
                if found_match:
                    break
            best_match = matches[0]
            for match in matches:
                if match.distance < best_match.distance:
                    best_match = match.distance
            if found_match:
                passed = True
                fail_reason = None
            else:
                match = None
                passed = False
                fail_reason = f'Did not match with {expected_relation}, best match = {best_match}.'
    except:
        match = None
        passed = False
        fail_reason = 'Exception.'
    return match, passed, fail_reason
예제 #5
0
def _test_reason(threshold, matcher, sentence, relation_name, entity1,
                 entity2):
    name = relation_name
    description = f'{relation_name}({entity1},{entity2})'
    relation_class = name2relation(relation_name)
    relation = relation_class(entity1, entity2)
    facts = [Fact(relation)]
    answer, passed_answer, fail_reason_answer = answer_question(
        threshold, matcher, sentence, relation_name, entity1, entity2, facts)
    if not passed_answer:
        passed = False
        fail_reason = fail_reason_answer
    else:
        if answer.answer == Answer.NO:
            passed = False
            fail_reason = 'Expected answer = yes, but got answer = no.'
        elif answer.answer == Answer.YES:
            passed = True
            fail_reason = None
        else:
            passed = False
            fail_reason = 'Expected answer = yes, but got answer = unknown.'
    return TestResult(name, description, passed, fail_reason)
def _tuple2fact(fact_tuple):
    relation_name, entity1, entity2 = fact_tuple
    relation_class = name2relation(relation_name)
    relation = relation_class(entity1, entity2)
    fact = Fact(relation)
    return fact