| Crates.io | metaphone3 |
| lib.rs | metaphone3 |
| version | 0.1.0 |
| created_at | 2025-12-16 09:40:56.631026+00 |
| updated_at | 2025-12-16 09:40:56.631026+00 |
| description | Metaphone3 phonetic algorithm |
| homepage | |
| repository | https://github.com/kakserpom/metaphone3-rs |
| max_upload_size | |
| id | 1987446 |
| size | 20,187,127 |
A pure Rust implementation of the Metaphone 3 phonetic encoding algorithm.
Metaphone 3 is a more accurate version of the original Soundex algorithm, designed so that similar-sounding words in American English share the same keys. This makes it useful for fuzzy matching, searching names, and comparing words phonetically.
smartstring for efficient string handling)Add this to your Cargo.toml:
[dependencies]
metaphone3 = "0.1"
use metaphone3::Metaphone3;
fn main() {
let mut encoder = Metaphone3::new();
let (primary, secondary) = encoder.encode("Smith");
println!("Primary: {}, Secondary: {}", primary, secondary);
// Output: Primary: SM0, Secondary: XMT
}
use metaphone3::Metaphone3;
fn main() {
// Enable vowel encoding
let mut encoder = Metaphone3::new()
.with_encode_vowels(true);
let (primary, _) = encoder.encode("beautiful");
println!("{}", primary); // Output: PATAFAL
// Enable exact encoding for stricter matching
let mut encoder = Metaphone3::new()
.with_encode_exact(true);
let (primary, secondary) = encoder.encode("edge");
println!("Primary: {}, Secondary: {}", primary, secondary);
}
The encoder is designed to be reused across multiple encode calls to reduce memory allocations:
use metaphone3::Metaphone3;
fn main() {
let mut encoder = Metaphone3::new();
let words = ["Smith", "Smyth", "Smithe", "Smythe", "Schmidt"];
for word in words {
let (primary, secondary) = encoder.encode(word);
println!("{}: {} / {}", word, primary, secondary);
}
}
Output:
Smith: SM0 / XMT
Smyth: SM0 / XMT
Smithe: SM0 / XMT
Smythe: SM0 / XMT
Schmidt: XMT /
Metaphone3The main encoder struct.
| Method | Description |
|---|---|
new() -> Self |
Creates a new encoder with default settings |
with_encode_vowels(self, bool) -> Self |
Enables/disables vowel encoding |
with_encode_exact(self, bool) -> Self |
Enables/disables exact encoding mode |
encode(&mut self, &str) -> (String, String) |
Encodes a word, returning (primary, secondary) keys |
| Option | Default | Description |
|---|---|---|
encode_vowels |
false |
When true, includes non-initial vowel sounds in the output |
encode_exact |
false |
When true, produces stricter encodings that differentiate similar sounds |
The encode() method returns a tuple of two strings:
Both encodings are limited to 8 characters maximum.
For best results when searching for phonetic matches:
use metaphone3::Metaphone3;
fn phonetic_match(encoder: &mut Metaphone3, word1: &str, word2: &str) -> bool {
let (p1, s1) = encoder.encode(word1);
let (p2, s2) = encoder.encode(word2);
p1 == p2 ||
(!s1.is_empty() && s1 == p2) ||
(!s2.is_empty() && p1 == s2) ||
(!s1.is_empty() && !s2.is_empty() && s1 == s2)
}
| Word | Primary | Secondary |
|---|---|---|
| Smith | SM0 | XMT |
| phonetics | FNTKS | |
| Xavier | SFR | |
| edge | AJ | |
| gnome | NM | |
| Thompson | TMPSN | |
| Aachen | AKN | AXN |
| Wroclaw | RKL |
Metaphone 3 was developed by Lawrence Philips as an improvement over the original Metaphone and Double Metaphone algorithms. It provides:
For more information about the Metaphone algorithm family, see the Wikipedia article.
The Metaphone3 encoder is not thread-safe. Each thread should use its own encoder instance. The encoder is designed to be cheap to construct, so creating one per thread is recommended.
MIT
Contributions are welcome! Please feel free to submit issues and pull requests.