11from typing import List , Optional
22
33from mindee .documents .base import TypeApiPrediction
4+ from mindee .fields .base import FieldPositionMixin
45from mindee .geometry import (
5- Polygon ,
66 get_centroid ,
77 get_min_max_x ,
88 get_min_max_y ,
99 is_point_in_polygon_y ,
10- polygon_from_prediction ,
1110)
1211
1312
14- class Word :
13+ class OcrWord ( FieldPositionMixin ) :
1514 """A single word."""
1615
1716 confidence : float
18- polygon : Polygon
17+ """The confidence score."""
1918 text : str
19+ """The extracted text."""
2020
2121 def __init__ (self , prediction : TypeApiPrediction ):
2222 self .confidence = prediction ["confidence" ]
23- self .polygon = polygon_from_prediction (prediction ["polygon" ])
2423 self .text = prediction ["text" ]
24+ self ._set_position (prediction )
2525
2626 def __str__ (self ) -> str :
2727 return self .text
2828
2929
30- class OcrLine (List [Word ]):
30+ class OcrLine (List [OcrWord ]):
3131 """A list of words which are on the same line."""
3232
3333 def sort_on_x (self ) -> None :
@@ -41,18 +41,18 @@ def __str__(self) -> str:
4141class OcrPage :
4242 """OCR extraction for a single page."""
4343
44- all_words : List [Word ]
44+ all_words : List [OcrWord ]
4545 """All the words on the page, in semi-random order."""
4646 _lines : List [OcrLine ]
4747
4848 def __init__ (self , prediction : TypeApiPrediction ):
4949 self .all_words = [
50- Word (word_prediction ) for word_prediction in prediction ["all_words" ]
50+ OcrWord (word_prediction ) for word_prediction in prediction ["all_words" ]
5151 ]
5252 self ._lines = []
5353
5454 @staticmethod
55- def _are_words_on_same_line (current_word : Word , next_word : Word ) -> bool :
55+ def _are_words_on_same_line (current_word : OcrWord , next_word : OcrWord ) -> bool :
5656 """Determine if two words are on the same line."""
5757 current_in_next = is_point_in_polygon_y (
5858 get_centroid (current_word .polygon ),
@@ -66,7 +66,7 @@ def _are_words_on_same_line(current_word: Word, next_word: Word) -> bool:
6666
6767 def _to_lines (self ) -> List [OcrLine ]:
6868 """Order all the words on the page into lines."""
69- current : Optional [Word ] = None
69+ current : Optional [OcrWord ] = None
7070 indexes : List [int ] = []
7171 lines : List [OcrLine ] = []
7272
@@ -110,6 +110,7 @@ class MVisionV1:
110110 """Mindee Vision V1."""
111111
112112 pages : List [OcrPage ]
113+ """List of pages."""
113114
114115 def __init__ (self , prediction : TypeApiPrediction ):
115116 self .pages = [
@@ -124,6 +125,7 @@ class Ocr:
124125 """OCR extraction from the entire document."""
125126
126127 mvision_v1 : MVisionV1
128+ """Mindee Vision v1 results."""
127129
128130 def __init__ (self , prediction : TypeApiPrediction ):
129131 self .mvision_v1 = MVisionV1 (prediction ["mvision-v1" ])
0 commit comments