12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import suggests
- import networkx as nx
- import pyvis
- import time
- from pyvis.network import Network
- import pickle
- import dataset
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
- #cursor=db.query('select q,cnt from gap_hhh where cnt > 100')
- #cursor=db.query('select q,cnt from gap_searchome where cnt > 50')
- cursor=db.query('SELECT q,cnt FROM hhh.gap_searchome where q not in (select q from hhh.gap_hhh) order by cnt desc;')
- def hierarchy_pos(G, root, levels=None, width=1., height=1.):
- '''If there is a cycle that is reachable from root, then this will see infinite recursion.
- G: the graph
- root: the root node
- levels: a dictionary
- key: level number (starting from 0)
- value: number of nodes in this level
- width: horizontal space allocated for drawing
- height: vertical space allocated for drawing'''
- TOTAL = "total"
- CURRENT = "current"
- def make_levels(levels, node=root, currentLevel=0, parent=None):
- """Compute the number of nodes for each level
- """
- if not currentLevel in levels:
- levels[currentLevel] = {TOTAL : 0, CURRENT : 0}
- levels[currentLevel][TOTAL] += 1
- neighbors = G.neighbors(node)
- for neighbor in neighbors:
- if not neighbor == parent:
- levels = make_levels(levels, neighbor, currentLevel + 1, node)
- return levels
- def make_pos(pos, node=root, currentLevel=0, parent=None, vert_loc=0):
- dx = 1/levels[currentLevel][TOTAL]
- left = dx/2
- pos[node] = ((left + dx*levels[currentLevel][CURRENT])*width, vert_loc)
- levels[currentLevel][CURRENT] += 1
- neighbors = G.neighbors(node)
- for neighbor in neighbors:
- if not neighbor == parent:
- pos = make_pos(pos, neighbor, currentLevel + 1, node, vert_loc-vert_gap)
- return pos
- if levels is None:
- levels = make_levels({})
- else:
- levels = {l:{TOTAL: levels[l], CURRENT:0} for l in levels}
- vert_gap = height / (max([l for l in levels])+1)
- return make_pos({})
- G = nx.Graph()
- elmt_dict={}
- root=None
- for c in cursor:
- q=c['q']
- elmts=q.split(' ')
- for e in elmts:
- if elmt_dict.get(e) is None:
- elmt_dict[e]=[q]
- else:
- elmt_dict[e].append(q)
- print(elmt_dict)
- idx=0
- for k,v in elmt_dict.items():
- if len(v)>2 and len(v)<20:
- for e in v:
- G.add_edge(k,e,weight=1)
- root=k
- idx+=1
- G.remove_edges_from( list(nx.selfloop_edges(G)))
- G2=nx.minimum_spanning_tree(G,weight=5)
- #G3=hierarchy_pos(G2,root)
- pyG = Network(height="750px", width="100%",bgcolor="#333333",font_color="white")
- #pyG.set_options()
- pyG.from_nx(G2)
- #pyG.enable_physics(False)
- #pyG.barnes_hut()
- pyG.show('gs.html')
- print(idx)
|