Metaphone
Metaphone is a Python library that provides implementations of the Metaphone and Double Metaphone phonetic algorithms. These algorithms are designed to index words by their English pronunciation, offering an improvement over Soundex for matching similar-sounding words and names, even with variations in spelling. The Double Metaphone algorithm further refines this by accounting for spelling peculiarities from various languages and can return both a primary and a secondary phonetic code. The current version is 0.6, and the project has a very slow release cadence, with the last PyPI update in 2016 and the latest GitHub release in 2020.
Common errors
-
SyntaxError: invalid syntax (on `next = self.next_chars.pop(0)`)
cause The `metaphone` library was written for Python 2, where `next` was not a reserved keyword. In Python 3, `next` is a built-in function, leading to a `SyntaxError` when a method or variable is named `next`.fixThis issue requires modifying the library's source code to rename the `next` method (e.g., to `_next_char`). Alternatively, use a Python 3 compatible fork or a different library that supports Metaphone algorithms on Python 3. -
AttributeError: 'SomeClass' object has no attribute 'next'
cause Similar to the `SyntaxError`, this typically arises in Python 3 environments when code attempts to call a method named `next` within the `metaphone` library, which conflicts with Python 3's built-in `next()` function and is likely not properly defined due to the `SyntaxError` in the original source.fixAs above, this requires a modification of the library's source code to rename the conflicting `next` method or usage of a Python 3 compatible alternative library. For example, changing `self.next` to `self._next` in the original library's `metaphone.py` file could resolve this.
Warnings
- breaking The `metaphone` library (version 0.6 and earlier) contains Python 2 specific syntax, particularly the use of `self.next` as a method name, which is a reserved keyword in Python 3. This will cause `SyntaxError` or `AttributeError` when run directly in Python 3 environments.
- gotcha The Metaphone algorithm itself (and by extension, this library's implementation) is primarily designed for English pronunciation. While Double Metaphone attempts to account for other language irregularities, its accuracy may vary significantly for non-English words or names, compared to language-specific phonetic algorithms or more advanced systems like Metaphone 3 (which is commercial).
Install
-
pip install metaphone
Imports
- doublemetaphone
from metaphone import doublemetaphone
- metaphone
from metaphone import metaphone
Quickstart
from metaphone import doublemetaphone
word = "architect"
primary_code, secondary_code = doublemetaphone(word)
print(f"Original word: {word}")
print(f"Double Metaphone codes: Primary='{primary_code}', Secondary='{secondary_code}'")
word_two = "Schmidt"
primary_code_two, secondary_code_two = doublemetaphone(word_two)
print(f"Original word: {word_two}")
print(f"Double Metaphone codes: Primary='{primary_code_two}', Secondary='{secondary_code_two}'")
from metaphone import metaphone
word_three = "example"
metaphone_code = metaphone(word_three)
print(f"Original word: {word_three}")
print(f"Metaphone code: '{metaphone_code}'")