Updated tests regarding the usage of MsdState

This commit is contained in:
2026-05-29 13:54:13 +02:00
parent 9ab9df76f6
commit 4e717ee587
4 changed files with 16 additions and 17 deletions
+6 -7
View File
@@ -287,10 +287,11 @@ class Msd:
if len(states) == 0:
states.add(self.State.UNKNOWN)
if self.expected_state not in states:
error_message = f"Given Msd '{self.code}' is unknown for expected state '{self.expected_state.name}'."
if self.require_valid:
raise MsdException(f"Given msd '{self.codecode}' is '{self.state.name}', but expected state is '{self.expected_state.name}'.")
raise MsdException(error_message)
else:
print(f"[WARN] The Msd '{self.code}' is unknown for expected state '{self.expected_state.name}'.")
print(f"[WARN] {error_message}")
return max(states)
def __str__(self):
@@ -370,12 +371,10 @@ class Converter:
msd(Msd): the JOS MSD to convert
language(str): the language for the Properties object to be generated: "en" (English) or "sl" (Slovene)
lemma(str): the lemma of the word form with the MSD
require_valid_flag(boolean): whether to raise a MsdException or only warn if a non-standard MSD is provided
warn_level_flag(boolean): whether to warn if cannot be sure of level of a property
Returns:
Properties: the result of the conversion of the Msd in the language requested
"""
category_char = msd.code[0].lower()
value_chars = msd.code[1:]
@@ -402,7 +401,7 @@ class Converter:
form_feature_map[feature_name] = feature_value
return Properties(category_name, lexeme_feature_map, form_feature_map, language)
def properties_to_msd(self, properties, language, expected_state=Msd.State.FULL):
def properties_to_msd(self, properties, language, expected_state=Msd.State.FULL, require_valid=False):
"""Convert Properties to Msd.
The language of the generated Msd is specified and can differ from the Properties language.
@@ -434,7 +433,7 @@ class Converter:
msd_code += '-'
i += 1
msd_code += position_map[position]
msd = Msd(msd_code, language, expected_state=expected_state)
msd = Msd(msd_code, language, expected_state=expected_state, require_valid=require_valid)
return msd
def msd_to_ud(self, msd, lemma):
@@ -448,7 +447,7 @@ class Converter:
"""
if msd.state != Msd.State.FULL:
raise MsdException(f"Msd must be full to be converted to UD.")
raise MsdException("Msd must be full to be converted to UD.")
upos_category, *upos_features = self.mte_to_ud_features[msd.code].split()
final_upos = ""
@@ -1,6 +1,6 @@
import unittest
from conversion_utils.jos_msds_and_properties import Converter, Msd, MsdException
from conversion_utils.jos_msds_and_properties import Converter, Msd, MsdException, MsdState
class JosMsdToPropertiesTestCase(unittest.TestCase):
@@ -57,14 +57,14 @@ class JosMsdToPropertiesTestCase(unittest.TestCase):
self.assertEqual(properties.form_feature_map, {})
def test_good_msd_with_require_valid(self):
properties = self.converter.msd_to_properties(Msd('Ncfpd', 'en'), 'en', require_valid_flag=True)
properties = self.converter.msd_to_properties(Msd('Ncfpd', 'en', require_valid=True), 'en')
self.assertEqual(properties.language, 'en')
self.assertEqual(properties.category, 'noun')
self.assertEqual(properties.lexeme_feature_map, {'type':'common', 'gender':'feminine'})
self.assertEqual(properties.form_feature_map, {'number':'plural', 'case':'dative'})
def test_bad_msd(self):
properties = self.converter.msd_to_properties(Msd('N---d', 'en'), 'en')
properties = self.converter.msd_to_properties(Msd('N---d', 'en', expected_state=MsdState.UNKNOWN), 'en')
self.assertEqual(properties.language, 'en')
self.assertEqual(properties.category, 'noun')
self.assertEqual(properties.lexeme_feature_map, {})
@@ -72,7 +72,7 @@ class JosMsdToPropertiesTestCase(unittest.TestCase):
def test_bad_msd_with_require_valid(self):
try:
self.converter.msd_to_properties(Msd('N---d', 'en'), 'en', require_valid_flag=True)
self.converter.msd_to_properties(Msd('N---d', 'en', require_valid=True), 'en')
fails = False
except MsdException:
fails = True
@@ -1,6 +1,6 @@
import unittest
from conversion_utils.jos_msds_and_properties import Converter, Properties, MsdException, Msd
from conversion_utils.jos_msds_and_properties import Converter, Properties, MsdException, Msd, MsdState
class JosPropertiesToMsdTestCase(unittest.TestCase):
@@ -43,12 +43,12 @@ class JosPropertiesToMsdTestCase(unittest.TestCase):
self.assertEqual(msd.code, 'U')
def test_good_msd_with_require_valid(self):
msd = self.converter.properties_to_msd(Properties('noun', {'type':'common', 'gender':'feminine'}, {'number':'dual', 'case':'nominative'}, 'en'), 'en', require_valid_flag=True)
msd = self.converter.properties_to_msd(Properties('noun', {'type':'common', 'gender':'feminine'}, {'number':'dual', 'case':'nominative'}, 'en'), 'en', require_valid=True)
self.assertEqual(msd.language, 'en')
self.assertEqual(msd.code, 'Ncfdn')
def test_bad_msd(self):
msd = self.converter.properties_to_msd(Properties('noun', {'type':'common'}, {'number':'dual'}, 'en'), 'en')
msd = self.converter.properties_to_msd(Properties('noun', {'type':'common'}, {'number':'dual'}, 'en'), 'en', expected_state=MsdState.UNKNOWN)
self.assertEqual(msd.language, 'en')
self.assertEqual(msd.code, 'Nc-d')
@@ -65,7 +65,7 @@ class JosPropertiesToMsdTestCase(unittest.TestCase):
def test_msd_to_jos_partial_msd(self):
try:
self.converter.msd_to_ud(Msd('Soz', 'sl'), 'vlada')
self.converter.msd_to_ud(Msd('Soz', 'sl', expected_state=MsdState.PARTIAL), 'vlada')
fails = False
except MsdException:
fails = True
@@ -73,7 +73,7 @@ class JosPropertiesToMsdTestCase(unittest.TestCase):
def test_bad_msd_with_require_valid(self):
try:
self.converter.properties_to_msd(Properties('noun', {'type':'common'}, {'number':'dual'}, 'en'), 'en', require_valid_flag=True)
self.converter.properties_to_msd(Properties('noun', {'type':'common'}, {'number':'dual'}, 'en'), 'en', require_valid=True)
fails = False
except MsdException:
fails = True
+1 -1
View File
@@ -6,7 +6,7 @@ with open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
setup(name='cjvt_conversion_utils',
version='0.4',
version='0.4.1',
description='CJVT conversion utilities',
long_description=long_description,
long_description_content_type="text/markdown",