2023-08-08 19:34:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
type Edge struct {
|
|
|
|
|
name string
|
|
|
|
|
weight float64
|
2023-08-10 19:42:14 +00:00
|
|
|
target *Node
|
2023-08-08 19:34:10 +00:00
|
|
|
scentenceId uint32
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 19:42:14 +00:00
|
|
|
type Node struct {
|
|
|
|
|
edges map[string]map[uint32]Edge
|
|
|
|
|
name string
|
|
|
|
|
start int
|
|
|
|
|
end int
|
|
|
|
|
weight float64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func makeNode(name string) *Node {
|
|
|
|
|
node := &Node{name: name, edges: make(map[string]map[uint32]Edge), start: 0, end: 0, weight: 0}
|
2023-08-08 19:34:10 +00:00
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Thesaurus struct {
|
|
|
|
|
pntrmap map[string]*Node
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (thes *Thesaurus) addEntry(start string, targets []string, scentenceId uint32) {
|
|
|
|
|
_, exists := thes.pntrmap[start]
|
|
|
|
|
if !exists {
|
2023-08-10 19:42:14 +00:00
|
|
|
thes.pntrmap[start] = makeNode(start)
|
2023-08-08 19:34:10 +00:00
|
|
|
}
|
|
|
|
|
thes.pntrmap[start].weight += 1
|
|
|
|
|
thes.addEdges(start, targets, scentenceId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (thes *Thesaurus) addEdges(start string, targets []string, scentenceId uint32) {
|
|
|
|
|
val := thes.pntrmap[start]
|
|
|
|
|
for _, s := range targets {
|
|
|
|
|
edgeVal, edgeExists := val.edges[s]
|
|
|
|
|
if edgeExists {
|
2023-08-10 19:42:14 +00:00
|
|
|
edge := edgeVal[scentenceId]
|
|
|
|
|
edge.weight += 1
|
|
|
|
|
edgeVal[scentenceId] = edge
|
2023-08-08 19:34:10 +00:00
|
|
|
val.edges[s] = edgeVal
|
|
|
|
|
} else {
|
|
|
|
|
targetVal, targetExists := thes.pntrmap[s]
|
|
|
|
|
if !targetExists {
|
2023-08-10 19:42:14 +00:00
|
|
|
thes.pntrmap[s] = makeNode(s)
|
2023-08-08 19:34:10 +00:00
|
|
|
targetVal = thes.pntrmap[s]
|
|
|
|
|
}
|
2023-08-10 19:42:14 +00:00
|
|
|
val.edges[s][scentenceId] = Edge{name: s, weight: 1, target: targetVal, scentenceId: scentenceId}
|
2023-08-08 19:34:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|