-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsogoudict_test.go
130 lines (124 loc) · 3.28 KB
/
sogoudict_test.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
120
121
122
123
124
125
126
127
128
129
130
package sogoudict_test
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/caiguanhao/sogoudict"
)
func Test_ErrInvalidDict(t *testing.T) {
_, err := sogoudict.Parse(&bytes.Reader{})
if err != sogoudict.ErrInvalidDict {
t.Error("error should be sogoudict.ErrInvalidDict")
}
}
func Example_parseFile() {
dict, err := sogoudict.ParseFile("test/fixtures/programming.scel")
if err != nil {
fmt.Println(err)
return
}
for _, item := range dict.Items {
fmt.Println(item.Text, strings.Join(item.Abbr, ""), item.Pinyin)
}
// Output:
// 哈希 hx [ha xi]
// 第一类对象 dyldx [di yi lei dui xiang]
// 方法 ff [fang fa]
// 初始化 csh [chu shi hua]
// 伪变量 wbl [wei bian liang]
// 全局变量 qjbl [quan ju bian liang]
// 局部变量 jbbl [ju bu bian liang]
// 实例变量 slbl [shi li bian liang]
// 类变量 lbl [lei bian liang]
// 变量 bl [bian liang]
// 常量 cl [chang liang]
// 析构函数 xghs [xi gou han shu]
// 构造函数 gzhs [gou zao han shu]
// 访问器 fwq [fang wen qi]
// 属性 sx [shu xing]
// 成员方法 cyff [cheng yuan fang fa]
// 成员函数 cyhs [cheng yuan han shu]
// 成员属性 cysx [cheng yuan shu xing]
// 成员 cy [cheng yuan]
// 实例 sl [shi li]
// 函数式 hss [han shu shi]
// 面向过程 mxgc [mian xiang guo cheng]
// 面向对象 mxdx [mian xiang dui xiang]
}
func Example_parseHTTP() {
// http://pinyin.sogou.com/dict/detail/index/33688
url := "http://download.pinyin.sogou.com/dict/download_cell.php?id=33688&name="
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Referer", "http://pinyin.sogou.com/")
resp, err := (&http.Client{}).Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
dict, err := sogoudict.Parse(bytes.NewReader(body))
if err != nil {
fmt.Println(err)
return
}
for _, item := range dict.Items {
fmt.Println(item.Text, strings.Join(item.Abbr, ""), item.Pinyin)
}
// Output:
// 春鸽 cg [chun ge]
// 法克鱿 fky [fa ke you]
// 雅麽蝶 ymd [ya mo die]
// 菊花蚕 jhc [ju hua can]
// 吟稻燕 ydy [yin dao yan]
// 吉跋猫 jbm [ji ba mao]
// 达菲鸡 dfj [da fei ji]
// 潜烈蟹 qlx [qian lie xie]
// 尾申鲸 wsj [wei shen jing]
// 草泥马 cnm [cao ni ma]
}
// https://github.com/caiguanhao/sogoudict/pull/1
func Test_PR1(t *testing.T) {
// this dict file might be partially corrupted, so the total number of items
// in this dict file is slightly less than the one on the web page
// http://pinyin.sogou.com/dict/detail/index/4
url := "http://download.pinyin.sogou.com/dict/download_cell.php?id=4&name="
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Referer", "http://pinyin.sogou.com/")
resp, err := (&http.Client{}).Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
dict, err := sogoudict.Parse(bytes.NewReader(body))
if err != nil {
fmt.Println(err)
return
}
if err != nil {
t.Error("error", err)
} else if len(dict.Items) < 8000 {
t.Error("bad items count", len(dict.Items))
}
}