-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (71 loc) · 1.57 KB
/
main.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
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"sort"
"strconv"
)
type Output struct {
text string
rank float32
}
func main() {
dataset := readCsvIntoRecords("sentences.csv")
questions := readCsvIntoRecords("questions.csv")
rankings := make([][]Output, len(questions))
for i, question := range questions {
temp := make([]Output, len(dataset))
for j, sentence := range dataset {
score := question.cosine_similarity(sentence)
temp[j] = Output{text: sentence.metadata["text"].(string), rank: float32(score)}
}
rankings[i] = temp
}
// Sorting for testing
for _, ranks := range rankings {
sort.Slice(ranks, func(i, j int) bool {
return ranks[i].rank > ranks[j].rank
})
}
fmt.Println(rankings)
}
func readCsvIntoRecords(name string) []Record {
file, err := os.Open(name)
if err != nil {
log.Fatal("Cannot read the file", err)
}
r := csv.NewReader(file)
embeddings := []Record{}
for {
row, err := r.Read()
if err == io.EOF {
break
}
tempEmbedding := readRecord(row)
if err != nil {
log.Fatal("Error reading file", err)
}
embeddings = append(embeddings, tempEmbedding)
}
if err := file.Close(); err != nil {
log.Fatal("Not able to close the file")
}
return embeddings
}
func readRecord(row []string) Record {
tempEmbedding := Record{
index: make([]float64, len(row)-1),
metadata: map[string]interface{}{"text": row[0]},
}
for i, value := range row[1:] {
point, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Fatal(err)
}
tempEmbedding.index[i] = point
}
return tempEmbedding
}