Contractions
The `contractions` library is a Python package (v0.1.73) designed for natural language processing (NLP) to expand English contractions and common slang terms into their full forms. It handles contractions like `you're` to `you are` and slang like `yall` to `you all`, aiming to standardize text for further analysis. It processes text by replacing shortened words and can also be extended with custom contraction rules. The library's last release was in November 2022, and it maintains a relatively stable release cadence.
Warnings
- gotcha The library resolves ambiguous contractions (e.g., 'he's' could be 'he is' or 'he has') by defaulting to the most common expansion ('he is'). This might not always be contextually accurate. More advanced NLP libraries or forks like `pycontractions` or `sane-contractions` attempt to address this contextually.
- gotcha The PyPI metadata historically listed compatibility up to Python 3.6, which could be misleading. Users have confirmed successful operation with Python 3.8+ and up to 3.10+.
- deprecated There is a community-maintained fork, `sane-contractions`, which claims the original `contractions` library has been unmaintained since 2021 and offers active maintenance, performance improvements, and Python 3.10+ specific support.
- gotcha Older versions of the library (prior to 0.0.18) were significantly slower. The official README strongly advises using versions greater than 0.0.18 for a 50x performance improvement.
Install
-
pip install contractions
Imports
- contractions
import contractions
- fix
contractions.fix("text")
Quickstart
import contractions
text = "I'm happy now, and yall're doing great. We've gotta go!"
expanded_text = contractions.fix(text)
print(f"Original: {text}")
print(f"Expanded: {expanded_text}")
# You can also add custom contractions
contractions.add('mychange', 'my change')
custom_text = "This is mychange."
expanded_custom_text = contractions.fix(custom_text)
print(f"Custom: {expanded_custom_text}")