Commit 7d77ae22 authored by Administrator's avatar Administrator

[FEATURE] Node size = sum of cooccurrences for each node.

parent 4e7b08ec
...@@ -487,6 +487,8 @@ def node_link(request, corpus_id): ...@@ -487,6 +487,8 @@ def node_link(request, corpus_id):
matrix = defaultdict(lambda : defaultdict(float)) matrix = defaultdict(lambda : defaultdict(float))
labels = dict() labels = dict()
weight = dict()
corpus = Node.objects.get(id=corpus_id) corpus = Node.objects.get(id=corpus_id)
type_cooc = NodeType.objects.get(name="Cooccurrence") type_cooc = NodeType.objects.get(name="Cooccurrence")
...@@ -505,6 +507,11 @@ def node_link(request, corpus_id): ...@@ -505,6 +507,11 @@ def node_link(request, corpus_id):
matrix[cooccurrence.ngramx.id][cooccurrence.ngramy.id] = cooccurrence.score matrix[cooccurrence.ngramx.id][cooccurrence.ngramy.id] = cooccurrence.score
matrix[cooccurrence.ngramy.id][cooccurrence.ngramx.id] = cooccurrence.score matrix[cooccurrence.ngramy.id][cooccurrence.ngramx.id] = cooccurrence.score
weight[cooccurrence.ngramy.terms] = weight.get(cooccurrence.ngramy.terms, 0) + cooccurrence.score
weight[cooccurrence.ngramx.terms] = weight.get(cooccurrence.ngramx.terms, 0) + cooccurrence.score
df = pd.DataFrame(matrix).T.fillna(0) df = pd.DataFrame(matrix).T.fillna(0)
x = copy(df.values) x = copy(df.values)
x = x / x.sum(axis=1) x = x / x.sum(axis=1)
...@@ -515,7 +522,7 @@ def node_link(request, corpus_id): ...@@ -515,7 +522,7 @@ def node_link(request, corpus_id):
#matrix_filtered = np.where(x > threshold, x, 0) #matrix_filtered = np.where(x > threshold, x, 0)
G = nx.from_numpy_matrix(matrix_filtered) G = nx.from_numpy_matrix(matrix_filtered)
G = nx.relabel_nodes(G, dict(enumerate([ labels[x] for x in list(df.columns)]))) G = nx.relabel_nodes(G, dict(enumerate([ labels[label] for label in list(df.columns)])))
#G = nx.relabel_nodes(G, dict(enumerate(df.columns))) #G = nx.relabel_nodes(G, dict(enumerate(df.columns)))
# Removing too connected nodes (find automatic way to do it) # Removing too connected nodes (find automatic way to do it)
...@@ -529,6 +536,7 @@ def node_link(request, corpus_id): ...@@ -529,6 +536,7 @@ def node_link(request, corpus_id):
try: try:
#node,type(labels[node]) #node,type(labels[node])
G.node[node]['label'] = node G.node[node]['label'] = node
G.node[node]['weight'] = weight[node]
# G.node[node]['color'] = '19,180,300' # G.node[node]['color'] = '19,180,300'
except Exception as error: except Exception as error:
print(error) print(error)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment