Commits

Mikhail Korobov  committed 6669a0e

inflect to loct/accs/gent/nomn if loc2/acc2/gen2/voct is requested and not found. Fix #8

  • Participants
  • Parent commits 0de56e5

Comments (0)

Files changed (3)

File pymorphy2/analyzer.py

 
 
     def _inflect(self, form, required_grammemes):
-        grammemes = form[1].updated_grammemes(required_grammemes)
-
         possible_results = [f for f in self.get_lexeme(form)
                             if required_grammemes <= f[1].grammemes]
 
+        if not possible_results:
+            required_grammemes = self.TagClass.fix_rare_cases(required_grammemes)
+            possible_results = [f for f in self.get_lexeme(form)
+                                if required_grammemes <= f[1].grammemes]
+
+        grammemes = form[1].updated_grammemes(required_grammemes)
         def similarity(frm):
             tag = frm[1]
             return len(grammemes & tag.grammemes)

File pymorphy2/tagset.py

         set(['plur', 'gent']),
     )
 
+    RARE_CASES = {
+        'gen2': 'gent',
+        'acc2': 'accs',
+        'loc2': 'loct',
+        'voct': 'nomn'
+    }
+
     def __init__(self, tag):
         self._str = tag
-
         # XXX: we loose information about which grammemes
         # belongs to lexeme and which belongs to form
         # (but this information seems useless for pymorphy2).
         return new_grammemes
 
     @classmethod
+    def fix_rare_cases(cls, grammemes):
+        """
+        Replace rare cases (loc2/voct/...) with common ones (loct/nomn/...).
+        """
+        return frozenset(cls.RARE_CASES.get(g,g) for g in grammemes)
+
+    @classmethod
     def _init_grammemes(cls, dict_grammemes):
         """
         Initialize various class attributes with grammeme

File tests/test_inflection.py

     assert_first_inflected_variant(word, grammemes, result)
 
 
-# TODO: see https://github.com/kmike/pymorphy2/issues/8
-@pytest.mark.xfail
 @with_test_data([
     ('валенок', ['gent'], 'валенка'),
     ('валенок', ['gen2'], 'валенка'),  # there is no gen2