Commit 5302388b authored by Jim Hoekstra's avatar Jim Hoekstra 👋🏻
Browse files

Merge branch 'issue/MSX-22' into 'master'

Issue/msx 22

See merge request !5
parents 59ed3ca3 22c81c76
......@@ -10,4 +10,4 @@ todo
## How to run
To run the development server locally, execute `python scripts/run_debug_server.py`, and
in the browser navigate to http://127.0.0.1:8050/
in the browser navigate to http://127.0.0.1:8050/msx/
import os
import dash
import dash_core_components as dcc
import dash_html_components as html
......@@ -27,30 +28,63 @@ app.layout = html.Div(children=[
html.Br(),
html.Div(children=[
html.Div(className='row', children=[
html.Div(className='col-2', children=[
html.H2(children='Input Word:'),
html.Div(className='col-1', children=[
html.H2(children='Term:'),
]),
html.Div(className='col-7', children=[
html.Div(className='col-9', children=[
dcc.Input(id='base-word-input', value='fruit', type='text', className='form-control form-control-lg'),
]),
html.Div(className='col-3', children=[
html.Button(id='base-word-submit', n_clicks=0, children='Submit Word', className='btn btn-success btn-lg')]),
html.Div(className='col-2', children=[
html.Button(id='base-word-submit', n_clicks_timestamp=0, children='Submit Term', className='btn btn-success btn-lg')]),
])
]),
html.Br(),
cyto_graph
cyto_graph,
html.Div(children=[
html.Div(className='row', children=[
html.Div(className='col-3', children=[
dcc.Input(id='add-word-input', value='', type='text', className='form-control form-control-lg'),
]),
html.Div(className='col-2', children=[
html.Button(id='add-word-button', n_clicks_timestamp=0, children='Add Association', className='btn btn-success btn-lg'),
]),
html.Div(className='col-4', children=[
]),
html.Div(className='col-3', children=[
html.Button(id='remove-word-button', n_clicks_timestamp=0, children='Remove Selected Association', className='btn btn-danger btn-lg')]),
]),
]),
]),
])
server = app.server
server.config['SECRET_KEY'] = os.environ['MSX_SECRET_KEY']
@app.callback(
Output(component_id='msx-graph', component_property='elements'),
Input(component_id='base-word-submit', component_property='n_clicks'),
State(component_id='base-word-input', component_property='value')
Input(component_id='base-word-submit', component_property='n_clicks_timestamp'),
Input(component_id='add-word-button', component_property='n_clicks_timestamp'),
Input(component_id='remove-word-button', component_property='n_clicks_timestamp'),
State(component_id='msx-graph', component_property='tapNodeData'),
State(component_id='base-word-input', component_property='value'),
State(component_id='add-word-input', component_property='value')
)
def update_graph(n_clicks, base_word):
graph.fill_with_associations(base_word)
return graph.get_graph_elements()
def select_node(submit_button_ts, add_button_ts, remove_button_ts, graph_selected, base_word, add_word):
if remove_button_ts > submit_button_ts and remove_button_ts > add_button_ts:
if graph_selected is not None:
graph.remove_node(graph_selected['label'])
if submit_button_ts > add_button_ts and submit_button_ts > remove_button_ts:
if base_word is not None and base_word != '':
graph.fill_with_associations(base_word)
if add_button_ts > submit_button_ts and add_button_ts > remove_button_ts:
if add_word is not None and add_word != '':
graph.add_node(add_word)
graph.add_edge(graph.get_base_word(), add_word)
graph_elements = graph.get_graph_elements()
return graph_elements
......@@ -9,6 +9,9 @@ class Graph:
self.nodes = []
self.edges = []
def get_base_word(self):
return self.associated_words.get_base_word()
def clear_graph_elements(self):
self.nodes = []
self.edges = []
......@@ -41,6 +44,26 @@ class Graph:
def add_edge(self, source, target):
self.edges.append({'data': {'source': source, 'target': target}})
def remove_node(self, node):
node_idx_to_remove = -1
for idx, node_dict in enumerate(self.nodes):
if node_dict['data']['id'] == node:
node_idx_to_remove = idx
if node_idx_to_remove >= 0:
self.nodes.pop(node_idx_to_remove)
self.remove_edges(node)
def remove_edges(self, node):
edges_idxs_to_remove = []
for idx, edge_dict in enumerate(self.edges):
if edge_dict['data']['source'] == node or edge_dict['data']['target'] == node:
edges_idxs_to_remove.append(idx)
for edge_idx_to_remove in edges_idxs_to_remove:
self.edges.pop(edge_idx_to_remove)
def get_graph_elements(self):
nodes_and_edges = []
nodes_and_edges.extend(self.nodes)
......@@ -50,6 +73,9 @@ class Graph:
def get_graph(self, component_id):
elements = self.get_graph_elements()
return cyto.Cytoscape(id=component_id,
layout={'name': 'cose'},
style={'width': '100%', 'height': '750px'},
elements=elements)
layout={'name': 'cose', 'animate': False},
style={'width': '100%', 'height': '550px'},
elements=elements,
userZoomingEnabled=False,
userPanningEnabled=False,
)
......@@ -14,7 +14,6 @@ class AssociatedWords:
self.gensim_result = None
self.filtered_results = None
self.words = None
self.scores = None
def set_base_word(self, word):
self.base_word = word
......@@ -24,8 +23,7 @@ class AssociatedWords:
def filter_results(self):
self.filtered_results = [result_tuple for result_tuple in self.gensim_result if self.passes_filter(result_tuple[0])]
self.words = [result_tuple[0] for result_tuple in self.filtered_results]
self.scores = [result_tuple[0] for result_tuple in self.filtered_results]
def passes_filter(self, word):
if 'www' in word or \
word in self.base_word or \
......
......@@ -20,4 +20,5 @@ retrying==1.3.3
scipy==1.6.1
six==1.15.0
smart-open==4.2.0
StringDist==1.0.9
Werkzeug==1.0.1
Supports Markdown
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