-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
エラーメッセージにおけるcontextとsourceを明確に区分する #624
Merged
Hiroshiba
merged 12 commits into
VOICEVOX:main
from
qryxip:divide-error-messages-into-contexts-and-causes
Oct 3, 2023
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4be6b8b
[skip ci] WIP
qryxip 9646634
[skip ci] 不要な`map_err`を削除
qryxip 6ddda13
[skip ci] 定数を関連定数に
qryxip ea5b2f7
Merge branch 'main' into divide-error-messages-into-contexts-and-causes
qryxip b2dc84d
`UseUserDict`
qryxip 152bd1d
`SaveUserDict`
qryxip d4a54f6
`LoadUserDict`
qryxip aee8edf
`ParseKana`
qryxip ee8863e
`ExtractFullContextLabel`
qryxip a0cde52
`GetSupportedDevices`
qryxip f3c203d
`LoadModel`
qryxip 1153311
`name` → `function`
qryxip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,26 +3,22 @@ use std::{ | |
path::{Path, PathBuf}, | ||
sync::Mutex, | ||
}; | ||
|
||
use anyhow::anyhow; | ||
use tempfile::NamedTempFile; | ||
|
||
use ::open_jtalk::*; | ||
|
||
use crate::{error::ErrorRepr, UserDict}; | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub(crate) enum OpenJtalkError { | ||
#[error("open_jtalk load error")] | ||
Load { mecab_dict_dir: PathBuf }, | ||
Comment on lines
-14
to
-15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここの数十行下にFIXMEとして書いたが、この |
||
#[error("open_jtalk extract_fullcontext error")] | ||
ExtractFullContext { | ||
text: String, | ||
#[source] | ||
source: Option<anyhow::Error>, | ||
}, | ||
#[error("`{name}`の実行が失敗しました")] | ||
pub(crate) struct OpenjtalkFunctionError { | ||
name: &'static str, | ||
Hiroshiba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[source] | ||
source: Option<Text2MecabError>, | ||
} | ||
|
||
type Result<T> = std::result::Result<T, OpenJtalkError>; | ||
|
||
/// テキスト解析器としてのOpen JTalk。 | ||
pub struct OpenJtalk { | ||
resources: Mutex<Resources>, | ||
|
@@ -53,8 +49,10 @@ impl OpenJtalk { | |
open_jtalk_dict_dir: impl AsRef<Path>, | ||
) -> crate::result::Result<Self> { | ||
let mut s = Self::new_without_dic(); | ||
s.load(open_jtalk_dict_dir) | ||
.map_err(|_| ErrorRepr::NotLoadedOpenjtalkDict)?; | ||
s.load(open_jtalk_dict_dir).map_err(|()| { | ||
// FIXME: 「システム辞書を読もうとしたけど読めなかった」というエラーをちゃんと用意する | ||
ErrorRepr::NotLoadedOpenjtalkDict | ||
})?; | ||
Ok(s) | ||
} | ||
|
||
|
@@ -70,13 +68,12 @@ impl OpenJtalk { | |
.ok_or(ErrorRepr::NotLoadedOpenjtalkDict)?; | ||
|
||
// ユーザー辞書用のcsvを作成 | ||
let mut temp_csv = | ||
NamedTempFile::new().map_err(|e| ErrorRepr::UseUserDict(e.to_string()))?; | ||
let mut temp_csv = NamedTempFile::new().map_err(|e| ErrorRepr::UseUserDict(e.into()))?; | ||
temp_csv | ||
.write_all(user_dict.to_mecab_format().as_bytes()) | ||
.map_err(|e| ErrorRepr::UseUserDict(e.to_string()))?; | ||
.map_err(|e| ErrorRepr::UseUserDict(e.into()))?; | ||
let temp_csv_path = temp_csv.into_temp_path(); | ||
let temp_dict = NamedTempFile::new().map_err(|e| ErrorRepr::UseUserDict(e.to_string()))?; | ||
let temp_dict = NamedTempFile::new().map_err(|e| ErrorRepr::UseUserDict(e.into()))?; | ||
let temp_dict_path = temp_dict.into_temp_path(); | ||
|
||
// Mecabでユーザー辞書をコンパイル | ||
|
@@ -100,15 +97,16 @@ impl OpenJtalk { | |
let result = mecab.load_with_userdic(Path::new(dict_dir), Some(Path::new(&temp_dict_path))); | ||
|
||
if !result { | ||
return Err( | ||
ErrorRepr::UseUserDict("辞書のコンパイルに失敗しました".to_string()).into(), | ||
); | ||
return Err(ErrorRepr::UseUserDict(anyhow!("辞書のコンパイルに失敗しました")).into()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) fn extract_fullcontext(&self, text: impl AsRef<str>) -> Result<Vec<String>> { | ||
pub(crate) fn extract_fullcontext( | ||
&self, | ||
text: impl AsRef<str>, | ||
) -> std::result::Result<Vec<String>, OpenjtalkFunctionError> { | ||
let Resources { | ||
mecab, | ||
njd, | ||
|
@@ -119,19 +117,16 @@ impl OpenJtalk { | |
njd.refresh(); | ||
mecab.refresh(); | ||
|
||
let mecab_text = | ||
text2mecab(text.as_ref()).map_err(|e| OpenJtalkError::ExtractFullContext { | ||
text: text.as_ref().into(), | ||
source: Some(e.into()), | ||
})?; | ||
let mecab_text = text2mecab(text.as_ref()).map_err(|e| OpenjtalkFunctionError { | ||
name: "text2mecab", | ||
source: Some(e), | ||
})?; | ||
if mecab.analysis(mecab_text) { | ||
njd.mecab2njd( | ||
mecab | ||
.get_feature() | ||
.ok_or(OpenJtalkError::ExtractFullContext { | ||
text: text.as_ref().into(), | ||
source: None, | ||
})?, | ||
mecab.get_feature().ok_or(OpenjtalkFunctionError { | ||
name: "Mecab_get_feature", | ||
source: None, | ||
})?, | ||
mecab.get_size(), | ||
); | ||
njd.set_pronunciation(); | ||
|
@@ -144,20 +139,20 @@ impl OpenJtalk { | |
jpcommon.make_label(); | ||
jpcommon | ||
.get_label_feature_to_iter() | ||
.ok_or_else(|| OpenJtalkError::ExtractFullContext { | ||
text: text.as_ref().into(), | ||
.ok_or(OpenjtalkFunctionError { | ||
name: "JPCommon_get_label_feature", | ||
source: None, | ||
}) | ||
.map(|iter| iter.map(|s| s.to_string()).collect()) | ||
} else { | ||
Err(OpenJtalkError::ExtractFullContext { | ||
text: text.as_ref().into(), | ||
Err(OpenjtalkFunctionError { | ||
name: "Mecab_analysis", | ||
source: None, | ||
}) | ||
} | ||
} | ||
|
||
fn load(&mut self, open_jtalk_dict_dir: impl AsRef<Path>) -> Result<()> { | ||
fn load(&mut self, open_jtalk_dict_dir: impl AsRef<Path>) -> std::result::Result<(), ()> { | ||
let result = self | ||
.resources | ||
.lock() | ||
|
@@ -169,9 +164,7 @@ impl OpenJtalk { | |
Ok(()) | ||
} else { | ||
self.dict_dir = None; | ||
Err(OpenJtalkError::Load { | ||
mecab_dict_dir: open_jtalk_dict_dir.as_ref().into(), | ||
}) | ||
Err(()) | ||
Hiroshiba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
|
@@ -275,9 +268,12 @@ mod tests { | |
} | ||
|
||
#[rstest] | ||
#[case("",Err(OpenJtalkError::ExtractFullContext{text:"".into(),source:None}))] | ||
#[case("", Err(OpenjtalkFunctionError { name: "Mecab_get_feature", source: None }))] | ||
#[case("こんにちは、ヒホです。", Ok(testdata_hello_hiho()))] | ||
fn extract_fullcontext_works(#[case] text: &str, #[case] expected: super::Result<Vec<String>>) { | ||
fn extract_fullcontext_works( | ||
#[case] text: &str, | ||
#[case] expected: std::result::Result<Vec<String>, OpenjtalkFunctionError>, | ||
) { | ||
let open_jtalk = OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap(); | ||
let result = open_jtalk.extract_fullcontext(text); | ||
assert_debug_fmt_eq!(expected, result); | ||
|
@@ -287,7 +283,7 @@ mod tests { | |
#[case("こんにちは、ヒホです。", Ok(testdata_hello_hiho()))] | ||
fn extract_fullcontext_loop_works( | ||
#[case] text: &str, | ||
#[case] expected: super::Result<Vec<String>>, | ||
#[case] expected: std::result::Result<Vec<String>, OpenjtalkFunctionError>, | ||
) { | ||
let open_jtalk = OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap(); | ||
for _ in 0..10 { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
めちゃめちゃ細かいですが、意外と解釈という言葉はドキュメントに出てこない気がしました。
正確にはメモリ足りなかったとかもあり得る気がするので、
処理
とかで良いかも。(まあ解釈でも別にいいかな、くらいの温度感です)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"could not interpret"のような文であれば世に溢れていると思ってました。
OOMについては... そもそもOpen JTalkは考慮しているんでしょうか? なんかC++の例外とかがそのまま発射されそうな気も... (ちなみにそんなことがもし起きた場合、Rust的にはUB)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、OOMは例えでした。
extract_fullcontext関数が失敗した==解釈に失敗した、ではない可能性があるなと思った次第です。
これが真かどうかはOpenJTalkAPIのドキュメントがないのでコードの深いとこまで読まないとわからないだろうから、含みを持たせた方が正確かなぁくらい。