cambridge_dict 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +7 -3
- data/lib/cambridge_dict/version.rb +1 -1
- data/lib/cambridge_dict.rb +18 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b1a1cd7771bcdca2f9784cbcb7d68bb65d685e06b5387a9ac1c1090529339f0
|
4
|
+
data.tar.gz: fa35b9a0d9c7640b1a4f71c0e01ec0ab913a9781026e9ed4ecaecd2341aebefe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd1fac189c809b80bb3938a29119efba3e834c4c9149caa4a8377ddee9eb1d6fbda8003bbd8c90434a85f7f632144113f7540774758374ff0a555a1925a19bf5
|
7
|
+
data.tar.gz: c338c5002025478c39951420698eee13cd2f4db41d28dd1cc9d90fafbb81c2cc04001935302c4d14033c0e1c3e56512c3b50a4695e15afdcb69eadc5557f157b
|
data/README.md
CHANGED
@@ -12,13 +12,13 @@ gem install cambridge_dict
|
|
12
12
|
|
13
13
|
```
|
14
14
|
# throws error
|
15
|
-
|
15
|
+
result = CambridgeDict::Client.lookup! "hello", language: "en"
|
16
16
|
|
17
17
|
# returns nil if not found
|
18
|
-
|
18
|
+
result = CambridgeDict::Client.lookup "hello", language: "en"
|
19
19
|
|
20
20
|
# can also create instance
|
21
|
-
client =
|
21
|
+
client = CambridgeDict::Client.new
|
22
22
|
result = client.lookup("back", language: "en")
|
23
23
|
|
24
24
|
return if result.nil?
|
@@ -64,6 +64,10 @@ return if result.nil?
|
|
64
64
|
:source=>"cald4-us",
|
65
65
|
:text=>"in return: ",
|
66
66
|
:translation=>nil,
|
67
|
+
|
68
|
+
# sometimes words is returned
|
69
|
+
result = client.lookup("a", language: "en")
|
70
|
+
result[:words] => ["A", "a"]
|
67
71
|
```
|
68
72
|
|
69
73
|
## Webserver Setup
|
data/lib/cambridge_dict.rb
CHANGED
@@ -37,17 +37,23 @@ module CambridgeDict
|
|
37
37
|
encoded_word = PARSER.escape(word)
|
38
38
|
wiki_verbs = fetch_verbs("#{@wiki_base}/#{encoded_word}")
|
39
39
|
|
40
|
-
|
40
|
+
words = extract_word(doc)
|
41
41
|
|
42
|
-
return nil if
|
42
|
+
return nil if words.nil?
|
43
43
|
|
44
|
-
{
|
45
|
-
word:
|
44
|
+
resp = {
|
45
|
+
word: words&.last,
|
46
46
|
pos: extract_pos(doc),
|
47
47
|
verbs: wiki_verbs,
|
48
48
|
pronunciations: extract_pronunciation(doc),
|
49
49
|
definitions: extract_definitions(doc)
|
50
50
|
}
|
51
|
+
|
52
|
+
if words.size > 1
|
53
|
+
resp[:words] = words
|
54
|
+
end
|
55
|
+
|
56
|
+
resp
|
51
57
|
end
|
52
58
|
|
53
59
|
def lookup!(word, language: 'en')
|
@@ -86,7 +92,7 @@ module CambridgeDict
|
|
86
92
|
doc = Nokogiri::HTML(response.body)
|
87
93
|
verbs = []
|
88
94
|
|
89
|
-
rows = doc.css('tr > td > p')
|
95
|
+
rows = doc.css('table:not(.toccolours) tr > td > p')
|
90
96
|
|
91
97
|
lines = rows.map {|i| i.children.collect {|c| c.text } }.flatten.map(&:strip).reject(&:empty?)
|
92
98
|
|
@@ -99,7 +105,7 @@ module CambridgeDict
|
|
99
105
|
end
|
100
106
|
|
101
107
|
def extract_word(doc)
|
102
|
-
doc.at_css('.superentry .tb')&.text
|
108
|
+
doc.at_css('.superentry .tb')&.text&.split ", "
|
103
109
|
end
|
104
110
|
|
105
111
|
def extract_pos(doc)
|
@@ -147,13 +153,14 @@ module CambridgeDict
|
|
147
153
|
|
148
154
|
pos = entry&.at_css('.pos.dpos')&.text
|
149
155
|
headword = entry&.at_css('.headword .hw')&.text
|
156
|
+
text = clean_string block.at_css('.def.ddef_d.db')&.text&.strip
|
150
157
|
|
151
158
|
definition = {
|
152
159
|
id: index,
|
153
160
|
headword: headword,
|
154
161
|
pos: pos,
|
155
162
|
source: source || nil,
|
156
|
-
text:
|
163
|
+
text: text,
|
157
164
|
translation: block.at_css('.def-body.ddef_b > span.trans.dtrans')&.text&.strip,
|
158
165
|
example: extract_examples(block)
|
159
166
|
}
|
@@ -177,6 +184,10 @@ module CambridgeDict
|
|
177
184
|
|
178
185
|
examples
|
179
186
|
end
|
187
|
+
|
188
|
+
def clean_string(input)
|
189
|
+
input&.gsub(/\s+/, ' ')&.strip
|
190
|
+
end
|
180
191
|
end
|
181
192
|
|
182
193
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cambridge_dict
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben D'Angelo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|