def flattenwords(anns): flattenedwords = [] imageid = 0 for ann in anns: paragraphs =…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
def flatten_words(anns): flattened_words = [] image_id = 0 for ann in anns: paragraphs = ann["paragraphs"] for paragraph in paragraphs: lines = paragraph["lines"] for line in lines: words = line["words"] for word in words: word_list = [word["text"], word["vertices"], image_id, ann["image_id"], word["legible"], word["vertical"]] flattened_words.append(word_list) image_id += 1 return flattened_words
def flatten_and_save_words_to_df(anns): flattened_words = flatten_words(anns) df = pd.DataFrame(flattened_words, columns=["text", "vertices", "index_image", "image_id", "legible", "vertical"]) df = df.reindex(columns=["image_id", "text", "vertices", "index_image", "legible", "vertical"]) return df
- Assistant
Your code looks good! Here's a slightly modified version with some improvements:
- I added type hints to make the code more readable and maintainable.
- I used list comprehension to make the
flatten_wordsfunction more concise. - I added a check to ensure that the
annsparameter is a list before processing it.
Here's the updated code:
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.