У меня есть фрейм данных pandas, содержащий примеры веб-чата между двумя людьми, клиентом и оператором службы поддержки.
Имя клиента всегда объявляется в первой строке веб-чата, когда клиент вступает в разговор.
Пример 1:
В: df['log'][0]
Out: [14:40:48] You are joining a chat with James[14:40:48] James: Hello, I\'m looking to find out more about the services and products you offer.[14:41:05] Greg: Thank you for contacting us. [17:41:14] Greg: Could I start by asking what services lines or products you are interested in knowing more about, please?[14:41:23] James: I would like to know more about your gardening and guttering service.[14:43:20] James: hello?[14:43:32] Greg: thank you, for more information on those please visit www.example.com/more_examples.[14:44:12] James: Thanks[14:44:38] James has exited the session.
Пример 2:
В: df['log'][1]
Out: [09:01:25] You are joining a chat with Roy Andrews[09:01:25] Roy Andrews: I\'m asking on behalf of partner whether she is able to still claim warranty on a coffee machine she purchased a year and a half ago? [09:02:00] Jamie: Thank you for contacting us. Could I start by asking for the type of coffee machine purchased please, and whether she still has a receipt?[09:02:23] Roy Andrews: BRX0403, she no longer has a receipt.[09:05:30] Jamie: Thank you, my interpretation is that she would not be able to claim as she is no longer under warranty. [09:08:46] Jamie: for more information on our product warranty policy please see www.brandx.com/warranty-policy/information[09:09:13] Roy Andrews: Thanks for the links, I will let her know.[09:09:15] Roy Andrews has exited the session.
Имена в чате всегда различаются, так как разные клиенты используют сервис веб-чата.
Клиент может войти в чат с одним или несколькими именами. Пример:
James
Ravi
Roy Andrews
.
Требования:
Я бы хотел отделить все экземпляры чата клиента (например, чат по James
и Roy Andrews
) от столбца df['log']
в новый столбец df[text_analysis]
.
Из примера 1 выше это будет выглядеть так:
В: df['text_analysis][0]
Out: [14:40:48] You are joining a chat with James[14:40:48] James: Hello, I\'m looking to find out more about the services and products you offer.[14:41:23] James: I would like to know more about your gardening and guttering service.[14:43:20] James: hello?[14:44:12] James: Thanks
EDIT:
Оптимальное решение будет извлекать подстроки, как указано в примере выше, и опускать конечную метку времени [14:44:38] James has exited the session.
.
Что я пробовал до сих пор:
Я извлек имена клиентов из столбца df['log']
в новый столбец с именем df['names']
, используя:
df['names'] = df['log'].apply(lambda x: x.split(' ')[7].split('[')[0])
Я хотел использовать имена в столбце df['names']
для использования в функции str.split()
pandas - что-то вроде:
df['log'].str.split(df['names'])
однако это не работает, и если в этом сценарии произошло разделение, я думаю, что оно не будет должным образом разделять чаты клиента и оператора услуг.
Также я попытался включить имена в решение типа regex:
df['log'].str.extract('([^.]*{}[^.]*)').format(df['log']))
Но это тоже не работает (потому что я предполагаю, что .extract()
не поддерживает формат.
Любая помощь будет принята с благодарностью.