Exemplo n.º 1
0
def test_amr_print_with_reentrancy():
    r: Node = Node('receive-01')
    w: Node = Node('we')
    t: Node = Node('thing')
    r2: Node = Node('remind-01')
    p: Node = Node('pay-01')
    h: Node = Node('hospital')
    n: Node = Node('now')
    a: Node = Node('already')
    r.add_child(w, 'ARG0')
    r.add_child(t, 'ARG1')
    r.add_child(h, 'ARG2')
    r.add_child(n, 'time')
    r.add_child(a, 'time')
    t.add_child(r2, 'ARG0-of')
    r2.add_child(p, 'ARG1')
    r2.add_child(w, 'ARG2')
    p.add_child(w, 'ARG0')
    generated_amr_str = r.amr_print_with_reentrancy()
    expected_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))"""
    smatch = calculate_smatch(generated_amr_str, expected_amr_str)
    assert smatch == 1
def test_pre_and_post_processing_eg_2():
    sentence = 'It is Santorum that is the by far major nonRomney candidate and Newt would appear to be the spoiler .'
    amr_str = """(a / and~e.11
      :op1 (c / candidate~e.10
            :ARG1-of (m / major-02~e.8
                  :degree (b / by-far~e.6,7))
            :mod (p3 / person~e.9 :polarity -~e.9 :wiki "Mitt_Romney"~e.9
                  :name (n2 / name~e.9 :op1 "Romney"~e.9))
            :domain~e.1,4 (p2 / person :wiki "Rick_Santorum"
                  :name (n / name :op1 "Santorum"~e.2)))
      :op2 (a2 / appear-02~e.14
            :ARG1 (s / spoil-01~e.18
                  :ARG0 (p4 / person :wiki "Newt_Gingrich"
                        :name (n3 / name :op1 "Newt"~e.12)))))"""
    amr: AMR = AMR.parse_string(amr_str)
    amr, new_sentence, metadata = train_pre_processing(amr, sentence)
    identified_concepts = IdentifiedConcepts()
    identified_concepts.create_from_amr('', amr)
    add_false_root(identified_concepts)
    vector_of_parents = generate_parent_list_vector(amr, identified_concepts)
    post_processing_on_parent_vector(identified_concepts, vector_of_parents,
                                     new_sentence, metadata)
    relations_dict = {
        ('and', 'candidate'): 'op1',
        ('and', 'appear-02'): 'op2',
        ('candidate', 'major-02'): 'ARG1-of',
        ('candidate', 'person'): 'mod',
        ('major-02', 'by-far'): 'degree',
        ('person', '-'): 'polarity',
        ('person', 'Mitt_Romney'): 'wiki',
        ('person', 'name'): 'name',
        ('person', 'Santorum'): 'wiki',
        ('name', 'Romney'): 'op1',
        ('name', 'Santorum'): 'op1',
        ('appear-02', 'spoil-01'): 'ARG1',
        ('spoil-01', 'person'): 'ARG0',
        ('person', 'Newt'): 'wiki',
        ('name', 'Newt'): 'op1'
    }
    amr_node: Node = generate_amr_node_for_vector_of_parents(
        identified_concepts, vector_of_parents, relations_dict)
    generated_amr_str = amr_node.amr_print_with_reentrancy()
    expected_amr_str = """(a / and~e.11
      :op1 (c / candidate~e.10
            :ARG1-of (m / major-02~e.8
                  :degree (b / by-far~e.6,7))
            :mod (p3 / person~e.9 :polarity -~e.9 :wiki "Mitt_Romney"~e.9
                  :name (n2 / name~e.9 :op1 "Romney"~e.9))
            :mod~e.1,4 (p2 / person :wiki "Santorum"
                  :name (n / name :op1 "Santorum"~e.2)))
      :op2 (a2 / appear-02~e.14
            :ARG1 (s / spoil-01~e.18
                  :ARG0 (p4 / person :wiki "Newt"
                        :name (n3 / name :op1 "Newt"~e.12)))))"""

    smatch = calculate_smatch(generated_amr_str, expected_amr_str)
    assert smatch == 1
Exemplo n.º 3
0
def test_generate_amr_node_for_vector_of_parents_example_1():
    amr_str = """(s / suppose-01~e.1 
                      :ARG0 (i / i~e.0) 
                      :ARG1 (p / possible-01~e.3 
                            :ARG1 (a / add-02~e.4 
                                  :ARG0 (y / you~e.2) 
                                  :ARG1 (p2 / probation~e.5 
                                        :ARG1-of (c / contrast-01~e.7 
                                              :ARG2 (r / replace-01~e.12 
                                                    :ARG1 p2 
                                                    :ARG2~e.13 (t / time~e.15 
                                                          :mod (j / jail~e.14)) 
                                                    :mod (j2 / just~e.10)))))))"""
    amr_str1 = """(d1 / suppose-01~e.1 
                      :ARG0 (i / i~e.0) 
                      :ARG1 (p / possible-01~e.3 
                            :ARG1 (a / add-02~e.4 
                                  :ARG0 (y / you~e.2) 
                                  :ARG1 (p2 / probation~e.5 
                                        :ARG1-of (c / contrast-01~e.7 
                                              :ARG2 (r / replace-01~e.12 
                                                    :ARG1 p2 
                                                    :mod (j2 / just~e.10)
                                                    :ARG2~e.13 (t / time~e.15 
                                                          :mod (j / jail~e.14)) 
                                                    ))))))"""
    amr: AMR = AMR.parse_string(amr_str)
    identified_concepts = IdentifiedConcepts()
    identified_concepts.create_from_amr('amr_1', amr)
    add_false_root(identified_concepts)
    vector_of_parents = generate_parent_list_vector(amr, identified_concepts)
    # transf parent vectors to vector of parents
    # i s y p a p2 c j2 r j  t
    # 1 2 3 4 5 6  7 8  9 10 11
    relations_dict = {
        ('suppose-01', 'i'): 'ARG0',
        ('suppose-01', 'possible-01'): 'ARG1',
        ('possible-01', 'add-02'): 'ARG1',
        ('add-02', 'you'): 'ARG0',
        ('add-02', 'probation'): 'ARG1',
        ('probation', 'contrast-01'): 'ARG1-of',
        ('contrast-01', 'replace-01'): 'ARG2',
        ('replace-01', 'probation'): 'ARG1',
        ('replace-01', 'time'): 'ARG2',
        ('replace-01', 'just'): 'mod',
        ('time', 'jail'): 'mod'
    }
    amr_node: Node = generate_amr_node_for_vector_of_parents(
        identified_concepts, vector_of_parents, relations_dict)
    generated_amr_str = amr_node.amr_print_with_reentrancy()
    smatch = calculate_smatch(generated_amr_str, amr_str)
    assert smatch == 1
def test_pre_and_post_processing_for_organization():
    sentence = 'Some propaganda activities of ZF have soon become viewed as jokes by the people .'
    amr_str = """(b / become-01~e.7
      :ARG1 a
      :ARG2 (v / view-02~e.8
            :ARG0~e.11 (p2 / person~e.13)
            :ARG1 (a / activity-06~e.2
                  :ARG0~e.3 (o / organization :wiki ZF
                        :name (n / name :op1 "ZF"~e.4))
                  :ARG1 (p / propaganda~e.1)
                  :quant (s / some~e.0))
            :ARG2~e.9 (t / thing~e.10
                  :ARG2-of~e.10 (j / joke-01~e.10)))
      :time~e.9 (s2 / soon~e.6))"""
    amr: AMR = AMR.parse_string(amr_str)
    amr, new_sentence, metadata = train_pre_processing(amr, sentence)
    identified_concepts = IdentifiedConcepts()
    identified_concepts.create_from_amr('', amr)
    add_false_root(identified_concepts)
    vector_of_parents = generate_parent_list_vector(amr, identified_concepts)
    post_processing_on_parent_vector(identified_concepts, vector_of_parents,
                                     new_sentence, metadata)
    relations_dict = {
        ('become-01', 'activity-06'): 'ARG1',
        ('become-01', 'view-02'): 'ARG2',
        ('become-01', 'soon'): 'time',
        ('view-02', 'person'): 'ARG0',
        ('view-02', 'activity-06'): 'ARG1',
        ('view-02', 'thing'): 'ARG2',
        ('activity-06', 'organization'): 'ARG0',
        ('activity-06', 'propaganda'): 'ARG1',
        ('activity-06', 'some'): 'quant',
        ('organization', 'ZF'): 'wiki',
        ('organization', 'name'): 'name',
        ('name', 'ZF'): 'op1',
        ('thing', 'joke-01'): 'ARG2-of'
    }
    amr_node: Node = generate_amr_node_for_vector_of_parents(
        identified_concepts, vector_of_parents, relations_dict)
    generated_amr_str = amr_node.amr_print_with_reentrancy()
    smatch = calculate_smatch(generated_amr_str, amr_str)
    assert smatch == 1
Exemplo n.º 5
0
def test_amr_print_with_literal():
    r: Node = Node('realize-01')
    neg: Node = Node('-')
    i: Node = Node('i')
    t2: Node = Node('threaten-01')
    c2: Node = Node('country')
    iran1: Node = Node(None, 'Iran')
    n: Node = Node('name')
    iran2: Node = Node(None, 'Iran')
    h: Node = Node('huge')
    s2: Node = Node('such')
    c: Node = Node('cause-01')
    a: Node = Node('amr-unknown')
    s: Node = Node('simple')
    r.add_child(neg, 'polarity')
    r.add_child(i, 'ARG0')
    r.add_child(t2, 'ARG1')
    r.add_child(s, 'manner')
    t2.add_child(c2, 'ARG0')
    t2.add_child(h, 'mod')
    t2.add_child(c, 'ARG1-of')
    c2.add_child(iran1, 'wiki')
    c2.add_child(n, 'name')
    n.add_child(iran2, 'op1')
    h.add_child(s2, 'degree')
    c.add_child(a, 'ARG0')
    generated_amr_str = r.amr_print()
    expected_amr_str = """(r / realize-01 :polarity~e.3 -~e.3
          :ARG0 (i / i~e.0)
          :ARG1 (t2 / threaten-01~e.11
                :ARG0 (c2 / country :wiki "Iran"
                      :name (n / name :op1 "Iran"~e.6))
                :mod (h / huge~e.10
                      :degree (s2 / such~e.8))
                :ARG1-of (c / cause-01~e.5
                      :ARG0~e.5 (a / amr-unknown~e.5)))
          :manner (s / simple~e.1))"""
    smatch = calculate_smatch(generated_amr_str,expected_amr_str)
    assert smatch == 1
Exemplo n.º 6
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