Пример #1
0
def create_subgraph_for_person_or_organization(original_tokens: List[str]):
    """
    Creates a subgraph (list of concepts and vector of parents) for PERSON or ORGANIZATION
    Has as input the original tokens in the sentence
    """
    # concepts list
    concepts: List[Concept] = []
    wiki_literal = '_'.join(original_tokens)
    wiki_literal_node = Concept(wiki_literal, wiki_literal)
    name_node = Concept('','name')
    op_nodes = []
    for original_token in original_tokens:
        op_nodes.append(Concept(original_token,original_token))
    concepts.append(wiki_literal_node)
    concepts.append(name_node)
    concepts.extend(op_nodes)

    # parent vector
    vector_of_parents = []
    # wiki
    vector_of_parents.append([-1])
    # name
    vector_of_parents.append([-1])
    # op literals (have parent name node)
    op_literals_parent = [[1] for i in range(len(op_nodes))]
    vector_of_parents.extend(op_literals_parent)

    return concepts, vector_of_parents
Пример #2
0
def test_create_from_amr_example_2():
    amr_str = """(a / and~e.0 
      :op2 (p / possible-01~e.8 
            :ARG1 (a3 / avoid-01~e.10 
                  :ARG0 (h / he~e.7) 
                  :ARG1 (c / censure-01~e.12 
                        :ARG1 h)) 
            :ARG1-of (a2 / actual-02~e.9) 
            :manner (p2 / promise-01~e.5 :polarity~e.2 -~e.2 
                  :ARG0 h 
                  :mod (a4 / any~e.4))))"""
    amr = AMR.parse_string(amr_str)
    custom_amr = CustomizedAMR()
    custom_amr.create_custom_AMR(amr)

    generated_concepts = IdentifiedConcepts()
    generated_concepts.create_from_amr('amr_id_2', amr)
    expected_concepts = IdentifiedConcepts()
    expected_concepts.amr_id = 'amr_id_2'
    expected_concepts.ordered_concepts = [
        Concept('a', 'and'),
        Concept('-', '-', 0),
        Concept('a4', 'any'),
        Concept('p2', 'promise-01'),
        Concept('h', 'he'),
        Concept('p', 'possible-01'),
        Concept('a2', 'actual-02'),
        Concept('a3', 'avoid-01'),
        Concept('c', 'censure-01')
    ]
    assert_identified_concepts(expected_concepts, generated_concepts)
Пример #3
0
def test_create_from_amr_example_reentrancy():
    amr_str = """(r / receive-01~e.4
                      :ARG0 (w / we~e.0)
                      :ARG1 (t / thing~e.7
                            :ARG0-of~e.7 (r2 / remind-01~e.7
                                  :ARG1 (p / pay-01~e.6
                                        :ARG0 w)
                                  :ARG2 w))
                      :ARG2~e.8 (h / hospital~e.10)
                      :time (n / now~e.2)
                      :time (a / already~e.3))"""
    amr = AMR.parse_string(amr_str)
    generated_concepts = IdentifiedConcepts()
    generated_concepts.create_from_amr('amr_id_reentrancy', amr)
    expected_concepts = IdentifiedConcepts()
    expected_concepts.amr_id = 'amr_id_reentrancy'
    expected_concepts.ordered_concepts = [
        Concept('w', 'we'),
        Concept('n', 'now'),
        Concept('a', 'already'),
        Concept('r', 'receive-01'),
        Concept('p', 'pay-01'),
        Concept('r2', 'remind-01'),
        Concept('t', 'thing'),
        Concept('h', 'hospital')
    ]
    assert_identified_concepts(expected_concepts, generated_concepts)
Пример #4
0
def test_strip_concept_sense():
    concept_name = 'recommend-01'
    stripped_concept = Concept.strip_concept_sense(concept_name)
    expected_concept = 'recommend'
    assert stripped_concept == expected_concept

    concept_name = 'go'
    stripped_concept = Concept.strip_concept_sense(concept_name)
    expected_concept = 'go'
    assert stripped_concept == expected_concept

    concept_name = '-'
    stripped_concept = Concept.strip_concept_sense(concept_name)
    expected_concept = '-'
    assert stripped_concept == expected_concept
Пример #5
0
def test_create_from_custom_amr_example_1():
    amr: AMR = AMR()
    amr.node_to_concepts = {'i': 'it', 'v': 'vigorous', 'a': 'advocate-01', 'r': 'recommend-01'}
    amr.node_to_tokens = {'i': ['0'], 'v': ['3'], 'a': ['4'], 'r': ['1']}
    amr.relation_to_tokens = {'manner': [('2', 'a')]}
    amr['i'] = {}
    amr['v'] = {}
    amr['a'] = {'ARG1': [('i',)], 'manner': [('v',)]}
    amr['r'] = {'ARG1': [('a',)]}
    generated_concepts = IdentifiedConcepts()
    generated_concepts.create_from_amr('amr_id_1', amr)
    expected_concepts = IdentifiedConcepts()
    expected_concepts.amr_id = 'amr_id_1'
    expected_concepts.ordered_concepts = [Concept('i', 'it'), Concept('r', 'recommend-01'), Concept('v', 'vigorous'),
                                          Concept('a', 'advocate-01')]
    assert_identified_concepts(expected_concepts, generated_concepts)
def test_generate_dataset_entry():
    amr_str = """(r / recommend-01~e.1
                    :ARG1 (a / advocate-01~e.4
                        :ARG1 (i / it~e.0)
                        :manner~e.2 (v / vigorous~e.3)))"""
    sentence = """It should be vigorously advocated ."""
    generated_entry: ArcsTrainingEntry = generate_dataset_entry('amr_id', amr_str, sentence, 0, 1, False, False)
    expected_identified_concepts = IdentifiedConcepts()
    expected_identified_concepts.amr_id = 'amr_id'
    expected_identified_concepts.ordered_concepts = [Concept('', 'ROOT'),
                                                     Concept('i', 'it'),
                                                     Concept('r', 'recommend-01'),
                                                     Concept('v', 'vigorous'),
                                                     Concept('a', 'advocate-01')]
    expected_parent_vectors = [(-1, 4, 0, 4, 2)]
    assert_identified_concepts(expected_identified_concepts, generated_entry.identified_concepts)
    assert_parent_vectors(expected_parent_vectors, generated_entry.parent_vectors)
Пример #7
0
def test_generate_parent_vector_example_2():
    amr_str = """(r / recommend-01~e.1
                    :ARG1 (a / advocate-01~e.4
                        :ARG1 (i / it~e.0)
                        :manner~e.2 (v / vigorous~e.3)))"""
    amr: AMR = AMR.parse_string(amr_str)
    identified_concepts = IdentifiedConcepts()
    identified_concepts.ordered_concepts = [
        Concept('', 'ROOT'),
        Concept('i', 'it'),
        Concept('r', 'recommend-01'),
        Concept('v', 'vigorous'),
        Concept('a', 'advocate-01')
    ]
    generated_parent_vector = generate_parent_vectors(amr, identified_concepts)
    expected_parent_vector = [[-1, 4, 0, 4, 2]]
    assert_parent_vectors(expected_parent_vector, generated_parent_vector)
Пример #8
0
def test_create_from_amr_example_1():
    amr_str = """(r / recommend-01~e.1
                    :ARG1 (a / advocate-01~e.4
                        :ARG1 (i / it~e.0)
                        :manner~e.2 (v / vigorous~e.3)))"""
    amr = AMR.parse_string(amr_str)
    generated_concepts = IdentifiedConcepts()
    generated_concepts.create_from_amr('amr_id_1', amr)
    expected_concepts = IdentifiedConcepts()
    expected_concepts.amr_id = 'amr_id_1'
    expected_concepts.ordered_concepts = [
        Concept('i', 'it'),
        Concept('r', 'recommend-01'),
        Concept('v', 'vigorous'),
        Concept('a', 'advocate-01')
    ]
    assert_identified_concepts(expected_concepts, generated_concepts)
def test_generate_parent_vector():
    amr: AMR = AMR()
    amr.roots = ['r']
    amr.reentrance_triples = []
    amr.node_to_concepts = {'i': 'it', 'v': 'vigorous', 'a': 'advocate-01', 'r': 'recommend-01'}
    amr.node_to_tokens = {'i': ['0'], 'v': ['3'], 'a': ['4'], 'r': ['1']}
    amr.relation_to_tokens = {'manner': [('2', 'a')]}
    amr['i'] = {}
    amr['v'] = {}
    amr['a'] = {'ARG1': [('i',)], 'manner': [('v',)]}
    amr['r'] = {'ARG1': [('a',)]}
    identified_concepts = IdentifiedConcepts()
    identified_concepts.ordered_concepts = [Concept('', 'ROOT'),
                                            Concept('i', 'it'),
                                            Concept('r', 'recommend-01'),
                                            Concept('v', 'vigorous'),
                                            Concept('a', 'advocate-01')]
    generated_parent_vector = generate_parent_vectors(amr, identified_concepts, 1)
    expected_parent_vector = [(-1, 4, 0, 4, 2)]
    assert_parent_vectors(expected_parent_vector, generated_parent_vector)
Пример #10
0
def test_generate_amr_node_for_vector_of_parents():
    identified_concepts: IdentifiedConcepts = IdentifiedConcepts()
    identified_concepts.ordered_concepts = [Concept('', 'ROOT'),  # 0
                                            Concept('i', 'it'),  # 1
                                            Concept('r', 'recommend-01'),  # 2
                                            Concept('v', 'vigorous'),  # 3
                                            Concept('a', 'advocate-01')  # 4
                                            ]
    predicted_vector_of_parents = [[-1], [4], [0], [4], [2]]
    relations_dict = {
        ('recommend-01', 'advocate-01'): 'ARG1',
        ('advocate-01', 'it'): 'ARG1',
        ('advocate-01', 'vigorous'): 'manner'
    }
    amr: Node = generate_amr_node_for_vector_of_parents(identified_concepts,
                                                        predicted_vector_of_parents,
                                                        relations_dict)
    generated_amr_str = amr.amr_print_with_reentrancy()
    gold_amr_str = """(r / recommend-01~e.1
                    :ARG1 (a / advocate-01~e.4
                        :ARG1 (i / it~e.0)
                        :manner~e.2 (v / vigorous~e.3)))"""
    smatch = calculate_smatch(generated_amr_str, gold_amr_str)
    assert smatch == 1
def test_post_processing_on_parent_vector():
    sentence = 'Comrade PERSON once said that the Communist Party will not be overthrown - ' \
               'if it falls , it will be brought down from within the party itself .'
    metadata = {1: ['Deng', 'Xiaoping']}
    identified_concepts = IdentifiedConcepts()
    identified_concepts.ordered_concepts = [Concept('', 'ROOT'), Concept('c', 'comrade'),
                                            Concept('h', 'have-org-role-91'),
                                            Concept('p', 'PERSON'), Concept('o', 'once'), Concept('s', 'say-01'),
                                            Concept('p2', 'political-party'),
                                            Concept('Communist_Party_of_China', 'Communist_Party_of_China'),
                                            Concept('n2', 'name'),
                                            Concept('Communist', 'Communist'), Concept('Party', 'Party'),
                                            Concept('-', '-'), Concept('o', 'overthrow-01'),
                                            Concept('a', 'and'), Concept('f', 'fall-05'), Concept('b', 'bring-down'),
                                            Concept('p3', 'person'), Concept('h2', 'have-org-role-91')
                                            ]
    vector_of_parents = [[-1], [2], [3],
                         [5], [5], [0],
                         [12, 14, 15, 16, 17],
                         [6], [6],
                         [8], [8],
                         [12], [13],
                         [5], [15], [13],
                         [15], [16]]
    post_processing_on_parent_vector(identified_concepts, vector_of_parents, sentence, metadata)
    expected_ordered_concepts = [Concept('', 'ROOT'), Concept('c', 'comrade'),
                                 Concept('h', 'have-org-role-91'),
                                 Concept('p', 'person'), Concept('o', 'once'), Concept('s', 'say-01'),
                                 Concept('p2', 'political-party'),
                                 Concept('Communist_Party_of_China', 'Communist_Party_of_China'),
                                 Concept('n2', 'name'),
                                 Concept('Communist', 'Communist'), Concept('Party', 'Party'),
                                 Concept('-', '-'), Concept('o', 'overthrow-01'),
                                 Concept('a', 'and'), Concept('f', 'fall-05'), Concept('b', 'bring-down'),
                                 Concept('p3', 'person'), Concept('h2', 'have-org-role-91'),
                                 Concept('Deng_Xiaoping', 'Deng_Xiaoping'), Concept('', 'name'),
                                 Concept('Deng', 'Deng'), Concept('Xiaoping', 'Xiaoping')
                                 ]
    expected_vector_of_parents = [[-1], [2], [3],
                                  [5], [5], [0],
                                  [12, 14, 15, 16, 17],
                                  [6], [6],
                                  [8], [8],
                                  [12], [13],
                                  [5], [15], [13],
                                  [15], [16],
                                  [3], [3],
                                  [19], [19]]
    assert identified_concepts.ordered_concepts == expected_ordered_concepts
    assert vector_of_parents == expected_vector_of_parents
Пример #12
0
def test_generate_parent_vector_example_2():
    amr_str = """(m / man~e.2 
      :ARG1-of (m2 / marry-01~e.1) 
      :ARG0-of (l / love-01~e.9 
            :ARG1~e.10 (y / you~e.11) 
            :ARG1-of (r / real-04~e.6) 
            :condition-of~e.4 (a3 / and~e.16 
                  :op1 (g / go-06~e.14 
                        :ARG2 (a / ahead~e.15) 
                        :mod (j / just~e.13)) 
                  :op2 (o2 / or~e.22 
                        :op1 (f / file-01~e.17 
                              :ARG4~e.18 (d / divorce-01~e.19) 
                              :time (n / now~e.20)) 
                        :op2 (m3 / move-01~e.25 
                              :ARG2 (o / out-06~e.26 
                                    :ARG2~e.27 (h / house~e.29 
                                          :poss~e.28 m~e.28)) 
                              :time n~e.30 
                              :mod (a2 / at-least~e.23,24))))))"""
    amr: AMR = AMR.parse_string(amr_str)
    identified_concepts = IdentifiedConcepts()
    identified_concepts.ordered_concepts = [
        Concept('', 'ROOT'),  # 0
        Concept('m2', 'marry-01'),  # 1
        Concept('m', 'man'),  # 2
        Concept('r', 'real-04'),  # 3
        Concept('l', 'love-01'),  # 4
        Concept('y', 'you'),  # 5
        Concept('j', 'just'),  # 6
        Concept('g', 'go-06'),  # 7
        Concept('a', 'ahead'),  # 8
        Concept('a3', 'and'),  # 9
        Concept('f', 'file-01'),  # 10
        Concept('d', 'divorce-01'),  # 11
        Concept('n', 'now'),  # 12
        Concept('o2', 'or'),  # 13
        Concept('a2', 'at-least'),  # 14
        Concept('m3', 'move-01'),  # 15
        Concept('o', 'out-06'),  # 16
        Concept('h', 'house')  # 17
    ]
    generated_parent_vector = generate_parent_vectors(amr, identified_concepts,
                                                      2)
    expected_parent_vector = [
        (-1, 2, 0, 4, 2, 4, 7, 9, 7, 4, 13, 10, 10, 9, 15, 13, 15, 16),
        (-1, 2, 0, 4, 2, 4, 7, 9, 7, 4, 13, 10, 15, 9, 15, 13, 15, 16)
    ]
    assert_parent_vectors(expected_parent_vector, generated_parent_vector)
Пример #13
0
def test__create_from_amr_with_2_polarites():
    amr_str = """(a / and~e.0
                      :op2 (p2 / practice-01~e.13
                            :ARG1 (l / loan-01~e.12
                                  :ARG2 (p / person~e.11
                                        :ARG0-of~e.11 (s / study-01~e.11)))
                            :mod (s2 / sane~e.10 :polarity~e.10 -~e.10)
                            :ARG1-of (i2 / identical-01~e.16
                                  :ARG2~e.19 (p3 / practice-01~e.24
                                        :ARG1 (l2 / loan-01~e.23
                                              :ARG1 (m / mortgage-01~e.22))
                                        :mod (s3 / sane~e.21 :polarity~e.21 -~e.21))
                                  :manner (w / way~e.18
                                        :mod (e / every~e.18)))
                            :ARG0-of (c2 / cause-01~e.3,8 
                                  :ARG1 (b / be-located-at-91~e.5,7
                                        :ARG1 (t / they~e.4)
                                        :ARG2 (t2 / there~e.6))
                                  :mod (o / only~e.2))))"""
    amr: AMR = AMR.parse_string(amr_str)
    generated_concepts = IdentifiedConcepts()
    generated_concepts.create_from_amr('amr_2_polarities', amr)
    expected_concepts = IdentifiedConcepts()
    expected_concepts.amr_id = 'amr_2_polarities'
    expected_concepts.ordered_concepts = [
        Concept('a', 'and'),
        Concept('o', 'only'),
        Concept('c2', 'cause-01'),
        Concept('t', 'they'),
        Concept('b', 'be-located-at-91'),
        Concept('t2', 'there'),
        Concept('-', '-', 0),
        Concept('s2', 'sane'),
        Concept('s', 'study-01'),
        Concept('p', 'person'),
        Concept('l', 'loan-01'),
        Concept('p2', 'practice-01'),
        Concept('i2', 'identical-01'),
        Concept('e', 'every'),
        Concept('w', 'way'),
        Concept('-', '-', 1),
        Concept('s3', 'sane'),
        Concept('m', 'mortgage-01'),
        Concept('l2', 'loan-01'),
        Concept('p3', 'practice-01')
    ]
    assert_identified_concepts(expected_concepts, generated_concepts)