Class: RecipeScrapers::Parsers::Vocabulary Private
- Inherits:
-
Object
- Object
- RecipeScrapers::Parsers::Vocabulary
- Defined in:
- lib/recipe_scrapers/parsers/vocabulary.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
The words the gem reads recipes with: units, pinch words, size adjectives, qualifiers and number words for Ingredients, and the words that label a numbered step. The gem bundles one vocabulary per language, plus a common one with the metric units.
Instance Attribute Summary collapse
-
#number_words ⇒ Hash{String => Numeric}
readonly
private
Amounts written as words, such as "two" => 2.
-
#pinches ⇒ Array<String>
readonly
private
Words that are a unit and an amount of one, such as "pinch".
-
#qualifiers ⇒ Array<String>
readonly
private
Words before an amount that are not part of it, such as "about".
-
#sizes ⇒ Array<String>
readonly
private
Adjectives that can come before a unit, such as "large".
-
#steps ⇒ Array<String>
readonly
private
Words that label a numbered step, such as "step" in "Step 2".
-
#units ⇒ Array<String>
readonly
private
Units, where a space matches any run of spaces or none.
Class Method Summary collapse
-
.all ⇒ Vocabulary
private
Every vocabulary in the catalog added together.
-
.alternation(words) ⇒ String
private
A regexp alternation of the words, longest first, that never matches when empty.
-
.catalog ⇒ Hash{Symbol => Hash}
private
Every vocabulary by name, the bundled ones loaded on first use.
-
.combine(names) ⇒ Vocabulary
private
The named vocabularies added together.
-
.define(name, languages: [], **words) ⇒ Hash
private
Adds a vocabulary to the catalog, next to the bundled ones.
-
.for(language) ⇒ Vocabulary
private
The vocabularies of the language combined.
-
.names_for(language) ⇒ Array<Symbol>
private
The common vocabulary plus those of the language, or every vocabulary when no file declares the language.
Instance Method Summary collapse
-
#+(other) ⇒ Vocabulary
private
Both word lists without repeats.
-
#initialize(number_words: {}, **lists) ⇒ Vocabulary
constructor
private
A new instance of Vocabulary.
Constructor Details
#initialize(number_words: {}, **lists) ⇒ Vocabulary
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Vocabulary.
113 114 115 116 117 118 119 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 113 def initialize(number_words: {}, **lists) unknown = lists.keys - WORD_LISTS raise ArgumentError, "unknown word lists: #{unknown.join(", ")}" unless unknown.empty? WORD_LISTS.each { |list| instance_variable_set(:"@#{list}", Array(lists[list]).uniq.freeze) } @number_words = number_words.freeze end |
Instance Attribute Details
#number_words ⇒ Hash{String => Numeric} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns amounts written as words, such as "two" => 2.
34 35 36 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 34 def number_words @number_words end |
#pinches ⇒ Array<String> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns words that are a unit and an amount of one, such as "pinch".
22 23 24 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 22 def pinches @pinches end |
#qualifiers ⇒ Array<String> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns words before an amount that are not part of it, such as "about".
28 29 30 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 28 def qualifiers @qualifiers end |
#sizes ⇒ Array<String> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns adjectives that can come before a unit, such as "large".
25 26 27 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 25 def sizes @sizes end |
#steps ⇒ Array<String> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns words that label a numbered step, such as "step" in "Step 2".
31 32 33 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 31 def steps @steps end |
#units ⇒ Array<String> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns units, where a space matches any run of spaces or none.
19 20 21 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 19 def units @units end |
Class Method Details
.all ⇒ Vocabulary
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns every vocabulary in the catalog added together.
67 68 69 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 67 def all combine(catalog.keys) end |
.alternation(words) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a regexp alternation of the words, longest first, that never matches when empty.
90 91 92 93 94 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 90 def alternation(words) return NEVER if words.empty? words.sort_by { |word| -word.length }.map { |word| Regexp.escape(word).gsub("\\ ", "\\s*") }.join("|") end |
.catalog ⇒ Hash{Symbol => Hash}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns every vocabulary by name, the bundled ones loaded on first use.
82 83 84 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 82 def catalog @catalog ||= load_bundled end |
.combine(names) ⇒ Vocabulary
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the named vocabularies added together.
75 76 77 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 75 def combine(names) names.map { |name| catalog.fetch(name)[:vocabulary] }.reduce(:+) end |
.define(name, languages: [], **words) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a vocabulary to the catalog, next to the bundled ones.
43 44 45 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 43 def define(name, languages: [], **words) catalog[name] = entry(languages, words) end |
.for(language) ⇒ Vocabulary
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the vocabularies of the language combined.
60 61 62 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 60 def for(language) combine(names_for(language)) end |
.names_for(language) ⇒ Array<Symbol>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the common vocabulary plus those of the language, or every vocabulary when no file declares the language.
52 53 54 55 56 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 52 def names_for(language) code = language.to_s.downcase[/\A[a-z]+/] spoken = catalog.filter_map { |name, entry| name if entry[:languages].include?(code) } spoken.empty? ? catalog.keys.sort : [:common, *spoken.sort] end |
Instance Method Details
#+(other) ⇒ Vocabulary
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns both word lists without repeats. The other one wins for a number word in both.
123 124 125 126 |
# File 'lib/recipe_scrapers/parsers/vocabulary.rb', line 123 def +(other) lists = WORD_LISTS.to_h { |list| [list, public_send(list) + other.public_send(list)] } Vocabulary.new(**lists, number_words: number_words.merge(other.number_words)) end |