Skip to content
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

kana_parser 実装 #155

Merged
merged 21 commits into from
Jul 1, 2022
Merged

kana_parser 実装 #155

merged 21 commits into from
Jul 1, 2022

Conversation

nebocco
Copy link
Contributor

@nebocco nebocco commented Jun 10, 2022

内容

core/src/engine/mora_list.cpp, kana_parser.cpp の内容を Rust で実装しました。

関連 Issue

ref #128

その他

存在する関数を一通り実装し、簡単なテストを書きました。とりあえずこれ自体で機能が完結しているものを作ろうとしたので、以下のような修正点・調整点が残っています。

  • エラーが独自型である(core の Error とは別物)
  • モジュールの階層構造をどうするか、整理整頓
  • 関数の公開範囲をどうするか
  • Rust 的にはもう少しすっきり書けそう

また、C++ での実装で計算量の悪い箇所がありましたがそのまま残しています。

@qwerty2501
Copy link
Contributor

元がengineディレクトリ切ってるのでそれに寄せてengine module作ったほうが良いと思います

@qwerty2501
Copy link
Contributor

エラーを独自型にした理由ってなんかあったりするんですか?

@nebocco
Copy link
Contributor Author

nebocco commented Jun 10, 2022

エラーを独自型にした理由ってなんかあったりするんですか?

一旦書き写すにあたって閉じたモジュールにしたかったためです。TTS機能の実装ももう少し先になりそうなのでそのときに対応させればいいかなと思っていました。

例えばTTS系統のエラーはcoreとは別に型を作ってまとめる、みたいなことも将来的にあるかな、と想像しています

@PickledChair
Copy link
Member

@nebocco PR ありがとうございます! 細かいところは後で詳しく見てみたいと思います。

プロジェクトの構成については @qwerty2501 さんの意見

元がengineディレクトリ切ってるのでそれに寄せてengine module作ったほうが良いと思います

に同意で、例えば

voicevox_core
├── Cargo.toml
├── build.rs
└── src
    ├── c_export.rs
    ├── engine
    │   ├── kana_parser.rs
    │   └── mod.rs
    ├── error.rs
    ├── internal.rs
    ├── lib.rs
    ├── result.rs
    └── status.rs

みたいな構造にするのが良いのかな、と思いました!

@Hiroshiba
Copy link
Member

あ!!
これはただの思いつきですが、engineディレクトリも良いのですが別クレート(例えばvoicevox_ttsとか)にすると、最終的にdll分けたりopenjtalkを含めたり含めなかったりできるかもと感じました。
(でもプルリクの主旨とは異なるので、engineディレクトリでも問題ないと思います。)

@Hiroshiba Hiroshiba closed this Jun 10, 2022
@Hiroshiba
Copy link
Member

すみません、間違えました🙇‍♂️

@Hiroshiba Hiroshiba reopened this Jun 10, 2022
Copy link
Member

@PickledChair PickledChair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

とりあえずテスト以外のコードをレビューしました(すみません、テストコードはまた後ほどレビューします……!)。確認よろしくお願いいたします。

crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
@nebocco
Copy link
Contributor Author

nebocco commented Jun 12, 2022

@PickledChair
レビューありがとうございます。修正いたします。

@nebocco
Copy link
Contributor Author

nebocco commented Jun 12, 2022

あ!! これはただの思いつきですが、engineディレクトリも良いのですが別クレート(例えばvoicevox_ttsとか)にすると、最終的にdll分けたりopenjtalkを含めたり含めなかったりできるかもと感じました。 (でもプルリクの主旨とは異なるので、engineディレクトリでも問題ないと思います。)

@Hiroshiba
ご提案ありがとうございます。確かに最終的には tts 側の機能は別クレートにまとめてしまったほうがよさそうですね。ただ engine モジュールごとまるっとコピペしてしまえば クレートを切り分けるのは比較的簡単だと思うので、クレート分割は後々 tts 機能を本格的に実装していくタイミングに譲りたいと思います。いかがでしょうか?

@nebocco
Copy link
Contributor Author

nebocco commented Jun 12, 2022

ひとまずいただいたレビューのうち以下の二点以外を修正しました。

@Hiroshiba
Copy link
Member

Hiroshiba commented Jun 12, 2022

ただ engine モジュールごとまるっとコピペしてしまえば クレートを切り分けるのは比較的簡単だと思うので、クレート分割は後々 tts 機能を本格的に実装していくタイミングに譲りたいと思います。いかがでしょうか?

TTS用のクレート分割は後のタイミングで大丈夫だと思います!

Copy link
Member

@PickledChair PickledChair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

申し訳ありません、なかなかレビューの時間が取れず、かなりお待たせしてしまいました……! テストコードについてもレビューしました。確認よろしくお願いします。

他の Rust 実装関連の PR がいくつかマージされ、おそらくコンフリクトが発生しているので、Draft を外す際は解消をよろしくお願いいたします……!

crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/mora_list.rs Outdated Show resolved Hide resolved
@nebocco
Copy link
Contributor Author

nebocco commented Jun 29, 2022

レビューありがとうございました。ご指摘いただいた点を修正いたしました。

@nebocco nebocco marked this pull request as ready for review June 29, 2022 02:58
Copy link
Member

@Hiroshiba Hiroshiba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!!

まだ実際に音声合成として動かせてないかもですが、一旦マージしちゃうのが良いのかなと思っています・・・!!

Copy link
Member

@PickledChair PickledChair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみません、前回のレビューが中途半端な書き方になってしまい、少し意図が伝わらなかった部分があったようなので、追加の change request を出しました(rstest を使うように書いたことについてなのですが、機能的に便利だから使うということの他に、voicevox_core クレートのテストが基本的に全て rstest を使って書かれており、可読性を上げるために全てのテストで rstest を似たような形式で用いたい、という意図があってコメントしていました。わかりづらくて申し訳ありませんでした……!)。

crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
crates/voicevox_core/src/engine/kana_parser.rs Outdated Show resolved Hide resolved
Comment on lines 2 to 33
#[derive(Clone, Debug)]
pub(super) struct MoraModel {
pub text: String,
pub consonant: Option<String>,
pub consonant_length: Option<f32>,
pub vowel: String,
pub vowel_length: f32,
pub pitch: f32,
}

#[allow(dead_code)] // TODO: remove this feature
#[derive(Debug)]
pub(super) struct AccentPhraseModel {
pub moras: Vec<MoraModel>,
pub accent: usize,
pub pause_mora: Option<MoraModel>,
pub is_interrogative: bool,
}

#[allow(dead_code)] // TODO: remove this feature
pub(super) struct AudioQueryModel {
accent_phrases: Vec<AccentPhraseModel>,
speed_scale: f32,
pitch_scale: f32,
intonation_scale: f32,
volume_scale: f32,
pre_phoneme_length: f32,
post_phoneme_length: f32,
output_sampling_rate: u32,
output_stereo: bool,
kana: String,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いったんderive_newとderive_getters使ってみようという方針になっていたはずなので各structにdrive(new,Getter)をしてfieldのpubを外してstructのimmutable性を高めたほうが良いと思います

Suggested change
#[derive(Clone, Debug)]
pub(super) struct MoraModel {
pub text: String,
pub consonant: Option<String>,
pub consonant_length: Option<f32>,
pub vowel: String,
pub vowel_length: f32,
pub pitch: f32,
}
#[allow(dead_code)] // TODO: remove this feature
#[derive(Debug)]
pub(super) struct AccentPhraseModel {
pub moras: Vec<MoraModel>,
pub accent: usize,
pub pause_mora: Option<MoraModel>,
pub is_interrogative: bool,
}
#[allow(dead_code)] // TODO: remove this feature
pub(super) struct AudioQueryModel {
accent_phrases: Vec<AccentPhraseModel>,
speed_scale: f32,
pitch_scale: f32,
intonation_scale: f32,
volume_scale: f32,
pre_phoneme_length: f32,
post_phoneme_length: f32,
output_sampling_rate: u32,
output_stereo: bool,
kana: String,
}
#[derive(Clone, Debug, new, Getter)]
pub(super) struct MoraModel {
text: String,
consonant: Option<String>,
consonant_length: Option<f32>,
vowel: String,
vowel_length: f32,
pitch: f32,
}
#[allow(dead_code)] // TODO: remove this feature
#[derive(Debug, new, Getter)]
pub(super) struct AccentPhraseModel {
moras: Vec<MoraModel>,
accent: usize,
pause_mora: Option<MoraModel>,
is_interrogative: bool,
}
#[allow(dead_code)] // TODO: remove this feature
#[derive(new, Getter)]
pub(super) struct AudioQueryModel {
accent_phrases: Vec<AccentPhraseModel>,
speed_scale: f32,
pitch_scale: f32,
intonation_scale: f32,
volume_scale: f32,
pre_phoneme_length: f32,
post_phoneme_length: f32,
output_sampling_rate: u32,
output_stereo: bool,
kana: String,
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

とりあえず new と Getters を追加したのですが、ごく一部の属性は mutable に扱うことがあるため、最低限の広さで public としてみました。

let accent_phrase = {
let mut accent_phrase = text_to_accent_phrase(&phrase)?;
if letter == PAUSE_DELIMITER {
accent_phrase.pause_mora = Some(MoraModel::new(
PAUSE_DELIMITER.to_string(),
None,
None,
"pau".to_string(),
));
}
accent_phrase.is_interrogative = is_interrogative;
accent_phrase

#[allow(dead_code)] // TODO: remove this feature
#[derive(Debug, new, Getters)]
pub(super) struct AccentPhraseModel {
moras: Vec<MoraModel>,
accent: usize,
pub(super) pause_mora: Option<MoraModel>,
pub(super) is_interrogative: bool,
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accessibilityを変えるのではなく、必要な部分にだけsetterをAccentPhraseModel に生やすように出来ますか?
機能上全てのフィールドを初期化後に変更する可能性があるのであればGettersを使わずに初めからpublicにしたほうがよいですが、今回のように部分的に変更するのであればsetterが良いと思います

nebocco and others added 7 commits June 30, 2022 17:07
fix: use rstest

Co-authored-by: Gray Suitcase <41382894+PickledChair@users.noreply.github.com>
Co-authored-by: qwerty2501 <939468+qwerty2501@users.noreply.github.com>
Co-authored-by: Gray Suitcase <41382894+PickledChair@users.noreply.github.com>
Co-authored-by: Gray Suitcase <41382894+PickledChair@users.noreply.github.com>
Copy link
Member

@PickledChair PickledChair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! PR ありがとうございました。長いレビュー期間になってしまい大変お待たせしてしまいました。お疲れ様でした!

#155 (comment) への対応に関して、 @qwerty2501 さんが特に問題ないという感じでしたらマージで良いと思います!

Copy link
Contributor

@qwerty2501 qwerty2501 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

対応はいってたんですね LGTM

@PickledChair PickledChair merged commit ad69533 into VOICEVOX:rust Jul 1, 2022
@Hiroshiba
Copy link
Member

PRありがとうございました!!
コアは結構ほしい機能がまだまだあったりするので、よかったらまたPRいただけると心強いです!!!

@nebocco nebocco deleted the rust branch July 1, 2022 07:06
qwerty2501 added a commit to qwerty2501/voicevox_core that referenced this pull request Jul 23, 2022
* WIP: kana parser

* add: parse_kana/create_kana

* add: error handling, tests

* fix: removed unused #[derive()]

* fix: cargo clippy

* modify: modified directory layout

* fmt: cargo fmt

* fixx: mistake

* modify: module layout

* fix: reflect reviews

* fix: use hashMap instead of BTreeMap

* change: use rstest for mod tests

* Update crates/voicevox_core/src/engine/kana_parser.rs

fix: use rstest

Co-authored-by: Gray Suitcase <41382894+PickledChair@users.noreply.github.com>

* update: use #[derive(new, Getter)]

Co-authored-by: qwerty2501 <939468+qwerty2501@users.noreply.github.com>

* fix: use rstest

Co-authored-by: Gray Suitcase <41382894+PickledChair@users.noreply.github.com>

* fix: use rstest

Co-authored-by: Gray Suitcase <41382894+PickledChair@users.noreply.github.com>

* fix: use new() and getters

* fmt: clippy

* update: define setters for requiring attrs

Co-authored-by: Gray Suitcase <41382894+PickledChair@users.noreply.github.com>
Co-authored-by: qwerty2501 <939468+qwerty2501@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants