Skip to content

Commit

Permalink
Merge pull request #6 from oolong-sh/graph-serialize
Browse files Browse the repository at this point in the history
Add graph serialization
  • Loading branch information
JackWilli authored Nov 23, 2024
2 parents 232cafc + 6ea7bcd commit 17f5f1e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ go.work.sum

# env file
.env

oolong
12 changes: 12 additions & 0 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/oolong-sh/oolong/internal/documents"
"github.com/oolong-sh/oolong/internal/linking/ngrams"
"github.com/oolong-sh/oolong/pkg/graph"
"github.com/oolong-sh/oolong/pkg/keywords"
"github.com/oolong-sh/oolong/pkg/notes"
)
Expand Down Expand Up @@ -87,5 +88,16 @@ func UpdateState(docs []*documents.Document) error {
panic(err)
}

kw := keywords.NGramsToKeywordsMap(state.NGrams)
n := notes.DocumentsToNotes(state.Documents)

dat, err := graph.SerializeGraph(kw, n, 0.1, 80)
if err != nil {
return err
}
if err := os.WriteFile("graph.json", dat, 0644); err != nil {
return err
}

return nil
}
88 changes: 88 additions & 0 deletions pkg/graph/graph.go
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
}
18 changes: 18 additions & 0 deletions pkg/keywords/keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,21 @@ func NGramsToKeywords(ngmap map[string]*ngrams.NGram) []Keyword {

return keywords
}

func NGramsToKeywordsMap(ngmap map[string]*ngrams.NGram) map[string]Keyword {
keywords := map[string]Keyword{}
threshold := 8.0

for k, v := range ngmap {
w := v.Weight()

if w > threshold {
keywords[k] = Keyword{
Keyword: k,
Weight: w,
}
}
}

return keywords
}

0 comments on commit 17f5f1e

Please sign in to comment.