def get_svg_file_url(node_id, isMap): c = Centrality() node_path = c.create_svg_url(node_id, isMap) try: file = urlopen(node_path) svg = file.read() except (IOError): print('File was not found:') print(node_path) return svg
def get_svg_file(node_id): c = Centrality() node_path = c.get_svg_path(node_id) try: with application.open_resource(node_path) as file: svg = file.read() except (IOError): print('File was not found:') print(node_path) return svg
def get_loc_sim_matrix(graph, graph1): centra = Centrality() aifsim = Aifsim() g_copy = graph.copy() g1_copy = graph1.copy() g_lnodes = centra.get_l_node_list(g_copy) g1_lnodes = centra.get_l_node_list(g1_copy) relsl, valsl, switched = aifsim.text_sim_matrix(g_lnodes, g1_lnodes) rels_dict = aifsim.rels_to_dict(relsl, switched) return rels_dict
def get_prop_sim_matrix(graph, graph1): centra = Centrality() aifsim = Aifsim() g_copy = graph.copy() g1_copy = graph1.copy() g_inodes = centra.get_i_node_list(g_copy) g1_inodes = centra.get_i_node_list(g1_copy) relsi, valsi, switched = aifsim.text_sim_matrix(g_inodes, g1_inodes) #if switched the relations have been switched order so they need reversed when creating the dictionary rels_dict = aifsim.rels_to_dict(relsi, switched) return rels_dict
def get_ordered_nodes(node_id): centra = Centrality() node_path = centra.get_nodeset_path(node_id) graph = centra.get_graph(node_path) n_graph = centra.remove_redundant_nodes(graph) i_nodes = centra.get_eigen_centrality(n_graph) ordered_nodes = centra.sort_by_centrality(i_nodes) return ordered_nodes
def get_graph_sim(aif_id1, aif_id2): centra = Centrality() aifsim = Aifsim() graph, json = aifsim.get_graph(aif_id1, centra) graph1, json1 = aifsim.get_graph(aif_id2, centra) graph = centra.remove_iso_analyst_nodes(graph) graph1 = centra.remove_iso_analyst_nodes(graph1) rep_form = aifsim.is_iat(graph, graph1, centra) g_copy = graph.copy() g1_copy = graph1.copy() graph_mean = 0 text_mean = 0 overall_mean = 0 if rep_form == 'diff': return 'Error' else: graph_mean = aifsim.call_diagram_parts_and_sum( g_copy, g1_copy, rep_form) if rep_form == 'aif': g_inodes = centra.get_i_node_list(g_copy) g1_inodes = centra.get_i_node_list(g1_copy) relsi, valsi, switched = aifsim.text_sim_matrix( g_inodes, g1_inodes) i_mean = aifsim.get_mean_of_list(valsi) text_mean = i_mean else: g_inodes = centra.get_i_node_list(g_copy) g1_inodes = centra.get_i_node_list(g1_copy) g_lnodes = centra.get_l_node_list(g_copy) g1_lnodes = centra.get_l_node_list(g1_copy) relsi, valsi, switched = aifsim.text_sim_matrix( g_inodes, g1_inodes) relsl, valsl, switched = aifsim.text_sim_matrix( g_lnodes, g1_lnodes) i_mean = aifsim.get_mean_of_list(valsi) l_mean = aifsim.get_mean_of_list(valsl) text_mean = aifsim.get_l_i_mean(l_mean, i_mean) overall_score = aifsim.get_l_i_mean(text_mean, graph_mean) return overall_score, text_mean, graph_mean
def get_arg_schemes(nodeset): cent = Centrality() j_url = cent.create_json_url(str(nodeset), True) graph = cent.get_graph_url(j_url) json_data = get_json_string(j_url) ras = cent.get_ras(graph) ras_i_list = cent.get_ra_i_nodes(graph, ras) ra_changes = [] for ns in ras_i_list: ra_id = ns[0] s_id = ns[1] e_id = ns[2] schemes = identifyScheme(e_id, s_id) if len(schemes) < 1: continue else: ra_tup = (ra_id, schemes[0]) ra_changes.append(ra_tup) #get json string and replace text at ID then upload print(ra_changes) if len(ra_changes) < 1: return '' else: n_json_data = replace_node(json_data, ra_changes) url_aif = 'http://www.aifdb.org/json/' jsn_data = json.dumps(n_json_data) map_response = aif_upload(url_aif, jsn_data) map_data = json.loads(map_response) fin_map_id = map_data['nodeSetID'] return fin_map_id
def ca_anchor(graph1, graph2): conf_matrix = [[0, 0],[0, 0]] aifsim = Aifsim() cent = Centrality() cas1 = cent.get_cas(graph1) cas2 = cent.get_cas(graph2) ca1_len = len(cas1) ca2_len = len(cas2) if ca1_len > 0 and ca2_len > 0: if ca1_len > ca2_len: for ca_i, ca in enumerate(cas1): cas2_id = '' yas1 = aifsim.get_ya_nodes_from_prop(ca, graph1) try: cas2_id = cas2[ca_i] except: cas2_id = '' if cas2_id == '': #conf_matrix[index][len(all_ya_text) + 1] = conf_matrix[index][len(all_ya_text) + 1] + 1 conf_matrix[1][0] = conf_matrix[1][0] + 1 else: yas2 = aifsim.get_ya_nodes(cas2_id, graph2) if yas1 == yas2: conf_matrix[0][0] = conf_matrix[0][0] + 1 else: conf_matrix[1][0] = conf_matrix[1][0] + 1 elif ca2_len > ca1_len: for ca_i, ca in enumerate(cas2): cas1_id = '' yas2 = aifsim.get_ya_nodes_from_prop(ca, graph2) try: cas1_id = cas1[ca_i] except: cas1_id = '' if cas1_id == '': #conf_matrix[index][len(all_ya_text) + 1] = conf_matrix[index][len(all_ya_text) + 1] + 1 conf_matrix[0][1] = conf_matrix[0][1] + 1 else: yas1 = aifsim.get_ya_nodes(cas1_id, graph1) if yas1 == yas2: conf_matrix[0][0] = conf_matrix[0][0] + 1 else: conf_matrix[0][1] = conf_matrix[0][1] + 1 else: for ca_i, ca in enumerate(cas1): ya1 = aifsim.get_ya_nodes_from_prop(ca, graph1) ya2 = aifsim.get_ya_nodes_from_prop(cas2[ca_i], graph2) if ya1 == ya2: conf_matrix[0][0] = conf_matrix[0][0] + 1 else: conf_matrix[1][0] = conf_matrix[1][0] + 1 elif ca1_len == 0 and ca2_len == 0: conf_matrix[1][1] = conf_matrix[1][1] + 1 elif ca1_len == 0: conf_matrix[0][1] = conf_matrix[0][1] + ca2_len elif ca2_len == 0: conf_matrix[1][0] = conf_matrix[1][0] + ca1_len return conf_matrix
def ra_anchor(graph1, graph2): conf_matrix = [[0, 0],[0, 0]] aifsim = Aifsim() cent = Centrality() ras1 = cent.get_ras(graph1) ras2 = cent.get_ras(graph2) ra1_len = len(ras1) ra2_len = len(ras2) if ra1_len > 0 and ra2_len > 0: if ra1_len > ra2_len: for ra_i, ra in enumerate(ras1): ras2_id = '' yas1 = aifsim.get_ya_nodes_from_prop(ra, graph1) try: ras2_id = ras2[ra_i] except: ras2_id = '' if ras2_id == '': #conf_matrix[index][len(all_ya_text) + 1] = conf_matrix[index][len(all_ya_text) + 1] + 1 conf_matrix[1][0] = conf_matrix[1][0] + 1 else: yas2 = get_ya_nodes(ras2_id, graph2) if yas1 == yas2: conf_matrix[0][0] = conf_matrix[0][0] + 1 else: conf_matrix[1][0] = conf_matrix[1][0] + 1 elif ra2_len > ra1_len: for ra_i, ra in enumerate(ras2): ras1_id = '' yas2 = aifsim.get_ya_nodes_from_prop(ra, graph2) try: ras1_id = ras1[ra_i] except: ras1_id = '' if ras1_id == '': #conf_matrix[index][len(all_ya_text) + 1] = conf_matrix[index][len(all_ya_text) + 1] + 1 conf_matrix[0][1] = conf_matrix[0][1] + 1 else: yas1 = aifsim.get_ya_nodes(ras1_id, graph1) if yas1 == yas2: conf_matrix[0][0] = conf_matrix[0][0] + 1 else: conf_matrix[0][1] = conf_matrix[0][1] + 1 else: for ra_i, ra in enumerate(ras1): ya1 = aifsim.get_ya_nodes_from_prop(ra, graph1) ya2 = aifsim.get_ya_nodes_from_prop(ras2[ra_i], graph2) if ya1 == ya2: conf_matrix[0][0] = conf_matrix[0][0] + 1 else: conf_matrix[1][0] = conf_matrix[1][0] + 1 elif ra1_len == 0 and ra2_len == 0: conf_matrix[1][1] = conf_matrix[1][1] + 1 elif ra1_len == 0: conf_matrix[0][1] = conf_matrix[0][1] + ra2_len elif ra2_len == 0: conf_matrix[1][0] = conf_matrix[1][0] + ra1_len return conf_matrix
def get_svg_file_path(node_id): c = Centrality() node_path = c.get_svg_path(node_id) return node_path
def render_text(): source_text = session.get('s_text', None) external_text = session.get('e_text', None) aif_mode = session.get('aif', None) han_mode = session.get('han', None) ex_aif_mode = session.get('e_aif', None) source_date = session.get('s_date', None) centra = Centrality() s_map_numbers = [] ex_map_numbers = [] ma_thresh = 0.85 ra_thresh = 0.55 s_l_i_nodes = [] ex_l_i_nodes = [] s_l_nodes = [] ex_l_nodes = [] h_l_i_nodes = [] h_l_nodes = [] if aif_mode == "true" and han_mode == "true" and ex_aif_mode == "false": # Source Map and Hansard sources = source_text.split(',') s_map_numbers = [int(i) for i in sources] central_nodes, s_l_i_nodes, s_l_nodes = centra.get_top_nodes_combined( s_map_numbers) source_topic_text = get_topic_text(central_nodes) txt_df = sent_to_df(source_topic_text) result = predict_topic(txt_df) hansard_fp = get_hansard_file_path(source_date, result, 'HansardDataAMF') hansard_map_num = check_hansard_path(hansard_fp) if hansard_map_num[0] == '': hansard_text = get_hansard_text(hansard_fp) hansard_text = hansard_text.decode("utf-8") ex_map_numbers = do_amf_calls(hansard_text, True) write_to_csv(ex_map_numbers, hansard_fp) else: ex_map_numbers = hansard_map_num ex_map_numbers = ast.literal_eval(ex_map_numbers) h_i_nodes, h_l_i_nodes, h_l_nodes = centra.get_top_nodes_combined( ex_map_numbers) relations = itc_matrix(central_nodes, h_i_nodes, ma_thresh, ra_thresh) if len(relations) > 0: #Build itc map map_id = build_itc_map(relations, s_l_i_nodes, h_l_i_nodes, s_l_nodes, h_l_nodes) elif aif_mode == "true" and han_mode == "false" and ex_aif_mode == "true": # Source Map and External Maps sources = source_text.split(',') s_map_numbers = [int(i) for i in sources] external = external_text.split(',') ex_map_numbers = [int(i) for i in external] central_nodes, s_l_i_nodes, s_l_nodes = centra.get_top_nodes_combined( s_map_numbers) ex_i_nodes, ex_l_i_nodes, ex_l_nodes = centra.get_all_nodes_combined( ex_map_numbers) relations = itc_matrix(central_nodes, ex_i_nodes, ma_thresh, ra_thresh) if len(relations) > 0: #Build itc map map_id = build_itc_map(relations, s_l_i_nodes, ex_l_i_nodes, s_l_nodes, ex_l_nodes) elif aif_mode == "false" and han_mode == "true" and ex_aif_mode == "false": # Source Text and Hansard s_map_numbers = do_amf_calls(source_text, False) central_nodes, s_l_i_nodes, s_l_nodes = centra.get_top_nodes_combined( s_map_numbers) source_topic_text = get_topic_text(central_nodes) txt_df = sent_to_df(source_topic_text) result = predict_topic(txt_df) hansard_fp = get_hansard_file_path(source_date, result, 'HansardDataAMF') hansard_map_num = check_hansard_path(hansard_fp) if hansard_map_num[0] == '': hansard_text = get_hansard_text(hansard_fp) hansard_text = hansard_text.decode("utf-8") ex_map_numbers = do_amf_calls(hansard_text, True) write_to_csv(ex_map_numbers, hansard_fp) else: ex_map_numbers = hansard_map_num ex_map_numbers = ast.literal_eval(ex_map_numbers) h_i_nodes, h_l_i_nodes, h_l_nodes = centra.get_top_nodes_combined( ex_map_numbers) #print(central_nodes, h_i_nodes) relations = itc_matrix(central_nodes, h_i_nodes, ma_thresh, ra_thresh) if len(relations) > 0: #Build itc map map_id = build_itc_map(relations, s_l_i_nodes, h_l_i_nodes, s_l_nodes, h_l_nodes) elif aif_mode == "false" and han_mode == "false" and ex_aif_mode == "false": # Source Text and External Text s_map_numbers = do_amf_calls(source_text, False) central_nodes, s_l_i_nodes, s_l_nodes = centra.get_top_nodes_combined( s_map_numbers) ex_map_numbers = do_amf_calls(external_text, False) ex_i_nodes, ex_l_i_nodes, ex_l_nodes = centra.get_all_nodes_combined( ex_map_numbers) relations = itc_matrix(central_nodes, ex_i_nodes, ma_thresh, ra_thresh) if len(relations) > 0: #Build itc map map_id = build_itc_map(relations, s_l_i_nodes, ex_l_i_nodes, s_l_nodes, ex_l_nodes) elif aif_mode == "true" and han_mode == "false" and ex_aif_mode == "false": # Source Text and External Text sources = source_text.split(',') s_map_numbers = [int(i) for i in sources] central_nodes, s_l_i_nodes, s_l_nodes = centra.get_top_nodes_combined( s_map_numbers) ex_map_numbers = do_amf_calls(external_text, False) ex_i_nodes, ex_l_i_nodes, ex_l_nodes = centra.get_all_nodes_combined( ex_map_numbers) relations = itc_matrix(central_nodes, ex_i_nodes, ma_thresh, ra_thresh) if len(relations) > 0: #Build itc map map_id = build_itc_map(relations, s_l_i_nodes, ex_l_i_nodes, s_l_nodes, ex_l_nodes) elif aif_mode == "false" and han_mode == "false" and ex_aif_mode == "true": # Source Text and External Map s_map_numbers = do_amf_calls(source_text, False) central_nodes, s_l_i_nodes, s_l_nodes = centra.get_top_nodes_combined( s_map_numbers) external = external_text.split(',') ex_map_numbers = [int(i) for i in external] ex_i_nodes, ex_l_i_nodes, ex_l_nodes = centra.get_all_nodes_combined( ex_map_numbers) relations = itc_matrix(central_nodes, ex_i_nodes, ma_thresh, ra_thresh) if len(relations) > 0: #Build itc map map_id = build_itc_map(relations, s_l_i_nodes, ex_l_i_nodes, s_l_nodes, ex_l_nodes) new_map_numbers = get_new_map_nums(s_map_numbers) if len(relations) > 0: itc_map_number = get_new_itc_map(map_id) itc_map_list = [itc_map_number] itc_map_view_list = create_argview_links(itc_map_list) itc_number = str(itc_map_view_list[0]) itc_relations = [(rels[1], rels[3], rels[4]) for rels in relations] else: itc_number = 'No ITC relations found' itc_relations = ['No ITC relations found'] source_map_numbers_links = create_argview_links(new_map_numbers) ex_map_number_links = create_argview_links(ex_map_numbers) return render_template('results.html', source_text=source_text, source_maps_links=source_map_numbers_links, ex_map_links=ex_map_number_links, itc_number=itc_number, itc_relations=itc_relations)
def get_ordered_nodes(node_id, isMap): centra = Centrality() #node_path = centra.get_nodeset_path(node_id) #Add extension for L-nodes here node_path = centra.create_json_url(node_id, isMap) graph = centra.get_graph_url(node_path) graph = centra.remove_iso_nodes(graph) l_nodes = centra.get_l_node_list(graph) l_node_i_node = centra.get_loc_prop_pair(graph) n_graph = centra.remove_redundant_nodes(graph) list_of_nodes = centra.list_nodes(graph) divergent_nodes = centra.get_divergent_nodes(n_graph) s_nodes = centra.get_s_node_list(n_graph) i_nodes = centra.get_eigen_centrality(n_graph) ordered_nodes = centra.sort_by_centrality(i_nodes) children, edges = centra.get_child_edges(n_graph) return ordered_nodes, list_of_nodes, divergent_nodes, children, edges, s_nodes, l_nodes, l_node_i_node