-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmodels.go
More file actions
141 lines (119 loc) · 2.94 KB
/
models.go
File metadata and controls
141 lines (119 loc) · 2.94 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bufio"
"encoding/json"
"io"
"os"
"strconv"
"strings"
)
const vectorDims = 300
type modelParser struct {
scanner *bufio.Scanner
}
func NewModelParser(rd io.Reader) modelParser {
return modelParser{
scanner: bufio.NewScanner(rd),
}
}
func (p *modelParser) parse() (map[string]([]float64), error) {
wordCoords := map[string]([]float64){}
for p.scanner.Scan() {
word, coords, err := parseModelLine(p.scanner.Text())
if err != nil {
return wordCoords, err
}
wordCoords[word] = coords
}
return wordCoords, p.scanner.Err()
}
func parseModelLine(line string) (string, []float64, error) {
words := strings.Split(strings.TrimSpace(line), " ")
coords := make([]float64, len(words)-1)
var err error
for i, coordString := range words[1:] {
coords[i], err = strconv.ParseFloat(coordString, 64)
if err != nil {
return "", nil, err
}
}
return words[0], coords, nil
}
type MonocleDoc struct {
Title string `json:"title"`
Href string `json:"href"`
Id string `json:"id"`
Module string `json:"module"`
Tokens map[string]float64 `json:"tokens"`
Content string `json:"content"`
coords []float64
}
type SerializedMonocleDoc struct {
Title string `json:"title"`
Href string `json:"href"`
Id string `json:"id"`
Module string `json:"module"`
Content string `json:"content"`
}
func (d *MonocleDoc) weightedTokenList() []string {
words := make([]string, 0, len(d.Tokens))
for token, count := range d.Tokens {
for i := count; i > 0; i-- {
words = append(words, token)
}
}
return words
}
func serializeDocs(docs []MonocleDoc) ([]byte, error) {
serializedDocs := make([]SerializedMonocleDoc, len(docs))
for i, doc := range docs {
serializedDocs[i] = SerializedMonocleDoc{
Title: doc.Title,
Href: doc.Href,
Id: doc.Id,
Module: doc.Module,
Content: doc.Content,
}
}
return json.Marshal(serializedDocs)
}
func parseModelFile(modelPath string) (map[string]([]float64), error) {
// get word vectors
modelFile, err := os.Open(modelPath)
if err != nil {
return nil, err
}
defer modelFile.Close()
parser := NewModelParser(modelFile)
wordCoords, err := parser.parse()
if err != nil {
return nil, err
}
return wordCoords, err
}
func parseDocsFile(docsPath string, wordCoords map[string]([]float64)) ([]MonocleDoc, error) {
// generate doc vectors
docsFile, err := os.Open(docsPath)
if err != nil {
return nil, err
}
defer docsFile.Close()
docsContent, err := io.ReadAll(docsFile)
if err != nil {
return nil, err
}
docsMaybeNoTokens := map[string]MonocleDoc{}
err = json.Unmarshal(docsContent, &docsMaybeNoTokens)
if err != nil {
return nil, err
}
docSlice := []MonocleDoc{}
for _, doc := range docsMaybeNoTokens {
tokens := doc.weightedTokenList()
if len(tokens) > 0 {
doc.coords = documentVector(wordCoords, tokens)
docSlice = append(docSlice, doc)
}
}
return docSlice, nil
}