forked from sidleal/nilcmetrix
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathranking.go
107 lines (87 loc) · 2.28 KB
/
ranking.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"github.com/sidleal/simpligo-cloze/web/tools/senter"
)
type SimpligoRankingResultItem struct {
Index int `json:"i"`
Sentence string `json:"s"`
Value int `json:"v"`
Color string `json:"c"`
}
func simpligoRankingHandler(w http.ResponseWriter, r *http.Request) {
pInfo := pageInfo
pInfo.ShowResults = false
text := r.FormValue("text")
if text != "" {
nWords := strings.Count(text, " ") + 1
if nWords > 1000 {
ret := "Text is too big. / Texto muito longo."
log.Println(ret)
pInfo.Message = ret
pInfo.ShowMessage = true
} else {
jsonFeatsList := "["
sentList := []string{}
parsed := senter.ParseText(text)
for _, p := range parsed.Paragraphs {
for _, s := range p.Sentences {
_, list, err := callMetrix("_all", s.Text)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "Error "+err.Error())
return
}
jsonFeatsList += metrixResultToJSON(list) + ","
sentList = append(sentList, s.Text)
}
}
jsonFeatsList = strings.Trim(jsonFeatsList, ",") + "]"
list := callSimpligoRanking(jsonFeatsList)
for i, v := range list {
it := SimpligoRankingResultItem{}
it.Index = i + 1
it.Sentence = sentList[i]
floatValue, _ := strconv.ParseFloat(v, 64)
it.Value = int(floatValue * 100)
if it.Value < 26 {
it.Color = "green"
} else if it.Value < 51 {
it.Color = "orange"
} else if it.Value < 76 {
it.Color = "darkorange"
} else {
it.Color = "red"
}
pInfo.SimpligoRankingList = append(pInfo.SimpligoRankingList, it)
}
pInfo.ShowResults = true
pInfo.Text = text
}
}
templateHandler(w, r, "ranking", pInfo)
}
func callSimpligoRanking(featList string) []string {
ret := []string{}
resp, err := http.Post("http://10.11.14.33:5000/api/v1/ranking/ranking3f", "text", bytes.NewReader([]byte(featList)))
if err != nil {
log.Println("Error: " + err.Error())
return ret
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Error reading response: " + err.Error())
return ret
}
log.Println(string(body))
ret = strings.Split(string(body), ",")
return ret
}