-
Notifications
You must be signed in to change notification settings - Fork 204
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
ユーザー辞書機能:単語の更新/削除機能を追加 #338
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d6e07f0
user_dict -> user_dict_word
takana-v d966183
PUT、DELETEを追加
takana-v d8e97a7
重複している処理を削除
takana-v ea34d2c
ミス修正
takana-v b840292
id引数を取るように変更
takana-v d266674
kwargs削除
takana-v 100c944
引数まわり調整
takana-v cde1728
kwargs使わなくなったので関数名変更
takana-v f178c05
テスト追加
takana-v 4c5d70c
テスト追加
takana-v c4fe9ba
idをurlに含めるように変更
takana-v 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import json | ||
from pathlib import Path | ||
from tempfile import TemporaryDirectory | ||
from unittest import TestCase | ||
|
||
from fastapi import HTTPException | ||
from pyopenjtalk import unset_user_dict | ||
|
||
from voicevox_engine.model import UserDictJson, UserDictWord | ||
from voicevox_engine.user_dict import ( | ||
apply_word, | ||
create_word, | ||
delete_word, | ||
read_dict, | ||
rewrite_word, | ||
) | ||
|
||
valid_dict_dict = { | ||
"next_id": 1, | ||
"words": { | ||
"0": { | ||
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. IDはintなんでしたっけ、stringなんでしたっけ |
||
"surface": "test", | ||
"cost": 8600, | ||
"part_of_speech": "名詞", | ||
"part_of_speech_detail_1": "固有名詞", | ||
"part_of_speech_detail_2": "一般", | ||
"part_of_speech_detail_3": "*", | ||
"inflectional_type": "*", | ||
"inflectional_form": "*", | ||
"stem": "*", | ||
"yomi": "テスト", | ||
"pronunciation": "テスト", | ||
"accent_type": 1, | ||
"accent_associative_rule": "*", | ||
} | ||
}, | ||
} | ||
|
||
|
||
class TestUserDict(TestCase): | ||
def setUp(self): | ||
self.tmp_dir = TemporaryDirectory() | ||
self.tmp_dir_path = Path(self.tmp_dir.name) | ||
|
||
def tearDown(self): | ||
unset_user_dict() | ||
self.tmp_dir.cleanup() | ||
|
||
def test_read_not_exist_json(self): | ||
self.assertEqual( | ||
read_dict(user_dict_path=(self.tmp_dir_path / "not_exist.json")), | ||
UserDictJson(**{"next_id": 0, "words": {}}), | ||
) | ||
|
||
def test_create_word(self): | ||
# 将来的に品詞などが追加された時にテストを増やす | ||
self.assertEqual( | ||
create_word(surface="test", pronunciation="テスト", accent_type=1), | ||
UserDictWord( | ||
surface="test", | ||
cost=8600, | ||
part_of_speech="名詞", | ||
part_of_speech_detail_1="固有名詞", | ||
part_of_speech_detail_2="一般", | ||
part_of_speech_detail_3="*", | ||
inflectional_type="*", | ||
inflectional_form="*", | ||
stem="*", | ||
yomi="テスト", | ||
pronunciation="テスト", | ||
accent_type=1, | ||
accent_associative_rule="*", | ||
), | ||
) | ||
|
||
def test_apply_word_without_json(self): | ||
user_dict_path = self.tmp_dir_path / "test_apply_word_without_json.json" | ||
apply_word( | ||
surface="test", | ||
pronunciation="テスト", | ||
accent_type=1, | ||
user_dict_path=user_dict_path, | ||
compiled_dict_path=(self.tmp_dir_path / "test_apply_word_without_json.dic"), | ||
) | ||
res = read_dict(user_dict_path=user_dict_path) | ||
self.assertEqual(len(res.words), 1) | ||
self.assertEqual(res.next_id, 1) | ||
self.assertEqual( | ||
( | ||
res.words[0].surface, | ||
res.words[0].pronunciation, | ||
res.words[0].accent_type, | ||
), | ||
("test", "テスト", 1), | ||
) | ||
|
||
def test_apply_word_with_json(self): | ||
user_dict_path = self.tmp_dir_path / "test_apply_word_with_json.json" | ||
user_dict_path.write_text( | ||
json.dumps(valid_dict_dict, ensure_ascii=False), encoding="utf-8" | ||
) | ||
apply_word( | ||
surface="test2", | ||
pronunciation="テストツー", | ||
accent_type=3, | ||
user_dict_path=user_dict_path, | ||
compiled_dict_path=(self.tmp_dir_path / "test_apply_word_with_json.dic"), | ||
) | ||
res = read_dict(user_dict_path=user_dict_path) | ||
self.assertEqual(len(res.words), 2) | ||
self.assertEqual(res.next_id, 2) | ||
self.assertEqual( | ||
( | ||
res.words[1].surface, | ||
res.words[1].pronunciation, | ||
res.words[1].accent_type, | ||
), | ||
("test2", "テストツー", 3), | ||
) | ||
|
||
def test_rewrite_word_invalid_id(self): | ||
user_dict_path = self.tmp_dir_path / "test_rewrite_word_invalid_id.json" | ||
user_dict_path.write_text( | ||
json.dumps(valid_dict_dict, ensure_ascii=False), encoding="utf-8" | ||
) | ||
self.assertRaises( | ||
HTTPException, | ||
rewrite_word, | ||
id=1, | ||
surface="test2", | ||
pronunciation="テストツー", | ||
accent_type=2, | ||
user_dict_path=user_dict_path, | ||
compiled_dict_path=(self.tmp_dir_path / "test_rewrite_word_invalid_id.dic"), | ||
) | ||
|
||
def test_rewrite_word_valid_id(self): | ||
user_dict_path = self.tmp_dir_path / "test_rewrite_word_valid_id.json" | ||
user_dict_path.write_text( | ||
json.dumps(valid_dict_dict, ensure_ascii=False), encoding="utf-8" | ||
) | ||
rewrite_word( | ||
id=0, | ||
surface="test2", | ||
pronunciation="テストツー", | ||
accent_type=2, | ||
user_dict_path=user_dict_path, | ||
compiled_dict_path=(self.tmp_dir_path / "test_rewrite_word_valid_id.dic"), | ||
) | ||
res = read_dict(user_dict_path=user_dict_path).words[0] | ||
self.assertEqual( | ||
(res.surface, res.pronunciation, res.accent_type), ("test2", "テストツー", 2) | ||
) | ||
|
||
def test_delete_word_invalid_id(self): | ||
user_dict_path = self.tmp_dir_path / "test_delete_word_invalid_id.json" | ||
user_dict_path.write_text( | ||
json.dumps(valid_dict_dict, ensure_ascii=False), encoding="utf-8" | ||
) | ||
self.assertRaises( | ||
HTTPException, | ||
delete_word, | ||
id=1, | ||
user_dict_path=user_dict_path, | ||
compiled_dict_path=(self.tmp_dir_path / "test_delete_word_invalid_id.dic"), | ||
) | ||
|
||
def test_delete_word_valid_id(self): | ||
user_dict_path = self.tmp_dir_path / "test_delete_word_valid_id.json" | ||
user_dict_path.write_text( | ||
json.dumps(valid_dict_dict, ensure_ascii=False), encoding="utf-8" | ||
) | ||
delete_word( | ||
id=0, | ||
user_dict_path=user_dict_path, | ||
compiled_dict_path=(self.tmp_dir_path / "test_delete_word_valid_id.dic"), | ||
) | ||
self.assertEqual(len(read_dict(user_dict_path=user_dict_path).words), 0) |
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
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.
将来辞書エディターみたないなのが出たりしたとき、
next_id
の数値と、word idがずれてしまう未来が見えました。uuidにしても良いかも?