与unnest_tokens相反 [英] Opposite of unnest_tokens
问题描述
这很可能是一个愚蠢的问题,但是我已经在Google和Google上搜索了,找不到解决方法.我认为这是因为我不知道用正确的方式来表达我的问题以进行搜索.
This is most likely a stupid question, but I've googled and googled and can't find a solution. I think it's because I don't know the right way to word my question to search.
我有一个数据框,已将其转换为R中的整洁文本格式,以消除停用词.我现在想整理"该数据框回到其原始格式.
I have a data frame that I have converted to tidy text format in R to get rid of stop words. I would now like to 'untidy' that data frame back to its original format.
unnest_tokens的反/反命令是什么?
What's the opposite / inverse command of unnest_tokens?
这是我正在使用的数据的样子.我试图从Silge和Robinson的整洁的文字书中复制分析,但使用的是意大利歌剧librettos.
here is what the data I'm working with look like. I'm trying to replicate analyses from Silge and Robinson's Tidy Text book but using Italian opera librettos.
character = c("FIGARO", "SUSANNA", "CONTE", "CHERUBINO")
line = c("Cinque... dieci.... venti... trenta... trentasei...quarantatre", "Ora sì ch'io son contenta; sembra fatto inver per me. Guarda un po', mio caro Figaro, guarda adesso il mio cappello.", "Susanna, mi sembri agitata e confusa.", "Il Conte ieri perché trovommi sol con Barbarina, il congedo mi diede; e se la Contessina, la mia bella comare, grazia non m'intercede, io vado via, io non ti vedo più, Susanna mia!")
sample_df = data.frame(character, line)
sample_df
character line
FIGARO Cinque... dieci.... venti... trenta... trentasei...quarantatre
SUSANNA Ora sì ch'io son contenta; sembra fatto inver per me. Guarda un po', mio caro Figaro, guarda adesso il mio cappello.
CONTE Susanna, mi sembri agitata e confusa.
CHERUBINO Il Conte ieri perché trovommi sol con Barbarina, il congedo mi diede; e se la Contessina, la mia bella comare, grazia non m'intercede, io vado via, io non ti vedo più, Susanna mia!
我将其转换为整洁的文字,因此可以摆脱停用词:
I turn it into tidy text so I can get rid of stop words:
tribble <- sample_df %>%
unnest_tokens(word, line)
# Get rid of stop words
# I had to make my own list of stop words for 18th century Italian opera
itstopwords <- data_frame(text=mystopwords)
names(itstopwords)[names(itstopwords)=="text"] <- "word"
tribble2 <- tribble %>%
anti_join(itstopwords)
现在我有这样的东西:
text word
FIGARO cinque
FIGARO dieci
FIGARO venti
FIGARO trenta
...
我想重新将其转换为字符名称和相关行的格式,以查看其他内容.基本上,我希望文本使用与以前相同的格式,但是要删除停用词.
I would like to get it back into the format of character name and the associated line to look at other things. Basically I would like the text in the same format it was before, but with stop words removed.
推荐答案
这不是一个愚蠢的问题!答案在某种程度上取决于您要尝试执行的操作,但是如果我想使用dplyr的group_by()
函数以整齐的形式对文本进行处理后,将文本恢复为原始格式,这将是我的典型方法.
Not a stupid question! The answer depends a bit on exactly what you are trying to do, but here would be my typical approach if I wanted to get my text back to its original form after some processing in its tidied form, using the group_by()
function from dplyr.
首先,让我们从原始文本转换为整齐的格式.
First, let's go from raw text to a tidied format.
library(tidyverse)
library(tidytext)
tidy_austen <- janeaustenr::austen_books() %>%
group_by(book) %>%
mutate(linenumber = row_number()) %>%
ungroup() %>%
unnest_tokens(word, text)
tidy_austen
#> # A tibble: 725,055 x 3
#> book linenumber word
#> <fct> <int> <chr>
#> 1 Sense & Sensibility 1 sense
#> 2 Sense & Sensibility 1 and
#> 3 Sense & Sensibility 1 sensibility
#> 4 Sense & Sensibility 3 by
#> 5 Sense & Sensibility 3 jane
#> 6 Sense & Sensibility 3 austen
#> 7 Sense & Sensibility 5 1811
#> 8 Sense & Sensibility 10 chapter
#> 9 Sense & Sensibility 10 1
#> 10 Sense & Sensibility 13 the
#> # … with 725,045 more rows
文本现在很整洁!但是我们可以将其弄乱,回到原来的形式.我通常使用dplyr中的group_by()
和summarize()
以及stringr中的str_c()
来解决这个问题.在这种特殊情况下,最后的文本是什么样的?
The text is tidy now! But we can untidy it, back to something sort of like its original form. I typically approach this using group_by()
and summarize()
from dplyr, and str_c()
from stringr. What does the text look like at the end, in this particular case?
tidy_austen %>%
group_by(book, linenumber) %>%
summarize(text = str_c(word, collapse = " ")) %>%
ungroup()
#> # A tibble: 62,272 x 3
#> book linenumber text
#> <fct> <int> <chr>
#> 1 Sense & Sensib… 1 sense and sensibility
#> 2 Sense & Sensib… 3 by jane austen
#> 3 Sense & Sensib… 5 1811
#> 4 Sense & Sensib… 10 chapter 1
#> 5 Sense & Sensib… 13 the family of dashwood had long been settled…
#> 6 Sense & Sensib… 14 was large and their residence was at norland…
#> 7 Sense & Sensib… 15 their property where for many generations th…
#> 8 Sense & Sensib… 16 respectable a manner as to engage the genera…
#> 9 Sense & Sensib… 17 surrounding acquaintance the late owner of t…
#> 10 Sense & Sensib… 18 man who lived to a very advanced age and who…
#> # … with 62,262 more rows
由 reprex软件包(v0.3.0)于2019-07-11创建 sup>
Created on 2019-07-11 by the reprex package (v0.3.0)
这篇关于与unnest_tokens相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!