-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from oolong-sh/graph-serialize
Add graph serialization
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,5 @@ go.work.sum | |
|
||
# env file | ||
.env | ||
|
||
oolong |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package graph | ||
|
||
import ( | ||
"encoding/json" | ||
"path/filepath" | ||
|
||
"github.com/oolong-sh/oolong/pkg/keywords" | ||
"github.com/oolong-sh/oolong/pkg/notes" | ||
) | ||
|
||
type NodeJSON struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Val float64 `json:"val"` | ||
} | ||
|
||
type LinkJSON struct { | ||
Source string `json:"source"` | ||
Target string `json:"target"` | ||
} | ||
|
||
type Graph struct { | ||
Nodes []NodeJSON `json:"nodes"` | ||
Links []LinkJSON `json:"links"` | ||
} | ||
|
||
func clamp(value, min, max float64) float64 { | ||
if value < min { | ||
return min | ||
} | ||
if value > max { | ||
return max | ||
} | ||
return value | ||
} | ||
|
||
const NOTE_NODE_VAL = 50 | ||
|
||
func SerializeGraph(keywordMap map[string]keywords.Keyword, notes []notes.Note, lowerBound, upperBound float64) ([]byte, error) { | ||
nodes := []NodeJSON{} | ||
links := []LinkJSON{} | ||
|
||
for _, keyword := range keywordMap { | ||
// Only add nodes above the minimum threshold | ||
if keyword.Weight >= lowerBound { | ||
clampedWeight := clamp(keyword.Weight, lowerBound, upperBound) | ||
nodes = append(nodes, NodeJSON{ | ||
ID: keyword.Keyword, | ||
Name: keyword.Keyword, | ||
Val: clampedWeight, | ||
}) | ||
} | ||
} | ||
|
||
for _, note := range notes { | ||
// Add Note node with a fixed value | ||
noteID := note.Path | ||
noteName := filepath.Base(note.Path) // /home/patrick/notes/home/blogs/bayes.md -> bayes.md | ||
nodes = append(nodes, NodeJSON{ | ||
ID: noteID, | ||
Name: noteName, | ||
Val: NOTE_NODE_VAL, | ||
}) | ||
|
||
// Link notes to keywords | ||
for keywordID := range note.Weights { | ||
keyword, exists := keywordMap[keywordID] | ||
if exists && keyword.Weight >= lowerBound { | ||
links = append(links, LinkJSON{ | ||
Source: noteID, | ||
Target: keyword.Keyword, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
graph := Graph{ | ||
Nodes: nodes, | ||
Links: links, | ||
} | ||
|
||
jsonData, err := json.Marshal(graph) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return jsonData, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters