Skip to content

Commit

Permalink
LanguageModel - Fix languages order and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Mar 22, 2024
1 parent f6a2e69 commit 153e06f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion orangecontrib/text/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def __init__(
"""
if languages is None:
# if languages not provided take all available languages
languages = sorted(filter(None, ISO2LANG), key=ISO2LANG.get)
languages = filter(None, ISO2LANG)
languages = sorted(languages, key=ISO2LANG.get)
if include_none:
languages = [None] + languages
super().__init__(iterable=languages)
Expand Down
29 changes: 28 additions & 1 deletion orangecontrib/text/tests/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,34 @@
from Orange.data import StringVariable, Domain

from orangecontrib.text import Corpus
from orangecontrib.text.language import detect_language, ISO2LANG
from orangecontrib.text.language import detect_language, ISO2LANG, LanguageModel


class TestLanguageModel(TestCase):
def test_model_without_languages(self):
# no None, all languages
lm = LanguageModel()
self.assertEqual(len(ISO2LANG) - 1, lm.rowCount())
all_langs = [lm.data(lm.index(i)) for i in range(lm.rowCount())]
expected = sorted(list(ISO2LANG.values())[:-1])
self.assertEqual(expected, all_langs)

lm = LanguageModel(include_none=True)
self.assertEqual(len(ISO2LANG), lm.rowCount())
all_langs = [lm.data(lm.index(i)) for i in range(lm.rowCount())]
expected = sorted(list(ISO2LANG.values())[:-1])
self.assertEqual(["(no language)"] + expected, all_langs)

def test_model_with_languages(self):
lm = LanguageModel(include_none=True, languages=["en", "ar", "it"])
self.assertEqual(4, lm.rowCount())
all_langs = [lm.data(lm.index(i)) for i in range(lm.rowCount())]
self.assertEqual(["(no language)", "Arabic", "English", "Italian"], all_langs)

lm = LanguageModel(languages=["en", "ar", "it"])
self.assertEqual(3, lm.rowCount())
all_langs = [lm.data(lm.index(i)) for i in range(lm.rowCount())]
self.assertEqual(["Arabic", "English", "Italian"], all_langs)


class TestLanguage(TestCase):
Expand Down

0 comments on commit 153e06f

Please sign in to comment.