@xuda.io/drive_module 1.1.975 → 1.1.977

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +24 -22
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -26,7 +26,7 @@ const crypto = require("node:crypto");
26
26
  const unzipper = require("unzipper");
27
27
  const fs = require("fs");
28
28
  const tesseract = require("node-tesseract-ocr");
29
-
29
+ const countryLanguage = require("country-language");
30
30
  ////////////////////////////////////////////
31
31
 
32
32
  [
@@ -1723,21 +1723,31 @@ const ocr_drive_file = async function (app_id, doc) {
1723
1723
  );
1724
1724
 
1725
1725
  var ocr = "";
1726
- const generalConfig = { oem: 1, psm: 3 };
1727
- const { franc } = await import("franc");
1728
1726
 
1727
+ function detectLanguageByCountry(cfCountry) {
1728
+ const countryData = countryLanguage.getCountry(cfCountry);
1729
+
1730
+ // Check if the country data is available
1731
+ if (
1732
+ countryData &&
1733
+ countryData.languages &&
1734
+ countryData.languages.length > 0
1735
+ ) {
1736
+ // Return the first official language of the country
1737
+ return countryData.languages[0].iso639_1;
1738
+ }
1739
+
1740
+ // Default to English if no language found
1741
+ return "en";
1742
+ }
1743
+ let lang = detectLanguageByCountry(doc.country);
1744
+ if (lang !== "en") {
1745
+ lang += "+en";
1746
+ }
1747
+ const generalConfig = { oem: 1, psm: 3, lang };
1729
1748
  switch (doc.mime.split("/")[0]) {
1730
1749
  case "image": {
1731
- const initialText = await tesseract.recognize(
1732
- target_file,
1733
- generalConfig
1734
- );
1735
-
1736
- // Detect the language from the extracted text
1737
- const detectedLang = franc(initialText);
1738
- debugger;
1739
- const tesseractLang = detectedLang || "eng";
1740
- ocr = await tesseract.recognize(target_file, tesseractLang);
1750
+ ocr = await tesseract.recognize(target_file, generalConfig);
1741
1751
 
1742
1752
  break;
1743
1753
  }
@@ -1749,15 +1759,7 @@ const ocr_drive_file = async function (app_id, doc) {
1749
1759
 
1750
1760
  const images = await convertPDF(target_file);
1751
1761
  for await (const imageData of images) {
1752
- const initialText = await tesseract.recognize(
1753
- imageData.path,
1754
- generalConfig
1755
- );
1756
-
1757
- // Detect the language from the extracted text
1758
- const detectedLang = franc(initialText);
1759
- const tesseractLang = detectedLang || "eng";
1760
- ocr += await tesseract.recognize(imageData.path, tesseractLang);
1762
+ ocr += await tesseract.recognize(imageData.path, generalConfig);
1761
1763
  }
1762
1764
  break;
1763
1765
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/drive_module",
3
- "version": "1.1.975",
3
+ "version": "1.1.977",
4
4
  "description": "Xuda Drive Server Module",
5
5
  "main": "index.js",
6
6
  "dependencies": {