Visualizing Concepts
We will use vis.js to visualize this learning graph.
First we need to convert the CSV file into JSON. ChatGPT
can generate this conversion for us.
Prompt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 | # first line must be
# ID,ConceptName,Dependencies,TaxonomyID
import csv
import json
def csv_to_visjs_json(csv_filename, json_filename):
nodes = []
edges = []
categories = {}
# Read the CSV file
with open(csv_filename, 'r', newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# Extract node information
try:
concept_id = int(row['ID'])
except ValueError:
print(f"Invalid ID '{row['ID']}' skipped.")
continue # Skip rows with invalid ID
concept_name = row['ConceptName'].strip()
if not concept_name:
print(f"Empty Concept Name for ID {concept_id} skipped.")
continue # Skip nodes without a name
try:
category_id = int(row['TaxonomyID'])
except ValueError:
print(f"Invalid TaxonomyID '{row['TaxonomyID']}' for ID {concept_id} skipped.")
continue # Skip rows with invalid Category ID
# Create node object
node = {
'id': concept_id,
'label': concept_name,
'group': category_id
}
nodes.append(node)
# Process Dependencies to create edges
dependency_list = row['Dependencies']
if dependency_list:
dependencies = dependency_list.split('|')
for dep in dependencies:
dep = dep.strip()
if dep:
try:
dep_id = int(dep)
edge = {
'from': concept_id,
'to': dep_id
}
edges.append(edge)
except ValueError:
print(f"Invalid DependencyID '{dep}' for ID {concept_id} skipped.")
continue # Skip invalid DependencyIDs
# If not using separate groups, omit the 'groups' key
data = {
'nodes': nodes,
'edges': edges
}
# Write the JSON output
with open(json_filename, 'w', encoding='utf-8') as jsonfile:
json.dump(data, jsonfile, indent=4)
print(f"Successfully converted '{csv_filename}' to '{json_filename}'.")
if __name__ == "__main__":
# Define input and output file names
input_csv = 'graph-algorithms.csv'
output_json = 'graph-algorithm.json'
# Convert CSV to JSON
csv_to_visjs_json(input_csv, output_json)
|