-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(character): add character_simple.py and fix regular expression
- Loading branch information
Showing
2 changed files
with
49 additions
and
15 deletions.
There are no files selected for viewing
62 changes: 48 additions & 14 deletions
62
hinghwa-dict-backend/word/character/dto/character_simple.py
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 |
---|---|---|
@@ -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 |
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