From a7f30c1c6d2017a6c3edc01be5fc56c6ac66c6a4 Mon Sep 17 00:00:00 2001 From: CapooL <2053217188@qq.com> Date: Wed, 6 Mar 2024 20:50:41 +0800 Subject: [PATCH] feat(character): add character_simple.py and fix regular expression --- .../word/character/dto/character_simple.py | 62 ++++++++++++++----- hinghwa-dict-backend/word/character/views.py | 2 +- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/hinghwa-dict-backend/word/character/dto/character_simple.py b/hinghwa-dict-backend/word/character/dto/character_simple.py index 65a1f326..c3dd7f7a 100644 --- a/hinghwa-dict-backend/word/character/dto/character_simple.py +++ b/hinghwa-dict-backend/word/character/dto/character_simple.py @@ -1,17 +1,51 @@ -from ...models import Character +from ...models import Character, Word, Pronunciation def character_simple(character: Character) -> dict: - response = { - "id": character.id, - "shengmu": character.shengmu, - "ipa": character.ipa, - "pinyin": character.pinyin, - "yunmu": character.yunmu, - "shengdiao": character.shengdiao, - "character": character.character, - "county": character.county, - "town": character.town, - "traditional": character.traditional, - } - return response + dic = {} + word = Word.objects.filter(standard_pinyin=character.pinyin).filter( + word=character.character + ) + word_id = word[0].id if word.exists() else None + source = Pronunciation.objects.filter(pinyin=character.pinyin) + source_value = source[0].source if source.exists() else None + + # 构建字典 + key = (character.character, character.traditional) + if key not in dic: + dic[key] = [] + + dic[key].append( + { + "id": character.id, + "shengmu": character.shengmu, + "ipa": character.ipa, + "pinyin": character.pinyin, + "yunmu": character.yunmu, + "shengdiao": character.shengdiao, + "character": character.character, + "county": character.county, + "town": character.town, + "traditional": character.traditional, + "word": {"id": word_id} if word else None, + "source": {"source": source_value} if source else None, + } + ) + + ans = [] + for key, value in dic.items(): + char, traditional = key + result_list = [ + {"county": item["county"], "town": item["town"], "characters": item} + for item in value + ] + ans.append( + { + "label": char, + "traditional": traditional, + "characters": result_list, + } + ) + ans = ans[0] + + return ans diff --git a/hinghwa-dict-backend/word/character/views.py b/hinghwa-dict-backend/word/character/views.py index 6c883071..b69976f2 100644 --- a/hinghwa-dict-backend/word/character/views.py +++ b/hinghwa-dict-backend/word/character/views.py @@ -285,7 +285,7 @@ def searchCharactersPinYinV2(request): try: if request.method == "GET": content = request.GET["search"] - search = re.findall(r"[a-zA-Z]+", content) + search = re.findall(r"[a-zA-Z]+\d*", content) result = [] for key in search: characters = Character.objects.filter(pinyin__icontains=key)