/
links.go
58 lines (48 loc) · 1.13 KB
/
links.go
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
package linker
import (
"crypto/sha256"
"encoding/hex"
"fmt"
)
// Segment is the linker internal segment type containing a document link as well
type Segment struct {
*Node
Text string `json:"text"`
}
// Node in the link graph
type Node struct {
hash string
Doc string `json:"doc"`
Seg int `json:"seg"`
}
func (n *Node) String() string {
return fmt.Sprintf("%s-%d", n.Doc, n.Seg)
}
// Hash returns a unique reproducible hash for each node
func (n *Node) Hash() string {
if n.hash == "" {
h := sha256.New()
h.Write([]byte(fmt.Sprintf("%s-%d", n.Doc, n.Seg)))
n.hash = hex.EncodeToString(h.Sum(nil))
}
return n.hash
}
// Edge between two nodes
type Edge struct {
hash string
Source *Node `json:"source"`
Target *Node `json:"target"`
Weight float32 `json:"weight"`
}
func (e *Edge) String() string {
return fmt.Sprintf("%s-%s#%v", e.Source, e.Target, e.Weight)
}
// Hash returns a unique reproducible hash for each edge
func (e *Edge) Hash() string {
if e.hash == "" {
h := sha256.New()
h.Write([]byte(fmt.Sprintf("%s-%s", e.Source, e.Target)))
e.hash = hex.EncodeToString(h.Sum(nil))
}
return e.hash
}