I have code analyse that code. And modify the code to handle those entity which have…
- User
I have code analyse that code. And modify the code to handle those entity which have exact same value but their bounding box on image can't be same because that entity address different purpose in invoice. Modify in smart way that main logic should not change.
import cv2 import pandas as pd import json from thefuzz import fuzz
def preprocess_entity(entity): """Preprocess entities by removing commas in numerical values.""" return entity.replace(",", "").strip()
def calculate_proximity_score(box_a, box_b): """Calculate a refined proximity score taking into account typical reading flows.""" vertical_overlap = max(0, min(box_a["bottom"], box_b["bottom"]) - max(box_a["top"], box_b["top"])) vertical_distance = 0 if vertical_overlap > 0 else min(abs(box_a["top"] - box_b["bottom"]), abs(box_a["bottom"] - box_b["top"])) horizontal_overlap = max(0, min(box_a["right"], box_b["right"]) - max(box_a["left"], box_b["left"])) horizontal_distance = 0 if horizontal_overlap > 0 else abs(box_a["right"] - box_b["left"]) return horizontal_distance + 2 * vertical_distance
def is_nearby(box_a, box_b, max_line_difference=1, max_distance=50): return calculate_proximity_score(box_a, box_b) <= max_distance + 2 * max_line_difference
def merge_boxes(boxes): """Merge a list of bounding boxes into a single bounding box.""" min_left = min(box["left"] for box in boxes) max_right = max(box["right"] for box in boxes) min_top = min(box["top"] for box in boxes) max_bottom = max(box["bottom"] for box in boxes) return {"left": min_left, "right": max_right, "top": min_top, "bottom": max_bottom}
def find_potential_matches(dataframe, token, threshold=80): potential_matches = [] for _, row in dataframe.iterrows(): ocr_text = preprocess_entity(row["text"]) score = fuzz.ratio(token, ocr_text) if score > threshold: potential_matches.append({ "box": {"left": row["left"], "right": row["right"], "top": row["top"], "bottom": row["bottom"]}, "score": score }) return potential_matches
def find_best_sequence_heuristic(matches_list): if not matches_list or len(matches_list[0]) == 0: return [] best_sequence = [min(matches_list[0], key=lambda match: match["score"])] for next_matches in matches_list[1:]: current_box = best_sequence[-1]["box"] next_best_match = min(next_matches, key=lambda match: calculate_proximity_score(current_box, match["box"])) best_sequence.append(next_best_match) return best_sequence
def process_single_token_entity(dataframe, entity, threshold=75): best_match = None best_score = threshold entity = preprocess_entity(entity) for _, row in dataframe.iterrows(): ocr_text = preprocess_entity(row["text"]) score = fuzz.ratio(entity, ocr_text) if score > best_score: best_score = score best_match = { "left": row["left"], "right": row["right"], "top": row["top"], "bottom": row["bottom"] } return best_match
def process_multi_token_entity(dataframe, entity, threshold=85): tokens = entity.split() all_potential_matches = [find_potential_matches(dataframe, token, threshold) for token in tokens] if not all([matches for matches in all_potential_matches]): return None best_sequence = find_best_sequence_heuristic(all_potential_matches) if best_sequence: boxes_to_merge = [match["box"] for match in best_sequence] return merge_boxes(boxes_to_merge) return None
- Assistant
- User
Return the complete code
- Assistant
- User
Write complete code with all the required function
- Assistant