Skip to content

Commit

Permalink
fix!: various fix for UserDictWord (#1002)
Browse files Browse the repository at this point in the history
1. C APIとRust APIのみ`priority`のデフォルトが`5`ではなく`0`になっていた
   ため、`5`に合わせることでPython, Java, VOICEVOX ENGINEに合わせる。
2. VOICEVOX ENGINEにならった場合、`surface`, `pronounciation`と同様
   `accent_type`もrequiredであるはずなので、そうしてしまう。
3. `impl Default for UserDictWord`を削除。
  • Loading branch information
qryxip authored Feb 12, 2025
1 parent 849f5b0 commit 71504d9
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 46 deletions.
1 change: 1 addition & 0 deletions crates/voicevox_core/src/__internal/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pub use crate::{
blocking::PerformInference, DEFAULT_CPU_NUM_THREADS, DEFAULT_ENABLE_INTERROGATIVE_UPSPEAK,
MARGIN,
},
user_dict::{DEFAULT_PRIORITY, DEFAULT_WORD_TYPE},
};
2 changes: 1 addition & 1 deletion crates/voicevox_core/src/user_dict/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ mod part_of_speech_data;
mod word;

pub(crate) use self::word::{to_zenkaku, validate_pronunciation, InvalidWordError};
pub use self::word::{UserDictWord, UserDictWordType};
pub use self::word::{UserDictWord, UserDictWordType, DEFAULT_PRIORITY, DEFAULT_WORD_TYPE};
16 changes: 3 additions & 13 deletions crates/voicevox_core/src/user_dict/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ impl InvalidWordError {

type InvalidWordResult<T> = std::result::Result<T, InvalidWordError>;

pub const DEFAULT_WORD_TYPE: UserDictWordType = UserDictWordType::CommonNoun;
pub const DEFAULT_PRIORITY: u32 = 5;

static PRONUNCIATION_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[ァ-ヴー]+$").unwrap());
static MORA_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Expand All @@ -94,19 +97,6 @@ static MORA_REGEX: LazyLock<Regex> = LazyLock::new(|| {
});
static SPACE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\p{Z}").unwrap());

impl Default for UserDictWord {
fn default() -> Self {
Self {
surface: "".to_string(),
pronunciation: "".to_string(),
accent_type: 0,
word_type: UserDictWordType::CommonNoun,
priority: 0,
mora_count: 0,
}
}
}

impl UserDictWord {
// TODO: これビルダースタイルにすべきでは?
#[doc(alias = "voicevox_user_dict_word_make")]
Expand Down
4 changes: 3 additions & 1 deletion crates/voicevox_core_c_api/include/voicevox_core.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use std::sync::Once;
use tracing_subscriber::fmt::format::Writer;
use tracing_subscriber::EnvFilter;
use uuid::Uuid;
use voicevox_core::__internal::interop::ToJsonValue as _;
use voicevox_core::{AccentPhrase, AudioQuery, StyleId, UserDictWord};
use voicevox_core::__internal::interop::{ToJsonValue as _, DEFAULT_PRIORITY, DEFAULT_WORD_TYPE};
use voicevox_core::{AccentPhrase, AudioQuery, StyleId};

fn init_logger_once() {
static ONCE: Once = Once::new();
Expand Down Expand Up @@ -1431,21 +1431,23 @@ pub enum VoicevoxUserDictWordType {
///
/// @param [in] surface 表記
/// @param [in] pronunciation 読み
/// @param [in] accent_type アクセント型
/// @returns ::VoicevoxUserDictWord
///
/// \orig-impl{voicevox_user_dict_word_make}
#[no_mangle]
pub extern "C" fn voicevox_user_dict_word_make(
surface: *const c_char,
pronunciation: *const c_char,
accent_type: usize,
) -> VoicevoxUserDictWord {
init_logger_once();
VoicevoxUserDictWord {
surface,
pronunciation,
accent_type: UserDictWord::default().accent_type(),
word_type: UserDictWord::default().word_type().into(),
priority: UserDictWord::default().priority(),
accent_type,
word_type: DEFAULT_WORD_TYPE.into(),
priority: DEFAULT_PRIORITY,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl assert_cdylib::TestCase for TestCase {
let mut word = lib.voicevox_user_dict_word_make(
c"this_word_should_not_exist_in_default_dictionary".as_ptr(),
c"アイウエオ".as_ptr(),
0,
);
word.word_type =
c_api::VoicevoxUserDictWordType_VOICEVOX_USER_DICT_WORD_TYPE_PROPER_NOUN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl assert_cdylib::TestCase for TestCase {
let dict = lib.voicevox_user_dict_new();

// 単語の追加のテスト
let word = lib.voicevox_user_dict_word_make(c"hoge".as_ptr(), c"ホゲ".as_ptr());
let word = lib.voicevox_user_dict_word_make(c"hoge".as_ptr(), c"ホゲ".as_ptr(), 0);

let word_uuid = add_word(dict, &word);

Expand All @@ -66,7 +66,7 @@ impl assert_cdylib::TestCase for TestCase {
assert_contains_uuid(&json, &word_uuid);

// 単語の変更のテスト
let word = lib.voicevox_user_dict_word_make(c"fuga".as_ptr(), c"フガ".as_ptr());
let word = lib.voicevox_user_dict_word_make(c"fuga".as_ptr(), c"フガ".as_ptr(), 0);

assert_ok(lib.voicevox_user_dict_update_word(dict, &word_uuid.into_bytes(), &word));

Expand All @@ -81,7 +81,7 @@ impl assert_cdylib::TestCase for TestCase {
// 辞書のインポートのテスト。
let other_dict = lib.voicevox_user_dict_new();

let other_word = lib.voicevox_user_dict_word_make(c"piyo".as_ptr(), c"ピヨ".as_ptr());
let other_word = lib.voicevox_user_dict_word_make(c"piyo".as_ptr(), c"ピヨ".as_ptr(), 0);

let other_word_uuid = add_word(other_dict, &other_word);

Expand All @@ -106,7 +106,7 @@ impl assert_cdylib::TestCase for TestCase {
// 辞書のセーブ・ロードのテスト
let temp_path = NamedTempFile::new().unwrap().into_temp_path();
let temp_path = CString::new(temp_path.to_str().unwrap()).unwrap();
let word = lib.voicevox_user_dict_word_make(c"hoge".as_ptr(), c"ホゲ".as_ptr());
let word = lib.voicevox_user_dict_word_make(c"hoge".as_ptr(), c"ホゲ".as_ptr(), 0);
let word_uuid = add_word(dict, &word);

assert_ok(lib.voicevox_user_dict_save(dict, temp_path.as_ptr()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,25 @@ public class UserDictWord {
*
* @param surface 言葉の表層形。
* @param pronunciation 言葉の発音。
* @param accentType アクセント型。
* @throws IllegalArgumentException pronunciationが不正な場合。
*/
public UserDictWord(String surface, String pronunciation) {
public UserDictWord(String surface, String pronunciation, int accentType) {
if (surface == null) {
throw new NullPointerException("surface");
}
if (pronunciation == null) {
throw new NullPointerException("pronunciation");
}
if (accentType < 0) {
throw new IllegalArgumentException("accentType");
}

this.surface = rsToZenkaku(surface);
rsValidatePronunciation(pronunciation);
this.pronunciation = pronunciation;
this.wordType = Type.COMMON_NOUN;
this.accentType = 0;
this.accentType = accentType;
this.priority = 5;
}

Expand All @@ -91,20 +95,6 @@ public UserDictWord wordType(Type wordType) {
return this;
}

/**
* アクセント型を設定する。
*
* @param accentType アクセント型。
* @return このインスタンス。
*/
public UserDictWord accentType(int accentType) {
if (accentType < 0) {
throw new IllegalArgumentException("accentType");
}
this.accentType = accentType;
return this;
}

/**
* 優先度を設定する。
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void checkLoad() throws RunModelException, InvalidModelDataException, LoadUserDi
"this_word_should_not_exist_in_default_dictionary",
synthesizer.metas()[0].styles[0].id);

userDict.addWord(new UserDictWord("this_word_should_not_exist_in_default_dictionary", "テスト"));
userDict.addWord(
new UserDictWord("this_word_should_not_exist_in_default_dictionary", "テスト", 1));
openJtalk.useUserDict(userDict);
AudioQuery query2 =
synthesizer.createAudioQuery(
Expand All @@ -44,21 +45,21 @@ void checkLoad() throws RunModelException, InvalidModelDataException, LoadUserDi
void checkManipulation() throws Exception {
UserDict userDict = new UserDict();
// 単語追加
String uuid = userDict.addWord(new UserDictWord("hoge", "ホゲ"));
String uuid = userDict.addWord(new UserDictWord("hoge", "ホゲ", 0));
assertTrue(userDict.toHashMap().get(uuid) != null);

// 単語更新
userDict.updateWord(uuid, new UserDictWord("hoge", "ホゲホゲ"));
userDict.updateWord(uuid, new UserDictWord("hoge", "ホゲホゲ", 0));
assertTrue(userDict.toHashMap().get(uuid).pronunciation.equals("ホゲホゲ"));

// 単語削除
userDict.removeWord(uuid);
assertTrue(userDict.toHashMap().get(uuid) == null);

// 辞書のインポート
userDict.addWord(new UserDictWord("hoge", "ホゲ"));
userDict.addWord(new UserDictWord("hoge", "ホゲ", 0));
UserDict userDict2 = new UserDict();
userDict2.addWord(new UserDictWord("fuga", "フガ"));
userDict2.addWord(new UserDictWord("fuga", "フガ", 0));
userDict.importDict(userDict2);
assertTrue(userDict.toHashMap().size() == 2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="this_word_should_not_exist_in_default_dictionary",
pronunciation="アイウエオ",
accent_type=0,
)
)
assert isinstance(uuid, UUID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="hoge",
pronunciation="ホゲ",
accent_type=0,
)
)
assert isinstance(uuid_a, UUID)
Expand All @@ -36,6 +37,7 @@ async def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="fuga",
pronunciation="フガ",
accent_type=0,
),
)

Expand All @@ -48,6 +50,7 @@ async def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="foo",
pronunciation="フー",
accent_type=0,
)
)

Expand All @@ -60,6 +63,7 @@ async def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="bar",
pronunciation="バー",
accent_type=0,
)
)
temp_path_fd, temp_path = tempfile.mkstemp()
Expand All @@ -80,5 +84,6 @@ async def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="",
pronunciation="カタカナ以外の文字",
accent_type=0,
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="this_word_should_not_exist_in_default_dictionary",
pronunciation="アイウエオ",
accent_type=0,
)
)
assert isinstance(uuid, UUID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="hoge",
pronunciation="ホゲ",
accent_type=0,
)
)
assert isinstance(uuid_a, UUID)
Expand All @@ -35,6 +36,7 @@ def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="fuga",
pronunciation="フガ",
accent_type=0,
),
)

Expand All @@ -47,6 +49,7 @@ def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="foo",
pronunciation="フー",
accent_type=0,
)
)

Expand All @@ -59,6 +62,7 @@ def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="bar",
pronunciation="バー",
accent_type=0,
)
)
temp_path_fd, temp_path = tempfile.mkstemp()
Expand All @@ -79,5 +83,6 @@ def test_user_dict_load() -> None:
voicevox_core.UserDictWord(
surface="",
pronunciation="カタカナ以外の文字",
accent_type=0,
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ class UserDictWord:
カタカナで表記する。
"""

accent_type: int = dataclasses.field(default=0)
accent_type: int
"""
アクセント型。
Expand Down

0 comments on commit 71504d9

Please sign in to comment.