You need to install the SpaCy model suitable for Chinese. You can do this by running
pip install spacy
python -m spacy download zh_core_web_sm
to install and download the required models.
import spacy
# 加载中文模型
nlp = spacy.load('zh_core_web_sm')
# 示例电话记录文本
text = """
Agent: 今天我们解决一下这个事情?
Customer: 不行,我今天明天都没有时间
Agent: 要不然我们下周一再电话沟通
Customer: 也不行,要不然大后天,大后天可以
Agent: 好的,那就大后天
"""
# 处理文本
doc = nlp(text)
# 存储找到的时间实体和相关上下文
appointments = []
# 遍历文档中的实体
for ent in doc.ents:
if ent.label_ == "DATE" or ent.label_ == "TIME":
# 检查实体前后的文本来确定上下文
start = ent.start - 5 if ent.start - 5 > 0 else 0
end = ent.end + 5 if ent.end + 5 < len(doc) else len(doc)
context = doc[start:end]
appointments.append((ent, context))
# 输出可能的约定时间
for appointment in appointments:
print(f"Time: {appointment[0]}, Context: {appointment[1]}")
Output results