mirror of
https://github.com/koodo-reader/koodo-reader.git
synced 2026-10-08 05:50:04 -04:00
feat: implement default OCR engine and language selection logic in ConvertDialog and Viewer components
This commit is contained in:
1 parent
539686b3f0
commit
589fac27e7
3 files changed
+94
-89
No files matched your search
@@ -6,13 +6,13 @@ import "./convertDialog.css";
|
||||
import { ConfigService } from "../../../assets/lib/kookit-extra-browser.min";
|
||||
import BookUtil from "../../../utils/file/bookUtil";
|
||||
import {
|
||||
getOcrPaddleLangList,
|
||||
ocrEngineList,
|
||||
ocrTesseractLangList,
|
||||
paraSpacingList,
|
||||
titleSizeList,
|
||||
} from "../../../constants/dropdownList";
|
||||
import toast from "react-hot-toast";
|
||||
import { getDefaultOcrEngine, getOcrLangList } from "../../../utils/common";
|
||||
|
||||
class ConvertDialog extends React.Component<
|
||||
ConvertDialogProps,
|
||||
@@ -27,36 +27,7 @@ class ConvertDialog extends React.Component<
|
||||
),
|
||||
};
|
||||
}
|
||||
getLangList = (engine: string) => {
|
||||
let list: any[];
|
||||
if (engine === "tesseract") {
|
||||
list = ocrTesseractLangList;
|
||||
} else if (engine === "official-ai-ocr") {
|
||||
list = [
|
||||
{
|
||||
label: "General",
|
||||
value: "general",
|
||||
lang: "general",
|
||||
},
|
||||
{
|
||||
label: "Accurate",
|
||||
value: "accurate",
|
||||
lang: "accurate",
|
||||
},
|
||||
];
|
||||
} else if (engine === "system-ocr") {
|
||||
list = [
|
||||
{
|
||||
label: "Auto",
|
||||
value: "auto",
|
||||
lang: "auto",
|
||||
},
|
||||
];
|
||||
} else {
|
||||
list = getOcrPaddleLangList();
|
||||
}
|
||||
return list;
|
||||
};
|
||||
|
||||
renderSwitchOption = (optionList: any[]) => {
|
||||
return optionList.map((item) => {
|
||||
return (
|
||||
@@ -170,16 +141,7 @@ class ConvertDialog extends React.Component<
|
||||
<select
|
||||
name=""
|
||||
className="lang-setting-dropdown"
|
||||
value={
|
||||
ConfigService.getReaderConfig(
|
||||
this.props.currentBook.description.indexOf("scanned") > -1
|
||||
? "scannedOcrEngine"
|
||||
: "textOcrEngine"
|
||||
) ||
|
||||
(this.props.currentBook.description.indexOf("scanned") > -1
|
||||
? "paddle"
|
||||
: "system-ocr")
|
||||
}
|
||||
value={getDefaultOcrEngine(this.props.currentBook)}
|
||||
onChange={(event) => {
|
||||
if (
|
||||
event.target.value === "official-ai-ocr" &&
|
||||
@@ -228,23 +190,15 @@ class ConvertDialog extends React.Component<
|
||||
this.forceUpdate();
|
||||
}}
|
||||
>
|
||||
{ocrEngineList
|
||||
.filter((item) => {
|
||||
if (!isElectron && item.value === "system-ocr") {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.map((item) => (
|
||||
<option
|
||||
value={item.value}
|
||||
key={item.value}
|
||||
className="lang-setting-option"
|
||||
>
|
||||
{this.props.t(item.label) +
|
||||
(item.isPro ? " (Pro)" : "")}
|
||||
</option>
|
||||
))}
|
||||
{ocrEngineList.map((item) => (
|
||||
<option
|
||||
value={item.value}
|
||||
key={item.value}
|
||||
className="lang-setting-option"
|
||||
>
|
||||
{this.props.t(item.label) + (item.isPro ? " (Pro)" : "")}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
{this.props.currentBook.description.indexOf("scanned") === -1 &&
|
||||
@@ -407,7 +361,7 @@ class ConvertDialog extends React.Component<
|
||||
);
|
||||
const currentLang = ConfigService.getReaderConfig("lang");
|
||||
|
||||
const match = this.getLangList(engine).find(
|
||||
const match = getOcrLangList(engine).find(
|
||||
(o: any) => o.lang === currentLang
|
||||
);
|
||||
return match ? match.value : "";
|
||||
@@ -431,13 +385,8 @@ class ConvertDialog extends React.Component<
|
||||
>
|
||||
{[
|
||||
{ label: "Please select", value: "", lang: "" },
|
||||
...(this.getLangList(
|
||||
ConfigService.getReaderConfig(
|
||||
this.props.currentBook.description.indexOf("scanned") >
|
||||
-1
|
||||
? "scannedOcrEngine"
|
||||
: "textOcrEngine"
|
||||
)
|
||||
...(getOcrLangList(
|
||||
getDefaultOcrEngine(this.props.currentBook)
|
||||
) || []),
|
||||
].map((item) => (
|
||||
<option
|
||||
|
||||
@@ -14,6 +14,8 @@ import Note from "../../models/Note";
|
||||
import PageWidget from "../pageWidget";
|
||||
import {
|
||||
BRUSH_WIDTHS,
|
||||
getDefaultOcrEngine,
|
||||
getDefaultOcrLang,
|
||||
getPageWidth,
|
||||
getParserRegex,
|
||||
getPdfPassword,
|
||||
@@ -25,16 +27,10 @@ import _ from "underscore";
|
||||
import { ConfigService } from "../../assets/lib/kookit-extra-browser.min";
|
||||
import * as Kookit from "../../assets/lib/kookit.min";
|
||||
import PopupRefer from "../../components/popups/popupRefer";
|
||||
import {
|
||||
ocrEngineList,
|
||||
ocrTesseractLangList,
|
||||
} from "../../constants/dropdownList";
|
||||
import DatabaseService from "../../utils/storage/databaseService";
|
||||
import { getOcrResult, getOcrResultV2 } from "../../utils/request/reader";
|
||||
import { BookHelper } from "../../assets/lib/kookit.min";
|
||||
import {
|
||||
parseWithSystemOCR,
|
||||
} from "../../utils/request/common";
|
||||
import { parseWithSystemOCR } from "../../utils/request/common";
|
||||
declare var window: any;
|
||||
let lock = false; //prevent from clicking too fasts
|
||||
|
||||
@@ -293,27 +289,19 @@ class Viewer extends React.Component<ViewerProps, ViewerState> {
|
||||
).includes(this.props.currentBook.key)
|
||||
? "yes"
|
||||
: "no",
|
||||
ocrLang: ConfigService.getReaderConfig(ocrLangKey)
|
||||
? ConfigService.getReaderConfig(ocrLangKey)
|
||||
: ConfigService.getReaderConfig(ocrEngineKey) === "tesseract"
|
||||
? ocrTesseractLangList.find(
|
||||
(item) => item.lang === ConfigService.getReaderConfig("lang")
|
||||
)?.value || "chi_sim"
|
||||
: ConfigService.getReaderConfig(ocrEngineKey)
|
||||
? ocrEngineList.find(
|
||||
(item) =>
|
||||
item.value === ConfigService.getReaderConfig(ocrEngineKey)
|
||||
)?.lang || "general"
|
||||
: "standard_v5_mobile",
|
||||
ocrLang: getDefaultOcrLang(
|
||||
getDefaultOcrEngine(this.props.currentBook),
|
||||
this.props.currentBook
|
||||
),
|
||||
externalWorker: {
|
||||
recognize:
|
||||
ConfigService.getReaderConfig(ocrEngineKey) === "system-ocr"
|
||||
getDefaultOcrEngine(this.props.currentBook) === "system-ocr"
|
||||
? parseWithSystemOCR
|
||||
: ConfigService.getReaderConfig(ocrLangKey) === "accurate"
|
||||
? getOcrResultV2
|
||||
: getOcrResult,
|
||||
? getOcrResultV2
|
||||
: getOcrResult,
|
||||
},
|
||||
ocrEngine: ConfigService.getReaderConfig(ocrEngineKey) || "paddle",
|
||||
ocrEngine: getDefaultOcrEngine(this.props.currentBook),
|
||||
serverRegion:
|
||||
getServerRegion() === "china" && this.props.isAuthed
|
||||
? "china"
|
||||
|
||||
@@ -31,6 +31,10 @@ import { driveList } from "../constants/driveList";
|
||||
import { updateUserConfig } from "./request/user";
|
||||
import { languageCNMap, languageENMap } from "../constants/ttsList";
|
||||
import { BookHelper } from "../assets/lib/kookit.min";
|
||||
import {
|
||||
getOcrPaddleLangList,
|
||||
ocrTesseractLangList,
|
||||
} from "../constants/dropdownList";
|
||||
declare var window: any;
|
||||
export const supportedFormats = [
|
||||
".epub",
|
||||
@@ -1917,3 +1921,67 @@ export const BRUSH_COLORS = [
|
||||
];
|
||||
|
||||
export const BRUSH_WIDTHS = [2, 4, 8, 14];
|
||||
export const getDefaultOcrEngine = (currentBook: any) => {
|
||||
const engine = ConfigService.getReaderConfig(
|
||||
currentBook.description.indexOf("scanned") > -1
|
||||
? "scannedOcrEngine"
|
||||
: "textOcrEngine"
|
||||
);
|
||||
if (engine) return engine;
|
||||
if (currentBook.description.indexOf("scanned") > -1) {
|
||||
return "paddle";
|
||||
} else {
|
||||
return "system-ocr";
|
||||
}
|
||||
};
|
||||
export const getOcrLangList = (engine: string) => {
|
||||
let list: any[];
|
||||
if (engine === "tesseract") {
|
||||
list = ocrTesseractLangList;
|
||||
} else if (engine === "official-ai-ocr") {
|
||||
list = [
|
||||
{
|
||||
label: "General",
|
||||
value: "general",
|
||||
lang: "general",
|
||||
},
|
||||
{
|
||||
label: "Accurate",
|
||||
value: "accurate",
|
||||
lang: "accurate",
|
||||
},
|
||||
];
|
||||
} else if (engine === "system-ocr") {
|
||||
list = [
|
||||
{
|
||||
label: "Auto",
|
||||
value: "auto",
|
||||
lang: "auto",
|
||||
},
|
||||
];
|
||||
} else {
|
||||
list = getOcrPaddleLangList();
|
||||
}
|
||||
return list;
|
||||
};
|
||||
export const getDefaultOcrLang = (engine: string, currentBook: any) => {
|
||||
const lang = ConfigService.getReaderConfig(
|
||||
currentBook.description.indexOf("scanned") > -1
|
||||
? "scannedOcrLang"
|
||||
: "textOcrLang"
|
||||
);
|
||||
if (lang) return lang;
|
||||
if (engine === "tesseract") {
|
||||
return (
|
||||
ocrTesseractLangList.find(
|
||||
(item) => item.lang === ConfigService.getReaderConfig("lang")
|
||||
)?.value || "chi_sim"
|
||||
);
|
||||
} else if (engine === "official-ai-ocr") {
|
||||
return "general";
|
||||
} else if (engine === "system-ocr") {
|
||||
return "auto";
|
||||
} else if (engine === "paddle") {
|
||||
return "standard_v5_mobile";
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user