Update dict api
This commit is contained in:
@@ -37,15 +37,16 @@ from common.constants import (
|
|||||||
DEFAULT_STYLE,
|
DEFAULT_STYLE,
|
||||||
DEFAULT_STYLE_WEIGHT,
|
DEFAULT_STYLE_WEIGHT,
|
||||||
LATEST_VERSION,
|
LATEST_VERSION,
|
||||||
USER_DICT_CSV_PATH,
|
|
||||||
USER_DICT_PATH,
|
|
||||||
Languages,
|
Languages,
|
||||||
)
|
)
|
||||||
from common.log import logger
|
from common.log import logger
|
||||||
from common.tts_model import ModelHolder
|
from common.tts_model import ModelHolder
|
||||||
from text.japanese import g2kata_tone, kata_tone2phone_tone
|
from text.japanese import g2kata_tone, kata_tone2phone_tone
|
||||||
|
from text.user_dict import apply_word, update_dict, read_dict, rewrite_word, delete_word
|
||||||
|
|
||||||
|
|
||||||
|
# ---フロントエンド部分に関する処理---
|
||||||
|
|
||||||
# エディターのビルドファイルを配置するディレクトリ
|
# エディターのビルドファイルを配置するディレクトリ
|
||||||
STATIC_DIR = Path("static")
|
STATIC_DIR = Path("static")
|
||||||
# エディターの最新のビルドファイルのダウンロード日時を記録するファイル
|
# エディターの最新のビルドファイルのダウンロード日時を記録するファイル
|
||||||
@@ -135,6 +136,10 @@ def save_last_download(latest_release):
|
|||||||
file.write(latest_release["published_at"])
|
file.write(latest_release["published_at"])
|
||||||
|
|
||||||
|
|
||||||
|
# ---フロントエンド部分に関する処理ここまで---
|
||||||
|
# 以降はAPIの設定
|
||||||
|
|
||||||
|
|
||||||
class AudioResponse(Response):
|
class AudioResponse(Response):
|
||||||
media_type = "audio/wav"
|
media_type = "audio/wav"
|
||||||
|
|
||||||
@@ -337,75 +342,54 @@ def multi_synthesis(request: MultiSynthesisRequest):
|
|||||||
return Response(content=wavContent.getvalue(), media_type="audio/wav")
|
return Response(content=wavContent.getvalue(), media_type="audio/wav")
|
||||||
|
|
||||||
|
|
||||||
class AddUserDictWordRequest(BaseModel):
|
class UserDictWordRequest(BaseModel):
|
||||||
surface: str
|
surface: str
|
||||||
pronunciation: str
|
pronunciation: str
|
||||||
accentType: str # アクセント核位置/モーラ数、例:1/3, アクセント核位置は1から始まる
|
accent_type: int # アクセント核位置(存在しない場合は0、1文字目は1)
|
||||||
priority: int = 5
|
priority: int = 5
|
||||||
|
|
||||||
|
|
||||||
# コストの値は以下の値を参考にしている
|
@router.get("/user_dict")
|
||||||
# https://github.com/VOICEVOX/voicevox_engine/blob/master/voicevox_engine/user_dict/part_of_speech_data.py
|
def get_user_dict():
|
||||||
COST_CANDIDATES = [-988, 3488, 4768, 6048, 7328, 8609, 8734, 8859, 8984, 9110, 14176]
|
return read_dict()
|
||||||
|
|
||||||
|
|
||||||
@router.post("/user_dict_word")
|
@router.post("/user_dict_word")
|
||||||
def add_user_dict_word(request: AddUserDictWordRequest):
|
def add_user_dict_word(request: UserDictWordRequest):
|
||||||
if request.surface == "" or request.pronunciation == "":
|
uuid = apply_word(
|
||||||
return JSONResponse(
|
surface=request.surface,
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
pronunciation=request.pronunciation,
|
||||||
content={"message": "単語か読みが空です。"},
|
accent_type=request.accent_type,
|
||||||
|
priority=request.priority,
|
||||||
)
|
)
|
||||||
user_dict_file = Path(USER_DICT_CSV_PATH)
|
update_dict()
|
||||||
user_dict_file.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
# 新規追加または更新する単語のCSV行
|
|
||||||
new_csv_row = f"{request.surface},,,{COST_CANDIDATES[request.priority]},名詞,固有名詞,一般,*,*,*,{request.surface},{request.pronunciation},{request.pronunciation},{request.accentType},*\n"
|
|
||||||
found = False
|
|
||||||
updated = False
|
|
||||||
|
|
||||||
# ユーザー辞書ファイルが存在する場合、既存の単語をチェックし、必要に応じて更新する
|
|
||||||
if user_dict_file.exists():
|
|
||||||
with user_dict_file.open(encoding="utf-8") as f:
|
|
||||||
lines = f.readlines()
|
|
||||||
|
|
||||||
with user_dict_file.open("w", encoding="utf-8") as f:
|
|
||||||
for line in lines:
|
|
||||||
if line.split(",")[0] == request.surface:
|
|
||||||
found = True
|
|
||||||
if line.strip() != new_csv_row.strip():
|
|
||||||
f.write(new_csv_row)
|
|
||||||
updated = True
|
|
||||||
else:
|
|
||||||
f.write(line)
|
|
||||||
else:
|
|
||||||
f.write(line)
|
|
||||||
# 単語が新しい場合、新規に追加
|
|
||||||
if not found:
|
|
||||||
with user_dict_file.open("a", encoding="utf-8") as f:
|
|
||||||
f.write(new_csv_row)
|
|
||||||
|
|
||||||
pyopenjtalk.unset_user_dict()
|
|
||||||
pyopenjtalk.mecab_dict_index(USER_DICT_CSV_PATH, USER_DICT_PATH)
|
|
||||||
pyopenjtalk.update_global_jtalk_with_user_dict(USER_DICT_PATH)
|
|
||||||
|
|
||||||
if updated:
|
|
||||||
return JSONResponse(
|
|
||||||
status_code=status.HTTP_200_OK,
|
|
||||||
content={"message": "単語が既に存在し、更新されました。"},
|
|
||||||
)
|
|
||||||
elif found:
|
|
||||||
return JSONResponse(
|
|
||||||
status_code=status.HTTP_200_OK,
|
|
||||||
content={"message": "既に登録されている単語です。更新はありません。"},
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=status.HTTP_201_CREATED,
|
status_code=status.HTTP_201_CREATED,
|
||||||
content={"message": "単語が新規追加されました。"},
|
content={"uuid": uuid},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("/user_dict_word/{uuid}")
|
||||||
|
def update_user_dict_word(uuid: str, request: UserDictWordRequest):
|
||||||
|
rewrite_word(
|
||||||
|
word_uuid=uuid,
|
||||||
|
surface=request.surface,
|
||||||
|
pronunciation=request.pronunciation,
|
||||||
|
accent_type=request.accent_type,
|
||||||
|
priority=request.priority,
|
||||||
|
)
|
||||||
|
update_dict()
|
||||||
|
return JSONResponse(status_code=status.HTTP_200_OK, content={"uuid": uuid})
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/user_dict_word/{uuid}")
|
||||||
|
def delete_user_dict_word(uuid: str):
|
||||||
|
delete_word(uuid)
|
||||||
|
update_dict()
|
||||||
|
return JSONResponse(status_code=status.HTTP_200_OK, content={"uuid": uuid})
|
||||||
|
|
||||||
|
|
||||||
app.include_router(router, prefix="/api")
|
app.include_router(router, prefix="/api")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import pyopenjtalk
|
|||||||
from num2words import num2words
|
from num2words import num2words
|
||||||
from transformers import AutoTokenizer
|
from transformers import AutoTokenizer
|
||||||
|
|
||||||
from common.constants import USER_DICT_PATH, USER_DICT_CSV_PATH
|
|
||||||
from common.log import logger
|
from common.log import logger
|
||||||
from text import punctuation
|
from text import punctuation
|
||||||
from text.japanese_mora_list import (
|
from text.japanese_mora_list import (
|
||||||
@@ -16,10 +15,10 @@ from text.japanese_mora_list import (
|
|||||||
mora_phonemes_to_mora_kata,
|
mora_phonemes_to_mora_kata,
|
||||||
)
|
)
|
||||||
|
|
||||||
if Path(USER_DICT_CSV_PATH).exists():
|
from text.user_dict import update_dict
|
||||||
pyopenjtalk.mecab_dict_index(USER_DICT_CSV_PATH, USER_DICT_PATH)
|
|
||||||
if Path(USER_DICT_PATH).exists():
|
# 最初にpyopenjtalkの辞書を更新
|
||||||
pyopenjtalk.update_global_jtalk_with_user_dict(USER_DICT_PATH)
|
update_dict()
|
||||||
|
|
||||||
# 子音の集合
|
# 子音の集合
|
||||||
COSONANTS = set(
|
COSONANTS = set(
|
||||||
|
|||||||
Reference in New Issue
Block a user