first viz; looks terrible

This commit is contained in:
Mark Eaton
2024-12-14 02:57:31 -05:00
parent da559db22b
commit 21badae763
2 changed files with 28 additions and 12 deletions
+21 -4
View File
@@ -1,4 +1,5 @@
import csv import csv
import json
import re import re
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
@@ -8,7 +9,7 @@ from pprint import pprint
stop_words = ['of', 'and', 'the', 'we', 'in', 'to', 'what', 'a', 'on', 'an', stop_words = ['of', 'and', 'the', 'we', 'in', 'to', 'what', 'a', 'on', 'an',
'do', 'for', 'with', 'is', 'it', 'that', 'this', 'as', 'by', 'do', 'for', 'with', 'is', 'it', 'that', 'this', 'as', 'by',
'are', 'using', 'from', 'how', 'has', 'be', 'or', 'can', 'our', 'are', 'using', 'from', 'how', 'has', 'be', 'or', 'can', 'our',
'at', 'why', 'when', 'introduction'] 'at', 'why', 'when', 'introduction', 'your']
file_path = 'data.csv' file_path = 'data.csv'
data = [] data = []
@@ -34,10 +35,13 @@ def extract_title_keywords(data):
re_words = [] re_words = []
for row in data: for row in data:
raw_words = row[2].split(' ') raw_words = row[2].split(' ')
row_words = []
for word in raw_words: for word in raw_words:
re_word = re.sub(r'[^a-zA-Z\-]', '', word) re_word = re.sub(r'[^a-zA-Z\-]', '', word)
re_words.append((re_word, row[2])) if re_word not in stop_words:
title_keywords = [(word.lower(), title) for (word, title) in re_words if word.lower() 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 return title_keywords
@@ -65,7 +69,20 @@ def dash_viz(data):
word_output[line[0]] = [line[1]] word_output[line[0]] = [line[1]]
else: else:
word_output[line[0]].append(line[1]) 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__': if __name__ == '__main__':
+6 -7
View File
@@ -1,19 +1,18 @@
from dash import Dash, html from dash import Dash, html
from pprint import pprint
import dash_cytoscape as cyto import dash_cytoscape as cyto
import json
app = Dash(__name__) app = Dash(__name__)
with open('viz_data.json') as f:
data = json.load(f)
app.layout = html.Div([ app.layout = html.Div([
html.P("Dash Cytoscape:"), html.P("Dash Cytoscape:"),
cyto.Cytoscape( cyto.Cytoscape(
id='cytoscape', id='cytoscape',
elements=[ elements=data,
{'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'}}
],
layout={'name': 'breadthfirst'}, layout={'name': 'breadthfirst'},
style={'width': '400px', 'height': '500px'} style={'width': '400px', 'height': '500px'}
) )