2023-07-24 21:12:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-10 19:42:14 +00:00
|
|
|
"net/http"
|
2023-07-24 21:12:15 +00:00
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/dominikbraun/graph"
|
|
|
|
|
"github.com/dominikbraun/graph/draw"
|
|
|
|
|
)
|
|
|
|
|
|
2023-08-08 19:34:10 +00:00
|
|
|
func drawNode(x graph.Graph[string, string], node *Node, drawn *[]string, limit int, weightLimit float64) graph.Graph[string, string] {
|
|
|
|
|
if limit <= 0 || len(*drawn) > 1000 {
|
2023-07-24 21:12:15 +00:00
|
|
|
return x
|
|
|
|
|
}
|
|
|
|
|
//fmt.Println(limit)
|
|
|
|
|
y := x
|
|
|
|
|
//fmt.Println(node.name)
|
|
|
|
|
if !contains(drawn, node.name) {
|
|
|
|
|
_ = x.AddVertex(node.name, graph.VertexAttribute("label", node.name))
|
|
|
|
|
*drawn = append(*drawn, node.name)
|
|
|
|
|
}
|
|
|
|
|
edgesCounter := 0
|
|
|
|
|
for _, e := range node.edges {
|
2023-08-08 19:34:10 +00:00
|
|
|
if e.target.weight < weightLimit {
|
2023-07-24 21:12:15 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
edgesCounter++
|
|
|
|
|
if edgesCounter >= 10 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
edgeIsDrawn := contains(drawn, e.target.name)
|
|
|
|
|
if !edgeIsDrawn {
|
|
|
|
|
_ = x.AddVertex(e.target.name, graph.VertexAttribute("label", e.target.name))
|
|
|
|
|
*drawn = append(*drawn, e.target.name)
|
|
|
|
|
}
|
|
|
|
|
_ = x.AddEdge(node.name, e.target.name)
|
|
|
|
|
|
2023-08-08 19:34:10 +00:00
|
|
|
y = drawNode(y, e.target, drawn, limit-1, weightLimit)
|
2023-07-24 21:12:15 +00:00
|
|
|
}
|
|
|
|
|
return y
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 19:42:14 +00:00
|
|
|
var thes = Thesaurus{pntrmap: make(map[string]*Node)}
|
|
|
|
|
|
2023-07-24 21:12:15 +00:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
|
|
records := readCsvFile("./csv_file2.csv")
|
|
|
|
|
for _, record := range records {
|
2023-08-08 19:34:10 +00:00
|
|
|
title := strings.ToLower(record[2])
|
2023-07-24 21:12:15 +00:00
|
|
|
words := strings.Split(title, " ")
|
2023-08-08 19:34:10 +00:00
|
|
|
scentenceId := hash(title)
|
2023-07-24 21:12:15 +00:00
|
|
|
for i := 0; i < len(words)-1; i++ {
|
2023-08-08 19:34:10 +00:00
|
|
|
thes.addEntry(trim(words[i]), []string{trim(words[i+1])}, scentenceId)
|
2023-07-24 21:12:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, node := range thes.pntrmap {
|
|
|
|
|
sum := 0.0
|
2023-08-08 19:34:10 +00:00
|
|
|
node.weight /= float64(len(thes.pntrmap))
|
2023-07-24 21:12:15 +00:00
|
|
|
for _, edge := range node.edges {
|
|
|
|
|
sum += edge.weight
|
|
|
|
|
}
|
|
|
|
|
for _, edge := range node.edges {
|
|
|
|
|
edge.weight = edge.weight / sum
|
|
|
|
|
//fmt.Println(edge.weight, node.name, edge.name)
|
|
|
|
|
}
|
2023-08-08 19:34:10 +00:00
|
|
|
//fmt.Println(node.weight)
|
2023-07-24 21:12:15 +00:00
|
|
|
}
|
2023-08-10 19:42:14 +00:00
|
|
|
|
2023-07-24 21:12:15 +00:00
|
|
|
g := graph.New(graph.StringHash, graph.Directed())
|
|
|
|
|
drawn := []string{}
|
|
|
|
|
//ctr := 0
|
|
|
|
|
//for _, node := range thes.pntrmap {
|
|
|
|
|
// if ctr >= 4 {
|
|
|
|
|
// break
|
|
|
|
|
// }
|
|
|
|
|
// ctr++
|
|
|
|
|
//
|
|
|
|
|
// fmt.Println(node.name)
|
|
|
|
|
// g = drawNode(g, node, &drawn, 4)
|
|
|
|
|
//}
|
2023-08-08 19:34:10 +00:00
|
|
|
g = drawNode(g, thes.pntrmap["the"], &drawn, 4, 0.3)
|
2023-07-24 21:12:15 +00:00
|
|
|
file, _ := os.Create("my-graph.gv")
|
|
|
|
|
_ = draw.DOT(g, file)
|
2023-08-10 19:42:14 +00:00
|
|
|
|
|
|
|
|
http.ListenAndServe("127.0.0.1:8080", http.HandlerFunc(Serve))
|
2023-07-24 21:12:15 +00:00
|
|
|
}
|