pengwang0104's picture
Upload folder using huggingface_hub (#3)
27f5aa2 verified
raw
history blame
1.62 kB
import json
import sys
from zhipuai import ZhipuAI
def extract_comments(file_path, max_comments=100):
comments = []
with open(file_path, "r", encoding="utf-8") as f:
for line in f:
try:
data = json.loads(line.strip())
if "text" in data:
comments.append(data["text"])
if len(comments) >= max_comments:
break
except Exception as e:
print("解析错误:", e)
continue
return comments
def build_prompt(comments):
joined_text = "\n".join(comments)
prompt = f"""以下是来自一个 YouTube 视频的观众留言,共 {len(comments)} 条评论,语言为阿拉伯语:{joined_text}
请你根据这些评论的语言风格、词汇、表达方式,判断这些评论者使用的主要阿拉伯方言是哪一种?(如:标准阿拉伯语方言、沙特方言、摩洛哥方言、阿尔及利亚方言、伊拉克方言等)
只输出一种最可能的方言。
"""
return prompt
def detect_dialect(prompt):
response = client.chat.completions.create(
model="glm-4-flash",
messages=[
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content
if __name__ == "__main__":
info_file = sys.argv[1]
api_key = sys.argv[2]
client = ZhipuAI(api_key=api_key)
comments = extract_comments(info_file, max_comments=50)
prompt = build_prompt(comments)
result = detect_dialect(prompt)
# print("\n 检测结果:")
print(result)