-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,9 @@ | |
], | ||
'prompt': [ | ||
'openai' | ||
], | ||
'lang-detect': [ | ||
'fasttext ' | ||
] | ||
} | ||
setup( | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
from unittest import TestCase | ||
from underthesea import lang_detect | ||
|
||
|
||
class TestLangDetect(TestCase): | ||
def test_lang_detect_1(self): | ||
actual = lang_detect("Bộ Công Thương xóa một tổng cục, giảm nhiều đầu mối") | ||
expected = "vi" | ||
self.assertEqual(actual, expected) | ||
|
||
def test_lang_detect_2(self): | ||
actual = lang_detect("Ceci est un texte français.") | ||
expected = "fr" | ||
self.assertEqual(actual, expected) | ||
|
||
def test_lang_detect_3(self): | ||
actual = lang_detect("如來の妙色身、 世間與に等しきは無し。") | ||
expected = "ja" | ||
self.assertEqual(actual, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import fasttext | ||
import os | ||
from underthesea.model_fetcher import ModelFetcher | ||
|
||
fasttext.FastText.eprint = lambda x: None | ||
lang_detect_model = None | ||
|
||
|
||
def lang_detect(text): | ||
global lang_detect_model | ||
model_name = "LANG_DETECT_FAST_TEXT" | ||
model_path = ModelFetcher.get_model_path(model_name) | ||
if not lang_detect_model: | ||
if not os.path.exists(model_path): | ||
ModelFetcher.download(model_name) | ||
try: | ||
lang_detect_model = fasttext.load_model(str(model_path)) | ||
except Exception: | ||
pass | ||
|
||
predictions = lang_detect_model.predict(text) | ||
language = predictions[0][0].replace('__label__', '') | ||
return language |