-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (94 loc) · 2.76 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
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
package main
import (
"os"
"flag"
"net/http"
"log"
"encoding/json"
"strconv"
)
const (
ACTION_START_LISTEN string = "start"
ACTION_GENPROFILE string = "genprofile"
)
var detection *Detector
var defaultLanguages []string
type Cfg struct {
Action string
XmlPath string
ProfilePath string
Profile string
Languages []string
Port uint
}
var defaultConfig = Cfg {
Action: "start",
XmlPath: "xml",
ProfilePath: "profiles",
Profile: "main",
Languages: []string{"all"},
Port: 3000,
}
type httpRequestStruct struct {
Text string `json:"text"`
Languages []string `json:"langs"`
Coefficients map[string]float64 `json:"coef"`
MaxTrials int `json:"max_trials"`
MaxIterations uint16 `json:"max_iterations"`
}
func init() {
flag.StringVar(&defaultConfig.ProfilePath, "profile-path", "profiles", "Path to profiles directory")
flag.StringVar(&defaultConfig.XmlPath, "xml-path", "xml", "Path to wiki abstract DB directory")
flag.StringVar(&defaultConfig.Profile, "profile", "main_3_7", "Profile name to be loaded")
flag.UintVar(&defaultConfig.Port, "port", 3000, "Port of HTTP listener")
flag.Parse()
defaultConfig.Action = flag.Arg(0)
if len(defaultConfig.Action) == 0 {
defaultConfig.Action = ACTION_START_LISTEN
}
detectConfig := DetectConfig{
Profile: defaultConfig.Profile,
XmlPath: defaultConfig.XmlPath,
ProfilePath: defaultConfig.ProfilePath,
Languages: defaultConfig.Languages,
}
detection = NewDetector(detectConfig)
defaultLanguages = make([]string, len(detection.classes),len(detection.classes))
counter := 0
for lang := range detection.classes {
defaultLanguages[counter] = lang
counter++
}
}
func main() {
if defaultConfig.Action == ACTION_GENPROFILE {
detection.GenerateProfileFromWikiXML()
//detection.SaveProfiles()
os.Exit(0)
}
if defaultConfig.Action == ACTION_START_LISTEN {
http.HandleFunc("/", detectLanguageHandler)
log.Fatal(http.ListenAndServe(":" + strconv.Itoa(int(defaultConfig.Port)), nil))
}
}
func detectLanguageHandler(rw http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
rw.Header().Set("Content-Type", "application/json")
var err error
requestData := httpRequestStruct{}
decoder := json.NewDecoder(req.Body)
err = decoder.Decode(&requestData)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
if len(requestData.Languages) == 0 || (len(requestData.Languages) == 1 && requestData.Languages[0] == ALL_LANGUAGES ) {
requestData.Languages = defaultLanguages
}
response := detection.Detect(requestData.Text, requestData.Languages, requestData.Coefficients, requestData.MaxTrials, requestData.MaxIterations);
err = json.NewEncoder(rw).Encode(response)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
}