Skip to content

Commit

Permalink
feat(character): add character_simple.py and fix regular expression
Browse files Browse the repository at this point in the history
  • Loading branch information
CapooL committed Mar 6, 2024
1 parent 384ad7d commit a7f30c1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
62 changes: 48 additions & 14 deletions hinghwa-dict-backend/word/character/dto/character_simple.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion hinghwa-dict-backend/word/character/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a7f30c1

Please sign in to comment.