def test_build_edge_dict(file_r_name, file_weights_name, path_w): # default with open(file_r_name, 'r') as file_json, \ open(path_w + 'edges.json', 'w') as file_w: edge_dict = lem.build_edge_dict(file_json) edges = {' - '.join(edge): count for edge, count in edge_dict.items()} pretty_json(edges, file_w) # different options path_w += 'options/' if not os.path.exists(path_w): os.makedirs(path_w) weights_options = (None, True) bastard_options = (True, False) non_cyrillic_options = (True, False) for weights in weights_options: for bastard in bastard_options: for non_cyrillic in non_cyrillic_options: with open(file_r_name, 'r') as file_json: if weights: file_weights = open(file_weights_name, 'r') else: file_weights = None file_w_name = '{0}edges (' \ 'weights={1}, ' \ 'bastard={2}, ' \ 'non_cyrillic={3}' \ ').json'.format(path_w, weights, bastard, non_cyrillic) file_w = open(file_w_name, 'w') edge_dict = lem.build_edge_dict(file_json, file_weights, bastard, non_cyrillic) edges = {' - '.join(edge): count for edge, count in edge_dict.items()} pretty_json(edges, file_w) print('Done test_build_edge_dict')
def test_write_edge_dict(file_r_name, path_w): # build default edge_dict with open(file_r_name, 'r') as file_json: edge_dict = lem.build_edge_dict(file_json) # default with open(path_w + 'edges.csv', 'w') as file_w: lem.write_edge_dict(file_w, edge_dict) # different options path_w += 'options/' if not os.path.exists(path_w): os.makedirs(path_w) # with cut with open(path_w + 'edges (cut=10).csv', 'w') as file_w: lem.write_edge_dict(file_w, edge_dict, cut=10) # with '\t' with open(path_w + 'edges.tsv', 'w') as file_w: lem.write_edge_dict(file_w, edge_dict, sep='\t') print('Done test_write_edge_dict')
node_dict = lem.build_node_dict(file_json, weights=file_weight, include_bastard=bastard, include_non_cyrillic=non_cyrillic) # записываем узлы в файл для Gephi lem.write_node_dict(file_nodes, node_dict) # отдельно сохраняем json-версию словаря узлов (для отладки) pretty_json(node_dict, file_nodes_json) # Создаём рёбра with open(file_json_name, 'r') as file_json, \ open(file_weight_name, 'r') as file_weight, \ open(file_edges_name, 'w') as file_edges, \ open(file_edges_json_name, 'w') as file_edges_json: # создаём словарь рёбер edge_dict = lem.build_edge_dict(file_json, weights=file_weight, include_bastard=bastard, include_non_cyrillic=non_cyrillic) # записываем рёбра в файл для Gephi lem.write_edge_dict(file_edges, edge_dict) # отдельно сохраняем json-версию словаря рёбер (для отладки) edges = {' - '.join(edge): count for edge, count in edge_dict.items()} pretty_json(edges, file_edges_json) print('Done Part2.1') ### Часть 2.2. # Чтобы получить узлы и рёбра без учёта веса запросов (то есть по # формулировкам), просто не будем передавать файл с весом в функции # построения узлов и рёбер. path_w_form = path_w + 'По формулировкам/' if not os.path.exists(path_w_form):