'commit'
This commit is contained in:
@@ -61,13 +61,22 @@ class XunFeiAudioEvaluator:
|
||||
def on_message(self, ws, message):
|
||||
"""WebSocket消息处理"""
|
||||
print(f"Received message: {message}")
|
||||
data = json.loads(message)
|
||||
|
||||
# 检查是否存在错误码
|
||||
if data.get("code") != 0:
|
||||
print(f"API错误: {data.get('message', '未知错误')}")
|
||||
ws.close()
|
||||
return
|
||||
try:
|
||||
data = json.loads(message)
|
||||
if data.get('code') != 0:
|
||||
self.results = {'error': data.get('message', 'Unknown error')}
|
||||
return
|
||||
|
||||
# 修复:移除状态码为2的硬性检查,只要有数据就解析
|
||||
inner_data = data.get('data', {})
|
||||
xml_b64 = inner_data.get('data', '')
|
||||
if xml_b64:
|
||||
xml_data = base64.b64decode(xml_b64)
|
||||
xml_content = xml_data.decode("utf-8")
|
||||
self.parse_evaluation_results(xml_content) # 确保中文也能进入解析
|
||||
except Exception as e:
|
||||
print(f"Error processing message: {e}")
|
||||
self.results = {'error': str(e)}
|
||||
|
||||
# 安全获取data字段
|
||||
response_data = data.get("data", {})
|
||||
@@ -158,8 +167,23 @@ class XunFeiAudioEvaluator:
|
||||
time.sleep(0.04)
|
||||
|
||||
def parse_evaluation_results(self, xml_content):
|
||||
root = ET.fromstring(xml_content)
|
||||
# 兼容中英文不同的根节点
|
||||
read_node = root.find('.//read_sentence') or root.find('.//read_chapter')
|
||||
if read_node is not None:
|
||||
self.results = {
|
||||
'total_score': read_node.attrib.get('total_score', '0'),
|
||||
'accuracy': read_node.attrib.get('accuracy_score', '0'),
|
||||
'fluency': read_node.attrib.get('fluency_score', '0'),
|
||||
'integrity': read_node.attrib.get('integrity_score', '0')
|
||||
}
|
||||
else:
|
||||
# 打印未匹配的XML结构用于调试
|
||||
print(f"No evaluation nodes found in XML: {xml_content}")
|
||||
self.results = {'error': '未找到有效评分节点'}
|
||||
"""解析评测结果XML并提取得分信息"""
|
||||
try:
|
||||
print(xml_content)
|
||||
root = ET.fromstring(xml_content)
|
||||
|
||||
# 查找read_chapter节点
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user