-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b20361
commit 1403898
Showing
9 changed files
with
771 additions
and
41 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from textblob import TextBlob | ||
|
||
|
||
class TextBlobCorrector: | ||
def __init__(self): | ||
return | ||
|
||
def correct(self, text: str) -> str: | ||
return str(TextBlob(text).correct()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,4 +37,5 @@ uvicorn>=0.18.0,<=0.23.2 | |
wget==3.2 | ||
xgbfir==0.3.1 | ||
xgboost>=1.1.1,<1.2.0 | ||
xlrd==1.2.0 | ||
xlrd==1.2.0 | ||
textblob==0.17.1 |
359 changes: 359 additions & 0 deletions
359
resources/benchmarks/tesseract_benchmark_sage-correction.txt
Large diffs are not rendered by default.
Oops, something went wrong.
259 changes: 259 additions & 0 deletions
259
resources/benchmarks/tesseract_benchmark_with_correction.txt
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import os | ||
from typing import Tuple | ||
|
||
import torch | ||
from sage.spelling_correction.corrector import Corrector | ||
from sage.spelling_correction import AvailableCorrectors | ||
from sage.spelling_correction import RuM2M100ModelForSpellingCorrection | ||
|
||
''' | ||
Install sage library (for ocr correction step): | ||
git clone https://github.com/ai-forever/sage.git | ||
cd sage | ||
pip install . | ||
pip install -r requirements.txt | ||
Note: sage use 5.2 Gb GPU ...... | ||
''' | ||
USE_GPU = True | ||
|
||
|
||
def correction(model: Corrector, ocr_text: str) -> str: | ||
|
||
corrected_lines = [] | ||
for line in ocr_text.split("\n"): | ||
corrected_lines.append(model.correct(line)[0]) | ||
corrected_text = "\n".join(corrected_lines) | ||
|
||
return corrected_text | ||
|
||
|
||
def init_correction_step(cache_dir: str) -> Tuple[Corrector, str]: | ||
|
||
corrected_path = os.path.join(cache_dir, "result_corrected") | ||
os.makedirs(corrected_path, exist_ok=True) | ||
corrector = RuM2M100ModelForSpellingCorrection.from_pretrained(AvailableCorrectors.m2m100_1B.value) # 4.49 Gb model (pytorch_model.bin) | ||
if torch.cuda.is_available() and USE_GPU: | ||
corrector.model.to(torch.device("cuda:0")) | ||
print("use CUDA") | ||
else: | ||
print("use CPU") | ||
return corrector, corrected_path | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from textblob import TextBlob | ||
|
||
|
||
class TextBlobCorrector: | ||
def __init__(self): | ||
return | ||
|
||
def correct(self, text: str) -> str: | ||
return str(TextBlob(text).correct()) |