-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.go
114 lines (90 loc) · 2.61 KB
/
import.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
Copyright © 2022 Samir Gadkari
*/
package cmd
import (
"fmt"
"github.com/find-in-docs/upload/pkg/config"
"github.com/find-in-docs/upload/pkg/data"
"github.com/find-in-docs/upload/pkg/transform"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// importCmd represents the import command
var importCmd = &cobra.Command{
Use: "import",
Short: "Import documents from the given file",
Long: `Specify a file with documents in JSON object form.
If it is a list of documents, don't include the [] list specifiers. ex:
{"review_id": 1, "text": "User review for ID 1"}
{"review_id": 2, "text": "User review for ID 2"}`,
Run: func(cmd *cobra.Command, args []string) {
config.LoadConfig()
stopwords := data.LoadStopwords(viper.GetString("englishStopwordsFile"))
disk := data.DiskSetup()
switch viper.GetString("output.type") {
case config.File.String():
var wordInts []data.WordInt
var wordToInt map[string]data.WordInt
wordsToInts := transform.WordsToInts(stopwords)
for {
v, ok := disk.LoadDoc()
if !ok {
break
}
wordInts, wordToInt = wordsToInts(v.Text)
disk.StoreData(v, wordInts)
}
disk.WriteWordIntMappings(wordToInt)
disk.Close()
case config.Database.String():
dbFunc := data.DBSetup()
var wordInts []data.WordInt
var wordToInt map[string]data.WordInt
tableName := "doc"
if err := dbFunc.OpenConnection(); err != nil {
break
}
if err := dbFunc.CreateTable(tableName); err != nil {
break
}
wordsToInts := transform.WordsToInts(stopwords)
for {
v, ok := disk.LoadDoc()
if !ok {
break
}
wordInts, wordToInt = wordsToInts(v.Text)
v.WordInts = wordInts
if err := dbFunc.StoreData(v, tableName, wordInts); err != nil {
break
}
}
if err := dbFunc.StoreWordIntMappings("word_to_int", wordToInt); err != nil {
break
}
fmt.Printf("Loading docs\n")
inputDocs, err := data.LoadDocs()
if err != nil {
break
}
fmt.Printf("Transforming WordToDocs\n")
if err := transform.WordToDocs(inputDocs, dbFunc.StoreWordToDocMappings); err != nil {
break
}
if err := dbFunc.CloseConnection(); err != nil {
break
}
}
},
}
func init() {
rootCmd.AddCommand(importCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// importCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// importCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}