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 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__':