diff --git a/pythainlp/corpus/core.py b/pythainlp/corpus/core.py index 664207158..70f0fa7e0 100644 --- a/pythainlp/corpus/core.py +++ b/pythainlp/corpus/core.py @@ -632,10 +632,10 @@ def _check_version(cause: str) -> bool: check = _version2int(temp_parts[0]) < v < _version2int(temp_parts[1]) elif cause.startswith("<="): temp = cause.replace("<=", "") - check = v <= _version2int(temp[0]) + check = v <= _version2int(temp) elif cause.startswith("<"): temp = cause.replace("<", "") - check = v < _version2int(temp[0]) + check = v < _version2int(temp) return check diff --git a/tests/core/test_corpus.py b/tests/core/test_corpus.py index 58ea2ec34..44854a99a 100644 --- a/tests/core/test_corpus.py +++ b/tests/core/test_corpus.py @@ -31,6 +31,7 @@ tnc, ttc, ) +from pythainlp.corpus.core import _check_version, _version2int from pythainlp.corpus.util import revise_newmm_default_wordset @@ -621,3 +622,18 @@ def test_thai_synonyms_loads_valid_rows(self): self.assertEqual( result["synonym"], [["cat", "kitty"], ["dog"]] ) + + def test_check_version(self): + # Reproduce: _check_version with <= and < used temp[0] which + # only gets the first character of the version string instead + # of the full version, causing incorrect comparisons. + self.assertTrue(_check_version("*")) + self.assertTrue(_check_version("<=9999.9.9")) + self.assertTrue(_check_version("<9999.9.9")) + self.assertTrue(_check_version(">=0.0.1")) + self.assertTrue(_check_version(">0.0.1")) + self.assertFalse(_check_version("<=0.0.1")) + self.assertFalse(_check_version("<0.0.1")) + # version2int consistency + self.assertNotEqual(_version2int("5.3.3"), _version2int("5")) + self.assertEqual(_version2int("2.0"), _version2int("2.0.0"))