Skip to content

Commit f03355c

Browse files
committed
merge package
1 parent 596d76a commit f03355c

15 files changed

+601
-65
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ Return values:
236236
#### 3.3. Query
237237

238238
```go
239-
func Query(vocabularyAsk vocabulary4mydictionary.VocabularyAskStruct) (success bool, vocabularyResult vocabulary4mydictionary.VocabularyResultStruct)
239+
func Query(vocabularyAsk VocabularyAskStruct) (success bool, vocabularyResult VocabularyResultStruct)
240240
```
241241

242242
It is the core function of MYDICTIONARY.
@@ -274,7 +274,7 @@ Return values:
274274
#### 3.5. Edit
275275

276276
```go
277-
func Edit(vocabularyEdit vocabulary4mydictionary.VocabularyEditStruct) (success bool, information string)
277+
func Edit(vocabularyEdit VocabularyEditStruct) (success bool, information string)
278278
```
279279

280280
This function is used to edit definitions and notes of a *vocabulary* in *collection* or *dictionary*. It **is NOT restricted by `"writable"` in *configuration***.

README.zh-Hans.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func CheckNetwork() (success bool, information string)
236236
#### 3.3. Query
237237

238238
```go
239-
func Query(vocabularyAsk vocabulary4mydictionary.VocabularyAskStruct) (success bool, vocabularyResult vocabulary4mydictionary.VocabularyResultStruct)
239+
func Query(vocabularyAsk VocabularyAskStruct) (success bool, vocabularyResult VocabularyResultStruct)
240240
```
241241

242242
该函数是MYDICTIONARY的核心。
@@ -274,7 +274,7 @@ func Save() (success bool, information string)
274274
#### 3.5. Edit
275275

276276
```go
277-
func Edit(vocabularyEdit vocabulary4mydictionary.VocabularyEditStruct) (success bool, information string)
277+
func Edit(vocabularyEdit VocabularyEditStruct) (success bool, information string)
278278
```
279279

280280
该函数用于编辑*生词本*或*离线词典*中*词条*的释义和笔记。该函数**不受*配置*中`"writable"`的制约。**

bing-dictionary.go

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package mydictionary
2+
3+
import (
4+
"net/url"
5+
"strings"
6+
"time"
7+
8+
"github.com/PuerkitoBio/goquery"
9+
)
10+
11+
const (
12+
bingDictionaryName = "Bing Dictionary"
13+
different = "different"
14+
)
15+
16+
// BingDictionaryStruct : Bing Dictionary struct
17+
type BingDictionaryStruct struct {
18+
cache CacheStruct
19+
}
20+
21+
// GetServiceName : get service name
22+
func (service *BingDictionaryStruct) GetServiceName() (value string) {
23+
value = bingDictionaryName
24+
return
25+
}
26+
27+
// GetCache : get cache
28+
func (service *BingDictionaryStruct) GetCache() (cache *CacheStruct) {
29+
cache = &service.cache
30+
return
31+
}
32+
33+
// Query : query vocabulary
34+
func (service *BingDictionaryStruct) Query(vocabularyAsk VocabularyAskStruct) (vocabularyAnswer VocabularyAnswerStruct) {
35+
var (
36+
err error
37+
queryString string
38+
item CacheItemStruct
39+
document *goquery.Document
40+
selection1 *goquery.Selection
41+
selection2 *goquery.Selection
42+
selection3 *goquery.Selection
43+
counter int
44+
)
45+
// set
46+
vocabularyAnswer.SourceName = bingDictionaryName
47+
vocabularyAnswer.Location.TableType = Online
48+
vocabularyAnswer.Location.TableIndex = -1
49+
vocabularyAnswer.Location.ItemIndex = -1
50+
// query cache
51+
queryString = url.QueryEscape(vocabularyAsk.Word)
52+
item, err = service.cache.Query(queryString)
53+
if err == nil {
54+
goto SET
55+
}
56+
// query online
57+
// get page
58+
document, err = goquery.NewDocument("https://cn.bing.com/dict/search?q=" + queryString)
59+
if err != nil {
60+
item.Status = err.Error()
61+
goto ADD
62+
}
63+
selection1 = document.Find(".qdef")
64+
if selection1.Nodes == nil {
65+
item.Status = "null: BD01"
66+
goto ADD
67+
}
68+
// get word
69+
selection2 = selection1.Find("#headword")
70+
if selection2.Nodes == nil {
71+
item.Status = "null: BD02"
72+
goto ADD
73+
}
74+
item.Word = selection2.Text()
75+
// get define
76+
selection3 = selection1.Find("ul").Find("li")
77+
if selection3.Nodes == nil {
78+
item.Status = "null: BD03"
79+
goto ADD
80+
}
81+
for i := 0; i < selection3.Size(); i++ {
82+
item.Definition = append(item.Definition, selection3.Eq(i).Find(".pos").Text()+" "+selection3.Eq(i).Find(".def").Text())
83+
}
84+
if len(item.Definition) == 1 && strings.Contains(item.Definition[0], "网络 ") {
85+
item.Status = "null: BD04"
86+
goto ADD
87+
}
88+
counter = 0
89+
for i := 0; i < len(item.Definition); i++ {
90+
if strings.Contains(item.Definition[0], "的过去式") ||
91+
strings.Contains(item.Definition[0], "的过去分词") ||
92+
strings.Contains(item.Definition[0], "的现在分词") ||
93+
strings.Contains(item.Definition[0], "的复数") {
94+
counter++
95+
}
96+
}
97+
if (len(item.Definition) > 1 && counter >= len(item.Definition)-1) ||
98+
(len(item.Definition) == 1 && counter == 1) {
99+
item.Status = "participle"
100+
goto ADD
101+
}
102+
// mark difference
103+
if strings.Compare(document.Find(".in_tip").Text(), "") != 0 {
104+
item.Status = different
105+
} else {
106+
item.Status = Basic
107+
}
108+
ADD:
109+
// add to cache
110+
item.QueryString = queryString
111+
item.CreationTime = time.Now().Unix()
112+
service.cache.Add(item)
113+
SET:
114+
// set
115+
vocabularyAnswer.Word = item.Word
116+
vocabularyAnswer.Definition = item.Definition
117+
vocabularyAnswer.Status = item.Status
118+
return
119+
}

cache.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package mydictionary
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
"strings"
9+
"time"
10+
)
11+
12+
// CacheItemStruct : item struct
13+
type CacheItemStruct struct {
14+
QueryString string `json:"queryString"`
15+
Word string `json:"word"`
16+
Definition []string `json:"definition"`
17+
Status string `json:"status"`
18+
CreationTime int64 `json:"creationTime"`
19+
}
20+
21+
// CacheStruct : cache struct
22+
type CacheStruct struct {
23+
path string
24+
shelfLifeDay int64
25+
Content []CacheItemStruct `json:"content"`
26+
}
27+
28+
// Read : read cache
29+
func (cache *CacheStruct) Read(path string, shelfLifeDay int64) (err error) {
30+
var (
31+
buf []byte
32+
watershed int64
33+
)
34+
// set
35+
cache.path = path
36+
cache.shelfLifeDay = shelfLifeDay
37+
// read
38+
buf, err = ioutil.ReadFile(cache.path)
39+
if err != nil {
40+
if os.IsNotExist(err) {
41+
return nil
42+
}
43+
return
44+
}
45+
err = json.Unmarshal(buf, cache)
46+
if err != nil {
47+
return
48+
}
49+
// remove item before watershed
50+
if cache.shelfLifeDay > 0 {
51+
watershed = time.Now().Unix() - cache.shelfLifeDay*24*60*60
52+
for i := len(cache.Content) - 1; i >= 0; i-- {
53+
if cache.Content[i].CreationTime < watershed {
54+
cache.Content = cache.Content[i+1:]
55+
break
56+
}
57+
}
58+
}
59+
return
60+
}
61+
62+
// Query : query item in cache
63+
func (cache *CacheStruct) Query(queryString string) (item CacheItemStruct, err error) {
64+
for i := 0; i < len(cache.Content); i++ {
65+
if strings.Compare(cache.Content[i].QueryString, queryString) == 0 {
66+
item = cache.Content[i]
67+
return
68+
}
69+
}
70+
err = fmt.Errorf("non-existent")
71+
return
72+
}
73+
74+
// Add : add item to cache
75+
func (cache *CacheStruct) Add(item CacheItemStruct) {
76+
cache.Content = append(cache.Content, item)
77+
}
78+
79+
// Write : write cache
80+
func (cache *CacheStruct) Write() (information string, err error) {
81+
var (
82+
buf []byte
83+
path string
84+
)
85+
// write
86+
buf, err = json.MarshalIndent(cache, "", "\t")
87+
if err != nil {
88+
return
89+
}
90+
os.Remove(path)
91+
err = ioutil.WriteFile(cache.path, buf, 0644)
92+
if err != nil {
93+
return
94+
}
95+
// output
96+
information = fmt.Sprintf("Cache \"%s\" has been updated.\n\n", cache.path)
97+
return
98+
}

collection.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
"github.com/360EntSecGroup-Skylar/excelize"
10-
"github.com/zzc-tongji/vocabulary4mydictionary"
1110
)
1211

1312
// collection
@@ -20,7 +19,7 @@ type collectionStruct struct {
2019
xlsx *excelize.File
2120
sheetName string
2221
columnIndex map[string]int
23-
content []vocabulary4mydictionary.VocabularyAnswerStruct
22+
content []VocabularyAnswerStruct
2423
}
2524

2625
// open and check .xlsx file
@@ -169,7 +168,7 @@ RECHECK:
169168
func (collection *collectionStruct) read(filePath string) (err error) {
170169
var (
171170
str string
172-
vocabularyAnswer vocabulary4mydictionary.VocabularyAnswerStruct
171+
vocabularyAnswer VocabularyAnswerStruct
173172
)
174173
if collection.readable {
175174
// check
@@ -178,7 +177,7 @@ func (collection *collectionStruct) read(filePath string) (err error) {
178177
return
179178
}
180179
// get space of content
181-
collection.content = make([]vocabulary4mydictionary.VocabularyAnswerStruct, 0)
180+
collection.content = make([]VocabularyAnswerStruct, 0)
182181
// ram image -> content
183182
for i := 2; ; i++ {
184183
// `xlsx:wd` -> .Word
@@ -219,7 +218,7 @@ func (collection *collectionStruct) read(filePath string) (err error) {
219218
}
220219
// others
221220
vocabularyAnswer.SourceName = collection.name
222-
vocabularyAnswer.Location.TableType = vocabulary4mydictionary.Collection
221+
vocabularyAnswer.Location.TableType = Collection
223222
vocabularyAnswer.Status = ""
224223
// add to collection
225224
collection.content = append(collection.content, vocabularyAnswer)
@@ -263,9 +262,9 @@ func (collection *collectionStruct) write() (information string, err error) {
263262
}
264263

265264
// query and update
266-
func (collection *collectionStruct) queryAndUpdate(vocabularyAsk vocabulary4mydictionary.VocabularyAskStruct) (vocabularyAnswerList []vocabulary4mydictionary.VocabularyAnswerStruct) {
265+
func (collection *collectionStruct) queryAndUpdate(vocabularyAsk VocabularyAskStruct) (vocabularyAnswerList []VocabularyAnswerStruct) {
267266
var (
268-
vocabularyAnswer vocabulary4mydictionary.VocabularyAnswerStruct
267+
vocabularyAnswer VocabularyAnswerStruct
269268
tm time.Time
270269
)
271270
if collection.readable {
@@ -283,7 +282,7 @@ func (collection *collectionStruct) queryAndUpdate(vocabularyAsk vocabulary4mydi
283282
}
284283
}
285284
vocabularyAnswer = collection.content[i]
286-
vocabularyAnswer.Status = vocabulary4mydictionary.Basic
285+
vocabularyAnswer.Status = Basic
287286
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
288287
if vocabularyAsk.Advance {
289288
continue
@@ -295,22 +294,22 @@ func (collection *collectionStruct) queryAndUpdate(vocabularyAsk vocabulary4mydi
295294
// advance
296295
if strings.Contains(collection.content[i].Word, vocabularyAsk.Word) {
297296
vocabularyAnswer = collection.content[i]
298-
vocabularyAnswer.Status = vocabulary4mydictionary.Advance
297+
vocabularyAnswer.Status = Advance
299298
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
300299
goto ADVANCE_END
301300
}
302301
for j := 0; j < len(collection.content[i].Definition); j++ {
303302
if strings.Contains(collection.content[i].Definition[j], vocabularyAsk.Word) {
304303
vocabularyAnswer = collection.content[i]
305-
vocabularyAnswer.Status = vocabulary4mydictionary.Advance
304+
vocabularyAnswer.Status = Advance
306305
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
307306
goto ADVANCE_END
308307
}
309308
}
310309
for j := 0; j < len(collection.content[i].Note); j++ {
311310
if strings.Contains(collection.content[i].Note[j], vocabularyAsk.Word) {
312311
vocabularyAnswer = collection.content[i]
313-
vocabularyAnswer.Status = vocabulary4mydictionary.Advance
312+
vocabularyAnswer.Status = Advance
314313
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
315314
goto ADVANCE_END
316315
}
@@ -323,21 +322,21 @@ func (collection *collectionStruct) queryAndUpdate(vocabularyAsk vocabulary4mydi
323322
}
324323

325324
// add vocabulary to collection
326-
func (collection *collectionStruct) add(vocabularyAnswerList []vocabulary4mydictionary.VocabularyAnswerStruct) {
325+
func (collection *collectionStruct) add(vocabularyAnswerList []VocabularyAnswerStruct) {
327326
var (
328327
existent bool
329328
index int
330-
vocabularyAnswer vocabulary4mydictionary.VocabularyAnswerStruct
329+
vocabularyAnswer VocabularyAnswerStruct
331330
tm time.Time
332331
)
333332
if collection.readable && collection.writable {
334333
// only available for collection which is readable and writable
335334
existent = false
336335
index = -1
337336
for i := 0; i < len(vocabularyAnswerList); i++ {
338-
if strings.Compare(vocabularyAnswerList[i].Status, vocabulary4mydictionary.Basic) == 0 {
337+
if strings.Compare(vocabularyAnswerList[i].Status, Basic) == 0 {
339338
// only for vocabulary with define from basic query
340-
if vocabularyAnswerList[i].Location.TableType == vocabulary4mydictionary.Online {
339+
if vocabularyAnswerList[i].Location.TableType == Online {
341340
// from online: check whether online source index match or not
342341
if strings.Compare(vocabularyAnswerList[i].SourceName, collection.onlineSource) == 0 {
343342
index = i
@@ -357,7 +356,7 @@ func (collection *collectionStruct) add(vocabularyAnswerList []vocabulary4mydict
357356
vocabularyAnswer.QueryCounter = 1
358357
vocabularyAnswer.QueryTime = fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", tm.Year(), tm.Month(), tm.Day(), tm.Hour(), tm.Minute(), tm.Second())
359358
vocabularyAnswer.SourceName = collection.name
360-
vocabularyAnswer.Location.TableType = vocabulary4mydictionary.Collection
359+
vocabularyAnswer.Location.TableType = Collection
361360
vocabularyAnswer.Status = ""
362361
// add
363362
collection.content = append(collection.content, vocabularyAnswer)

0 commit comments

Comments
 (0)