Moved converter specifications to constructor

This commit is contained in:
Cyprian Laskowski 2021-09-10 14:12:04 +02:00
parent f14879f619
commit c078765774
2 changed files with 14 additions and 11 deletions

View File

@ -198,10 +198,13 @@ class Msd:
class MsdToProperties:
def convert(self, specifications, msd, lemma, properties_language):
def __init__(self, specifications):
self.specifications = specifications
def convert(self, msd, lemma, properties_language):
category_char = msd.code[0].lower()
value_chars = msd.code[1:]
category = specifications.find_category(category_char, msd.language)
category = self.specifications.find_category(category_char, msd.language)
feature_value_list = []
for (index, value_char) in enumerate(value_chars, start=1):
if (value_char != '-'):

View File

@ -9,53 +9,53 @@ class JosMsdToPropertiesTestCase(unittest.TestCase):
def setUp(self):
specifications_file_name = os.path.join(os.path.dirname(__file__), '../resources/msd-sl.spc.xml')
builder = SpecificationsBuilder()
self.specifications = builder.build(specifications_file_name)
self.converter = MsdToProperties()
specifications = builder.build(specifications_file_name)
self.converter = MsdToProperties(specifications)
def test_en_en(self):
properties = self.converter.convert(self.specifications, Msd('Ncfpd', 'en'), 'miza', 'en')
properties = self.converter.convert(Msd('Ncfpd', 'en'), 'miza', '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_en_sl(self):
properties = self.converter.convert(self.specifications, Msd('Ncfpd', 'en'), 'miza', 'sl')
properties = self.converter.convert(Msd('Ncfpd', 'en'), 'miza', 'sl')
self.assertEqual(properties.language, 'sl')
self.assertEqual(properties.category, 'samostalnik')
self.assertEqual(properties.lexeme_feature_map, {'vrsta':'občno_ime', 'spol':'ženski'})
self.assertEqual(properties.form_feature_map, {'število':'množina', 'sklon':'dajalnik'})
def test_sl_en(self):
properties = self.converter.convert(self.specifications, Msd('Sozmd', 'sl'), 'miza', 'en')
properties = self.converter.convert(Msd('Sozmd', 'sl'), 'miza', '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_sl_sl(self):
properties = self.converter.convert(self.specifications, Msd('Sozmd', 'sl'), 'miza', 'sl')
properties = self.converter.convert(Msd('Sozmd', 'sl'), 'miza', 'sl')
self.assertEqual(properties.language, 'sl')
self.assertEqual(properties.category, 'samostalnik')
self.assertEqual(properties.lexeme_feature_map, {'vrsta':'občno_ime', 'spol':'ženski'})
self.assertEqual(properties.form_feature_map, {'število':'množina', 'sklon':'dajalnik'})
def test_exception_feature_level(self): # testing level exception: ('pronoun', 8, 'ti')
properties = self.converter.convert(self.specifications, Msd('Pp2-sd--y', 'en'), 'ti', 'sl')
properties = self.converter.convert(Msd('Pp2-sd--y', 'en'), 'ti', 'sl')
self.assertEqual(properties.language, 'sl')
self.assertEqual(properties.category, 'zaimek')
self.assertEqual(properties.lexeme_feature_map, {'vrsta':'osebni', 'oseba':'druga'})
self.assertEqual(properties.form_feature_map, {'število':'ednina', 'sklon':'dajalnik', 'naslonskost':'klitična'})
def test_normal_feature_level(self): # invalid msd, but useful for testing contrast with previous test
properties = self.converter.convert(self.specifications, Msd('Pp2-pd--y', 'en'), 'vi', 'sl')
properties = self.converter.convert(Msd('Pp2-pd--y', 'en'), 'vi', 'sl')
self.assertEqual(properties.language, 'sl')
self.assertEqual(properties.category, 'zaimek')
self.assertEqual(properties.lexeme_feature_map, {'vrsta':'osebni', 'oseba':'druga', 'naslonskost':'klitična'})
self.assertEqual(properties.form_feature_map, {'število':'množina', 'sklon':'dajalnik'})
def test_featureless(self):
properties = self.converter.convert(self.specifications, Msd('V', 'sl'), 'in', 'en')
properties = self.converter.convert(Msd('V', 'sl'), 'in', 'en')
self.assertEqual(properties.language, 'en')
self.assertEqual(properties.category, 'conjunction')
self.assertEqual(properties.lexeme_feature_map, {})