Skip to content

Commit b25c064

Browse files
committed
Recognize and after nesting (#22)
1 parent 0904d14 commit b25c064

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

data/test-samples-parsed

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ Halfvolle yoghurt met L rhamnosus Gorbach & Goldin, L acidophilus en B lactis, 2
1616
Water, champignon˄1 12%, schouderham˄2 2,5%, plantaardige olie, TARWEBLOEM, gemodificeerd zetmeel, magere MELKPOEDER, zout, bieslook 10,5%, gistextract (bevat GERST), aroma, champignonsapconcentraat˄1 0,1% , fructose, mineraalzout (kalium), uienpoeder, knoflook, witte wijnextract, balsamicoazijn (wijnazijn, druivenmost), ˄1op duurzame wijze geteeld., ˄2Beter Leven keurmerk 1 ster. Kan ei, soja, selderij, mosterd bevatten.
1717
Tomaat~ 84% (tomaat, tomatenpuree), wortel, ui, ROOM, suiker, Italiaanse KAAS (Parmigiano Reggiano BOB◊ 1,1%, Grana Padano BOB◊ 0,8% (bevat lysozym van EI)), extra olijfolie verkregen bij de eerste persing, rijstzetmeel, basilicum, peterselie, knoflook, maïszetmeel, tijm, MELKWEI, aroma, zwarte peper, zuurteregelaar: citroenzuur, tomat~ 84% (tomaat, tomatenpuree) , wortel, ui, ROOM, suiker, italiaanse KAAS (Parmigiano Reggiano BOB◊ 1,1%, Grana Padano BOB◊ 0,8% (bevat lysozym van EI)), ~ op duurzame wijze geteeld., ◊ Beschermde Oorsprongsbenaming.
1818
Wraphapje mozzarella-tomaat: 36% tomatenwrap , 29% half zongedroogde tomaat (27% tomaat, zonnebloemolie, knoflook, zout, oregano, marjolein, peterselie), 20% mozzarella , 15% groene pesto . , Wraphapje geitenkaas-beenham: 41% geitenkaas , 33% wrap , 22% beenham , 4% honing. Allergie-informatie: bevat tarwe (gluten), lactose, melkeiwit, ei, cashewnoot, geitenmelkeiwit. Gemaakt in een bedrijf waar ook pinda's en andere noten worden verwerkt.
19+
Wheat Flour [with Calcium, Iron, Niacin (B3) and Thiamin (B1)] and Wholemeal Wheat Flour, Water, Yeast, Vegetable Oils (Sunflower, Rapeseed and Sustainable Palm in varying proportions), Salt, Wheat Gluten, Malted Barley Flour, Emulsifiers: E471, E472e, Soya Flour, Preservative: Calcium Propionate, Flavouring, Flour Treatment Agent: Ascorbic Acid (Vitamin C)

lib/food_ingredient_parser/loose/scanner.rb

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module FoodIngredientParser::Loose
44
class Scanner
55

66
SEP_CHARS = "|;,.".freeze
7+
AND_SEP_RE = /\A\s*(and|en|und)\s+/i.freeze
78
MARK_CHARS = "¹²³⁴⁵ᵃᵇᶜᵈᵉᶠᵍªº⁽⁾†‡⁺•°▪◊#^˄*~".freeze
89
PREFIX_RE = /\A\s*(ingredients(\s*list)?|contains|ingred[iï][eë]nt(en)?(declaratie)?|bevat|dit zit er\s?in|samenstelling|zutaten)\b\s*[:;.]?\s*/i.freeze
910
NOTE_RE = /\A\b(dit product kan\b|deze verpakking kan\b|kan sporen\b.*?\bbevatten\b|voor allergenen\b|allergenen\b|allergie[- ]informatie(\s*:|\b)|E\s*=|gemaakt in\b|geproduceerd in\b|bevat mogelijk\b|kijk voor meer\b|allergie-info|in de fabriek\b|in dit bedrijf\b|voor [0-9,.]+ (g\.?|gr\.?|ram|ml).*\bis [0-9,.]+ (g\.?|gr\.?|ram|ml).*\bgebruikt\b)/i.freeze
@@ -75,6 +76,11 @@ def scan_iteration_standard
7576
elsif ")]".include?(c) # close nesting
7677
add_child
7778
close_parent
79+
# after bracket check for 'and' to not lose text
80+
if is_and_sep?(@i+1)
81+
@i += and_sep_len(@i+1)
82+
add_child
83+
end
7884
elsif is_notes_start? # usually a dot marks the start of notes
7985
close_all_ancestors
8086
@iterator = :notes
@@ -148,6 +154,15 @@ def is_sep?(chars: SEP_CHARS)
148154
chars.include?(c) && @s[@i-1..@i+1] !~ /\A\d.\d\z/
149155
end
150156

157+
def is_and_sep?(i = @i)
158+
and_sep_len(i) > 0
159+
end
160+
161+
def and_sep_len(i = @i)
162+
m = @s[i..-1].match(AND_SEP_RE)
163+
m ? m.offset(0).last : 0
164+
end
165+
151166
def is_mark?(i = @i)
152167
mark_len(i) > 0 && @s[i..i+1] !~ /\A°[CF]/
153168
end

lib/food_ingredient_parser/strict/grammar/ingredient.treetop

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ module FoodIngredientParser::Strict::Grammar
55
include IngredientColoned
66

77
rule ingredient
8-
ws* ( ingredient_nested / ingredient_coloned / ingredient_simple_with_amount )
8+
ws*
9+
(
10+
ingredient_nested ( ws* and ws+ ingredient )? /
11+
ingredient_coloned /
12+
ingredient_simple_with_amount
13+
)
914
end
1015

1116
end

0 commit comments

Comments
 (0)