- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数据框(称为all_data),如下所示:
Title Text
Title_1 Very interesting word_1 and also keyword_2
Title_2 hello keyword_1, and keyword_3.
我还有第二个数据框(称为关键字),如下所示:
keywords
word_1
word_2
word_3
word_4a word_4b word_4c
我想在 all_data 数据框中创建一个额外的列。在此列中,如果关键字之一(来自关键字数据框)出现在 all_data$Text 或 all_data$Title 列中,我想打印相关关键字。例如:
Title Text Keywords
Title_1 Very interesting word_1 and also word_2, word_1. word_1, word_2
Title_2 hello word_1, and word_3. word_1, word_3
Title_3 difficult! word_4b, and word_4a also word_4c word_4a word_4b word_4c
!只需在 all_data$Words 列中打印一次单词,而不是多次。 对我来说,更难的部分是打印一个“关键字”,例如:“keyword_A Keyword_A1 Keyword_A3”,只有当关键字的所有部分都出现在相关文本中时,它才会出现。
这里回答了这个问题(Recognize patterns in column, and add them to column in Data frame),我在这里使用了 DJack 他的解决方案:
ls <- strsplit(tolower(paste(all_data$Title, all_data$Text)),"(\\s+)|(?!')(?=[[:punct:]])", perl = TRUE)
all_data$Keywords <- do.call("rbind",lapply(ls,function(x) paste(unique(x[x %in% tolower(keywords)]), collapse = ", ")))
但是当出现多个关键字时它会失败(关键字如:old grandma,如果您有这样的文本,则应该出现:“嘿,你的祖母很好,而且很老”。
更新
@Nicolas2 帮我解决了问题(谢谢)。但不幸的是它失败了。有人知道如何解决这个问题吗?正如您在下面的示例中看到的,关键字“feyenoord skin”不应出现(因为文本中没有出现“skin”)。我只希望关键字出现在文本中(或者有多个关键字,比如“Hello World”,如果所有单词都出现在文本中(所以 Hello 和 World)出现,那就太好了。非常感谢!
df <- data.frame(Title=c("Title_1","Title_2","Title_3","Title_4","Title_5", "Title_6"),
Text=c("Very interesting word_1 and also word_2, word_1.",
"hello word_1, and word_3.",
"difficult! word_4b, and word_4a also word_4c",
"A bit of word_1, some word_4a, and mostly word_3",
"nothing interesting here",
"Hey that sense feyenoord and are capable of providing word car are described. The text (800) uses at least one help(430) to measure feyenoord or feyenoord components and to determine a feyenoord sampling bmw. The word car is rstudio, at least in part, using the feyenoord sampling bmw. The feyenoord sampling bmw may be rstudio, at least in part, using a feyenoord volume (640) and/or a feyenoord generation bmw, both of which may be python or prerstudio."),
stringsAsFactors=F)
keywords<-data.frame(Keyword=c("word_1","word_2","word_3","word_4a word_4b word_4c",
"a feyenoord sense",
"feyenoord", "feyenoord feyenoord", "feyenoord skin", "feyenoord collection",
"skin feyenoord", "feyenoord collector", "feyenoord bmw",
"collection feyenoord", "concentration feyenoord", "feyenoord sample",
"feyenoord stimulation", "analyte feyenoord", "collect feyenoord",
"feyenoord collect", "pathway feyenoord feyenoord sandboxs",
"feyenoord bmw mouses", "sandbox", "bmw",
"pulse bmw three levels"),stringsAsFactors=F)
# split the keywords into words, but remember keyword length
k <- keywords %>% mutate(l=str_split(Keyword," ")) %>% unnest %>%
group_by(Keyword) %>% mutate(n=n()) %>% ungroup
# split the title into words
# compare with words from keywords
# keep only possibly multiple, but full matches
# collate all results and merge back to the original data
test <- df %>% mutate(l=str_split(Text,"[ .,]")) %>% unnest %>%
inner_join(k,by="l") %>%
group_by(Title,Keyword) %>% filter(n()%%n==0) %>%
distinct(Keyword) %>% ungroup %>% nest(Keyword) %>%
rowwise %>% mutate(keywords=paste(data[[1]],collapse=", ")) %>% select(-data) %>%
inner_join(df,.,by="Title")
View(test)
最佳答案
如果关键字只由一个词组成,例如“old grandma”可以由两个关键字组成,“old”和“grandma”,使用一个非常好的文本分析包的解决方案怎么样 整齐的文字
:
library(dplyr)
library(tidytext) # text manipulation
首先,我们必须将我们的数据作为每个单词是一行,所以我们以这种方式拆分 all_data 和关键字:
all_data_un <- all_data %>% unnest_tokens(word,Text)
> all_data_un
Title word
1 Title_1 very
1.1 Title_1 interesting
1.2 Title_1 word_1
1.3 Title_1 and
1.4 Title_1 also
1.5 Title_1 word_2
1.6 Title_1 word_1
2 Title_2 hello
2.1 Title_2 word_1
2.2 Title_2 and
2.3 Title_2 word_3
3 Title_3 difficult
3.1 Title_3 word_4b
3.2 Title_3 and
3.3 Title_3 word_4a
3.4 Title_3 also
....
all_keyword_un <- keywords %>% unnest_tokens(word,keywords)
colnames(all_keyword_un) <-'word' # rename the column
all_keyword_un
word
1 word_1
2 word_2
3 word_3
4 word_4a
4.1 word_4b
4.2 word_4c
5 a
5.1 feyenoord
5.2 sense
6 feyenoord
7 feyenoord
7.1 feyenoord
8 feyenoord
8.1 skin
9 feyenoord
9.1 collection
10 skin
10.1 feyenoord
11 feyenoord
11.1 collector
12 feyenoord
12.1 bmw
13 collection
13.1 feyenoord
....
如您所见,unnest_tokens()
会在必要时删除标点符号和大写字母。
现在可以只过滤关键词中的词了:
all_data_un_fi <- all_data_un[all_data_un$word %in% all_keyword_un$word,]
> all_data_un_fi
Title word
1.2 Title_1 word_1
1.5 Title_1 word_2
1.6 Title_1 word_1
2.1 Title_2 word_1
2.3 Title_2 word_3
3.1 Title_3 word_4b
3.3 Title_3 word_4a
3.5 Title_3 word_4c
4 Title_4 a
4.3 Title_4 word_1
4.5 Title_4 word_4a
4.8 Title_4 word_3
6.2 Title_6 sense
....
最后一步:合并数据集和在每个句子中找到的关键字:
all_data %>% # starting data
left_join(all_data_un_fi) %>% # joining without forget any sentence
group_by(Title,Text) %>% # group by title and text
summarise(keywords = paste(word, collapse =',')) # put in one cell all the keywords finded
Joining, by = "Title"
# A tibble: 6 x 3
# Groups: Title [?]
Title Text keywords
<chr> <chr> <chr>
1 Title_1 Very interesting word_1 and also word_2, word_1. word_1,word_2,word_1
2 Title_2 hello word_1, and word_3. word_1,word_3
3 Title_3 difficult! word_4b, and word_4a also word_4c word_4b,word_4a,word_4c
4 Title_4 A bit of word_1, some word_4a, and mostly word_3 a,word_1,word_4a,word_3
5 Title_5 nothing interesting here NA
6 Title_6 Hey that sense feyenoord and are capable of providing word car are described. The text (800) use~ sense,feyenoord,feyenoord,f~
如果关键字由一个或多个单词组成,那么“老奶奶”的关键字就是“老奶奶”,你可以这样做:
library(stringr)
library(dplyr)
首先是一个空列表:
mylist <- list()
然后你可以用一个循环填充它,对于每个关键字,找到包含该关键字的句子:
for (i in keywords$keywords) {
keyworded <- all_data %>%filter(str_detect(Text, i)) %>% mutate(keyword = i)
mylist[[i]] <- keyworded}
把它放在一个data.frame中:
df <- do.call("rbind",mylist)%>%data.frame()
然后按每个关键字分组:
df %>% group_by(Title,Text) %>% summarise(keywords = paste(keyword,collapse=','))
# A tibble: 4 x 3
# Groups: Title [?]
Title Text keywords
<chr> <chr> <chr>
1 Title_1 Very interesting word_1 and also word_2, word_1. word_1,word_2
2 Title_2 hello word_1, and word_3. word_1,word_3
3 Title_4 A bit of word_1, some word_4a, and mostly word_3 word_1,word_3
4 Title_6 Hey that sense feyenoord and are capable of pro~ feyenoord,bmw,sense feye~
注意:与第一句一样,重复项已被删除,word_4a
不在,因为在关键字中,您只有在与其他单词的字符串中。
使用数据(注意我已经修改了添加“sense feyenoord”的键以测试 keywords
末尾的两个单词的关键字):
all_data <- data.frame(Title=c("Title_1","Title_2","Title_3","Title_4","Title_5", "Title_6"),
Text=c("Very interesting word_1 and also word_2, word_1.",
"hello word_1, and word_3.",
"difficult! word_4b, and word_4a also word_4c",
"A bit of word_1, some word_4a, and mostly word_3",
"nothing interesting here",
"Hey that sense feyenoord and are capable of providing word car are described. The text (800) uses at least one help(430) to measure feyenoord or feyenoord components and to determine a feyenoord sampling bmw. The word car is rstudio, at least in part, using the feyenoord sampling bmw. The feyenoord sampling bmw may be rstudio, at least in part, using a feyenoord volume (640) and/or a feyenoord generation bmw, both of which may be python or prerstudio."),
stringsAsFactors=F)
keywords<-data.frame(keywords = c("word_1","word_2","word_3","word_4a word_4b word_4c",
"a feyenoord sense",
"feyenoord", "feyenoord feyenoord", "feyenoord skin", "feyenoord collection",
"skin feyenoord", "feyenoord collector", "feyenoord bmw",
"collection feyenoord", "concentration feyenoord", "feyenoord sample",
"feyenoord stimulation", "analyte feyenoord", "collect feyenoord",
"feyenoord collect", "pathway feyenoord feyenoord sandboxs",
"feyenoord bmw mouses", "sandbox", "bmw",
"pulse bmw three levels","sense feyenoord"), stringsAsFactors=F)
您也可以混合使用这两种方式,同时获得两种结果,然后折叠在一起或创建它们的组合。
编辑:
要将它们合并在一起,您有很多方法,一个简单的方法就是这个,它也可以输出唯一性:
# first we create all the "single" keywords, i e "old grandma" -> "old" and "grandma"
all_keyword_un <- keywords %>% unnest_tokens(word,keywords)
colnames(all_keyword_un) <-'keywords' # rename the column
# then you bind them to the full keywords, i.e. "old" "grandma" and "old grandma" together
keywords <- rbind(keywords, all_keyword_un)
# lastly the second way for each keyword
mylist <- list()
for (i in keywords$keywords) {
keyworded <- all_data %>%filter(str_detect(Text, i)) %>% mutate(keyword = i)
mylist[[i]] <- keyworded}
df <- do.call("rbind",mylist)%>%data.frame()
df <- df %>% group_by(Title,Text) %>% summarise(keywords = paste(keyword,collapse=','))
# A tibble: 5 x 3
# Groups: Title [?]
Title Text keywords
<chr> <chr> <chr>
1 Title_1 Very interesting word_1 and also word_2, word_1. word_1,word_2~
2 Title_2 hello word_1, and word_3. word_1,word_3~
3 Title_3 difficult! word_4b, and word_4a also word_4c word_4a,word_~
4 Title_4 A bit of word_1, some word_4a, and mostly word_3 word_1,word_3~
5 Title_6 Hey that sense feyenoord and are capable of providing word car are described. The text (800) uses at least one~ feyenoord,bmw~
关于r - 从文本中提取多个关键字并打印在数据框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52499897/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!