Ejemplo n.º 1
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)
Ejemplo n.º 2
0
def read_original_graphs(file_type, filter_path="deft", cache=True):
    """
        Returns a list of (amr_id, sentence, AMR, CustomizedAMR) quadruples
        Loads the list from a dump file if present, else generates it and saves it to a dump file
        :param file_type - data set partition (training, dev or test)
        :param filter_path - filtering criteria for data files
        :param cache - allow to load from dump file if true, else calculate from original file and save new dump
    """
    if filter_path is None:
        filter_path = "deft"
    dir_path = AMR_ALIGNMENTS_SPLIT + "/" + file_type

    parsed_data = []

    directory_content = listdir(dir_path)
    original_corpus = sorted(
        [x for x in directory_content if "dump" not in x and filter_path in x])

    for file_name in original_corpus:
        original_file_path = dir_path + "/" + file_name
        dump_file_path = dir_path + "/original_graphs_dumps/" + file_name + ".dump"
        print(original_file_path)

        if cache and path.exists(dump_file_path):
            print("cache")
            with open(dump_file_path, "rb") as dump_file:
                parsed_data += js.load(dump_file)
        else:
            print("generate")
            file_data = input_file_parser.extract_data_records(
                original_file_path)

            parsed_file_data = []
            failed_amrs_in_file = 0

            for amr_triple in file_data:
                try:
                    camr_graph = AMR.parse_string(amr_triple[1])

                    custom_amr_graph = CustomizedAMR()
                    custom_amr_graph.create_custom_AMR(camr_graph)

                    parsed_file_data.append((amr_triple[2], amr_triple[0],
                                             camr_graph, custom_amr_graph))
                except Exception as _:
                    # print "Exception when parsing AMR with ID: %s in file %s with error: %s\n" % (
                    #    amr_triple[2], file_name, e)
                    failed_amrs_in_file += 1

            if not path.exists(path.dirname(dump_file_path)):
                makedirs(path.dirname(dump_file_path))
            with open(dump_file_path, "wb") as dump_file:
                js.dump(parsed_file_data, dump_file)
            parsed_data += parsed_file_data

            print(("%d / %d in %s" %
                   (failed_amrs_in_file, len(file_data), original_file_path)))

    return parsed_data
Ejemplo n.º 3
0
def test_create_custom_AMR_example_no_exception():
    amr_str = """(p / pay-01~e.16 
      :ARG0 (p2 / person 
            :mod (s / star~e.2 
                  :mod (p3 / pop~e.1)) 
            :mod (d3 / debt~e.13 
                  :consist-of (m / monetary-quantity :quant 240000000~e.10,11 
                        :unit (d2 / dollar~e.9)) 
                  :ARG1-of (s2 / say-01~e.6))) 
      :ARG1 (m2 / monetary-quantity :quant 6~e.17 
            :unit (f / figure~e.18)) 
      :ARG2~e.26 (a / and~e.30 
            :op1 (d / doctor~e.29 
                  :mod (v / voodoo~e.28) 
                  :mod (a2 / another~e.27)) 
            :op2 (w / woman~e.34 :wiki - 
                  :name (n / name~e.35 :op1 "Samia"~e.36) 
                  :mod (m3 / mystery~e.32) 
                  :mod (c2 / country :wiki "Egypt" 
                        :name (n2 / name~e.35 :op1 "Egypt"~e.33)) 
                  :ARG1-of (c3 / come-01~e.39 
                        :ARG4~e.40 p2~e.41 
                        :accompanier~e.42 (l / letter~e.44 
                              :mod~e.45 (g / greet-01~e.46 
                                    :ARG0~e.47 (p7 / person 
                                          :ARG0-of (h2 / have-org-role-91 
                                                :ARG1 c6 
                                                :ARG2 (p4 / prince~e.53 
                                                      :ARG1-of (r2 / rank-01~e.51 
                                                            :ARG1-of (h / high-02~e.49)))) 
                                          :ARG0-of (p5 / purport-01 
                                                :ARG1 (p6 / person :wiki "Mohammed_bin_Nawwaf_bin_Abdulaziz" 
                                                      :name (n4 / name~e.35 :op1 "Nawaf"~e.56 :op2 "Bin"~e.57 :op3 "Abdulaziz"~e.58 :op4 "Al"~e.59 :op5 "Saud"~e.61) 
                                                      :ARG0-of (h3 / have-org-role-91~e.68 
                                                            :ARG1 (c6 / country :wiki "Saudi_Arabia" 
                                                                  :name (n5 / name~e.35 :op1 "Saudi"~e.69 :op2 "Arabia"~e.70)) 
                                                            :ARG2 (c5 / chief~e.65 
                                                                  :topic~e.66 (i / intelligence~e.67)) 
                                                            :time (n6 / now~e.63)))))))))) 
      :ARG3~e.19 (c / cleanse-01~e.22 
            :manner (r / ritual~e.21) 
            :ARG0-of (u / use-01~e.23 
                  :ARG1 (b / blood~e.25 
                        :mod (s3 / sheep~e.24)))))"""
    amr = AMR.parse_string(amr_str)
    custom_amr = CustomizedAMR()
    custom_amr.create_custom_AMR(amr)
Ejemplo n.º 4
0
def map_from_amr_to_custom_amr_dataset_dict(dataset_dict):
    """
    Takes as input a dictionary of the form:
        {dataset: [sentence, amr, amr_id]}
    Outputs a dictionary of the form:
        {dataset: [sentence, custom_amr, amr_id]}
    """
    custom_amr_dataset_dict = {}
    for dataset, data in dataset_dict.items():
        new_format_data = []
        for data_item in data:
            amr = data_item[1]
            custom_amr = CustomizedAMR()
            custom_amr.create_custom_AMR(amr)
            new_format_data.append((data_item[0], custom_amr, data_item[2]))
        custom_amr_dataset_dict[dataset] = new_format_data
    return custom_amr_dataset_dict
Ejemplo n.º 5
0
 def __init__(self, no_of_swaps):
     self.no_of_swaps = no_of_swaps
     self.amr_graph = CustomizedAMR()
     self.buffer = []
     self.buffer_indices = []
     self.stack = []
     self.actions = []
     self.removed_indices = []
     self.current_token = 0
Ejemplo n.º 6
0
 def __add_unaligned_concepts_randomly(self, custom_amr: CustomizedAMR,
                                       all_concepts_vars):
     all_concepts_vars_set = set(all_concepts_vars)
     ordered_concepts_vars = set(
         [c.variable for c in self.ordered_concepts])
     unaligned_concept_vars = all_concepts_vars_set.difference(
         ordered_concepts_vars)
     for unaligned_concept_var in unaligned_concept_vars:
         concept_name = custom_amr.get_concept_for_var(
             unaligned_concept_var)
         concept = Concept(unaligned_concept_var, concept_name)
         random_index = random.randint(0, len(self.ordered_concepts) - 1)
         self.ordered_concepts.insert(random_index, concept)
Ejemplo n.º 7
0
def test_get_children_list_repr():
    custom_amr = CustomizedAMR()
    custom_amr.tokens_to_concepts_dict = {}
    custom_amr.tokens_to_concept_list_dict = {}
    # (child,parent) : (relation, children of child, token aligned to child)
    custom_amr.relations_dict = {('a', 'a'): ('value', ['a'], ['0'])}
    custom_amr.parent_dict = {'a': 'a'}
    generated_list_repr = get_children_list_repr(custom_amr, 'amr_id')
    expected_list_repr = ChildrenListRepresentation()
    expected_list_repr.root = 0
    expected_list_repr.children_dict = {0: [0]}
    assert expected_list_repr == generated_list_repr, \
        'expected ' + str(expected_list_repr) + ' got ' + str(generated_list_repr)
Ejemplo n.º 8
0
if __name__ == "__main__":

    parser_parameters = ParserParameters(max_len=50, with_enhanced_dep_info=False,
                                         with_target_semantic_labels=False, with_reattach=True,
                                         with_gold_concept_labels=True, with_gold_relation_labels=True)

    sentence = "It looks like we will also bring in whales ."
    amr = AMR.parse_string("""
    (l / look-02~e.1
          :ARG1~e.2 (b / bring-01~e.6
                :ARG0 (w / we~e.3)
                :ARG1~e.7 (w2 / whale~e.8)
                :mod (a / also~e.5)))
    """)

    custom_AMR = CustomizedAMR()
    custom_AMR.create_custom_AMR(amr)

    actions = ActionSequenceGenerator.generate_action_sequence(custom_AMR, sentence)
    acts_i = [a.index for a in actions]

    act = ActionConceptTransfer()
    act.load_from_action_objects(actions)
    actions_re = act.populate_new_actions(acts_i)

    tokens = tokenizer_util.text_to_sequence(sentence)

    print("Original actions:")
    for act in actions:
        print(act)
    print("Reconstructed actions:")
def test_apply_preprocessing_steps_on_instance_quantity_after_named_entitity():
    amr_str = """(p / pay-01~e.16 
      :ARG0 (p2 / person 
            :mod (s / star~e.2 
                  :mod (p3 / pop~e.1)) 
            :mod (d3 / debt~e.13 
                  :consist-of (m / monetary-quantity :quant 240000000~e.10,11 
                        :unit (d2 / dollar~e.9)) 
                  :ARG1-of (s2 / say-01~e.6))) 
      :ARG1 (m2 / monetary-quantity :quant 6~e.17 
            :unit (f / figure~e.18)) 
      :ARG2~e.26 (a / and~e.30 
            :op1 (d / doctor~e.29 
                  :mod (v / voodoo~e.28) 
                  :mod (a2 / another~e.27)) 
            :op2 (w / woman~e.34 :wiki - 
                  :name (n / name~e.35 :op1 "Samia"~e.36) 
                  :mod (m3 / mystery~e.32) 
                  :mod (c2 / country :wiki "Egypt" 
                        :name (n2 / name~e.35 :op1 "Egypt"~e.33)) 
                  :ARG1-of (c3 / come-01~e.39 
                        :ARG4~e.40 p2~e.41 
                        :accompanier~e.42 (l / letter~e.44 
                              :mod~e.45 (g / greet-01~e.46 
                                    :ARG0~e.47 (p7 / person 
                                          :ARG0-of (h2 / have-org-role-91 
                                                :ARG1 c6 
                                                :ARG2 (p4 / prince~e.53 
                                                      :ARG1-of (r2 / rank-01~e.51 
                                                            :ARG1-of (h / high-02~e.49)))) 
                                          :ARG0-of (p5 / purport-01 
                                                :ARG1 (p6 / person :wiki "Mohammed_bin_Nawwaf_bin_Abdulaziz" 
                                                      :name (n4 / name~e.35 :op1 "Nawaf"~e.56 :op2 "Bin"~e.57 :op3 "Abdulaziz"~e.58 :op4 "Al"~e.59 :op5 "Saud"~e.61) 
                                                      :ARG0-of (h3 / have-org-role-91~e.68 
                                                            :ARG1 (c6 / country :wiki "Saudi_Arabia" 
                                                                  :name (n5 / name~e.35 :op1 "Saudi"~e.69 :op2 "Arabia"~e.70)) 
                                                            :ARG2 (c5 / chief~e.65 
                                                                  :topic~e.66 (i / intelligence~e.67)) 
                                                            :time (n6 / now~e.63)))))))))) 
      :ARG3~e.19 (c / cleanse-01~e.22 
            :manner (r / ritual~e.21) 
            :ARG0-of (u / use-01~e.23 
                  :ARG1 (b / blood~e.25 
                        :mod (s3 / sheep~e.24)))))"""
    sentence = """The pop star , who is said to be $ 240 million in debt , had paid six figures for a ritual 
    cleansing using sheep blood to another voodoo doctor and a mysterious Egyptian woman named Samia , who came to 
    him with a letter of greeting from a high - ranking Saudi prince , purportedly Nawaf Bin Abdulaziz Al - Saud , 
    now the chief of intelligence of Saudi Arabia ... """
    amr = AMR.parse_string(amr_str)
    print("Before preprocessing ")
    print(sentence)
    print(amr)
    new_amr, sentence, _ = apply_preprocessing_steps_on_instance(
        amr, sentence, [NamedEntitiesPreprocessingStep()])
    print("After NE preprocessing ")
    print(sentence)
    print(new_amr)
    # new_amr2, sentence, _ = apply_preprocessing_steps_on_instance(new_amr, sentence,
    #                                                               [
    #                                                                   QuantitiesPreprocessingStep()
    #                                                               ])
    # print("After Q preprocessing ")
    # print(sentence)
    # print(new_amr2)
    custom_amr = CustomizedAMR()
    # custom_amr.create_custom_AMR(new_amr_list[0][1])
    custom_amr.create_custom_AMR(new_amr)