Phonetics
The `phonetics` library is a Python module designed to compute phonetic keys of strings for indexing or fuzzy matching. It implements several classic phonetic algorithms including Soundex, NYSISS, Metaphone, and Double Metaphone. The library's last release was in March 2018, and it is marked with a '3 - Alpha' development status, primarily supporting Python 2.x and early Python 3.x versions up to 3.5.
Common errors
-
TypeError: object of type 'NoneType' has no len()
cause This error often occurs when one of the phonetic functions (e.g., `dmetaphone`) is called with an input that results in `None` being returned for one of its internal steps, and subsequent operations try to apply a length check to this `None` value. This can happen with unusual or empty string inputs, especially with older Python versions.fixEnsure that the input strings to phonetic functions are valid (non-empty, appropriate characters). Wrap calls in a `try-except` block to gracefully handle `None` returns or `TypeError`s. Example: `result = phonetics.dmetaphone(my_string); if result: print(result)`. -
AttributeError: module 'phonetics' has no attribute 'some_new_function'
cause Users might expect functionalities or algorithms present in other `phonetics`-related libraries (e.g., `pyphonetics`, `cologne-phonetics`) or newer versions of general phonetic tools to be available in this specific `phonetics` library.fixVerify the exact functions and classes exposed by *this* `phonetics` library by consulting its PyPI page or source code (Soundex, NYSISS, Metaphone, Double Metaphone are confirmed). If you need other algorithms, install and use a different library.
Warnings
- breaking The `phonetics` library explicitly supports Python versions up to 3.5. Running it on newer Python versions (3.6+) may lead to compatibility issues, unexpected errors, or incorrect behavior due to changes in Python's standard library or language syntax over time.
- gotcha The library is in 'Alpha' development status, indicating it may not be production-ready and could have undocumented quirks or bugs. Its last release was in 2018, suggesting a lack of active maintenance and potential security vulnerabilities.
- gotcha The `phonetics` library focuses on traditional phonetic algorithms (Soundex, Metaphone, NYSISS) for string key generation, which are primarily designed for English or Germanic languages. It does not provide advanced linguistic analysis, IPA (International Phonetic Alphabet) transcription, or support for a wide range of global languages.
Install
-
pip install phonetics
Imports
- phonetics
from phonetics import Soundex
import phonetics
Quickstart
import phonetics
# Soundex algorithm
print(f"Soundex for 'Python': {phonetics.soundex('Python')}")
print(f"Soundex for 'Pythn': {phonetics.soundex('Pythn')}")
# Metaphone algorithm
print(f"Metaphone for 'example': {phonetics.metaphone('example')}")
print(f"Metaphone for 'xylophone': {phonetics.metaphone('xylophone')}")
# Double Metaphone algorithm
# Returns a tuple of primary and secondary keys
primary_key, secondary_key = phonetics.dmetaphone('Danger')
print(f"Double Metaphone for 'Danger': ({primary_key}, {secondary_key})")
primary_key, secondary_key = phonetics.dmetaphone('Denger')
print(f"Double Metaphone for 'Denger': ({primary_key}, {secondary_key})")