correction for subcorpus and more
[iramuteq] / graph_to_json.py
1
2
3 import codecs
4 import os
5 import json
6 from random import randint
7
8
9 #pathout = '/home/pierre/fac/lerass/charlie/charliehebdo-0107-0112-corpus_fr_com_corpus_3/charlie_0712_religion_1/charliehebdo-0107-0112-corpus_fr_com_simitxt_1'
10 #edgesfile = os.path.join(pathout, 'edges.csv')
11 #nodesfile = os.path.join(pathout,'nodes.csv')
12 #jsonout = os.path.join(pathout,'network.json')
13
14 class GraphToJson :
15     def __init__(self, nodesfile, edgesfile, jsonout, parametres = {}):
16
17         with codecs.open(edgesfile, 'r', 'utf8') as f :
18             content = f.read()
19         content = content.replace('"', '')
20         content = content.splitlines()
21         try :
22             titles_edges = content.pop(0)
23             titles_edges = titles_edges.split('\t')
24             edges = [line.split('\t') for line in content]
25         except :
26             edges = None
27             
28         with codecs.open(nodesfile, 'r', 'utf8') as f :
29             content = f.read()
30         content = content.replace('"','')
31         content = content.splitlines()
32         titles = content.pop(0)
33         titles = titles.split('\t')
34         #titles.insert(0,'')
35
36         xr = titles.index('x')
37         yr = titles.index('y')
38         try :
39             zr = titles.index('z')
40         except :
41             zr = None
42         wr = titles.index('weight')
43         try :
44             r = titles.index('r')
45             g = titles.index('g')
46             b = titles.index('b')
47         except :
48             r = None
49         ni = titles.index('name')
50
51         nodes = [line.split('\t') for line in content]
52
53         graph = {'edges': [], 'nodes' : {}}
54         
55         we = titles_edges.index('weight')
56         if edges is not None :
57             for edge in edges :
58                 graph['edges'].append({'source' : edge[0], 'target' : edge[1], 'weight' : edge[we]})
59         
60         
61         coefcoord = parametres.get('coefcoord', 1)
62         coefweight = parametres.get('coefweight', 1)
63         
64         
65         for node in nodes :
66             if zr is not None :
67                 graph['nodes'][node[ni]] = {"location" : [float(node[xr])*coefcoord, float(node[yr])*coefcoord, float(node[zr])*coefcoord], 'weight' : float(node[wr])/coefweight, 'color': (int(node[r]),int(node[g]),int(node[b]))}
68             else :
69                 x = parametres.get('randomx', 0)
70                 if x :
71                     x = randint(-150,150)
72                 graph['nodes'][node[ni]] = {"location" : [ x, float(node[xr]), float(node[yr])], 'weight' : float(node[wr]), 'color': (int(node[r]),int(node[g]),int(node[b]))}
73
74         with open(jsonout, 'w') as f :
75             json.dump(graph, f)
76
77 if __name__ == '__main__' :
78     GraphToJson('/home/pierre/workspace/iramuteq/dev/blender-graphs/nodes.csv', '/home/pierre/workspace/iramuteq/dev/blender-graphs/edges.csv', '/home/pierre/workspace/iramuteq/dev/blender-graphs/L2_L2.json', {})