Registry / ai-ml / contractions

contractions

JSON →
library0.1.73pypypi✓ verified 86d ago

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.

pip install contractions
INSTALL
IMPORT
SIG · CONTRACTIONS
C
contractions
ai-mlpythonv0.1.73
Install
2.0s avg
Import
77ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.73 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.080s · 20.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.0s · import 0.074s · 21MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

contractions
import contractions
fix
contractions.fix("text")
from contractions import fix; fix("text")
The primary function is typically accessed via the imported module name.

Expands common English contractions and slang in a given string. Demonstrates adding a custom contraction.

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}")
Debug
Known issues
gotchaThe 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.
fix
For critical applications requiring context-aware contraction expansion, consider alternative libraries (e.g., `pycontractions`) or implement custom rules to handle specific ambiguous cases.
affects: All versions
gotchaThe 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+.
fix
The library generally works with recent Python 3 versions (3.8+). If you encounter issues, ensure you are using a version greater than 0.0.18 for performance improvements.
affects: <=0.1.73
deprecatedThere 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.
fix
For new projects or if active maintenance, modern Python features, and guaranteed Python 3.10+ compatibility are crucial, consider evaluating `sane-contractions` instead. The original library had a release in late 2022, indicating some continued activity.
affects: All versions
gotchaOlder 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.
fix
Ensure you are using `contractions` version 0.0.18 or higher for optimal performance. The current version (0.1.73) already incorporates these improvements.
affects: <=0.0.18
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'contractions'
The `contractions` library has not been installed in the Python environment, or the environment where it's installed is not the one being used.
fix
Install the library using pip: `pip install contractions`
ImportError: cannot import name 'CONTRACTION_MAP' from 'contractions'
Developers are trying to import an internal or deprecated symbol (`CONTRACTION_MAP`) that is not part of the `contractions` library's public API or has been renamed/removed in the installed version. The correct public attribute for accessing the contraction dictionary is `contractions_dict`.
fix
Instead of `from contractions import CONTRACTION_MAP`, use `from contractions import contractions_dict` to access the map, or simply use `contractions.fix()` for expansion without needing the map directly.
Contractions like "'s" or ambiguous forms are not expanding correctly (e.g., "My name's Sam" expands incorrectly or "She's" always expands to "She is" instead of "She has").
The `contractions` library, particularly in older versions or for specific ambiguous cases, might not correctly handle all instances of possessive "'s" or contractions that have multiple possible expansions (e.g., "She's" can be "She is" or "She has"). The library typically applies a default expansion or struggles with context-dependent ambiguity.
fix
For basic usage, ensure you are using `contractions.fix(text)`. For complex or ambiguous contractions, review the output and consider using a more context-aware NLP library if the `contractions` library's default behavior is insufficient for your specific use case. The `pycontractions` library is an alternative that attempts to handle ambiguity using word embeddings and grammar checking.
Upgrade
Version history
0.1.73latest on PyPI · released Nov 15, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
contractions — pip install contractions · libregistry