Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve OCR parser #492

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 119 additions & 26 deletions internal/models/ocr.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func (a *AzureDILayout) Recipe() Recipe {
for i := 0; i < len(a.AnalyzeResult.Paragraphs); i++ {
p := a.AnalyzeResult.Paragraphs[i]

if len(p.Content) == 1 {
continue
}

if p.Role == "sectionHeading" && strings.HasPrefix(strings.ToLower(p.Content), "utens") {
if i > 1 {
recipe.Name = a.AnalyzeResult.Paragraphs[i-1].Content
Expand Down Expand Up @@ -127,6 +131,12 @@ func (a *AzureDILayout) Recipe() Recipe {
if p.Role == "title" || p.Role == "sectionHeading" {
recipe.Name = p.Content
continue
} else if p.Role == "pageHeader" {
header := regex.Digit.ReplaceAllString(p.Content, "")
header = strings.ReplaceAll(header, ".", "")
if header != "" && len(p.Content) < 20 {
recipe.Category = strings.ToLower(strings.TrimSpace(header))
}
}
continue
}
Expand All @@ -140,35 +150,70 @@ func (a *AzureDILayout) Recipe() Recipe {
}
}

if len(recipe.Ingredients) == 0 && isIngredient(p.Content) {
var isFound bool
for _, line := range a.AnalyzeResult.Pages[p.BoundingRegions[0].PageNumber-1].Lines {
if strings.Contains(p.Content, line.Content) {
isFound = true
if recipe.Cuisine == "" && recipe.Description == "" && len(p.Content) < 20 {
doc, _ := prose.NewDocument(p.Content)
if slices.ContainsFunc(doc.Entities(), func(e prose.Entity) bool { return e.Label == "GPE" }) {
recipe.Cuisine = strings.ToLower(p.Content)
continue
}
}

if len(recipe.Ingredients) == 0 {
if isIngredient(p.Content) {
var isFound bool
var isSkipped bool
for _, line := range a.AnalyzeResult.Pages[p.BoundingRegions[0].PageNumber-1].Lines {
if !isSkipped {
if len(line.Content) < 2 {
continue
} else if strings.Contains(p.Content, line.Content) {
isSkipped = true
} else {
continue
}
}

if len(line.Content) < 2 {
continue
} else if strings.Contains(p.Content, line.Content) || strings.HasPrefix(strings.ToLower(line.Content), "for") {
isFound = true
recipe.Ingredients = append(recipe.Ingredients, line.Content)
continue
} else if !isFound {
continue
} else if !isIngredient(line.Content) {
i = slices.IndexFunc(a.AnalyzeResult.Paragraphs, func(paragraph azureDIParagraph) bool {
return strings.Contains(paragraph.Content, line.Content)
}) - 1
break
}

recipe.Ingredients = append(recipe.Ingredients, line.Content)
continue
} else if !isFound {
continue
} else if !isIngredient(line.Content) {
i = slices.IndexFunc(a.AnalyzeResult.Paragraphs, func(paragraph azureDIParagraph) bool {
return strings.Contains(paragraph.Content, line.Content)
}) - 1
break
}
continue
} else if strings.HasPrefix(strings.ToUpper(p.Content), "FÜR DIE") {
for i2, p2 := range a.AnalyzeResult.Paragraphs[i:] {
diff := len(p2.Content) - len(recipe.Name)
if p2.Role == "title" && (diff < -3 || diff > 3) {
i += i2
break
}
recipe.Ingredients = append(recipe.Ingredients, p2.Content)
}
continue
} else if recipe.Description != "" {
if strings.HasSuffix(strings.ToLower(p.Content), "servings") {
parsed, err := strconv.ParseInt(regex.Digit.FindString(p.Content), 10, 16)
if err == nil {
recipe.Yield = int16(parsed)
}

recipe.Ingredients = append(recipe.Ingredients, line.Content)
}
continue
} else if len(recipe.Ingredients) == 0 && strings.HasPrefix(strings.ToUpper(p.Content), "FÜR DIE") {
for i2, p2 := range a.AnalyzeResult.Paragraphs[i:] {
diff := len(p2.Content) - len(recipe.Name)
if p2.Role == "title" && (diff < -3 || diff > 3) {
i += i2
break
continue
}
recipe.Ingredients = append(recipe.Ingredients, p2.Content)

recipe.Description += "\n\n" + p.Content
continue
}
continue
}

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

if len(recipe.Instructions) == 0 {
if isIngredient(p.Content) {
var isFound bool
for _, line := range a.AnalyzeResult.Pages[p.BoundingRegions[0].PageNumber-1].Lines {
if strings.Contains(p.Content, line.Content) {
isFound = true
recipe.Ingredients = append(recipe.Ingredients, line.Content)
continue
} else if !isFound {
continue
} else if !isIngredient(line.Content) {
i = slices.IndexFunc(a.AnalyzeResult.Paragraphs, func(paragraph azureDIParagraph) bool {
return strings.Contains(paragraph.Content, line.Content)
}) - 1
break
}

recipe.Ingredients = append(recipe.Ingredients, line.Content)
}
continue
}

for i2, p2 := range a.AnalyzeResult.Paragraphs[i:] {
if len(strings.Split(p2.Content, " ")) < 2 {
_, err := strconv.ParseInt(p2.Content, 10, 64)
Expand Down Expand Up @@ -218,7 +284,33 @@ func (a *AzureDILayout) Recipe() Recipe {
recipe.Description = "Recipe created using Azure AI Document Intelligence."
}

// Transfer ingredients in the instructions to ingredients.
for _, ing := range recipe.Instructions {
if isIngredient(ing) || strings.HasPrefix(strings.ToLower(ing), "for") {
recipe.Ingredients = append(recipe.Ingredients, ing)
} else {
break
}
}

elements := make(map[string]bool)
for _, v := range recipe.Ingredients {
elements[v] = true
}

result := make([]string, 0)
for _, v := range recipe.Instructions {
if !elements[v] {
result = append(result, v)
}
}

if len(result) == 0 {
result = append(result, "No instructions found in image.")
}

recipe.Ingredients = extensions.Unique(recipe.Ingredients)
recipe.Instructions = result
return recipe
}

Expand All @@ -231,7 +323,7 @@ type AzureDIError struct {
}

func isIngredient(s string) bool {
if s == "" {
if s == "" || strings.Contains(strings.ToLower(s), "serving") {
return false
}

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

if idx == nil {
doc, _ := prose.NewDocument(s)
if doc.Tokens()[0].Tag == "IN" {
if doc.Tokens()[0].Tag == "IN" ||
slices.ContainsFunc(doc.Tokens(), func(e prose.Token) bool { return strings.Contains(e.Tag, "GPE") }) {
return false
}
}
Expand Down
36 changes: 35 additions & 1 deletion internal/models/ocr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func TestAzureVision_Recipe(t *testing.T) {
file: "recipe8.json",
want: models.Recipe{
Category: "uncategorized",
Cuisine: "einschub: mitte",
Description: "Recipe created using Azure AI Document Intelligence.",
Images: []uuid.UUID{},
Ingredients: []string{
Expand Down Expand Up @@ -266,9 +267,10 @@ func TestAzureVision_Recipe(t *testing.T) {
"helene Speise- stärke",
"Blatt Gelatine Qualsiz Gold extre",
"Kuvertüre",
"1",
"Vorbereiten",
},
Instructions: []string{
"1 Vorbereiten",
"Backblech mit Backpapier belegen. Springformboden fetten und mit Backpapier belegen. Backofen vorheizen.",
"Ober- und Unterhitze: etwa 180 ℃ Heißluft: etwa 160 ℃",
"Biskuitteig zubereiten",
Expand All @@ -291,6 +293,38 @@ func TestAzureVision_Recipe(t *testing.T) {
Yield: 12,
},
},
{
name: "recipe9.jpg",
file: "recipe9.json",
want: models.Recipe{
Category: "poultry",
Cuisine: "(java, indonesia)",
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.",
Images: []uuid.UUID{},
Ingredients: []string{
"2 tablespoons Crisp-Fried Shallots (page 84),",
"optional",
"FOR THE FLAVORING PASTE",
"1 tablespoon coriander seeds",
"1 fresh red Holland chile or other fresh long, hot",
"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",
"2 cloves garlic, coarsely chopped",
"1 piece fresh or thawed, frozen galangal,",
"11/2 inches (4 centimeters) long, peeled and thinly sliced against the grain (about 11/2 tablespoons; optional)",
"1 piece fresh ginger, 2 inches (5 centimeters) long, peeled and thinly sliced against the grain (about 2 tablespoons)",
},
Instructions: []string{"No instructions found in image."},
Keywords: []string{},
Name: "JAVANESE CHICKEN CURRY Opor Ayam",
Nutrition: models.Nutrition{},
Times: models.Times{},
Tools: []models.HowToItem{},
UpdatedAt: time.Time{},
URL: "OCR",
Videos: []models.VideoObject{},
Yield: 4,
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Loading
Loading