Skip to content

Commit 0b9c6da

Browse files
enh: better exception catcher in language detecction
Signed-off-by: thiswillbeyourgithub <[email protected]>
1 parent 80f7f32 commit 0b9c6da

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

wdoc/utils/misc.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@
6262
import ftlangdetect
6363

6464
def language_detector(text: str) -> float:
65-
return ftlangdetect.detect(text.lower())["score"]
65+
try:
66+
return ftlangdetect.detect(text.lower())["score"]
67+
except Exception as e:
68+
logger.info(
69+
f"Error when running ftlangdetect: '{e}'. First 100 chars of the str were '{text[:100]}'. Assuming probability of 1."
70+
)
71+
return 1.0
6672

6773
assert isinstance(language_detector("This is a test"), float)
6874
except Exception as err:
@@ -77,7 +83,13 @@ def language_detector(text: str) -> float:
7783
import langdetect
7884

7985
def language_detector(text: str) -> float:
80-
return langdetect.detect_langs(text.lower())[0].prob
86+
try:
87+
return langdetect.detect_langs(text.lower())[0].prob
88+
except Exception as e:
89+
logger.info(
90+
f"Error when running langdetect: '{e}'. First 100 chars of the str were '{text[:100]}'. Assuming probability of 1."
91+
)
92+
return 1.0
8193

8294
assert isinstance(language_detector("This is a test"), float)
8395
except Exception as err:

0 commit comments

Comments
 (0)