gpt4 book ai didi

r - 在与 R 匹配模式后从文本字符串中提取多个段落

转载 作者:行者123 更新时间:2023-12-05 03:20:32 28 4
gpt4 key购买 nike

我有一个数据框,其中有一列 text 作为两个人之间的采访记录字符串。

格式是 firstname lastname m:ss 然后是一个换行符,一段文本,一个换行符,然后是 firstname lastname m:ss 一个换行符和一段文本,如下所示。

John Smith 2:03
Is this great?
Joe Blogs 2:24
Yes it is.
John Smith 2:35
Is it the greatest of all time?
Joe Blogs 2:47
Clearly.

我想将 2 个新字符串放入我的 df 中的两个新列中。每个人一个,包含姓名后串在一起的文本段落。

每个文本文件中的面试官都是两个人中的一个,所以我可以匹配他们的名字,将他们分配给“面试官”。我在 df 的 interviewer 列中也有面试官姓名。

面试官姓名:John Smith、Jane Doe

期望的输出:

<表类="s-表"><头>面试官主题<正文>这很好吗?它是有史以来最伟大的吗?是的。很明显。

我不确定实现我所追求目标的最佳方式。我已经使用 tidyr extract to the interviewer, date etc. from the file names to put them into columns in my dataframe, 但我没有足够的经验知道如何使用它来拆分这样的文本 - 或者如果有更好的方法去做。

最佳答案

更新:字符串独立版本:

df %>%
filter(row_number() %% 2 == 0) %>%
group_by(x = rep(c("subject", "interviewer"), length.out = n())) %>%
mutate(text = paste(text, collapse = " ")) %>%
filter(row_number() %% 2 == 0) %>%
pivot_wider(names_from = x, values_from = text)
 subject                                        interviewer        
<chr> <chr>
1 Is this great? Is it the greatest of all time? Yes it is. Clearly.

第一个答案:我们可以这样做:

library(dplyr)
library(tidyr)
library(stringr)

df %>%
filter(str_detect(text, '\\.|\\?')) %>%
mutate(x = ifelse(str_detect(text, '\\.'), "subject", "interviewer")) %>%
arrange(x) %>%
group_by(x) %>%
summarise(text = paste(text, collapse = " ")) %>%
pivot_wider(names_from = x, values_from = text)
 interviewer                                    subject            
<chr> <chr>
1 Is this great? Is it the greatest of all time? Yes it is. Clearly.

关于r - 在与 R 匹配模式后从文本字符串中提取多个段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73145027/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com