替换r中的正则表达式

时间:2017-09-01 16:48:46

标签: r regex gsub

我有一个文本列,其中包含客户与代理之间电话呼叫的语音记录。在对原始文本值进行一些文本操作之后,假设我有一个类似下面的向量作为示例:

text <- " customer:customer text1 agent:agent text 1 customer:customer text2 agent:agent text 2&#34;

(注意向量文本开头的空格。)

问题:如何将客户和代理文本从原始源字段(本例中为text向量)中提取到两个单独的字段中?

# desired outputs:
# field for customer texts
"customer text1, customer text2"
# field for agent texts
"agent text1, agent text2"

到目前为止我所做的事情(对正则表达式主题的经验有限)是:

customerText <- gsub("^ customer:| agent:(.*)", "", text)
customerText 
[1] "customer text1"

编辑:

请考虑以下基于数据帧的方法的可重现代码,而不是基于矢量的方法。

> callid <- c("1","2")
> conversation <- c(" customer:customer text 1 agent:agent text 1 customer:customer text 2 agent:agent text 2",
+                   " agent:agent text 8 customer:customer text 8 agent:agent text 9 customer:customer text 9")
> conversationCustomer <- c("customer text 1, customer text 2", "customer text 8, customer text 9")
> conversationAgent <- c("agent text 1, agent text 2", "agent text 8, agent text 9")
> df <- data.frame(callid, conversation)
> dfDesired <- data.frame(callid, conversation, conversationCustomer, conversationAgent)
> rm(callid, conversation, conversationCustomer, conversationAgent)
> 
> df
  callid                                                                             conversation
1      1  customer:customer text 1 agent:agent text 1 customer:customer text 2 agent:agent text 2
2      2  agent:agent text 8 customer:customer text 8 agent:agent text 9 customer:customer text 9
> dfDesired
  callid                                                                             conversation             conversationCustomer          conversationAgent
1      1  customer:customer text 1 agent:agent text 1 customer:customer text 2 agent:agent text 2 customer text 1, customer text 2 agent text 1, agent text 2
2      2  agent:agent text 8 customer:customer text 8 agent:agent text 9 customer:customer text 9 customer text 8, customer text 9 agent text 8, agent text 9

谢谢!

2 个答案:

答案 0 :(得分:1)

我们可以使用str_extract

library(stringr)
v1 <- str_extract_all(text, "(?<=:)(customer\\s+\\w+\\s*\\d*)|(agent\\s+\\w+\\s*\\d*)")[[1]]
v1[c(TRUE, FALSE)]
v1[c(FALSE, TRUE)]

或使用strsplit

v1 <- strsplit(trimws(text), "(customer|agent):\\s*")[[1]]
v2 <- trimws(v1[nzchar(v1)])
toString(v2[c(TRUE, FALSE)])
toString(v2[c(FALSE, TRUE)])

答案 1 :(得分:0)

现在,我可以解决它,如下所示。我想在正则表达式方面经验丰富的人可能会缩短它。

df$conversationCustomer <- gsub("agent:.*?customer:", ",", df$conversation)  # replaces any text starting with "agent:" and ending with "customer:" and assigns the customer text to new variable.
df$conversationCustomer <- gsub("agent:.*", "", df$conversationCustomer) # this is for the agent texts at the end of conversation those I couldn't clean the "agent:" part using first regex 
df$conversationCustomer <- gsub("customer:", "", df$conversationCustomer) # this is for removing the "customer:" in the conversations those starts with customer text. (Again, I couldn't clean "customer:" part using first regex.)
df$conversationAgent <- gsub("customer:.*?agent:", ",", df$conversation)
df$conversationAgent <- gsub("customer:.*", "", df$conversationAgent)
df$conversationAgent <- gsub("agent:", "", df$conversationAgent)