/
blueMandoTools_util.go
83 lines (70 loc) · 1.91 KB
/
blueMandoTools_util.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
package main
import (
"fmt"
"github.com/jsoendermann/blueMandoTools/cedict"
"github.com/jsoendermann/blueMandoTools/chinese"
"github.com/jsoendermann/blueMandoTools/moedict"
"net/http"
"strings"
)
func getLastPathComponent(request *http.Request) string {
// get the word or sentence from the path
pathElements := strings.Split(request.URL.Path, "/")
requestData := pathElements[len(pathElements)-1]
return requestData
}
func getColors(request *http.Request) []string {
colors := make([]string, 5)
for i := 0; i <= 4; i++ {
colors[i] = request.FormValue(fmt.Sprintf("tone%d", i))
}
return colors
}
func findMoeEntriesForWords(words []string, charSet chinese.CharSet) ([]moedict.Entry, error) {
moeEntries := make([]moedict.Entry, 0)
// TODO explain diff. between trad and simp
if charSet == chinese.Trad {
for _, word := range words {
entry, err := moedict.FindEntry(word)
if err != nil {
return nil, err
}
if entry != nil {
moeEntries = append(moeEntries, *entry)
}
}
}
// if the sentences is in simplified characters, find cedict records first
// (there may be multiple records per word) and find moedict records using
// the traditional word of the cedict records.
if charSet == chinese.Simp {
for _, word := range words {
records, err := cedict.FindRecords(word, chinese.Simp)
if err != nil {
return nil, err
}
individualRecords := make([]cedict.Record, 0)
for _, r := range records {
recordInSlice := false
for _, ir := range individualRecords {
if r.Trad == ir.Trad {
recordInSlice = true
}
}
if !recordInSlice {
individualRecords = append(individualRecords, r)
}
}
for _, record := range individualRecords {
entry, err := moedict.FindEntry(record.Trad)
if err != nil {
return nil, err
}
if entry != nil {
moeEntries = append(moeEntries, *entry)
}
}
}
}
return moeEntries, nil
}