如何根据规则在客户和客户服务代理之间划分段落?

2024-06-16 18:41:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一段文字记录了客户和客户服务代理之间的对话。如何将对话分开并创建两个列表(或任何其他格式,如字典),其中一个仅包含客户文本,另一个仅包含代理文本

示例段落:
代理人姓名:你好!我的名字是X。我今天能为你做什么?(4m 46s)客户:我的名字是Y。这是我的问题(4m 57s)代理名称:这是解决方案(5m 40s)代理名称:你在吗?顾客:是的,我还在这里。我还是不明白。。。(6m 40)代理名称:Ok。让我们换一种方式试试。代理人姓名:这能解决问题吗?(7m 40s)代理商名称:感谢您联系客服

预期输出:
仅包含代理文本的列表:[“代理名称:你好!我的名字是X。我今天能为你做些什么?(4m 46s);“代理名称:你在吗?(6m 30s);“代理名称:好的。让我们试试另一种方法。(6m 50s);“代理名称:这能解决问题吗?(7m 40s)代理名称:谢谢你联系客服。”

仅包含客户文本的列表:[“客户:我的名字是Y。这是我的问题(4m 57s)”,“客户:是的,我还在这里。我仍然不明白…(6m 40)”

谢谢大家!


Tags: 文本名称代理列表客户格式记录对话
1条回答
网友
1楼 · 发布于 2024-06-16 18:41:51

鉴于:

txt='''\
Agent Name: Hello! My name is X. How can I help you today? ( 4m 46s ) Customer: My name is Y. Here is my issue ( 4m 57s ) Agent Name: Here's the solution ( 5m 40s ) Agent Name: Are you there? ( 6m 30s ) Customer: Yes I'm still here. I still don't understand... ( 6m 40s ) Agent Name: Ok. Let's try another way. ( 6m 50s ) Agent Name: Does that solve the problem? (7m 40s) Agent Name: Thank you for contacting the customer service.'''

您可以使用re.findall

s1='Agent Name:'
s2='Customer:'
>>> re.findall(rf'({s1}.*?(?={s2}|\Z))', txt)
['Agent Name: Hello! My name is X. How can I help you today? ( 4m 46s ) ', "Agent Name: Here's the solution ( 5m 40s ) Agent Name: Are you there? ( 6m 30s ) ", "Agent Name: Ok. Let's try another way. ( 6m 50s ) Agent Name: Does that solve the problem? (7m 40s) Agent Name: Thank you for contacting the customer service."]

>>> re.findall(rf'({s2}.*?(?={s1}|\Z))', txt)
['Customer: My name is Y. Here is my issue ( 4m 57s ) ', "Customer: Yes I'm still here. I still don't understand... ( 6m 40s ) "]

相关问题 更多 >