diff --git a/main.go b/main.go index 001b4d9..09acb83 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "net/http" "os" "strings" @@ -40,8 +41,9 @@ func drawNode(x graph.Graph[string, string], node *Node, drawn *[]string, limit return y } +var thes = Thesaurus{pntrmap: make(map[string]*Node)} + func main() { - thes := Thesaurus{pntrmap: make(map[string]*Node)} records := readCsvFile("./csv_file2.csv") for _, record := range records { @@ -64,6 +66,7 @@ func main() { } //fmt.Println(node.weight) } + g := graph.New(graph.StringHash, graph.Directed()) drawn := []string{} //ctr := 0 @@ -79,4 +82,6 @@ func main() { g = drawNode(g, thes.pntrmap["the"], &drawn, 4, 0.3) file, _ := os.Create("my-graph.gv") _ = draw.DOT(g, file) + + http.ListenAndServe("127.0.0.1:8080", http.HandlerFunc(Serve)) } diff --git a/my-graph.gv b/my-graph.gv index 83d1fef..36b5061 100644 --- a/my-graph.gv +++ b/my-graph.gv @@ -1,319 +1,171 @@ strict digraph { - "for" [ label="for", weight=0 ]; - - "for" -> "is" [ weight=0 ]; - - "for" -> "for" [ weight=0 ]; - - "for" -> "on" [ weight=0 ]; - - "for" -> "the" [ weight=0 ]; - - "for" -> "are" [ weight=0 ]; - - "for" -> "your" [ weight=0 ]; - - "for" -> "this" [ weight=0 ]; - - "for" -> "and" [ weight=0 ]; - - "for" -> "in" [ weight=0 ]; - - "for" -> "a" [ weight=0 ]; - - "for" -> "to" [ weight=0 ]; - - "for" -> "that" [ weight=0 ]; - - "for" -> "you" [ weight=0 ]; - "are" [ label="are", weight=0 ]; - "are" -> "a" [ weight=0 ]; - - "are" -> "is" [ weight=0 ]; - - "are" -> "and" [ weight=0 ]; - - "are" -> "the" [ weight=0 ]; - - "are" -> "for" [ weight=0 ]; - - "are" -> "this" [ weight=0 ]; - - "are" -> "on" [ weight=0 ]; - - "are" -> "are" [ weight=0 ]; - - "are" -> "of" [ weight=0 ]; - - "are" -> "that" [ weight=0 ]; - - "are" -> "in" [ weight=0 ]; - - "are" -> "your" [ weight=0 ]; - - "are" -> "you" [ weight=0 ]; - "are" -> "to" [ weight=0 ]; - "of" [ label="of", weight=0 ]; + "are" -> "are" [ weight=0 ]; - "of" -> "a" [ weight=0 ]; + "are" -> "this" [ weight=0 ]; - "of" -> "of" [ weight=0 ]; + "are" -> "your" [ weight=0 ]; - "of" -> "you" [ weight=0 ]; + "are" -> "a" [ weight=0 ]; - "of" -> "and" [ weight=0 ]; + "are" -> "the" [ weight=0 ]; - "of" -> "in" [ weight=0 ]; + "are" -> "is" [ weight=0 ]; - "of" -> "are" [ weight=0 ]; + "are" -> "in" [ weight=0 ]; - "of" -> "is" [ weight=0 ]; + "are" -> "that" [ weight=0 ]; - "of" -> "for" [ weight=0 ]; + "are" -> "of" [ weight=0 ]; - "of" -> "your" [ weight=0 ]; + "are" -> "for" [ weight=0 ]; - "of" -> "the" [ weight=0 ]; + "are" -> "on" [ weight=0 ]; - "of" -> "to" [ weight=0 ]; + "are" -> "you" [ weight=0 ]; - "of" -> "de" [ weight=0 ]; - - "of" -> "this" [ weight=0 ]; - - "of" -> "that" [ weight=0 ]; - - "of" -> "on" [ weight=0 ]; - - "de" [ label="de", weight=0 ]; - - "de" -> "the" [ weight=0 ]; - - "de" -> "on" [ weight=0 ]; - - "de" -> "you" [ weight=0 ]; - - "de" -> "to" [ weight=0 ]; - - "de" -> "that" [ weight=0 ]; - - "de" -> "a" [ weight=0 ]; - - "de" -> "and" [ weight=0 ]; - - "de" -> "this" [ weight=0 ]; - - "de" -> "de" [ weight=0 ]; - - "this" [ label="this", weight=0 ]; - - "this" -> "for" [ weight=0 ]; - - "this" -> "and" [ weight=0 ]; - - "this" -> "a" [ weight=0 ]; - - "this" -> "are" [ weight=0 ]; - - "this" -> "on" [ weight=0 ]; - - "this" -> "your" [ weight=0 ]; - - "this" -> "to" [ weight=0 ]; - - "this" -> "is" [ weight=0 ]; - - "this" -> "of" [ weight=0 ]; - - "this" -> "the" [ weight=0 ]; - - "this" -> "this" [ weight=0 ]; - - "this" -> "in" [ weight=0 ]; - - "this" -> "that" [ weight=0 ]; - - "this" -> "you" [ weight=0 ]; - - "a" [ label="a", weight=0 ]; - - "a" -> "for" [ weight=0 ]; - - "a" -> "de" [ weight=0 ]; - - "a" -> "and" [ weight=0 ]; - - "a" -> "is" [ weight=0 ]; - - "a" -> "the" [ weight=0 ]; - - "a" -> "on" [ weight=0 ]; - - "a" -> "this" [ weight=0 ]; - - "a" -> "that" [ weight=0 ]; - - "a" -> "in" [ weight=0 ]; - - "a" -> "to" [ weight=0 ]; - - "a" -> "a" [ weight=0 ]; - - "is" [ label="is", weight=0 ]; - - "is" -> "that" [ weight=0 ]; - - "is" -> "this" [ weight=0 ]; - - "is" -> "and" [ weight=0 ]; - - "is" -> "the" [ weight=0 ]; - - "is" -> "to" [ weight=0 ]; - - "is" -> "you" [ weight=0 ]; - - "is" -> "a" [ weight=0 ]; - - "is" -> "for" [ weight=0 ]; - - "is" -> "your" [ weight=0 ]; - - "is" -> "of" [ weight=0 ]; - - "is" -> "on" [ weight=0 ]; - - "is" -> "in" [ weight=0 ]; - - "your" [ label="your", weight=0 ]; - - "your" -> "the" [ weight=0 ]; - - "your" -> "you" [ weight=0 ]; - - "your" -> "a" [ weight=0 ]; - - "your" -> "on" [ weight=0 ]; - - "your" -> "your" [ weight=0 ]; - - "your" -> "this" [ weight=0 ]; - - "your" -> "for" [ weight=0 ]; - - "your" -> "are" [ weight=0 ]; - - "your" -> "to" [ weight=0 ]; - - "the" [ label="the", weight=0 ]; - - "the" -> "in" [ weight=0 ]; - - "the" -> "a" [ weight=0 ]; - - "the" -> "of" [ weight=0 ]; - - "the" -> "your" [ weight=0 ]; - - "the" -> "that" [ weight=0 ]; - - "the" -> "the" [ weight=0 ]; - - "the" -> "and" [ weight=0 ]; - - "the" -> "you" [ weight=0 ]; - - "the" -> "this" [ weight=0 ]; - - "the" -> "to" [ weight=0 ]; - - "the" -> "on" [ weight=0 ]; - - "the" -> "is" [ weight=0 ]; - - "on" [ label="on", weight=0 ]; - - "on" -> "your" [ weight=0 ]; - - "on" -> "a" [ weight=0 ]; - - "on" -> "is" [ weight=0 ]; - - "on" -> "in" [ weight=0 ]; - - "on" -> "are" [ weight=0 ]; - - "on" -> "this" [ weight=0 ]; - - "on" -> "to" [ weight=0 ]; - - "on" -> "the" [ weight=0 ]; - - "on" -> "for" [ weight=0 ]; - - "on" -> "you" [ weight=0 ]; - - "on" -> "that" [ weight=0 ]; - - "on" -> "and" [ weight=0 ]; - - "on" -> "on" [ weight=0 ]; + "are" -> "and" [ weight=0 ]; "you" [ label="you", weight=0 ]; + "you" -> "that" [ weight=0 ]; + "you" -> "are" [ weight=0 ]; - "you" -> "on" [ weight=0 ]; - - "you" -> "to" [ weight=0 ]; - - "you" -> "your" [ weight=0 ]; - "you" -> "you" [ weight=0 ]; - "you" -> "in" [ weight=0 ]; - - "you" -> "for" [ weight=0 ]; + "you" -> "on" [ weight=0 ]; "you" -> "and" [ weight=0 ]; - "you" -> "the" [ weight=0 ]; - - "you" -> "is" [ weight=0 ]; - "you" -> "of" [ weight=0 ]; - "you" -> "this" [ weight=0 ]; + "you" -> "your" [ weight=0 ]; + + "you" -> "the" [ weight=0 ]; "you" -> "a" [ weight=0 ]; "you" -> "de" [ weight=0 ]; - "you" -> "that" [ weight=0 ]; + "you" -> "to" [ weight=0 ]; + + "you" -> "in" [ weight=0 ]; + + "you" -> "is" [ weight=0 ]; + + "you" -> "for" [ weight=0 ]; + + "you" -> "this" [ weight=0 ]; + + "to" [ label="to", weight=0 ]; + + "to" -> "on" [ weight=0 ]; + + "to" -> "your" [ weight=0 ]; + + "to" -> "for" [ weight=0 ]; + + "to" -> "is" [ weight=0 ]; + + "to" -> "you" [ weight=0 ]; + + "to" -> "that" [ weight=0 ]; + + "to" -> "are" [ weight=0 ]; + + "to" -> "to" [ weight=0 ]; + + "to" -> "and" [ weight=0 ]; + + "to" -> "a" [ weight=0 ]; + + "to" -> "this" [ weight=0 ]; + + "to" -> "the" [ weight=0 ]; + + "to" -> "in" [ weight=0 ]; + + "and" [ label="and", weight=0 ]; + + "and" -> "your" [ weight=0 ]; + + "and" -> "you" [ weight=0 ]; + + "and" -> "the" [ weight=0 ]; + + "and" -> "of" [ weight=0 ]; + + "and" -> "to" [ weight=0 ]; + + "and" -> "and" [ weight=0 ]; + + "and" -> "are" [ weight=0 ]; + + "and" -> "a" [ weight=0 ]; + + "and" -> "is" [ weight=0 ]; + + "and" -> "on" [ weight=0 ]; + + "and" -> "this" [ weight=0 ]; + + "and" -> "for" [ weight=0 ]; + + "and" -> "that" [ weight=0 ]; + + "and" -> "in" [ weight=0 ]; + + "that" [ label="that", weight=0 ]; + + "that" -> "your" [ weight=0 ]; + + "that" -> "the" [ weight=0 ]; + + "that" -> "for" [ weight=0 ]; + + "that" -> "in" [ weight=0 ]; + + "that" -> "you" [ weight=0 ]; + + "that" -> "to" [ weight=0 ]; + + "that" -> "this" [ weight=0 ]; + + "that" -> "of" [ weight=0 ]; + + "that" -> "and" [ weight=0 ]; + + "that" -> "on" [ weight=0 ]; + + "that" -> "a" [ weight=0 ]; + + "that" -> "are" [ weight=0 ]; + + "that" -> "is" [ weight=0 ]; + + "that" -> "that" [ weight=0 ]; "in" [ label="in", weight=0 ]; - "in" -> "a" [ weight=0 ]; - - "in" -> "the" [ weight=0 ]; - - "in" -> "this" [ weight=0 ]; - "in" -> "for" [ weight=0 ]; + "in" -> "on" [ weight=0 ]; + "in" -> "you" [ weight=0 ]; + "in" -> "the" [ weight=0 ]; + "in" -> "your" [ weight=0 ]; "in" -> "to" [ weight=0 ]; - "in" -> "and" [ weight=0 ]; + "in" -> "this" [ weight=0 ]; "in" -> "is" [ weight=0 ]; @@ -321,96 +173,244 @@ strict digraph { "in" -> "are" [ weight=0 ]; - "in" -> "on" [ weight=0 ]; + "in" -> "and" [ weight=0 ]; + + "in" -> "a" [ weight=0 ]; "in" -> "that" [ weight=0 ]; - "and" [ label="and", weight=0 ]; + "for" [ label="for", weight=0 ]; - "and" -> "a" [ weight=0 ]; + "for" -> "in" [ weight=0 ]; - "and" -> "and" [ weight=0 ]; + "for" -> "your" [ weight=0 ]; - "and" -> "this" [ weight=0 ]; + "for" -> "are" [ weight=0 ]; - "and" -> "is" [ weight=0 ]; + "for" -> "you" [ weight=0 ]; - "and" -> "to" [ weight=0 ]; + "for" -> "for" [ weight=0 ]; - "and" -> "for" [ weight=0 ]; + "for" -> "and" [ weight=0 ]; - "and" -> "in" [ weight=0 ]; + "for" -> "to" [ weight=0 ]; - "and" -> "that" [ weight=0 ]; + "for" -> "that" [ weight=0 ]; - "and" -> "on" [ weight=0 ]; + "for" -> "the" [ weight=0 ]; - "and" -> "of" [ weight=0 ]; + "for" -> "this" [ weight=0 ]; - "and" -> "are" [ weight=0 ]; + "for" -> "a" [ weight=0 ]; - "and" -> "your" [ weight=0 ]; + "for" -> "is" [ weight=0 ]; - "and" -> "the" [ weight=0 ]; + "for" -> "on" [ weight=0 ]; - "and" -> "you" [ weight=0 ]; + "a" [ label="a", weight=0 ]; - "to" [ label="to", weight=0 ]; + "a" -> "the" [ weight=0 ]; - "to" -> "for" [ weight=0 ]; + "a" -> "to" [ weight=0 ]; - "to" -> "that" [ weight=0 ]; + "a" -> "that" [ weight=0 ]; - "to" -> "your" [ weight=0 ]; + "a" -> "a" [ weight=0 ]; - "to" -> "in" [ weight=0 ]; + "a" -> "this" [ weight=0 ]; - "to" -> "is" [ weight=0 ]; + "a" -> "on" [ weight=0 ]; - "to" -> "are" [ weight=0 ]; + "a" -> "in" [ weight=0 ]; - "to" -> "to" [ weight=0 ]; + "a" -> "and" [ weight=0 ]; - "to" -> "on" [ weight=0 ]; + "a" -> "is" [ weight=0 ]; - "to" -> "a" [ weight=0 ]; + "a" -> "for" [ weight=0 ]; - "to" -> "the" [ weight=0 ]; + "a" -> "de" [ weight=0 ]; - "to" -> "you" [ weight=0 ]; + "the" [ label="the", weight=0 ]; - "to" -> "this" [ weight=0 ]; + "the" -> "in" [ weight=0 ]; - "to" -> "and" [ weight=0 ]; + "the" -> "a" [ weight=0 ]; - "that" [ label="that", weight=0 ]; + "the" -> "the" [ weight=0 ]; - "that" -> "for" [ weight=0 ]; + "the" -> "and" [ weight=0 ]; - "that" -> "on" [ weight=0 ]; + "the" -> "is" [ weight=0 ]; - "that" -> "is" [ weight=0 ]; + "the" -> "that" [ weight=0 ]; - "that" -> "in" [ weight=0 ]; + "the" -> "this" [ weight=0 ]; - "that" -> "this" [ weight=0 ]; + "the" -> "your" [ weight=0 ]; - "that" -> "to" [ weight=0 ]; + "the" -> "on" [ weight=0 ]; - "that" -> "and" [ weight=0 ]; + "the" -> "of" [ weight=0 ]; - "that" -> "are" [ weight=0 ]; + "the" -> "you" [ weight=0 ]; - "that" -> "that" [ weight=0 ]; + "the" -> "to" [ weight=0 ]; - "that" -> "of" [ weight=0 ]; + "de" [ label="de", weight=0 ]; - "that" -> "a" [ weight=0 ]; + "de" -> "this" [ weight=0 ]; - "that" -> "the" [ weight=0 ]; + "de" -> "on" [ weight=0 ]; - "that" -> "your" [ weight=0 ]; + "de" -> "the" [ weight=0 ]; - "that" -> "you" [ weight=0 ]; + "de" -> "de" [ weight=0 ]; + + "de" -> "you" [ weight=0 ]; + + "de" -> "a" [ weight=0 ]; + + "de" -> "that" [ weight=0 ]; + + "de" -> "and" [ weight=0 ]; + + "de" -> "to" [ weight=0 ]; + + "this" [ label="this", weight=0 ]; + + "this" -> "is" [ weight=0 ]; + + "this" -> "this" [ weight=0 ]; + + "this" -> "that" [ weight=0 ]; + + "this" -> "the" [ weight=0 ]; + + "this" -> "to" [ weight=0 ]; + + "this" -> "are" [ weight=0 ]; + + "this" -> "of" [ weight=0 ]; + + "this" -> "and" [ weight=0 ]; + + "this" -> "for" [ weight=0 ]; + + "this" -> "you" [ weight=0 ]; + + "this" -> "a" [ weight=0 ]; + + "this" -> "in" [ weight=0 ]; + + "this" -> "your" [ weight=0 ]; + + "this" -> "on" [ weight=0 ]; + + "is" [ label="is", weight=0 ]; + + "is" -> "your" [ weight=0 ]; + + "is" -> "the" [ weight=0 ]; + + "is" -> "and" [ weight=0 ]; + + "is" -> "on" [ weight=0 ]; + + "is" -> "a" [ weight=0 ]; + + "is" -> "to" [ weight=0 ]; + + "is" -> "of" [ weight=0 ]; + + "is" -> "this" [ weight=0 ]; + + "is" -> "for" [ weight=0 ]; + + "is" -> "you" [ weight=0 ]; + + "is" -> "that" [ weight=0 ]; + + "is" -> "in" [ weight=0 ]; + + "your" [ label="your", weight=0 ]; + + "your" -> "for" [ weight=0 ]; + + "your" -> "are" [ weight=0 ]; + + "your" -> "this" [ weight=0 ]; + + "your" -> "to" [ weight=0 ]; + + "your" -> "on" [ weight=0 ]; + + "your" -> "the" [ weight=0 ]; + + "your" -> "a" [ weight=0 ]; + + "your" -> "your" [ weight=0 ]; + + "your" -> "you" [ weight=0 ]; + + "of" [ label="of", weight=0 ]; + + "of" -> "this" [ weight=0 ]; + + "of" -> "is" [ weight=0 ]; + + "of" -> "de" [ weight=0 ]; + + "of" -> "in" [ weight=0 ]; + + "of" -> "for" [ weight=0 ]; + + "of" -> "of" [ weight=0 ]; + + "of" -> "and" [ weight=0 ]; + + "of" -> "to" [ weight=0 ]; + + "of" -> "a" [ weight=0 ]; + + "of" -> "are" [ weight=0 ]; + + "of" -> "on" [ weight=0 ]; + + "of" -> "the" [ weight=0 ]; + + "of" -> "that" [ weight=0 ]; + + "of" -> "your" [ weight=0 ]; + + "of" -> "you" [ weight=0 ]; + + "on" [ label="on", weight=0 ]; + + "on" -> "to" [ weight=0 ]; + + "on" -> "this" [ weight=0 ]; + + "on" -> "you" [ weight=0 ]; + + "on" -> "is" [ weight=0 ]; + + "on" -> "in" [ weight=0 ]; + + "on" -> "on" [ weight=0 ]; + + "on" -> "your" [ weight=0 ]; + + "on" -> "for" [ weight=0 ]; + + "on" -> "and" [ weight=0 ]; + + "on" -> "that" [ weight=0 ]; + + "on" -> "a" [ weight=0 ]; + + "on" -> "the" [ weight=0 ]; + + "on" -> "are" [ weight=0 ]; } diff --git a/thesaurus.go b/thesaurus.go index 8044b90..3b92073 100644 --- a/thesaurus.go +++ b/thesaurus.go @@ -1,22 +1,22 @@ package main type Edge struct { - name string - weight float64 - target *Node -} - -type Node struct { - edges map[string]Edge name string - start int - end int weight float64 + target *Node scentenceId uint32 } -func makeNode(name string, scentenceId uint32) *Node { - node := &Node{name: name, edges: make(map[string]Edge), start: 0, end: 0, weight: 0, scentenceId: scentenceId} +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} return node } @@ -27,7 +27,7 @@ type Thesaurus struct { func (thes *Thesaurus) addEntry(start string, targets []string, scentenceId uint32) { _, exists := thes.pntrmap[start] if !exists { - thes.pntrmap[start] = makeNode(start, scentenceId) + thes.pntrmap[start] = makeNode(start) } thes.pntrmap[start].weight += 1 thes.addEdges(start, targets, scentenceId) @@ -38,15 +38,17 @@ func (thes *Thesaurus) addEdges(start string, targets []string, scentenceId uint for _, s := range targets { edgeVal, edgeExists := val.edges[s] if edgeExists { - edgeVal.weight += 1 + edge := edgeVal[scentenceId] + edge.weight += 1 + edgeVal[scentenceId] = edge val.edges[s] = edgeVal } else { targetVal, targetExists := thes.pntrmap[s] if !targetExists { - thes.pntrmap[s] = makeNode(s, scentenceId) + thes.pntrmap[s] = makeNode(s) targetVal = thes.pntrmap[s] } - val.edges[s] = Edge{name: s, weight: 1, target: targetVal} + val.edges[s][scentenceId] = Edge{name: s, weight: 1, target: targetVal, scentenceId: scentenceId} } } } diff --git a/util.go b/util.go index 778d8ce..48c2666 100644 --- a/util.go +++ b/util.go @@ -1,12 +1,16 @@ package main import ( + "bytes" "encoding/csv" + "encoding/json" "fmt" "hash/fnv" + "io" "log" "os" "strings" + "sync" ) func hash(s string) uint32 { @@ -63,3 +67,50 @@ func contains(slc *[]string, str string) bool { } return false } + +var Marshal = func(v interface{}) (io.Reader, error) { + b, err := json.MarshalIndent(v, "", "\t") + if err != nil { + return nil, err + } + return bytes.NewReader(b), nil +} +var lock sync.Mutex + +// Unmarshal is a function that unmarshals the data from the +// reader into the specified value. +// By default, it uses the JSON unmarshaller. +var Unmarshal = func(r io.Reader, v interface{}) error { + return json.NewDecoder(r).Decode(v) +} + +// Load loads the file at path into v. +// Use os.IsNotExist() to see if the returned error is due +// to the file being missing. +func Load(path string, v interface{}) error { + lock.Lock() + defer lock.Unlock() + f, err := os.Open(path) + if err != nil { + return err + } + defer f.Close() + return Unmarshal(f, v) +} + +// Save saves a representation of v to the file at path. +func Save(path string, v interface{}) error { + lock.Lock() + defer lock.Unlock() + f, err := os.Create(path) + if err != nil { + return err + } + defer f.Close() + r, err := Marshal(v) + if err != nil { + return err + } + _, err = io.Copy(f, r) + return err +}