-
-
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.
- Loading branch information
Vu Anh
committed
Jun 22, 2024
1 parent
d8d2159
commit 95434e5
Showing
1 changed file
with
24 additions
and
22 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,17 @@ | |
# -*- coding: utf-8 -*- | ||
import os | ||
import sys | ||
from functools import lru_cache | ||
|
||
|
||
__author__ = """Vu Anh""" | ||
__email__ = '[email protected]' | ||
|
||
# Check python version | ||
try: | ||
version_info = sys.version_info | ||
if version_info < (3, 6, 0): | ||
raise RuntimeError("underthesea requires Python 3.6 or later") | ||
if version_info < (3, 7, 0): | ||
raise RuntimeError("underthesea requires Python 3.y or later") | ||
except Exception: | ||
pass | ||
|
||
|
@@ -40,33 +42,33 @@ | |
from .pipeline.chunking import chunk | ||
from .pipeline.ner import ner | ||
|
||
try: | ||
from underthesea.pipeline.classification import classify | ||
except Exception: | ||
pass | ||
try: | ||
from underthesea.pipeline.sentiment import sentiment | ||
except Exception: | ||
pass | ||
|
||
try: | ||
from underthesea.pipeline.lang_detect import lang_detect | ||
except Exception as e: | ||
print(e) | ||
|
||
optional_imports = { | ||
'classify': 'underthesea.pipeline.classification', | ||
'sentiment': 'underthesea.pipeline.sentiment', | ||
'lang_detect': 'underthesea.pipeline.lang_detect', | ||
'dependency_parse': 'underthesea.pipeline.dependency_parse' | ||
} | ||
|
||
# lazy loading | ||
def dependency_parse(*args, **kwargs): | ||
from underthesea.pipeline.dependency_parse import dependency_parse | ||
return dependency_parse(*args, **kwargs) | ||
@lru_cache(maxsize=None) | ||
def get_optional_import(module_name, object_name): | ||
try: | ||
module = __import__(module_name, fromlist=[object_name]) | ||
return getattr(module, object_name) | ||
except ImportError: | ||
return None | ||
|
||
for name, module in optional_imports.items(): | ||
globals()[name] = get_optional_import(module, name) | ||
|
||
__all__ = [ | ||
'sent_tokenize', | ||
'text_normalize', | ||
'word_tokenize', 'pos_tag', 'chunk', | ||
'word_tokenize', | ||
'pos_tag', | ||
'chunk', | ||
'ner', | ||
'lang_detect', | ||
'classify', 'sentiment', | ||
'classify', | ||
'sentiment', | ||
'dependency_parse' | ||
] |