-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultitran.go
77 lines (70 loc) · 1.37 KB
/
multitran.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
package main
import (
"code.google.com/p/go-charset/charset"
_ "code.google.com/p/go-charset/data"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
)
const (
MEANINGS_COUNT = 3
)
func loadUrl(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
t, err := charset.TranslatorFrom("windows-1251")
if err != nil {
return "", err
}
_, translatedBytes, err := t.Translate(bodyBytes, true)
if err != nil {
return "", err
}
return string(translatedBytes), nil
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run multitran.go <word>")
return
}
arg_word := os.Args[1]
x, err := loadUrl("http://www.multitran.ru/c/m.exe?l1=1&l2=2&s=" + arg_word)
if err == nil {
ind := strings.Index(x, `title="Общая лексика"`)
if ind == -1 {
return
}
tail := x[ind:]
ind = strings.Index(tail, "<td>")
if ind == -1 {
return
}
tail = tail[ind:]
ind = strings.Index(tail, "</td>")
if ind == -1 {
return
}
tail = tail[:ind]
re := regexp.MustCompile(`<[^>]+>`)
tail = re.ReplaceAllString(tail, "")
words := strings.Split(tail, "; ")
fmt.Print(arg_word, "\t")
for i, w := range words {
fmt.Print(w, ";")
if i == MEANINGS_COUNT-1 {
break
}
}
fmt.Println()
}
}