PyMorphy2
PyMorphy2 (version 0.9.1) is a morphological analyzer and inflection engine for the Russian and Ukrainian languages. It provides functionalities for POS tagging, word inflection, and normalization using OpenCorpora.org dictionaries and heuristic algorithms for unknown words. The project's release cadence is infrequent, with the last significant update in September 2020.
Common errors
-
DictionaryNotFound: Dictionary for language 'ru' not found. Download it using 'python -m pymorphy2.downloader'.
cause The `pymorphy2-dicts` package, which contains the necessary morphological dictionaries, is not installed or cannot be found by `pymorphy2`.fixInstall the dictionary package: `pip install pymorphy2-dicts`. -
ImportError: cannot import name 'MorphAnalyzer' from 'pymorphy2'
cause This error typically occurs if `pymorphy2` is not properly installed or if there's a naming conflict in your environment (e.g., a local file named `pymorphy2.py`).fixVerify installation with `pip show pymorphy2`. If installed, check for conflicting local files or environment path issues. A common mistake is `import pymorphy2.MorphAnalyzer` instead of `from pymorphy2 import MorphAnalyzer`. -
Segmentation fault (core dumped)
cause Reported in some GitHub issues, particularly with Python 3.7+ and specific installation methods (e.g., `pymorphy2[fast]`). This often indicates low-level C extension issues, possibly related to `DAWG-Python` dependencies.fixTry `pip install pymorphy2` without the `[fast]` extra. Ensure all dependencies are up-to-date. If issues persist, consider using a Python version that is known to be stable with `pymorphy2` (e.g., Python 3.8/3.9) or consult the project's GitHub issues for workarounds.
Warnings
- breaking PyMorphy2 versions 0.9.x dropped support for Python 2.6 and Python 3.2-3.4. While Python 2.7 is still technically supported by 0.9.1, explicit support for Python 2.x is planned to be removed in a future v1.0, and Python 3.5+ is recommended. Newer Python versions (3.11, 3.12) report issues.
- gotcha The original `pymorphy2` project is largely unmaintained, with GitHub issues indicating it might be abandoned. This means limited support for newer Python versions, bug fixes, or new features. A community-driven fork (`pymorphy2-fork`) exists to continue development.
- gotcha Directly comparing `tag.POS` or other `tag` attributes with arbitrary string values (e.g., `tag.POS == 'plur'`) can lead to `ValueError` if the string is not a valid grammeme for that attribute. Use `tag.grammemes` for comprehensive checks or refer to documentation for valid grammeme values.
- gotcha When installing `pymorphy2` in environments like Jupyter Notebook or Google Colab after the process has started (e.g., `!pip install`), dictionary discovery might fail. Version 0.9.1 fixed this, but older versions or unusual setups might still encounter it.
Install
-
pip install pymorphy2 -
pip install pymorphy2 pymorphy2-dicts
Imports
- MorphAnalyzer
from pymorphy2 import MorphAnalyzer
Quickstart
import pymorphy2
morph = pymorphy2.MorphAnalyzer()
word = 'стекло'
# Analyze a word
parsed_word = morph.parse(word)[0]
print(f"Original word: {word}")
print(f"Normal form: {parsed_word.normal_form}")
print(f"Part of speech: {parsed_word.tag.POS}")
print(f"All tags: {parsed_word.tag}")
# Inflect a word
inflected = parsed_word.inflect({'gent'})
if inflected: # Check if inflection was successful
print(f"Inflected to genitive: {inflected.word}")
else:
print(f"Could not inflect '{word}' to genitive.")