ingredient_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8cd55a427f0fe20d91ab4d7fba6c5d2256413f50
4
+ data.tar.gz: b94e059db2fd760fafceb3e2338423668b71cd03
5
+ SHA512:
6
+ metadata.gz: 30febba279bb04f2c4376e14c4e77a5d403d8491b7bd856d4183aa17ca1ed74e739aeca26a1ad61270bfffaece09a4cf7eb9149647e952be948c4f3100fd22dd
7
+ data.tar.gz: b5beeaab0c79b26ea37677345b67fb2804ff4da9279e17fcafc0cd586f037eb4f8a0fc5295bdb10d71b904e8bc75a534f9ac653c1f35be6d0c0bd7450420ecfa
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ingredient_parser.gemspec
4
+ gemspec
5
+
6
+ group :tools do
7
+ gem 'byebug'
8
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Luke Rodgers
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ # IngredientParser
2
+
3
+ [![Build Status](https://travis-ci.org/lukeasrodgers/ingredient_parser.svg?branch=master)](https://travis-ci.org/lukeasrodgers/ingredient_parser)
4
+
5
+ Basic recipe ingredient parser using [parslet](https://github.com/kschiess/parslet).
6
+
7
+ It is not, and will never be, perfect. If it can't separate out a name from an amount, it will fallback to just returning the whole string as the name.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'ingredient_parser'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install ingredient_parser
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ 2.1.2 :002 > ingr = IngredientParser.parse('10 tablespoons of bananas')
29
+ => #<IngredientParser::Ingredient:0x007f92652c2938 @name="bananas", @amount="10 tablespoons">
30
+ 2.1.2 :003 > ingr.name
31
+ => "bananas"
32
+ 2.1.2 :004 > ingr.amount
33
+ => "10 tablespoons"
34
+ 2.1.2 :005 >
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lukeasrodgers/ingredient_parser.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
51
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ingredient_parser"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ingredient_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ingredient_parser"
8
+ spec.version = IngredientParser::VERSION
9
+ spec.authors = ["Luke Rodgers"]
10
+ spec.email = ["lukeasrodgers@gmail.com"]
11
+
12
+ spec.summary = %q{Basic recipe ingredient parser.}
13
+ spec.homepage = "https://github.com/lukeasrodgers/ingredient_parser"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'parslet', '~> 1.6'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
@@ -0,0 +1,26 @@
1
+ require 'parslet'
2
+
3
+ require "ingredient_parser/version"
4
+ require 'ingredient_parser/parser'
5
+ require 'ingredient_parser/ingredient'
6
+
7
+ module Parslet
8
+ class ParseFailed < StandardError
9
+ alias_method :parse_failure_cause, :cause
10
+
11
+ def cause
12
+ nil
13
+ end
14
+ end
15
+ end
16
+
17
+ module IngredientParser
18
+ extend self
19
+
20
+ @parser = Parser.new
21
+
22
+ def self.parse(str)
23
+ parsed = @parser.parse(str)
24
+ Ingredient.new(parsed[:name], parsed[:amount])
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module IngredientParser
2
+ class Ingredient
3
+ attr_reader :name, :amount
4
+ def initialize(name, amount)
5
+ @name = normalize(name)
6
+ @amount = normalize(amount)
7
+ end
8
+
9
+ private
10
+
11
+ def normalize(value)
12
+ case value
13
+ when NilClass
14
+ nil
15
+ when Parslet::Slice
16
+ value.str.strip
17
+ when String
18
+ if value.empty?
19
+ nil
20
+ else
21
+ value.strip
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ module IngredientParser
4
+ class Parser < Parslet::Parser
5
+ rule(:integer) { match('[0-9]').repeat(1) >> space? }
6
+
7
+ rule(:space) { match('\s').repeat(1) }
8
+ rule(:space?) { space.maybe }
9
+
10
+ rule(:half) { match('½') >> space? }
11
+
12
+ rule(:quantity) { integer | half }
13
+ rule(:quantity?) { quantity.maybe }
14
+
15
+ rule(:weighted) { match('[0-9]').repeat(1) >> weighted_separator >> weight }
16
+
17
+ rule(:hyphen) { str('-') }
18
+ rule(:space_separator) { match('\s') }
19
+ rule(:weighted_separator) { space_separator | hyphen }
20
+ rule(:weight) { pound | ounce | gram | kilogram }
21
+ rule(:pound) { str('pound') | str('lb') | str('lb.') }
22
+ rule(:ounce) { str('ounce') | str('oz') | str('oz.') }
23
+ rule(:gram) { str('gram') | str('g') | str('g.') }
24
+ rule(:kilogram) { str('kilogram') | str('kg') | str('kg.') | str('kilo') | str('k') }
25
+
26
+ rule(:litre) { str('litre') | str('liter') }
27
+ rule(:pint) { str('pint') }
28
+ rule(:cup) { str('cup') }
29
+ rule(:tablespoon) { str('tablespoon') | str('T') >> space | str('tbsp') >> str('.').maybe }
30
+ rule(:teaspoon) { str('teaspoon') | str('t') >> space | str('tsp') >> str('.').maybe }
31
+
32
+ rule(:volume) { litre | pint | cup | tablespoon | teaspoon}
33
+
34
+ rule(:measurement) { (weight >> str('s').maybe) | (volume >> str('s').maybe) }
35
+
36
+ rule(:large) { str('large') }
37
+ rule(:small) { str('small') }
38
+ rule(:medium) { str('medium') }
39
+ rule(:adjective) { large | small | medium }
40
+
41
+ rule(:quantified) { weighted | measurement | adjective }
42
+ rule(:quantified?) { quantified.maybe }
43
+
44
+ rule(:package) { str('package') >> str('s').maybe }
45
+ rule(:container) { str('container') >> str('s').maybe }
46
+ rule(:bottle) { str('bottle') >> str('s').maybe }
47
+ rule(:tin) { str('tin') >> str('s').maybe }
48
+ rule(:abstract_container) { package | bottle | tin | container }
49
+ rule(:abstract_container?) { space? >> abstract_container.maybe }
50
+
51
+ rule(:amount) { quantity? >> quantified? >> abstract_container? }
52
+ rule(:amount?) { amount.maybe }
53
+
54
+ rule(:name) { any.repeat(1) }
55
+
56
+ rule(:expression) { amount?.as(:amount) >> str('of').maybe >> name.as(:name) }
57
+ root(:expression)
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module IngredientParser
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ingredient_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luke Rodgers
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parslet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - lukeasrodgers@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - ingredient_parser.gemspec
86
+ - lib/ingredient_parser.rb
87
+ - lib/ingredient_parser/ingredient.rb
88
+ - lib/ingredient_parser/parser.rb
89
+ - lib/ingredient_parser/version.rb
90
+ homepage: https://github.com/lukeasrodgers/ingredient_parser
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Basic recipe ingredient parser.
114
+ test_files: []