Skip to content

Commit 657cb66

Browse files
authored
Improve OCR parser (#492)
1 parent 15ee246 commit 657cb66

File tree

5 files changed

+3375
-28
lines changed

5 files changed

+3375
-28
lines changed

.idea/vcs.xml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/models/ocr.go

+119-26
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ func (a *AzureDILayout) Recipe() Recipe {
9595
for i := 0; i < len(a.AnalyzeResult.Paragraphs); i++ {
9696
p := a.AnalyzeResult.Paragraphs[i]
9797

98+
if len(p.Content) == 1 {
99+
continue
100+
}
101+
98102
if p.Role == "sectionHeading" && strings.HasPrefix(strings.ToLower(p.Content), "utens") {
99103
if i > 1 {
100104
recipe.Name = a.AnalyzeResult.Paragraphs[i-1].Content
@@ -127,6 +131,12 @@ func (a *AzureDILayout) Recipe() Recipe {
127131
if p.Role == "title" || p.Role == "sectionHeading" {
128132
recipe.Name = p.Content
129133
continue
134+
} else if p.Role == "pageHeader" {
135+
header := regex.Digit.ReplaceAllString(p.Content, "")
136+
header = strings.ReplaceAll(header, ".", "")
137+
if header != "" && len(p.Content) < 20 {
138+
recipe.Category = strings.ToLower(strings.TrimSpace(header))
139+
}
130140
}
131141
continue
132142
}
@@ -140,35 +150,70 @@ func (a *AzureDILayout) Recipe() Recipe {
140150
}
141151
}
142152

143-
if len(recipe.Ingredients) == 0 && isIngredient(p.Content) {
144-
var isFound bool
145-
for _, line := range a.AnalyzeResult.Pages[p.BoundingRegions[0].PageNumber-1].Lines {
146-
if strings.Contains(p.Content, line.Content) {
147-
isFound = true
153+
if recipe.Cuisine == "" && recipe.Description == "" && len(p.Content) < 20 {
154+
doc, _ := prose.NewDocument(p.Content)
155+
if slices.ContainsFunc(doc.Entities(), func(e prose.Entity) bool { return e.Label == "GPE" }) {
156+
recipe.Cuisine = strings.ToLower(p.Content)
157+
continue
158+
}
159+
}
160+
161+
if len(recipe.Ingredients) == 0 {
162+
if isIngredient(p.Content) {
163+
var isFound bool
164+
var isSkipped bool
165+
for _, line := range a.AnalyzeResult.Pages[p.BoundingRegions[0].PageNumber-1].Lines {
166+
if !isSkipped {
167+
if len(line.Content) < 2 {
168+
continue
169+
} else if strings.Contains(p.Content, line.Content) {
170+
isSkipped = true
171+
} else {
172+
continue
173+
}
174+
}
175+
176+
if len(line.Content) < 2 {
177+
continue
178+
} else if strings.Contains(p.Content, line.Content) || strings.HasPrefix(strings.ToLower(line.Content), "for") {
179+
isFound = true
180+
recipe.Ingredients = append(recipe.Ingredients, line.Content)
181+
continue
182+
} else if !isFound {
183+
continue
184+
} else if !isIngredient(line.Content) {
185+
i = slices.IndexFunc(a.AnalyzeResult.Paragraphs, func(paragraph azureDIParagraph) bool {
186+
return strings.Contains(paragraph.Content, line.Content)
187+
}) - 1
188+
break
189+
}
190+
148191
recipe.Ingredients = append(recipe.Ingredients, line.Content)
149-
continue
150-
} else if !isFound {
151-
continue
152-
} else if !isIngredient(line.Content) {
153-
i = slices.IndexFunc(a.AnalyzeResult.Paragraphs, func(paragraph azureDIParagraph) bool {
154-
return strings.Contains(paragraph.Content, line.Content)
155-
}) - 1
156-
break
157192
}
193+
continue
194+
} else if strings.HasPrefix(strings.ToUpper(p.Content), "FÜR DIE") {
195+
for i2, p2 := range a.AnalyzeResult.Paragraphs[i:] {
196+
diff := len(p2.Content) - len(recipe.Name)
197+
if p2.Role == "title" && (diff < -3 || diff > 3) {
198+
i += i2
199+
break
200+
}
201+
recipe.Ingredients = append(recipe.Ingredients, p2.Content)
202+
}
203+
continue
204+
} else if recipe.Description != "" {
205+
if strings.HasSuffix(strings.ToLower(p.Content), "servings") {
206+
parsed, err := strconv.ParseInt(regex.Digit.FindString(p.Content), 10, 16)
207+
if err == nil {
208+
recipe.Yield = int16(parsed)
209+
}
158210

159-
recipe.Ingredients = append(recipe.Ingredients, line.Content)
160-
}
161-
continue
162-
} else if len(recipe.Ingredients) == 0 && strings.HasPrefix(strings.ToUpper(p.Content), "FÜR DIE") {
163-
for i2, p2 := range a.AnalyzeResult.Paragraphs[i:] {
164-
diff := len(p2.Content) - len(recipe.Name)
165-
if p2.Role == "title" && (diff < -3 || diff > 3) {
166-
i += i2
167-
break
211+
continue
168212
}
169-
recipe.Ingredients = append(recipe.Ingredients, p2.Content)
213+
214+
recipe.Description += "\n\n" + p.Content
215+
continue
170216
}
171-
continue
172217
}
173218

174219
if len(recipe.Ingredients) == 0 {
@@ -177,6 +222,27 @@ func (a *AzureDILayout) Recipe() Recipe {
177222
}
178223

179224
if len(recipe.Instructions) == 0 {
225+
if isIngredient(p.Content) {
226+
var isFound bool
227+
for _, line := range a.AnalyzeResult.Pages[p.BoundingRegions[0].PageNumber-1].Lines {
228+
if strings.Contains(p.Content, line.Content) {
229+
isFound = true
230+
recipe.Ingredients = append(recipe.Ingredients, line.Content)
231+
continue
232+
} else if !isFound {
233+
continue
234+
} else if !isIngredient(line.Content) {
235+
i = slices.IndexFunc(a.AnalyzeResult.Paragraphs, func(paragraph azureDIParagraph) bool {
236+
return strings.Contains(paragraph.Content, line.Content)
237+
}) - 1
238+
break
239+
}
240+
241+
recipe.Ingredients = append(recipe.Ingredients, line.Content)
242+
}
243+
continue
244+
}
245+
180246
for i2, p2 := range a.AnalyzeResult.Paragraphs[i:] {
181247
if len(strings.Split(p2.Content, " ")) < 2 {
182248
_, err := strconv.ParseInt(p2.Content, 10, 64)
@@ -218,7 +284,33 @@ func (a *AzureDILayout) Recipe() Recipe {
218284
recipe.Description = "Recipe created using Azure AI Document Intelligence."
219285
}
220286

287+
// Transfer ingredients in the instructions to ingredients.
288+
for _, ing := range recipe.Instructions {
289+
if isIngredient(ing) || strings.HasPrefix(strings.ToLower(ing), "for") {
290+
recipe.Ingredients = append(recipe.Ingredients, ing)
291+
} else {
292+
break
293+
}
294+
}
295+
296+
elements := make(map[string]bool)
297+
for _, v := range recipe.Ingredients {
298+
elements[v] = true
299+
}
300+
301+
result := make([]string, 0)
302+
for _, v := range recipe.Instructions {
303+
if !elements[v] {
304+
result = append(result, v)
305+
}
306+
}
307+
308+
if len(result) == 0 {
309+
result = append(result, "No instructions found in image.")
310+
}
311+
221312
recipe.Ingredients = extensions.Unique(recipe.Ingredients)
313+
recipe.Instructions = result
222314
return recipe
223315
}
224316

@@ -231,7 +323,7 @@ type AzureDIError struct {
231323
}
232324

233325
func isIngredient(s string) bool {
234-
if s == "" {
326+
if s == "" || strings.Contains(strings.ToLower(s), "serving") {
235327
return false
236328
}
237329

@@ -243,7 +335,8 @@ func isIngredient(s string) bool {
243335

244336
if idx == nil {
245337
doc, _ := prose.NewDocument(s)
246-
if doc.Tokens()[0].Tag == "IN" {
338+
if doc.Tokens()[0].Tag == "IN" ||
339+
slices.ContainsFunc(doc.Tokens(), func(e prose.Token) bool { return strings.Contains(e.Tag, "GPE") }) {
247340
return false
248341
}
249342
}

internal/models/ocr_test.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ func TestAzureVision_Recipe(t *testing.T) {
229229
file: "recipe8.json",
230230
want: models.Recipe{
231231
Category: "uncategorized",
232+
Cuisine: "einschub: mitte",
232233
Description: "Recipe created using Azure AI Document Intelligence.",
233234
Images: []uuid.UUID{},
234235
Ingredients: []string{
@@ -266,9 +267,10 @@ func TestAzureVision_Recipe(t *testing.T) {
266267
"helene Speise- stärke",
267268
"Blatt Gelatine Qualsiz Gold extre",
268269
"Kuvertüre",
270+
"1",
271+
"Vorbereiten",
269272
},
270273
Instructions: []string{
271-
"1 Vorbereiten",
272274
"Backblech mit Backpapier belegen. Springformboden fetten und mit Backpapier belegen. Backofen vorheizen.",
273275
"Ober- und Unterhitze: etwa 180 ℃ Heißluft: etwa 160 ℃",
274276
"Biskuitteig zubereiten",
@@ -291,6 +293,38 @@ func TestAzureVision_Recipe(t *testing.T) {
291293
Yield: 12,
292294
},
293295
},
296+
{
297+
name: "recipe9.jpg",
298+
file: "recipe9.json",
299+
want: models.Recipe{
300+
Category: "poultry",
301+
Cuisine: "(java, indonesia)",
302+
Description: "A gorgeous coconut-milk curry from Java, Indonesia, perfumed with lemongrass, ginger, cinnamon sticks, and coriander. It's one of the benchmark dishes by which Indonesian home cooks are judged. If a young cook's opor ayam is as rich and delicate as it should be, she is well on her way to becoming skilled in the kitchen. The dish is a perfect showcase for a high-quality free-range chicken. A whole bird, cut into small, bone-in serving pieces, will yield the best results, though whole chicken parts can be substituted without really compromising the taste of the dish. If you have the inclina- tion to make them, fried shallots are traditionally strewn on top just before the dish is served.\n\nDaun salam leaves, the seasoning herb prized in Indonesian cooking, helps give this dish its unique aroma. I've often seen bay leaves listed as a substitute for daun salam in recipe books on Indonesian cuisine. While bay leaves have an aggressively mentholated taste, daun salam leaves are subtle, with a faintly foresty flavor. The only thing the two herbs share in common is that they are both green leaves and grow on trees. If you are unable to find daun salam leaves, omit them. The dish will still taste exquisite.",
303+
Images: []uuid.UUID{},
304+
Ingredients: []string{
305+
"2 tablespoons Crisp-Fried Shallots (page 84),",
306+
"optional",
307+
"FOR THE FLAVORING PASTE",
308+
"1 tablespoon coriander seeds",
309+
"1 fresh red Holland chile or other fresh long, hot",
310+
"1 fresh red Holland chile or other fresh long, hot red chile such as Fresno or cayenne, stemmed and coarsely chopped (optional, but provides subtle heat and color) 6 shallots (about 5 ounces/140 grams), coarsely chopped",
311+
"2 cloves garlic, coarsely chopped",
312+
"1 piece fresh or thawed, frozen galangal,",
313+
"11/2 inches (4 centimeters) long, peeled and thinly sliced against the grain (about 11/2 tablespoons; optional)",
314+
"1 piece fresh ginger, 2 inches (5 centimeters) long, peeled and thinly sliced against the grain (about 2 tablespoons)",
315+
},
316+
Instructions: []string{"No instructions found in image."},
317+
Keywords: []string{},
318+
Name: "JAVANESE CHICKEN CURRY Opor Ayam",
319+
Nutrition: models.Nutrition{},
320+
Times: models.Times{},
321+
Tools: []models.HowToItem{},
322+
UpdatedAt: time.Time{},
323+
URL: "OCR",
324+
Videos: []models.VideoObject{},
325+
Yield: 4,
326+
},
327+
},
294328
}
295329
for _, tc := range testcases {
296330
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)