From b1370b2ee28d638d8c2cc17781431bf60dfc4bfc Mon Sep 17 00:00:00 2001 From: Luka Date: Tue, 3 Dec 2019 18:14:43 +0100 Subject: [PATCH] Added multiple attribute results --- Result.py | 12 +++++++++--- Tree.py | 8 ++++---- dependency-parsetree.py | 40 ++++++++++++++++++++++------------------ 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/Result.py b/Result.py index ee3c788..4c410be 100644 --- a/Result.py +++ b/Result.py @@ -2,10 +2,16 @@ import copy class Result(object): - def __init__(self, string, architecture_order): - self.key = string + def __init__(self, node, architecture_order, create_output_strings): + self.array = [[create_output_string(node) for create_output_string in create_output_strings]] + if len(self.array[0]) > 1: + self.key = '{' + ','.join(self.array[0]) + '}' + else: + # output_string = create_output_strings[0](node) + self.key = self.array[0][0] + # self.array = [[output_string]] self.order_key = str([architecture_order]) - self.array = [string] + # order with original numbers in sentences # self.order = str([architecture_order]) # order with numbers from 0 to n of n-gram diff --git a/Tree.py b/Tree.py index 80d1311..a10f218 100644 --- a/Tree.py +++ b/Tree.py @@ -324,11 +324,11 @@ class Tree(object): # self.create_output_children(partial_subtrees_architecture[i_answer], [str([self.index])], filters, partial_subtrees_index[i_answer], partial_subtrees_deprel[i_answer])) merged_partial_subtrees.append( - self.create_output_children(partial_subtrees[i_answer], [Result(create_output_string(self), self.index)], filters, partial_subtrees_index[i_answer], partial_subtrees_deprel[i_answer])) + self.create_output_children(partial_subtrees[i_answer], [Result(self, self.index, create_output_string)], filters, partial_subtrees_index[i_answer], partial_subtrees_deprel[i_answer])) i_answer += 1 else: - merged_partial_subtrees.append([Result(create_output_string(self), self.index)]) + merged_partial_subtrees.append([Result(self, self.index, create_output_string)]) # merged_partial_subtrees.append([create_output_string(self)]) # merged_partial_subtrees_architecture.append([str([self.index])]) @@ -341,11 +341,11 @@ class Tree(object): # self.create_output_children(partial_subtrees_architecture[i_answer], [str([self.index])], filters, partial_subtrees_index[i_answer], partial_subtrees_deprel[i_answer])) merged_partial_subtrees.append( - self.create_output_children(partial_subtrees[i_answer], [Result(create_output_string(self), self.index)], filters, partial_subtrees_index[i_answer], partial_subtrees_deprel[i_answer])) + self.create_output_children(partial_subtrees[i_answer], [Result(self, self.index, create_output_string)], filters, partial_subtrees_index[i_answer], partial_subtrees_deprel[i_answer])) i_answer += 1 else: - merged_partial_subtrees.append([Result(create_output_string(self), self.index)]) + merged_partial_subtrees.append([Result(self, self.index, create_output_string)]) # merged_partial_subtrees.append([create_output_string(self)]) # merged_partial_subtrees_architecture.append([str([self.index])]) diff --git a/dependency-parsetree.py b/dependency-parsetree.py index 45f0a88..09c878f 100644 --- a/dependency-parsetree.py +++ b/dependency-parsetree.py @@ -384,20 +384,24 @@ def main(): # set filters - assert config.get('settings', 'node_type') in ['deprel', 'lemma', 'upos', 'xpos', 'form', 'feats'], '"node_type" is not set up correctly' - cpu_cores = config.getint('settings', 'cpu_cores') - if config.get('settings', 'node_type') == 'deprel': - create_output_string_funct = create_output_string_deprel - elif config.get('settings', 'node_type') == 'lemma': - create_output_string_funct = create_output_string_lemma - elif config.get('settings', 'node_type') == 'upos': - create_output_string_funct = create_output_string_upos - elif config.get('settings', 'node_type') == 'xpos': - create_output_string_funct = create_output_string_xpos - elif config.get('settings', 'node_type') == 'feats': - create_output_string_funct = create_output_string_feats - else: - create_output_string_funct = create_output_string_form + node_types = config.get('settings', 'node_type').split('+') + create_output_string_functs = [] + for node_type in node_types: + assert node_type in ['deprel', 'lemma', 'upos', 'xpos', 'form', 'feats'], '"node_type" is not set up correctly' + cpu_cores = config.getint('settings', 'cpu_cores') + if node_type == 'deprel': + create_output_string_funct = create_output_string_deprel + elif node_type == 'lemma': + create_output_string_funct = create_output_string_lemma + elif node_type == 'upos': + create_output_string_funct = create_output_string_upos + elif node_type == 'xpos': + create_output_string_funct = create_output_string_xpos + elif node_type == 'feats': + create_output_string_funct = create_output_string_feats + else: + create_output_string_funct = create_output_string_form + create_output_string_functs.append(create_output_string_funct) result_dict = {} filters = {} @@ -447,7 +451,7 @@ def main(): # 1.02 s (16 cores) if cpu_cores > 1: - all_subtrees = p.map(tree_calculations, [(tree, query_tree, create_output_string_funct, filters) for tree in all_trees]) + all_subtrees = p.map(tree_calculations, [(tree, query_tree, create_output_string_functs, filters) for tree in all_trees]) # for subtrees in all_subtrees: for tree_i, subtrees in enumerate(all_subtrees): @@ -471,7 +475,7 @@ def main(): # text = Če pa ostane odrasel otrok doma, se starši le težko sprijaznijo s tem, da je "velik", otrok pa ima ves čas občutek, da se njegovi starši po nepotrebnem vtikajo v njegovo življenje. # for tree_i, tree in enumerate(all_trees[5170:]): # for tree in all_trees: - subtrees = tree_calculations((tree, query_tree, create_output_string_funct, filters)) + subtrees = tree_calculations((tree, query_tree, create_output_string_functs, filters)) for query_results in subtrees: for r in query_results: # if r == '(" < , < je < velik) < tem': @@ -512,7 +516,7 @@ def main(): len_words = tree_size_range[-1] else: len_words = int(len(config.get('settings', 'query').split(" "))/2 + 1) - header = ["Structure"] + ["Word #" + str(i) for i in range(1, len_words + 1)] + ['Number of occurences'] + header = ["Structure"] + ["Word #" + str(i) + "-" + node_type for i in range(1, len_words + 1) for node_type in node_types] + ['Number of occurences'] if config.get('settings', 'relative_number'): header += ['Relative frequency'] if config.get('settings', 'nodes_number'): @@ -522,7 +526,7 @@ def main(): # body for k, v in sorted_list: - words_only = v['object'].array + ['' for i in range(tree_size_range[-1] - len(v['object'].array))] + words_only = [word_att for word in v['object'].array for word_att in word] + ['' for i in range((tree_size_range[-1] - len(v['object'].array)) * len(v['object'].array[0]))] # words_only = printable_answers(k) row = [k] + words_only + [str(v['number'])] if config.get('settings', 'relative_number'):