Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

language plugin factories #10

Merged
merged 2 commits into from
Nov 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions ovos_plugin_manager/language.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from ovos_plugin_manager.utils import load_plugin, find_plugins, PluginTypes
from ovos_utils.configuration import read_mycroft_config
from ovos_utils.log import LOG


def find_tx_plugins():
Expand All @@ -17,3 +19,84 @@ def load_lang_detect_plugin(module_name):
return load_plugin(module_name, PluginTypes.LANG_DETECT)


class OVOSLangDetectionFactory:
""" replicates the base neon class, but uses only OPM enabled plugins"""
MAPPINGS = {
"libretranslate": "libretranslate_detection_plug",
"google": "googletranslate_detection_plug",
"amazon": "amazontranslate_detection_plug",
"cld2": "cld2_plug",
"cld3": "cld3_plug",
"langdetect": "langdetect_plug",
"fastlang": "fastlang_plug",
"lingua_podre": "lingua_podre_plug"
}

@staticmethod
def create(config=None):
"""Factory method to create a LangDetection engine based on configuration.

The configuration file ``mycroft.conf`` contains a ``language`` section with
the name of a LangDetection module to be read by this method.

"language": {
"detection_module": <engine_name>
}
"""
try:
config = config or read_mycroft_config()
if "language" in config:
config = config["language"]
lang_module = config.get("detection_module", "libretranslate_detection_plug")
if lang_module in OVOSLangDetectionFactory.MAPPINGS:
lang_module = OVOSLangDetectionFactory.MAPPINGS[lang_module]

clazz = load_lang_detect_plugin(lang_module)
if clazz is None:
raise ValueError(f'Language Detection plugin {lang_module} not found')
LOG.info(f'Loaded the Language Detection plugin {lang_module}')
return clazz()
except Exception:
# The Language Detection backend failed to start.
LOG.exception('The selected Language Detection plugin could not be loaded!')
raise


class OVOSLangTranslationFactory:
""" replicates the base neon class, but uses only OPM enabled plugins"""
MAPPINGS = {
"libretranslate": "libretranslate_plug",
"google": "googletranslate_plug",
"amazon": "amazontranslate_plug",
"apertium": "apertium_plug"
}

@staticmethod
def create(config=None):
"""Factory method to create a LangTranslation engine based on configuration.

The configuration file ``mycroft.conf`` contains a ``language`` section with
the name of a LangDetection module to be read by this method.


"language": {
"translation_module": <engine_name>
}
"""
try:
config = config or read_mycroft_config()
if "language" in config:
config = config["language"]
lang_module = config.get("detection_module", "libretranslate_detection_plug")
if lang_module in OVOSLangDetectionFactory.MAPPINGS:
lang_module = OVOSLangDetectionFactory.MAPPINGS[lang_module]

clazz = load_tx_plugin(lang_module)
if clazz is None:
raise ValueError(f'Language Translation plugin {lang_module} not found')
LOG.info(f'Loaded the Language Translation plugin {lang_module}')
return clazz()
except Exception:
# The Language Detection backend failed to start.
LOG.exception('The selected Language Translation plugin could not be loaded!')
raise