TiberiuCristianLeon commited on
Commit
f8289a7
·
verified ·
1 Parent(s): 651c500

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -24,19 +24,22 @@ def timer(func):
24
  return detected_lang, confidence, execution_times
25
  return detection
26
 
27
- def langdetect(input_text):
28
- from langdetect import detect, detect_langs
29
- from langdetect import DetectorFactory
30
- DetectorFactory.seed = 0
31
- langcode = detect(input_text)
32
- langecode_probabilities: list[Language] = detect_langs(input_text)
33
- return langcode, round(number=langecode_probabilities[0].prob * 100, ndigits=2)
34
-
35
- def langid(self) -> tuple[str, float]:
36
- import langid
37
- result: tuple[str, float] = langid.classify(self.text)
38
- langcode, langecode_probabilities = result
39
- return langcode, round(number=langecode_probabilities * 10, ndigits=2)
 
 
 
40
 
41
  @timer
42
  def detect_language(input_text: str, used_libraries: list[str]) -> tuple[str, str]:
@@ -56,13 +59,14 @@ def detect_language(input_text: str, used_libraries: list[str]) -> tuple[str, st
56
  ("en", 1.0)
57
  """
58
  print(used_libraries)
 
59
  detections = []
60
  if 'langdetect' in used_libraries:
61
- langcode, confidence_score = langdetect(input_text)
62
  tupletoappend = langcode, confidence_score
63
  detections.append(tupletoappend)
64
  if 'langid' in used_libraries:
65
- langcode, confidence_score = langid(input_text)
66
  tupletoappend = langcode, confidence_score
67
  detections.append(tupletoappend)
68
  return detections
 
24
  return detected_lang, confidence, execution_times
25
  return detection
26
 
27
+ class Detect():
28
+ def __init__(self, text: str) -> None:
29
+ self.text: str = text
30
+ def langdetect(self) -> tuple[str, float]:
31
+ from langdetect import detect, detect_langs
32
+ from langdetect import DetectorFactory
33
+ DetectorFactory.seed = 0
34
+ langcode = detect(self.text)
35
+ langecode_probabilities: list[Language] = detect_langs(self.text)
36
+ return langcode, round(number=langecode_probabilities[0].prob * 100, ndigits=2)
37
+
38
+ def langid(self) -> tuple[str, float]:
39
+ import langid
40
+ result: tuple[str, float] = langid.classify(self.text)
41
+ langcode, langecode_probabilities = result
42
+ return langcode, round(number=langecode_probabilities * 10, ndigits=2)
43
 
44
  @timer
45
  def detect_language(input_text: str, used_libraries: list[str]) -> tuple[str, str]:
 
59
  ("en", 1.0)
60
  """
61
  print(used_libraries)
62
+ detectinstance = Detect(input_text)
63
  detections = []
64
  if 'langdetect' in used_libraries:
65
+ langcode, confidence_score = detectinstance.langdetect(input_text)
66
  tupletoappend = langcode, confidence_score
67
  detections.append(tupletoappend)
68
  if 'langid' in used_libraries:
69
+ langcode, confidence_score = detectinstance.langid(input_text)
70
  tupletoappend = langcode, confidence_score
71
  detections.append(tupletoappend)
72
  return detections