Skip to content

Commit

Permalink
Merge pull request #255 from akaza-im/iike
Browse files Browse the repository at this point in the history
`井伊家` の複合語化処理にアドホックな対応を追加
  • Loading branch information
tokuhirom authored Jan 29, 2023
2 parents bcd032b + a0be603 commit d4697b3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
17 changes: 16 additions & 1 deletion akaza-data/src/tokenizer/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ pub trait AkazaTokenizer {
}

/// マージ処理に利用する為の中間表現
#[derive(Debug)]
pub(crate) struct IntermediateToken {
surface: String,
yomi: String,
hinshi: String,
subhinshi: String,
subsubhinshi: String,
}

impl IntermediateToken {
Expand All @@ -16,12 +18,14 @@ impl IntermediateToken {
yomi: String,
hinshi: String,
subhinshi: String,
subsubhinshi: String,
) -> IntermediateToken {
IntermediateToken {
surface,
yomi,
hinshi,
subhinshi,
subsubhinshi,
}
}
}
Expand Down Expand Up @@ -72,8 +76,19 @@ pub(crate) fn merge_terms_ipadic(intermediates: Vec<IntermediateToken>) -> Strin
|| token.subhinshi == "接続助詞"
|| token.subhinshi == "接尾"
{
// println!("PREV_TOKEN: {:?}", prev_token);
// println!("TOKEN: {:?}", token);
surface += token.surface.as_str();
yomi += token.yomi.as_str();
yomi += if token.surface == "家"
&& token.yomi == "か"
&& prev_token.subsubhinshi == "人名"
{
// 人名 + 家 のケースに ipadic だと「か」と読んでしまう
// 問題があるので、その場合は「家/け」に読み替える。
"け"
} else {
token.yomi.as_str()
};

j += 1;
prev_token = token;
Expand Down
30 changes: 29 additions & 1 deletion akaza-data/src/tokenizer/vibrato.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Context;
use std::fs::File;
use std::time::SystemTime;

use anyhow::Context;
use kelp::{kata2hira, ConvOption};
use log::info;
use vibrato::{Dictionary, Tokenizer};
Expand Down Expand Up @@ -61,6 +61,7 @@ impl AkazaTokenizer for VibratoTokenizer {

let hinshi = feature[0];
let subhinshi = if feature.len() > 2 { feature[1] } else { "UNK" };
let subsubhinshi = if feature.len() > 3 { feature[2] } else { "UNK" };
let yomi = if feature.len() > 7 {
feature[7]
} else {
Expand All @@ -74,6 +75,7 @@ impl AkazaTokenizer for VibratoTokenizer {
yomi.to_string(),
hinshi.to_string(),
subhinshi.to_string(),
subsubhinshi.to_string(),
);
intermediates.push(intermediate);
// println!("{}/{}/{}", token.surface(), hinshi, yomi);
Expand Down Expand Up @@ -121,4 +123,30 @@ mod tests {
);
Ok(())
}

#[test]
fn test_iika() -> anyhow::Result<()> {
// 井伊家が ipadic だと いい/か になるが、「か」が接尾なので、
// 複合語化されてしまう。これはひじょうに問題である。
// 「いいか」というのはよく出て来る表現なので。。
// しかも「井伊家」は wikipedia でもよく出ているので、割とコストが低くなってしまう。
// 井伊家だけに限った問題ではないので、mecab への辞書登録ではカバーが難しい。
// よって、接尾の「家」は特別扱いして、固有名詞,人名の場合のあとにくる「家」は「け」と読むようにする。

/*
井伊家
井伊 名詞,固有名詞,人名,姓,*,*,井伊,イイ,イイ
家 名詞,接尾,一般,*,*,*,家,カ,カ
EOS
*/

let _ = env_logger::builder()
.filter_level(LevelFilter::Info)
.is_test(true)
.try_init();

let runner = VibratoTokenizer::new("work/vibrato/ipadic-mecab-2_7_0/system.dic", None)?;
assert_eq!(runner.tokenize("井伊家")?, "井伊家/いいけ");
Ok(())
}
}

0 comments on commit d4697b3

Please sign in to comment.