first viz; looks terrible
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import csv
|
||||
import json
|
||||
import re
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
@@ -8,7 +9,7 @@ from pprint import pprint
|
||||
stop_words = ['of', 'and', 'the', 'we', 'in', 'to', 'what', 'a', 'on', 'an',
|
||||
'do', 'for', 'with', 'is', 'it', 'that', 'this', 'as', 'by',
|
||||
'are', 'using', 'from', 'how', 'has', 'be', 'or', 'can', 'our',
|
||||
'at', 'why', 'when', 'introduction']
|
||||
'at', 'why', 'when', 'introduction', 'your']
|
||||
file_path = 'data.csv'
|
||||
data = []
|
||||
|
||||
@@ -34,10 +35,13 @@ def extract_title_keywords(data):
|
||||
re_words = []
|
||||
for row in data:
|
||||
raw_words = row[2].split(' ')
|
||||
row_words = []
|
||||
for word in raw_words:
|
||||
re_word = re.sub(r'[^a-zA-Z\-]', '', word)
|
||||
re_words.append((re_word, row[2]))
|
||||
title_keywords = [(word.lower(), title) for (word, title) in re_words if word.lower() not in stop_words]
|
||||
if re_word not in stop_words:
|
||||
re_words.append(re_word.lower())
|
||||
row_words.append(re_word.lower())
|
||||
title_keywords = [(word, row_words) for word in re_words]
|
||||
return title_keywords
|
||||
|
||||
|
||||
@@ -65,7 +69,20 @@ def dash_viz(data):
|
||||
word_output[line[0]] = [line[1]]
|
||||
else:
|
||||
word_output[line[0]].append(line[1])
|
||||
pprint(word_output)
|
||||
output_data = []
|
||||
for word in word_output:
|
||||
if word:
|
||||
output_data.append({'data': {'id': word, 'label': word}})
|
||||
for all_words in word_output.values():
|
||||
for title_words in all_words:
|
||||
edges = [(word1, word2) for i, word1 in enumerate(title_words) for word2
|
||||
in title_words[i+1:]]
|
||||
for edge1, edge2 in edges:
|
||||
if edge1 and edge2:
|
||||
output_data.append({'data': {'source': edge1, 'target': edge2}})
|
||||
pprint(output_data)
|
||||
with open('viz_data.json', 'w') as file:
|
||||
json.dump(output_data, file)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
from dash import Dash, html
|
||||
from pprint import pprint
|
||||
import dash_cytoscape as cyto
|
||||
import json
|
||||
|
||||
app = Dash(__name__)
|
||||
|
||||
with open('viz_data.json') as f:
|
||||
data = json.load(f)
|
||||
|
||||
app.layout = html.Div([
|
||||
html.P("Dash Cytoscape:"),
|
||||
cyto.Cytoscape(
|
||||
id='cytoscape',
|
||||
elements=[
|
||||
{'data': {'id': 'ca', 'label': 'Canada'}},
|
||||
{'data': {'id': 'on', 'label': 'Ontario'}},
|
||||
{'data': {'id': 'qc', 'label': 'Quebec'}},
|
||||
{'data': {'source': 'ca', 'target': 'on'}},
|
||||
{'data': {'source': 'ca', 'target': 'qc'}}
|
||||
],
|
||||
elements=data,
|
||||
layout={'name': 'breadthfirst'},
|
||||
style={'width': '400px', 'height': '500px'}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user