englishMate / post_process.py
hanbinChen's picture
Refactor imports and update configuration for LLM response processing; add post-processing functionality and enhance README documentation.
25fca04
import json
RESPONSE = """
{
"english": "I went for a walk in the park yesterday and saw many people exercising there.",
"important_words": [
{
"word_en": "exercise",
"meaning_ch": "to exercise",
"usage": "e.g. I go to the gym to exercise every day."
},
{
"word_en": "park",
"meaning_ch": "a public place for recreation",
"usage": "e.g. I took a walk in the park on Sunday."
},
{
"word_en": "scatter",
"meaning_ch": "to walk or move about without a regular or planned route",
"usage": "e.g. I like to scatter around the city to explore new places."
}
]
}
"""
def process_response(response: str) -> (str, list):
"""处理LLM响应,提取需要展示的信息"""
# response into json
response = json.loads(response)
eng_translation = response.get("english")
important_words_list = response.get("important_words")
print("English Translation:", eng_translation)
print("Important Words:", important_words_list)
return eng_translation, important_words_list
# 测试新的函数
if __name__ == "__main__":
english_translation, important_words = process_response(RESPONSE)
# check type then print
print(type(english_translation), english_translation)
print(type(important_words), important_words)