python获取通话记录中的下次预约时间的脚本

import spacy
import re


# 加载中文模型
nlp = spacy.load('zh_core_web_sm')

# 示例电话记录文本
# text = """
# Agent: 今天我们解决一下这个事情?
# Customer: 不行,我今天明天都没有时间
# Agent: 要不然我们下周一再电话沟通
# Customer: 也不行,要不然后天,后天可以
# Agent: 恩,后天再联系
# """

# text = """
# Agent: 今天我们解决一下这个事情?
# Customer: 不行,我今天明天都没有时间
# Agent: 要不然我们下周一再电话沟通
# Customer: 也不行,要不然下周二,周二可以
# Agent: 恩,下周二再联系
# """

text = """
Agent: 今天我们解决一下这个事情?
Customer: 恩,什么时间解决?
Agent: 你周几有时间啊?
Customer: 我这周没有时间,11月21日可以,其他时间都约满了
Agent: 好的,到时候联系
"""

# 处理文本
doc = nlp(text)

# 使用正则表达式识别明确的日期、"下周X"和"后天"格式的日期
dates = re.findall(r'\d{1,9}月\d{1,9}日', text)
next_week_dates = re.findall(r'下周[一二三四五六日]', text)
relative_dates = re.findall(r'后天', text)

# 将找到的日期添加到列表中
date_entities = [doc.char_span(*m.span()) for m in re.finditer(r'\d{1,2}月\d{1,2}日', text) if doc.char_span(*m.span())]
next_week_entities = [doc.char_span(*m.span()) for m in re.finditer(r'下周[一二三四五六日]', text) if doc.char_span(*m.span())]
relative_date_entities = [doc.char_span(*m.span()) for m in re.finditer(r'后天', text) if doc.char_span(*m.span())]

# 确定最终时间
final_appointment = None
for ent in relative_date_entities + next_week_entities + date_entities:
    # 检查每个时间实体后面是否有确认语言
    for i in range(ent.end, min(ent.end + 5, len(doc))):
        if doc[i].text in ["好的", "可以", "那就", "恩"]:
            final_appointment = ent.text
            break
    if final_appointment:
        break

# 输出最终约定时间
if final_appointment:
    print(f"下次电话联系时间: {final_appointment}")
else:
    print("没有找到联系时间")

你可能感兴趣的:(python,开发语言)