从聊天对话中提取相关文本

时间:2018-04-11 12:52:08

标签: r

我有以下聊天对话:

聊天开始时间:2018年4月2日,星期一,10:23:30(+0100)聊天来源:GB - 我的帐户(已登录)代理Navin P(1s)Navin:感谢您联系XYZ,您正在和纳文谈话。我该怎么帮忙? (34s)访客:你好,我刚刚从brillian宽带切换到fab光纤 (39s)纳文:嗨 (42s)纳文:早上好。 (47s)访客:我发现这个优秀的宽带速度如此之慢 (52s)纳文:我今天能帮到你吗? (1分1秒)访客:我们被告知将是一天切换 (1分5秒)纳文:我会帮你的。 (1分11秒)访客:你能告诉我何时会发生这种情况

我希望使用 R 仅提取上面的相关文字。我基本上只希望访问者评论出现在结果中。

我想要的结果如下:

我发现这么好的宽带如此之慢 我们被告知将是一个切换的日子 你可以通过

告诉我什么时候会发生这种情况

我尝试使用gsub和strsplit完成此操作,但无济于事。感谢这里的投入。

mytext <- paste(c("Agent Navin P ( 1s ) Navin: Thanks for contacting XYZ, you are talking to Navin. How can I help? ( 34s ) Visitor:", 
    "Hello , I?ve just currently switched from brillian broadband to fab fibre ( 39s ) Navin: Hi ( 42s ) Navin: Good morning. ( 47s )", 
    "Visitor: I find the brilliant broadband so slow ( 52s ) Navin: How can i help you today? ( 1m 1s ) Visitor: And we got told would be", 
    "a day to get switched over ( 1m 5s ) Navin: I'll help you with it. ( 1m 11s ) Visitor: Can you tell me when this will happen by"
), collapse = ' ')

2 个答案:

答案 0 :(得分:1)

我确定有很多方法可以做到这一点。我分开访问者并使用sub来删除Navin的答案。对于替换,我们需要在最后添加[-1],因为我们在第一个&#34;访问者&#34;之前不需要任何内容​​。我们分手了。

str <- "Chat Started: Monday, April 02, 2018, 10:23:30 (+0100) Chat Origin: GB - My Account (Signed In) Agent Navin P ( 1s ) Navin: Thanks for contacting XYZ, you are talking to Navin. How can I help? ( 34s ) Visitor: Hello , I?ve just currently switched from brillian broadband to fab fibre ( 39s ) Navin: Hi ( 42s ) Navin: Good morning. ( 47s ) Visitor: I find the brilliant broadband so slow ( 52s ) Navin: How can i help you today? ( 1m 1s ) Visitor: And we got told would be a day to get switched over ( 1m 5s ) Navin: I'll help you with it. ( 1m 11s ) Visitor: Can you tell me when this will happen by"
str <- strsplit(str," Visitor: ")[[1]]
sub(" \\((.*?)\\) Navin:.*","",str)[-1]

# [1] "Hello , I?ve just currently switched from brillian broadband to fab fibre"
# [2] "I find the brilliant broadband so slow"                                   
# [3] "And we got told would be a day to get switched over"                      
# [4] "Can you tell me when this will happen by"

如果你想要一个像你的例子一样的行,你可以使用paste

paste(sub(" \\((.*?)\\) Navin:.*","",str)[-1],collapse = " ")
# [1] "Hello , I?ve just currently switched from brillian broadband to fab fibre I find the brilliant broadband so slow And we got told would be a day to get switched over Can you tell me when this will happen by"

如果此人的姓名不一致&#34;导航&#34;,您可以使用\\w+匹配sub查询中任何人的姓名以将其删除。

sub(" \\((.*?)\\) \\w+:.*","",str)[-1]

答案 1 :(得分:1)

保留更多信息的一种可能解决方案:

mytext <- paste(c("Agent Navin P ( 1s ) Navin: Thanks for contacting XYZ, you are talking to Navin. How can I help? ( 34s ) Visitor:", 
    "Hello , I?ve just currently switched from brillian broadband to fab fibre ( 39s ) Navin: Hi ( 42s ) Navin: Good morning. ( 47s )", 
    "Visitor: I find the brilliant broadband so slow ( 52s ) Navin: How can i help you today? ( 1m 1s ) Visitor: And we got told would be", 
    "a day to get switched over ( 1m 5s ) Navin: I'll help you with it. ( 1m 11s ) Visitor: Can you tell me when this will happen by"
), collapse = ' ')


library(dplyr); library(textshape); library(stringi)

mytext %>%
    stri_replace_all_regex('(\\( [0-9ms ]+ \\))(\\s+)', '$1<<splithere>>') %>%
    stri_split_fixed('<<splithere>>') %>%
    lapply(function(x) {
        x %>%
            split_transcript() %>%
            mutate(dialogue = ifelse(!grepl('\\(\\s*([0-9ms ]+)\\s\\)', dialogue), paste(dialogue, '( - )'), dialogue)) %>%
            extract(dialogue, c('dialogue', 'timestamp'), '(^.+)\\s\\(\\s*([0-9ms -]+)\\s\\)')
    })

## [[1]]
##                  person                                                                  dialogue timestamp
## 1  Agent Navin P ( 1s )                                                             Agent Navin P        1s
## 2                 Navin      Thanks for contacting XYZ, you are talking to Navin. How can I help?       34s
## 3               Visitor Hello , I?ve just currently switched from brillian broadband to fab fibre       39s
## 4                 Navin                                                                        Hi       42s
## 5                 Navin                                                             Good morning.       47s
## 6               Visitor                                    I find the brilliant broadband so slow       52s
## 7                 Navin                                                 How can i help you today?     1m 1s
## 8               Visitor                       And we got told would be a day to get switched over     1m 5s
## 9                 Navin                                                    I'll help you with it.    1m 11s
## 10              Visitor                                  Can you tell me when this will happen by         -

然后你可以按人等过滤。

相关问题