ingreedy 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ingreedy.rb +13 -0
- data/lib/ingreedy/unit_parser.rb +91 -0
- data/lib/ingreedy/version.rb +4 -0
- metadata +59 -0
data/lib/ingreedy.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
class UnitParser
|
2
|
+
|
3
|
+
attr_reader :amount, :unit, :ingredient
|
4
|
+
|
5
|
+
def initialize(query)
|
6
|
+
@query = query.downcase
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse
|
10
|
+
ingreedy_regex = %r{
|
11
|
+
(?<amount> \d+(\.\d+)? ) {0}
|
12
|
+
(?<fraction> \d\/\d ) {0}
|
13
|
+
|
14
|
+
(?<container_amount> \d+(\.\d+)?) {0}
|
15
|
+
(?<container_unit> .+) {0}
|
16
|
+
(?<container_size> \(\g<container_amount>\s\g<container_unit>\)) {0}
|
17
|
+
(?<unit_and_ingredient> .+ ) {0}
|
18
|
+
|
19
|
+
(\g<amount>\s)?(\g<fraction>\s)?(\g<container_size>\s)?\g<unit_and_ingredient>
|
20
|
+
}x
|
21
|
+
results = ingreedy_regex.match(@query)
|
22
|
+
|
23
|
+
@ingredient_string = results[:unit_and_ingredient]
|
24
|
+
@container_amount = results[:container_amount]
|
25
|
+
@container_unit = results[:container_unit]
|
26
|
+
|
27
|
+
parse_amount results[:amount], results[:fraction]
|
28
|
+
parse_unit_and_ingredient
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def parse_amount(amount_string, fraction_string)
|
34
|
+
fraction = 0
|
35
|
+
if fraction_string
|
36
|
+
numbers = fraction_string.split("\/")
|
37
|
+
numerator = numbers[0].to_f
|
38
|
+
denominator = numbers[1].to_f
|
39
|
+
fraction = numerator / denominator
|
40
|
+
end
|
41
|
+
@amount = amount_string.to_f + fraction
|
42
|
+
@amount *= @container_amount.to_f if @container_amount
|
43
|
+
end
|
44
|
+
def set_unit_variations(unit, variations)
|
45
|
+
variations.each do |abbrev|
|
46
|
+
@unit_map[abbrev] = unit
|
47
|
+
end
|
48
|
+
end
|
49
|
+
def create_unit_map
|
50
|
+
@unit_map = {}
|
51
|
+
# english units
|
52
|
+
set_unit_variations :cup, ["c.", "c", "cup", "cups"]
|
53
|
+
set_unit_variations :fluid_ounce, ["fl. oz.", "fl oz", "fluid ounce", "fluid ounces"]
|
54
|
+
set_unit_variations :gallon, ["gal", "gal.", "gallon", "gallons"]
|
55
|
+
set_unit_variations :ounce, ["oz", "oz.", "ounce", "ounces"]
|
56
|
+
set_unit_variations :pint, ["pt", "pt.", "pint", "pints"]
|
57
|
+
set_unit_variations :pound, ["lb", "lb.", "pound", "pounds"]
|
58
|
+
set_unit_variations :quart, ["qt", "qt.", "qts", "qts.", "quart", "quarts"]
|
59
|
+
set_unit_variations :tablespoon, ["tbsp.", "tbsp", "T", "T.", "tablespoon", "tablespoons"]
|
60
|
+
set_unit_variations :teaspoon, ["tsp.", "tsp", "t", "t.", "teaspoon", "teaspoons"]
|
61
|
+
# metric units
|
62
|
+
set_unit_variations :gram, ["g", "g.", "gr", "gr.", "gram", "grams"]
|
63
|
+
set_unit_variations :kilogram, ["kg", "kg.", "kilogram", "kilograms"]
|
64
|
+
set_unit_variations :liter, ["l", "l.", "liter", "liters"]
|
65
|
+
set_unit_variations :milligram, ["mg", "mg.", "milligram", "milligrams"]
|
66
|
+
set_unit_variations :milliliter, ["ml", "ml.", "milliliter", "milliliters"]
|
67
|
+
end
|
68
|
+
def parse_unit
|
69
|
+
create_unit_map if @unit_map.nil?
|
70
|
+
|
71
|
+
@unit_map.each do |abbrev, unit|
|
72
|
+
if @ingredient_string.start_with?(abbrev + " ")
|
73
|
+
# if a unit is found, remove it from the ingredient string
|
74
|
+
@ingredient_string.sub! abbrev, ""
|
75
|
+
@unit = unit
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# if we still don't have a unit, check to see if we have a container unit
|
80
|
+
if @unit.nil? and @container_unit
|
81
|
+
@unit_map.each do |abbrev, unit|
|
82
|
+
@unit = unit if abbrev == @container_unit
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
def parse_unit_and_ingredient
|
87
|
+
parse_unit
|
88
|
+
# clean up ingredient string
|
89
|
+
@ingredient = @ingredient_string.lstrip.rstrip
|
90
|
+
end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ingreedy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ian C. Anderson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-25 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Natural language recipe ingredient parser that supports numeric amount, units, and ingredient
|
18
|
+
email:
|
19
|
+
- anderson.ian.c@gmail.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- lib/ingreedy/unit_parser.rb
|
28
|
+
- lib/ingreedy/version.rb
|
29
|
+
- lib/ingreedy.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://github.com/iancanderson/ingreedy
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.6.2
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Recipe parser
|
58
|
+
test_files: []
|
59
|
+
|