-
Notifications
You must be signed in to change notification settings - Fork 11
/
knn.go
99 lines (96 loc) · 2.74 KB
/
knn.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bufio"
"fmt"
"github.com/lokicui/doc2vec-golang/doc2vec"
"github.com/lokicui/doc2vec-golang/common"
"github.com/lokicui/doc2vec-golang/segmenter"
"log"
"os"
"strconv"
"strings"
)
func get_segmented_query(text string) string {
seg := segmenter.GetSegmenter()
// qItems, err := seg.SegmentQuery(text, false)
// if err != nil {
// return ""
// }
// if len(qItems) == 0 {
// return ""
// }
// qWords := []string{}
// for _, item := range qItems {
// word := common.SBC2DBC(item.Word)
// qWords = append(qWords, word)
// }
qWords := []string{}
for item := range seg.Cut(text, false) {
word := common.SBC2DBC(item.Text())
qWords = append(qWords, word)
}
return strings.Join(qWords, " ")
}
func main() {
fname := os.Args[1]
d2v := doc2vec.NewDoc2Vec(true, false, true, 5, 50, 1)
err := d2v.LoadModel(fname)
if err != nil {
log.Fatal(err)
}
for true {
reader := bufio.NewReader(os.Stdin)
fmt.Println("please select operation type:\n\t0:word2words\n\t1:doc_likelihood\n\t2:leave one out key words\n\t3:sen2words\n\t4:sen2docs\n\t5:word2docs\n\t6:doc2docs\n\t7:doc2words\n\t")
text, _ := reader.ReadString('\n')
text = strings.Trim(text, "\n")
switch text {
case "0":
fmt.Printf("Enter text:")
text, _ = reader.ReadString('\n')
d2v.Word2Words(strings.Trim(text, "\n"))
case "1":
fmt.Printf("Enter text:")
text, _ = reader.ReadString('\n')
likelihood := d2v.GetLikelihood4Doc(get_segmented_query(strings.Trim(text, "\n")))
fmt.Printf("%v\t%v\n", text, likelihood)
case "2":
fmt.Printf("Enter text:")
text, _ = reader.ReadString('\n')
d2v.GetLeaveOneOutKwds(get_segmented_query(strings.Trim(text, "\n")), 50)
case "3":
fmt.Printf("Enter text:")
text, _ = reader.ReadString('\n')
d2v.Sen2Words(get_segmented_query(strings.Trim(text, "\n")), 50)
case "4":
fmt.Printf("Enter text:")
text, _ = reader.ReadString('\n')
d2v.Sen2Docs(get_segmented_query(strings.Trim(text, "\n")), 50)
case "5":
fmt.Printf("Enter text:")
text, _ = reader.ReadString('\n')
d2v.Word2Docs(strings.Trim(text, "\n"))
case "6":
fmt.Printf("Enter text:")
text, _ = reader.ReadString('\n')
idx, _ := strconv.Atoi(strings.Trim(text, "\n"))
d2v.Doc2Docs(idx)
case "7":
fmt.Printf("Enter text:")
text, _ = reader.ReadString('\n')
idx, _ := strconv.Atoi(strings.Trim(text, "\n"))
d2v.Doc2Words(idx)
}
}
td := d2v.GetCorpus()
for _, worditem := range td.GetAllWords() {
fmt.Printf("%+v\n", worditem)
}
for _, words := range td.GetAllDocWords() {
sen := []string{}
for _, word := range words {
sen = append(sen, word.Word)
}
ss := strings.Join(sen, " ")
fmt.Println(ss)
}
}