Skip to content

Commit 7fcfb4f

Browse files
committed
Updates to docs and revised metaphone function
Revised `double_metaphone` function to `encoding` to better reflect its utility as well as to be consistent with the equivalent cmu function. Update README to reflect the recent changes to the API. Remove an unused variable in the error module.
1 parent c8b91c1 commit 7fcfb4f

File tree

7 files changed

+317
-304
lines changed

7 files changed

+317
-304
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ Cargo.lock
1818
#**/*.rs.bk
1919
#Cargo.lock
2020

21-
cmudict.test
21+
cmudict.test
22+
cmudict.json

README.md

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,52 @@ talking to a wall, a piecemeal natural language processing library.
2929
extern crate ttaw;
3030
use ttaw;
3131

32-
assert_eq!(Ok(true), ttaw::rhyme("far", "tar"));
33-
assert_eq!(Ok(false), ttaw::rhyme("shopping", "cart"));
32+
// Initialize the CmuDict with a path to the existing serialized CMU dictionary
33+
// or a directoy containing it. If the dictionary doesn't exisit, it will be
34+
// downloaded and serialized at the location specified by the path parameter.
35+
let cmudict = ttaw::cmu::CmuDict::new("cmudict.json").unwrap();
36+
37+
assert_eq!(Ok(true), cmudict.rhyme("far", "tar"));
38+
assert_eq!(Ok(true), ttaw::metaphone::rhyme("far", "tar"));
39+
40+
assert_eq!(Ok(false), cmudict.rhyme("shopping", "cart"));
41+
assert_eq!(Ok(false), ttaw::metaphone::rhyme("shopping", "cart"));
3442

3543
// Deviations in cmu and metaphone
3644
assert_eq!(true, ttaw::metaphone::rhyme("hear", "near"));
37-
assert_eq!(Ok(false), ttaw::cmu::rhyme("hear", "near"));
45+
assert_eq!(Ok(false), cmudict.rhyme("hear", "near"));
3846
```
3947

4048
## Alliteration
4149
```rust
4250
extern crate ttaw;
4351
use ttaw;
44-
assert_eq!(Ok(true), ttaw::alliteration("bounding","bears"));
45-
assert_eq!(Ok(false), ttaw::alliteration("lazy", "dog"));
4652

53+
// Initialize the CmuDict with a path to the existing serialized CMU dictionary
54+
// or a directoy containing it. If the dictionary doesn't exisit, it will be
55+
// downloaded and serialized at the location specified by the path parameter.
56+
let cmudict = ttaw::cmu::CmuDict::new("cmudict.json").unwrap();
57+
58+
assert_eq!(Ok(true), cmudict.alliteration("bounding","bears"));
4759
assert_eq!(true, ttaw::metaphone::alliteration("bounding","bears"));
48-
assert_eq!(Ok(true), ttaw::cmu::alliteration("bounding","bears"));
60+
61+
assert_eq!(Ok(false), cmudict.alliteration("lazy", "dog"));
62+
assert_eq!(false, ttaw::metaphone::alliteration("lazy", "dog"));
4963
```
5064

5165

5266
## CMUdict
5367
```rust
5468
extern crate ttaw;
5569
use ttaw;
70+
71+
// Initialize the CmuDict with a path to the existing serialized CMU dictionary
72+
// or a directoy containing it. If the dictionary doesn't exisit, it will be
73+
// downloaded and serialized at the location specified by the path parameter.
74+
let cmudict = ttaw::cmu::CmuDict::new("cmudict.json").unwrap();
75+
5676
assert_eq!(
57-
ttaw::cmu::cmu("unearthed"),
77+
cmudict.encoding(("unearthed"),
5878
Ok(Some(vec![vec![
5979
"AH0".to_string(),
6080
"N".to_string(),
@@ -69,15 +89,15 @@ assert_eq!(
6989
```rust
7090
extern crate ttaw;
7191
use ttaw;
72-
assert_eq!(ttaw::metaphone::double_metaphone("Arnow").primary, "ARN");
73-
assert_eq!(ttaw::metaphone::double_metaphone("Arnow").secondary, "ARNF");
92+
assert_eq!(ttaw::metaphone::encoding("Arnow").primary, "ARN");
93+
assert_eq!(ttaw::metaphone::encoding("Arnow").secondary, "ARNF");
7494

7595
assert_eq!(
76-
ttaw::metaphone::double_metaphone("detestable").primary,
96+
ttaw::metaphone::encoding("detestable").primary,
7797
"TTSTPL"
7898
);
7999
assert_eq!(
80-
ttaw::metaphone::double_metaphone("detestable").secondary,
100+
ttaw::metaphone::encoding("detestable").secondary,
81101
"TTSTPL"
82102
);
83103
```

src/cmu.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub struct CmuDict {
1515
}
1616

1717
impl CmuDict {
18+
/// Initialize the CmuDict with a path to the existing serialized CMU dictionary
19+
/// or a directoy containing it. If the dictionary doesn't exisit, it will be
20+
/// downloaded and serialized at the location specified by the path parameter.
1821
pub fn new(path: &str) -> Result<CmuDict, Error> {
1922
match from_json_file(&Path::new(path)) {
2023
Ok(d) => Ok(CmuDict { dict: d }),
@@ -27,7 +30,7 @@ impl CmuDict {
2730
/// ```rust
2831
/// extern crate ttaw;
2932
/// use ttaw::cmu::CmuDict;
30-
/// let cmudict = CmuDict::new("cmu.dict").unwrap();
33+
/// let cmudict = CmuDict::new("cmudict.json").unwrap();
3134
/// assert_eq!(
3235
/// cmudict.encoding("permeability"),
3336
/// Ok(Some(vec![vec![
@@ -65,7 +68,7 @@ impl CmuDict {
6568
/// ```rust
6669
/// extern crate ttaw;
6770
/// use ttaw::cmu::CmuDict;
68-
/// let cmudict = CmuDict::new("cmu.dict").unwrap();
71+
/// let cmudict = CmuDict::new("cmudict.json").unwrap();
6972
/// // Does rhyme
7073
/// assert!(cmudict.rhyme("hissed", "mist").unwrap());
7174
/// assert!(cmudict.rhyme("tryst", "wrist").unwrap());
@@ -90,7 +93,7 @@ impl CmuDict {
9093
/// ```rust
9194
/// extern crate ttaw;
9295
/// use ttaw::cmu::CmuDict;
93-
/// let cmudict = CmuDict::new("cmu.dict").unwrap();
96+
/// let cmudict = CmuDict::new("cmudict.json").unwrap();
9497
// // Does alliterate
9598
/// assert!(cmudict.alliteration("bouncing", "bears").unwrap());
9699
/// assert!(cmudict.alliteration("snappy", "snails").unwrap());

src/error.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ mod tests {
4444
fn display() {
4545
let input = "input error".to_string();
4646
let progam = "program error".to_string();
47-
let path = "path error".to_string();
4847

4948
assert_eq!(input, format!("{}", Error::InputError(input.clone())));
5049
assert_eq!(progam, format!("{}", Error::ProgramError(progam.clone())));

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate pest;
44
/// ```rust
55
/// extern crate ttaw;
66
/// use ttaw;
7-
/// let cmudict = ttaw::cmu::CmuDict::new("cmudict.test").unwrap();
7+
/// let cmudict = ttaw::cmu::CmuDict::new("cmudict.json").unwrap();
88
/// assert_eq!(
99
/// cmudict.encoding("permeability"),
1010
/// Ok(Some(vec![vec![
@@ -38,11 +38,11 @@ extern crate pest;
3838
/// ```rust
3939
/// extern crate ttaw;
4040
/// use ttaw;
41-
/// assert_eq!(ttaw::metaphone::double_metaphone("Arnow").primary, "ARN");
42-
/// assert_eq!(ttaw::metaphone::double_metaphone("Arnow").secondary, "ARNF");
41+
/// assert_eq!(ttaw::metaphone::encoding("Arnow").primary, "ARN");
42+
/// assert_eq!(ttaw::metaphone::encoding("Arnow").secondary, "ARNF");
4343
///
44-
/// assert_eq!(ttaw::metaphone::double_metaphone("detestable").primary, "TTSTPL");
45-
/// assert_eq!(ttaw::metaphone::double_metaphone("detestable").secondary, "TTSTPL");
44+
/// assert_eq!(ttaw::metaphone::encoding("detestable").primary, "TTSTPL");
45+
/// assert_eq!(ttaw::metaphone::encoding("detestable").secondary, "TTSTPL");
4646
/// ```
4747
#[macro_use]
4848
extern crate pest_derive;

src/metaphone.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ pub fn rhyme(a: &str, b: &str) -> bool {
5151
return false;
5252
}
5353

54-
let a_phonetic = double_metaphone(a);
55-
let b_phonetic = double_metaphone(b);
54+
let a_phonetic = encoding(a);
55+
let b_phonetic = encoding(b);
5656

5757
log::info!(
5858
"|{: ^10} | {: ^10} | {: ^10} |",
5959
a,
6060
a_phonetic.primary,
6161
a_phonetic.secondary
6262
);
63-
63+
6464
log::info!(
6565
"|{: ^10} | {: ^10} | {: ^10} |",
6666
b,
@@ -119,17 +119,21 @@ pub fn alliteration(a: &str, b: &str) -> bool {
119119
return false;
120120
}
121121

122-
let a_phonetic = double_metaphone(a);
123-
let b_phonetic = double_metaphone(b);
122+
let a_phonetic = encoding(a);
123+
let b_phonetic = encoding(b);
124124

125125
log::info!(
126126
"|{: ^10} | {: ^10} | {: ^10} |",
127-
a, a_phonetic.primary, a_phonetic.secondary
127+
a,
128+
a_phonetic.primary,
129+
a_phonetic.secondary
128130
);
129131

130132
log::info!(
131133
"|{: ^10} | {: ^10} | {: ^10} |",
132-
b, b_phonetic.primary, b_phonetic.secondary
134+
b,
135+
b_phonetic.primary,
136+
b_phonetic.secondary
133137
);
134138

135139
let mut a_phonetic_head_primary = a_phonetic.primary;
@@ -172,14 +176,14 @@ pub fn alliteration(a: &str, b: &str) -> bool {
172176
/// ```rust
173177
/// extern crate ttaw;
174178
/// use ttaw;
175-
/// assert_eq!(ttaw::metaphone::double_metaphone("Arnow").primary, "ARN");
176-
/// assert_eq!(ttaw::metaphone::double_metaphone("Arnow").secondary, "ARNF");
179+
/// assert_eq!(ttaw::metaphone::encoding("Arnow").primary, "ARN");
180+
/// assert_eq!(ttaw::metaphone::encoding("Arnow").secondary, "ARNF");
177181
///
178-
/// assert_eq!(ttaw::metaphone::double_metaphone("detestable").primary, "TTSTPL");
179-
/// assert_eq!(ttaw::metaphone::double_metaphone("detestable").secondary, "TTSTPL");
182+
/// assert_eq!(ttaw::metaphone::encoding("detestable").primary, "TTSTPL");
183+
/// assert_eq!(ttaw::metaphone::encoding("detestable").secondary, "TTSTPL");
180184
/// ```
181185
///
182-
pub fn double_metaphone(input: &str) -> DoubleMetaphone {
186+
pub fn encoding(input: &str) -> DoubleMetaphone {
183187
let mut state = State::new();
184188
let word: String = input.to_uppercase() + " ";
185189

0 commit comments

Comments
 (0)