def test_build_node_dict(file_r_name, file_weights_name, path_w): # default with open(file_r_name, 'r') as file_json, \ open(path_w + 'nodes.json', 'w') as file_w: node_dict = lem.build_node_dict(file_json) pretty_json(node_dict, 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}nodes (' \ 'weights={1}, ' \ 'bastard={2}, ' \ 'non_cyrillic={3}' \ ').json'.format(path_w, weights, bastard, non_cyrillic) file_w = open(file_w_name, 'w') node_dict = lem.build_node_dict(file_json, file_weights, bastard, non_cyrillic) pretty_json(node_dict, file_w) print('Done test_build_node_dict')
def test_write_node_dict(file_r_name, path_w): # build default node_dict with open(file_r_name, 'r') as file_json: node_dict = lem.build_node_dict(file_json) # default with open(path_w + 'nodes.csv', 'w') as file_w: lem.write_node_dict(file_w, node_dict) # different options path_w += 'options/' if not os.path.exists(path_w): os.makedirs(path_w) # with cut with open(path_w + 'nodes (cut=10).csv', 'w') as file_w: lem.write_node_dict(file_w, node_dict, cut=10) # with '\t' with open(path_w + 'nodes.tsv', 'w') as file_w: lem.write_node_dict(file_w, node_dict, sep='\t') print('Done test_write_node_dict')
# рассмотренные ранее. Если не указать файл с весами, то каждой строке будет # присвоен единичный вес. file_nodes_name = path_w + 'nodes.csv' file_edges_name = path_w + 'edges.csv' file_nodes_json_name = path_w + 'nodes.json' file_edges_json_name = path_w + 'edges.json' # Создаём узлы with open(file_json_name, 'r') as file_json, \ open(file_weight_name, 'r') as file_weight, \ open(file_nodes_name, 'w') as file_nodes, \ open(file_nodes_json_name, 'w') as file_nodes_json: # создаём словарь узлов 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)