2017-10-13 21 views
0

이것은 어리석은 질문 일 가능성이 높습니다.하지만 저는 인터넷 검색을 시도하고 해결책을 찾지 못했습니다. 내 질문에 대해 검색 할 수있는 올바른 방법을 모르기 때문입니다.unnest_tokens의 반대편

나는 정지 문자를 없애기 위해 R으로 깔끔한 텍스트 형식으로 변환 한 데이터 프레임을 가지고 있습니다. 이제 데이터 프레임을 원래 형식으로 되돌릴 수 있습니다.

unnest_tokens의 반대/반대 명령은 무엇입니까?

편집 : 여기 내가 같이 작동하는 데이터가 있습니다. Silge와 Robinson의 Tidy Text 책에서 분석을 복제하려고하지만 이탈리아 오페라 리브레토를 사용하고 있습니다.

text word 
FIGARO cinque 
FIGARO dieci 
FIGARO venti 
FIGARO trenta 
... 

나는 그것을 다시 좀하고 싶습니다 :

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) 

는 지금은 이런 일이 : 나는 중지 단어 제거 할 수 있도록

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! 

나는 깔끔한 텍스트로 돌려 문자 이름의 형식과 관련 선을 다른 것들을 볼 수 있습니다. 기본적으로 나는 이전과 같은 형식으로 텍스트를 원하지만 정지 단어는 제거합니다.

+0

안녕, 읽기 [이] (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)를하고 질문을 수정하시기 바랍니다. 자신의 데이터가 어떤 것인지, 무엇을했는지에 대해 더 많이 알면 다른 사용자가 귀하를 도울 수 있습니다. – shea

답변

1

어리석은 질문이 아닙니다. 그 대답은 당신이하려는 일에 조금 달렸지 만, 내 텍스트를 정돈 된 형태로 처리 한 후에 원래 텍스트로 되 돌리고 싶다면 내 전형적인 접근 방식이 될 것입니다. map purrr 함수를 사용하십시오.

먼저 원시 텍스트에서 정돈 된 형식으로가 봅시다.

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 
#>     <fctr>  <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 

텍스트는 이제 깔끔! 그러나 우리는 원래 형태와 비슷한 형태로 되돌릴 수 있습니다. 나는 일반적으로 tidyr에서 nest을 사용하여이 방법에 접근 한 다음 purrr의 일부 map 함수를 사용합니다.

nested_austen <- tidy_austen %>% 
    nest(word) %>% 
    mutate(text = map(data, unlist), 
     text = map_chr(text, paste, collapse = " ")) 

nested_austen 
#> # A tibble: 62,272 x 4 
#>     book linenumber    data 
#>     <fctr>  <int>   <list> 
#> 1 Sense & Sensibility   1 <tibble [3 x 1]> 
#> 2 Sense & Sensibility   3 <tibble [3 x 1]> 
#> 3 Sense & Sensibility   5 <tibble [1 x 1]> 
#> 4 Sense & Sensibility   10 <tibble [2 x 1]> 
#> 5 Sense & Sensibility   13 <tibble [12 x 1]> 
#> 6 Sense & Sensibility   14 <tibble [13 x 1]> 
#> 7 Sense & Sensibility   15 <tibble [11 x 1]> 
#> 8 Sense & Sensibility   16 <tibble [12 x 1]> 
#> 9 Sense & Sensibility   17 <tibble [11 x 1]> 
#> 10 Sense & Sensibility   18 <tibble [15 x 1]> 
#> # ... with 62,262 more rows, and 1 more variables: text <chr> 

텍스트가이 특별한 경우에, 마지막에 어떤 모습입니까?

nested_austen %>% 
    select(text) 
#> # A tibble: 62,272 x 1 
#>                 text 
#>                 <chr> 
#> 1            sense and sensibility 
#> 2              by jane austen 
#> 3                1811 
#> 4               chapter 1 
#> 5 the family of dashwood had long been settled in sussex their estate 
#> 6 was large and their residence was at norland park in the centre of 
#> 7  their property where for many generations they had lived in so 
#> 8 respectable a manner as to engage the general good opinion of their 
#> 9 surrounding acquaintance the late owner of this estate was a single 
#> 10 man who lived to a very advanced age and who for many years of his 
#> # ... with 62,262 more rows