-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetector.py
More file actions
25 lines (20 loc) · 751 Bytes
/
detector.py
File metadata and controls
25 lines (20 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from langdetect import detect, DetectorFactory
from langdetect.lang_detect_exception import LangDetectException
# Ensure consistent results
DetectorFactory.seed = 0
class Detector:
def __init__(self, target_langs=None, min_chars=50):
# By default, target Persian (fa) which covers Dari as well
self.target_langs = target_langs or ["fa"]
self.min_chars = min_chars
def is_valid_language(self, text: str) -> bool:
"""
Check if the text is in Dari/Persian (fa).
"""
if not text or len(text) < self.min_chars:
return False
try:
lang = detect(text)
return lang in self.target_langs
except LangDetectException:
return False