Skip to content

Commit

Permalink
Merge 2cbc650 into 00d1b65
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 committed Apr 21, 2019
2 parents 00d1b65 + 2cbc650 commit 90eb589
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 31 deletions.
13 changes: 10 additions & 3 deletions src/lib.rs
Expand Up @@ -64,6 +64,7 @@ mod pinyin_map;

pub use dict::PHONETIC_SYMBOL_MAP;
pub use pinyin_map::PINYIN_HASHMAP;
use std::collections::HashSet;

// 声母表
const _INITIALS: [&str; 21] = [
Expand Down Expand Up @@ -196,12 +197,18 @@ fn to_fixed(p: &str, a: &Args) -> String {
}

fn apply_style(pys: Vec<String>, a: &Args) -> Vec<String> {
let mut new_pys: Vec<String> = vec![];
let mut result: Vec<String> = vec![];
// Unfortunately, HashSet does not guarantee ordering
let mut set: HashSet<String> = HashSet::new();
for v in pys {
let s = to_fixed(&v, a);
new_pys.push(s);
if !set.contains(&s) {
set.insert(s.clone());
result.push(s);
}
}
new_pys

return result;
}

fn single_pinyin(c: char, a: &Args) -> Vec<String> {
Expand Down
42 changes: 14 additions & 28 deletions tests/lib.rs
@@ -1,27 +1,8 @@
extern crate pinyin;

struct TestCase {
hans: String,
args: pinyin::Args,
result: Vec<Vec<String>>,
lazy_result: Vec<String>,
}
mod test_case;

impl TestCase {
pub fn new(
hans: String,
args: pinyin::Args,
result: Vec<Vec<String>>,
lazy_result: Vec<String>,
) -> TestCase {
TestCase {
hans,
args,
result,
lazy_result,
}
}
}
use test_case::TestCase;

#[test]
fn test_pinyin() {
Expand Down Expand Up @@ -140,18 +121,23 @@ fn test_pinyin() {
],
vec!["o1ng".to_string(), "uo2".to_string(), "e2n".to_string()],
),
TestCase::new(
TestCase::new2(
"中国人".to_string(),
pinyin::Args {
style: pinyin::Style::Normal,
heteronym: true,
},
vec![
vec!["zhong".to_string(), "zhong".to_string()],
vec!["guo".to_string()],
vec!["ren".to_string()],
],
vec!["zhong".to_string(), "guo".to_string(), "ren".to_string()],
vec![vec!["zhong"], vec!["guo"], vec!["ren"]],
vec!["zhong", "guo", "ren"],
),
TestCase::new2(
"阿拉巴".to_string(),
pinyin::Args {
style: pinyin::Style::Normal,
heteronym: true,
},
vec![vec!["a", "e"], vec!["la"], vec!["ba"]],
vec!["a", "la", "ba"],
),
];
for data in &test_data {
Expand Down
41 changes: 41 additions & 0 deletions tests/test_case.rs
@@ -0,0 +1,41 @@
extern crate pinyin;

pub struct TestCase {
pub hans: String,
pub args: pinyin::Args,
pub result: Vec<Vec<String>>,
pub lazy_result: Vec<String>,
}

impl TestCase {
pub fn new(
hans: String,
args: pinyin::Args,
result: Vec<Vec<String>>,
lazy_result: Vec<String>,
) -> TestCase {
TestCase {
hans,
args,
result,
lazy_result,
}
}

pub fn new2(
hans: String,
args: pinyin::Args,
result: Vec<Vec<&str>>,
lazy_result: Vec<&str>,
) -> TestCase {
return TestCase {
hans: hans,
args: args,
result: result
.into_iter()
.map(|vec| vec.into_iter().map(String::from).collect())
.collect(),
lazy_result: lazy_result.into_iter().map(String::from).collect(),
};
}
}

0 comments on commit 90eb589

Please sign in to comment.