-
Notifications
You must be signed in to change notification settings - Fork 4
/
varnam_learn.go
119 lines (93 loc) · 3.11 KB
/
varnam_learn.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
115
116
117
118
119
package main
import (
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/labstack/echo/v4"
"github.com/varnamproject/govarnam/govarnamgo"
)
const defaultChanSize = 1000
var (
learnChannels map[string]chan string
trainChannel map[string]chan trainArgs
)
// initChannels method will initialize learn and train channels.
func (app *App) initChannels() {
learnChannels = make(map[string]chan string)
trainChannel = make(map[string]chan trainArgs)
for _, scheme := range schemeDetails {
learnChannels[scheme.Identifier] = make(chan string, defaultChanSize)
trainChannel[scheme.Identifier] = make(chan trainArgs, defaultChanSize)
handle, err := govarnamgo.InitFromID(scheme.Identifier)
if err != nil {
app.log.Fatal("Unable to initialize varnam for lang", scheme.LangCode)
}
go app.listenForWords(scheme.Identifier, handle)
}
}
func (app *App) listenForWords(lang string, handle *govarnamgo.VarnamHandle) {
for {
select {
case word := <-learnChannels[lang]:
if err := handle.Learn(strings.TrimSpace(word), 0); err != nil {
app.log.Printf("Failed to learn %s. %s\n", word, err.Error())
}
case args := <-trainChannel[lang]:
if err := handle.Train(strings.TrimSpace(args.Pattern), strings.TrimSpace(args.Word)); err != nil {
app.log.Printf("error training word: %s, pattern: %s, err:%s", args.Word, args.Pattern, err.Error())
}
}
}
}
func learnWordsFromFile(c echo.Context, langCode string, fileToLearn string, removeFile bool) {
c.Response().WriteHeader(http.StatusOK)
start := time.Now()
sendOutput := func(msg string) {
_, _ = c.Response().Write([]byte(msg))
c.Response().Flush()
}
sendOutput(fmt.Sprintf("Learning from %s\n", fileToLearn))
_, _ = getOrCreateHandler(langCode, func(handle *govarnamgo.VarnamHandle) (data interface{}, err error) {
learnStatus, verr := handle.LearnFromFile(fileToLearn)
end := time.Now()
if verr != nil {
sendOutput(fmt.Sprintf("Error learning: '%s'\n", verr.Error()))
} else {
sendOutput(fmt.Sprintf("Finished Learning. TotalWords: %d, Failed: %d. Took %s\n", learnStatus.TotalWords, learnStatus.FailedWords, end.Sub(start)))
}
if removeFile {
if err = os.Remove(fileToLearn); err != nil {
sendOutput(fmt.Sprintf("Error deleting '%s'. %s\n", fileToLearn, err.Error()))
}
}
return
})
}
func importLearningsFromFile(c echo.Context, langCode string, fileToImport string, removeFile bool) error {
c.Response().WriteHeader(http.StatusOK)
start := time.Now()
sendOutput := func(msg string) {
_, _ = c.Response().Write([]byte(msg))
c.Response().Flush()
}
sendOutput(fmt.Sprintf("Importing from %s\n", fileToImport))
var importError error
_, _ = getOrCreateHandler(langCode, func(handle *govarnamgo.VarnamHandle) (data interface{}, err error) {
err = handle.Import(fileToImport)
end := time.Now()
if err != nil {
importError = err
} else {
sendOutput(fmt.Sprintf("Import completed. Took %s\n", end.Sub(start)))
}
if removeFile {
if err = os.Remove(fileToImport); err != nil {
sendOutput(fmt.Sprintf("Error deleting '%s'. %s\n", fileToImport, err.Error()))
}
}
return
})
return importError
}