This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbing-dictionary.go
120 lines (113 loc) · 3.12 KB
/
bing-dictionary.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
package service4mydictionary
import (
"net/url"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/zzc-tongji/vocabulary4mydictionary"
)
const (
bingDictionaryName = "Bing Dictionary"
different = "different"
)
// BingDictionaryStruct : Bing Dictionary struct
type BingDictionaryStruct struct {
cache CacheStruct
}
// GetServiceName : get service name
func (service *BingDictionaryStruct) GetServiceName() (value string) {
value = bingDictionaryName
return
}
// GetCache : get cache
func (service *BingDictionaryStruct) GetCache() (cache *CacheStruct) {
cache = &service.cache
return
}
// Query : query vocabulary
func (service *BingDictionaryStruct) Query(vocabularyAsk vocabulary4mydictionary.VocabularyAskStruct) (vocabularyAnswer vocabulary4mydictionary.VocabularyAnswerStruct) {
var (
err error
queryString string
item CacheItemStruct
document *goquery.Document
selection1 *goquery.Selection
selection2 *goquery.Selection
selection3 *goquery.Selection
counter int
)
// set
vocabularyAnswer.SourceName = bingDictionaryName
vocabularyAnswer.Location.TableType = vocabulary4mydictionary.Online
vocabularyAnswer.Location.TableIndex = -1
vocabularyAnswer.Location.ItemIndex = -1
// query cache
queryString = url.QueryEscape(vocabularyAsk.Word)
item, err = service.cache.Query(queryString)
if err == nil {
goto SET
}
// query online
// get page
document, err = goquery.NewDocument("https://cn.bing.com/dict/search?q=" + queryString)
if err != nil {
item.Status = err.Error()
goto ADD
}
selection1 = document.Find(".qdef")
if selection1.Nodes == nil {
item.Status = "null: BD01"
goto ADD
}
// get word
selection2 = selection1.Find("#headword")
if selection2.Nodes == nil {
item.Status = "null: BD02"
goto ADD
}
item.Word = selection2.Text()
// get define
selection3 = selection1.Find("ul").Find("li")
if selection3.Nodes == nil {
item.Status = "null: BD03"
goto ADD
}
for i := 0; i < selection3.Size(); i++ {
item.Definition = append(item.Definition, selection3.Eq(i).Find(".pos").Text()+" "+selection3.Eq(i).Find(".def").Text())
}
if len(item.Definition) == 1 && strings.Contains(item.Definition[0], "网络 ") {
item.Status = "null: BD04"
goto ADD
}
counter = 0
for i := 0; i < len(item.Definition); i++ {
if strings.Contains(item.Definition[0], "的过去式") ||
strings.Contains(item.Definition[0], "的过去分词") ||
strings.Contains(item.Definition[0], "的现在分词") ||
strings.Contains(item.Definition[0], "的复数") {
counter++
}
}
if (len(item.Definition) > 1 && counter >= len(item.Definition)-1) ||
(len(item.Definition) == 1 && counter == 1) {
item.Status = "participle"
goto ADD
}
// mark difference
if strings.Compare(document.Find(".in_tip").Text(), "") != 0 {
item.Status = different
} else {
item.Status = vocabulary4mydictionary.Basic
}
ADD:
// add to cache
item.QueryString = queryString
item.CreationTime = time.Now().Unix()
service.cache.Add(item)
SET:
// set
vocabularyAnswer.Word = item.Word
vocabularyAnswer.Definition = item.Definition
vocabularyAnswer.Status = item.Status
return
}