ingreedy 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -6
- data/lib/ingreedy.rb +4 -0
- data/lib/ingreedy/amount_parser.rb +1 -5
- data/lib/ingreedy/root_parser.rb +5 -1
- data/lib/ingreedy/version.rb +1 -1
- data/spec/ingreedy_spec.rb +59 -52
- metadata +3 -4
- data/.rvmrc +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 452f17ed3a15ae973346ea074ee682bf173d1663
|
4
|
+
data.tar.gz: 6face6a2f60f6506160d815c69cd0546107fb787
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: acdc3c1f4f4c135188c9c92e4a83441d569539f4a9b0af31cdfd08e8ca653ce81689dff985d72814d8b71f10f59b4d1eb936cf46f6d35e99aeb1a0b31cc42275
|
7
|
+
data.tar.gz: b0801f2d7eb26f02eb14cd615102b00dfe5eb695ed03f3b53dd32bf2f62c2df1cc70ae5cebcd552634403de39dc94c3d859d1271b31ba8f3930ad77d03668421
|
data/.travis.yml
CHANGED
data/lib/ingreedy.rb
CHANGED
@@ -5,6 +5,8 @@ require File.join(path, "ingreedy_parser")
|
|
5
5
|
require File.join(path, "dictionary_collection")
|
6
6
|
|
7
7
|
module Ingreedy
|
8
|
+
ParseFailed = Class.new(StandardError)
|
9
|
+
|
8
10
|
def self.locale
|
9
11
|
@locale ||= nil
|
10
12
|
end
|
@@ -16,6 +18,8 @@ module Ingreedy
|
|
16
18
|
def self.parse(query)
|
17
19
|
parser = Parser.new(query)
|
18
20
|
parser.parse
|
21
|
+
rescue Parslet::ParseFailed => e
|
22
|
+
fail ParseFailed.new(e.message), e.backtrace
|
19
23
|
end
|
20
24
|
|
21
25
|
def self.dictionaries
|
@@ -47,15 +47,11 @@ module Ingreedy
|
|
47
47
|
word_digits.map { |d| stri(d) }.inject(:|) || any
|
48
48
|
end
|
49
49
|
|
50
|
-
rule(:amount_unit_separator) do
|
51
|
-
whitespace | str("-")
|
52
|
-
end
|
53
|
-
|
54
50
|
rule(:amount) do
|
55
51
|
fraction |
|
56
52
|
float.as(:float_amount) |
|
57
53
|
integer.as(:integer_amount) |
|
58
|
-
word_digit.as(:word_integer_amount) >>
|
54
|
+
word_digit.as(:word_integer_amount) >> whitespace
|
59
55
|
end
|
60
56
|
|
61
57
|
root(:amount)
|
data/lib/ingreedy/root_parser.rb
CHANGED
@@ -3,11 +3,15 @@ module Ingreedy
|
|
3
3
|
rule(:range) do
|
4
4
|
AmountParser.new.as(:amount) >>
|
5
5
|
whitespace.maybe >>
|
6
|
-
|
6
|
+
range_separator >>
|
7
7
|
whitespace.maybe >>
|
8
8
|
AmountParser.new.as(:amount_end)
|
9
9
|
end
|
10
10
|
|
11
|
+
rule(:range_separator) do
|
12
|
+
str("-") | str("~")
|
13
|
+
end
|
14
|
+
|
11
15
|
rule(:amount) do
|
12
16
|
AmountParser.new.as(:amount)
|
13
17
|
end
|
data/lib/ingreedy/version.rb
CHANGED
data/spec/ingreedy_spec.rb
CHANGED
@@ -1,46 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require "spec_helper"
|
3
3
|
|
4
|
-
describe Ingreedy do
|
5
|
-
context "amount parsing" do
|
6
|
-
{
|
7
|
-
"1 cup flour" => 1,
|
8
|
-
"one cup flour" => 1,
|
9
|
-
"1 1/2 cups flour" => "3/2",
|
10
|
-
"¼ cups flour" => "1/4",
|
11
|
-
"1 ½ cups flour" => "3/2",
|
12
|
-
"1½ cups flour" => "3/2",
|
13
|
-
"1.0 cup flour" => 1,
|
14
|
-
"1.5 cups flour" => "3/2",
|
15
|
-
"1,5 cups flour" => "3/2",
|
16
|
-
"1 2/3 cups flour" => "5/3",
|
17
|
-
"1 (28 ounce) can crushed tomatoes" => 1,
|
18
|
-
"2 (28 ounce) can crushed tomatoes" => 2,
|
19
|
-
"3 28 ounce can crushed tomatoes" => 3,
|
20
|
-
"one 28 ounce can crushed tomatoes" => 1,
|
21
|
-
"two five-ounce can crushed tomatoes" => 2,
|
22
|
-
"two 28 ounce cans crushed tomatoes" => 2,
|
23
|
-
"three 28 ounce cans crushed tomatoes" => 3,
|
24
|
-
"1/2 cups flour" => "1/2",
|
25
|
-
".25 cups flour" => "1/4",
|
26
|
-
"12oz tequila" => 12,
|
27
|
-
"1 banana" => 1,
|
28
|
-
}.each do |query, expected|
|
29
|
-
it "parses the correct amount as a rational" do
|
30
|
-
expect(Ingreedy.parse(query)).to parse_the_amount(expected.to_r)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context "unit parsing" do
|
36
|
-
context "(english)" do
|
37
|
-
end
|
38
|
-
context "(metric)" do
|
39
|
-
end
|
40
|
-
context "(nonstandard)" do
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
4
|
+
describe Ingreedy, ".parse" do
|
44
5
|
it "parses a simple example correctly" do
|
45
6
|
result = Ingreedy.parse("1 lb potatoes")
|
46
7
|
|
@@ -50,7 +11,37 @@ describe Ingreedy do
|
|
50
11
|
end
|
51
12
|
end
|
52
13
|
|
53
|
-
describe "
|
14
|
+
describe Ingreedy, "amount parsing" do
|
15
|
+
{
|
16
|
+
"1 cup flour" => 1,
|
17
|
+
"one cup flour" => 1,
|
18
|
+
"1 1/2 cups flour" => "3/2",
|
19
|
+
"¼ cups flour" => "1/4",
|
20
|
+
"1 ½ cups flour" => "3/2",
|
21
|
+
"1½ cups flour" => "3/2",
|
22
|
+
"1.0 cup flour" => 1,
|
23
|
+
"1.5 cups flour" => "3/2",
|
24
|
+
"1,5 cups flour" => "3/2",
|
25
|
+
"1 2/3 cups flour" => "5/3",
|
26
|
+
"1 (28 ounce) can crushed tomatoes" => 1,
|
27
|
+
"2 (28 ounce) can crushed tomatoes" => 2,
|
28
|
+
"3 28 ounce can crushed tomatoes" => 3,
|
29
|
+
"one 28 ounce can crushed tomatoes" => 1,
|
30
|
+
"two five-ounce can crushed tomatoes" => 2,
|
31
|
+
"two 28 ounce cans crushed tomatoes" => 2,
|
32
|
+
"three 28 ounce cans crushed tomatoes" => 3,
|
33
|
+
"1/2 cups flour" => "1/2",
|
34
|
+
".25 cups flour" => "1/4",
|
35
|
+
"12oz tequila" => 12,
|
36
|
+
"1 banana" => 1,
|
37
|
+
}.each do |query, expected|
|
38
|
+
it "parses the correct amount as a rational" do
|
39
|
+
expect(Ingreedy.parse(query)).to parse_the_amount(expected.to_r)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe Ingreedy, "english units" do
|
54
45
|
context "abbreviated" do
|
55
46
|
{
|
56
47
|
"1 c flour" => :cup,
|
@@ -88,7 +79,7 @@ describe "english units" do
|
|
88
79
|
"1 LB flour" => :pound,
|
89
80
|
"1 tSP sugar" => :teaspoon,
|
90
81
|
}.each do |query, expected|
|
91
|
-
it "parses the
|
82
|
+
it "parses the #{expected} unit correctly" do
|
92
83
|
expect(Ingreedy.parse(query)).to parse_the_unit(expected)
|
93
84
|
end
|
94
85
|
end
|
@@ -118,7 +109,7 @@ describe "english units" do
|
|
118
109
|
end
|
119
110
|
end
|
120
111
|
|
121
|
-
describe "metric units" do
|
112
|
+
describe Ingreedy, "metric units" do
|
122
113
|
context "abbreviated" do
|
123
114
|
{
|
124
115
|
"1 g flour" => :gram,
|
@@ -160,7 +151,7 @@ describe "metric units" do
|
|
160
151
|
end
|
161
152
|
end
|
162
153
|
|
163
|
-
describe "nonstandard units" do
|
154
|
+
describe Ingreedy, "nonstandard units" do
|
164
155
|
{
|
165
156
|
"1 pinch pepper" => :pinch,
|
166
157
|
"2 pinches pepper" => :pinch,
|
@@ -180,7 +171,7 @@ describe "nonstandard units" do
|
|
180
171
|
end
|
181
172
|
end
|
182
173
|
|
183
|
-
describe "without units" do
|
174
|
+
describe Ingreedy, "without units" do
|
184
175
|
it "parses correctly" do
|
185
176
|
result = Ingreedy.parse "3 eggs, lightly beaten"
|
186
177
|
|
@@ -190,7 +181,7 @@ describe "without units" do
|
|
190
181
|
end
|
191
182
|
end
|
192
183
|
|
193
|
-
describe "container as part of quantity" do
|
184
|
+
describe Ingreedy, "container as part of quantity" do
|
194
185
|
it "parses correctly" do
|
195
186
|
result = Ingreedy.parse "160g (2 cans) of tomatoes"
|
196
187
|
|
@@ -229,7 +220,7 @@ describe "container as part of quantity" do
|
|
229
220
|
end
|
230
221
|
end
|
231
222
|
|
232
|
-
describe "with 'a' as quantity and preposition 'of'" do
|
223
|
+
describe Ingreedy, "with 'a' as quantity and preposition 'of'" do
|
233
224
|
it "parses correctly" do
|
234
225
|
result = Ingreedy.parse "a dash of ginger"
|
235
226
|
|
@@ -239,7 +230,7 @@ describe "with 'a' as quantity and preposition 'of'" do
|
|
239
230
|
end
|
240
231
|
end
|
241
232
|
|
242
|
-
describe "with 'reverse format'" do
|
233
|
+
describe Ingreedy, "with 'reverse format'" do
|
243
234
|
it "works with words containing a 'word digit'" do
|
244
235
|
result = Ingreedy.parse "salt 200g"
|
245
236
|
|
@@ -265,7 +256,7 @@ describe "with 'reverse format'" do
|
|
265
256
|
end
|
266
257
|
end
|
267
258
|
|
268
|
-
describe "Given a range" do
|
259
|
+
describe Ingreedy, "Given a range" do
|
269
260
|
it "works with simple ranges" do
|
270
261
|
result = Ingreedy.parse "1-2 tbsp salt"
|
271
262
|
|
@@ -281,9 +272,17 @@ describe "Given a range" do
|
|
281
272
|
expect(result.unit).to eq(:tablespoon)
|
282
273
|
expect(result.ingredient).to eq("salt")
|
283
274
|
end
|
275
|
+
|
276
|
+
it "works with tilde" do
|
277
|
+
result = Ingreedy.parse "1~2 tbsp salt"
|
278
|
+
|
279
|
+
expect(result.amount).to eq([1, 2])
|
280
|
+
expect(result.unit).to eq(:tablespoon)
|
281
|
+
expect(result.ingredient).to eq("salt")
|
282
|
+
end
|
284
283
|
end
|
285
284
|
|
286
|
-
describe "parsing in language with no prepositions" do
|
285
|
+
describe Ingreedy, "parsing in language with no prepositions" do
|
287
286
|
before(:all) do
|
288
287
|
Ingreedy.dictionaries[:id] = {
|
289
288
|
units: {
|
@@ -307,7 +306,7 @@ describe "parsing in language with no prepositions" do
|
|
307
306
|
end
|
308
307
|
end
|
309
308
|
|
310
|
-
describe "custom dictionaries" do
|
309
|
+
describe Ingreedy, "custom dictionaries" do
|
311
310
|
context "using Ingreedy.locale=" do
|
312
311
|
before(:all) do
|
313
312
|
Ingreedy.dictionaries[:fr] = {
|
@@ -371,8 +370,16 @@ describe "custom dictionaries" do
|
|
371
370
|
end
|
372
371
|
end
|
373
372
|
|
374
|
-
describe "ingredient formatting" do
|
373
|
+
describe Ingreedy, "ingredient formatting" do
|
375
374
|
it "strips preceding or trailing whitespace" do
|
376
375
|
expect(Ingreedy.parse("1 cup flour ").ingredient).to eq("flour")
|
377
376
|
end
|
378
377
|
end
|
378
|
+
|
379
|
+
describe Ingreedy, "error handling" do
|
380
|
+
it "wraps Parslet exceptions in a custom exception" do
|
381
|
+
expect do
|
382
|
+
Ingreedy.parse("nonsense")
|
383
|
+
end.to raise_error Ingreedy::ParseFailed
|
384
|
+
end
|
385
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ingreedy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian C. Anderson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parslet
|
@@ -116,7 +116,6 @@ files:
|
|
116
116
|
- ".gitignore"
|
117
117
|
- ".rspec"
|
118
118
|
- ".rubocop.yml"
|
119
|
-
- ".rvmrc"
|
120
119
|
- ".travis.yml"
|
121
120
|
- Gemfile
|
122
121
|
- README.md
|
@@ -158,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
157
|
version: '0'
|
159
158
|
requirements: []
|
160
159
|
rubyforge_project:
|
161
|
-
rubygems_version: 2.4.
|
160
|
+
rubygems_version: 2.4.8
|
162
161
|
signing_key:
|
163
162
|
specification_version: 4
|
164
163
|
summary: Recipe parser
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm --create 1.9.3@ingreedy
|