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

Update translator to use neon_lang_config #126

Merged
merged 6 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion .github/workflows/core_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ jobs:
run: |
pytest test/test_skill_utils.py
env:
GITHUB_TOKEN: ${{secrets.neon_token}}
GITHUB_TOKEN: ${{secrets.neon_token}}
- name: Test Language
NeonDaniel marked this conversation as resolved.
Show resolved Hide resolved
run: |
pytest test/test_language.py
8 changes: 3 additions & 5 deletions neon_core/language/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,13 @@ class TranslatorFactory:

@staticmethod
def create(module=None):
# TODO: Get this from neon config
config = Configuration.get().get("language", {})
module = module or config.get("translation_module", "google")
config = get_neon_lang_config()
module = module or config.get("translation_module", "libretranslate_plug")
if module not in DetectorFactory.CLASSES:
# plugin!
clazz = load_tx_plugin(module)
else:
clazz = TranslatorFactory.CLASSES.get(module)

config["keys"] = get_private_keys()
return clazz(config)

Expand All @@ -87,7 +85,7 @@ class DetectorFactory:
@staticmethod
def create(module=None):
config = get_neon_lang_config()
module = module or config.get("detection_module", "fastlang")
module = module or config.get("detection_module", "libretranslate_detection_plug")

if module not in DetectorFactory.CLASSES:
# plugin!
Expand Down
3 changes: 2 additions & 1 deletion requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest
pytest-cov
mock==4.0.2
mock==4.0.2
neon-lang-plugin-libretranslate>=0.1.2
Empty file added test/lang_res/en-uk/res
Empty file.
Empty file added test/lang_res/en-us/res
Empty file.
Empty file added test/lang_res/en/res
Empty file.
Empty file added test/lang_res/es-es/res
Empty file.
82 changes: 82 additions & 0 deletions test/test_language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# # NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System
# # All trademark and other rights reserved by their respective owners
# # Copyright 2008-2021 Neongecko.com Inc.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import sys
import unittest

from ovos_plugin_manager.templates.language import LanguageDetector, LanguageTranslator

sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from neon_core.language import *


resolved_languages = {
"en": "en",
"en-uk": "en-uk",
"en-au": "en",
"es-mx": "es-es",
"es-es": "es-es"
}


class LanguageTests(unittest.TestCase):
def test_get_lang_config(self):
config = get_lang_config()
self.assertIsInstance(config, dict)
self.assertIsInstance(config['internal'], str)
self.assertIsInstance(config['user'], str)

def test_get_language_dir_valid(self):
NeonKirill marked this conversation as resolved.
Show resolved Hide resolved
base_dir = os.path.join(os.path.dirname(__file__), "lang_res")
self.assertEqual(get_language_dir(base_dir), os.path.join(base_dir, "en-us"))
for search, result in resolved_languages.items():
self.assertEqual(get_language_dir(base_dir, search), os.path.join(base_dir, result))
# self.assertEqual(get_language_dir(base_dir, "en-uk"), os.path.join(base_dir, "en-uk"))
# self.assertEqual(get_language_dir(base_dir, "en-au"), os.path.join(base_dir, "en"))
# self.assertEqual(get_language_dir(base_dir, "es-mx"), os.path.join(base_dir, "es-es"))
# self.assertEqual(get_language_dir(base_dir, "es-es"), os.path.join(base_dir, "es-es"))
# self.assertEqual(get_language_dir(base_dir, "es"), os.path.join(base_dir, "es-es"))

def test_get_language_dir_invalid(self):
base_dir = os.path.join(os.path.dirname(__file__), "lang_res")
self.assertEqual(get_language_dir(base_dir, "ru"), os.path.join(base_dir, "ru"))
self.assertEqual(get_language_dir(base_dir, "ru-ru"), os.path.join(base_dir, "ru-ru"))

def test_translator(self):
translator = TranslatorFactory.create("libretranslate_plug")
self.assertIsInstance(translator, LanguageTranslator)
output = translator.translate("hello", "es-es", "en-us")
self.assertEqual(output.lower(), "hola")

def test_detector(self):
detector = DetectorFactory.create("libretranslate_detection_plug")
self.assertIsInstance(detector, LanguageDetector)
lang = detector.detect("hello")
self.assertEqual(lang, "en")


if __name__ == '__main__':
unittest.main()