2017-11-09 9 views
1

Dies ist ein bizarres Puzzle. Ich habe 2 Texte von Gutenbergr - Alice im Wunderland und Ulysses heruntergeladen. Die stop_words verschwinden von Alice, aber sie sind immer noch in Ulysses. Dieses Problem ist auch beim Ersetzen von anti_join mit Filter (! Wort% in% stop_words $ word) bestehen geblieben.R tidytext stop_words filtern nicht konsistent von gutenberg Downloads

Wie bekomme ich die stop_words aus Ulysses?

Danke für Ihre Hilfe!

Plot of top 15 tf_idf for Alice & Ulysses

library(gutenbergr) 
library(dplyr) 
library(stringr) 
library(tidytext) 
library(ggplot2) 

titles <- c("Alice's Adventures in Wonderland", "Ulysses") 


books <- gutenberg_works(title %in% titles) %>% 
    gutenberg_download(meta_fields = c("title", "author")) 


data(stop_words) 


tidy_books <- books %>% 
    unnest_tokens(word, text) %>% 
    anti_join(stop_words) %>% 
    count(title, word, sort=TRUE) %>% 
    ungroup() 


plot_tidy_books <- tidy_books %>% 
    bind_tf_idf(word, title, n) %>% 
    arrange(desc(tf_idf))  %>% 
    mutate(word = factor(word, levels = rev(unique(word)))) %>% 
    mutate(title = factor(title, levels = unique(title))) 


plot_tidy_books %>% 
    group_by(title) %>% 
    arrange(desc(n))%>% 
    top_n(15, tf_idf) %>% 
    mutate(word=reorder(word, tf_idf)) %>% 
    ggplot(aes(word, tf_idf, fill=title)) + 
    geom_col(show.legend = FALSE) + 
    labs(x=NULL, y="tf-idf") + 
    facet_wrap(~title, ncol=2, scales="free") + 
    coord_flip() 

Antwort

1

Nach einem bisschen in den Token versehen Ulysses Graben, der Text „es“ ist eigentlich eine richtige Marke Apostroph statt einen Apostroph. stop_words in tidytext verwendet ein Apostroph. Sie müssen das richtige einzelne Zitat durch ein Apostroph ersetzen.

fand ich dies aus, indem:

> utf8ToInt('it’s') 
[1] 105 116 8217 115 

die 8217 mir here führen googeln. Von dort ist es so einfach wie greifen die C++/Java-Quelle \u2019 und Hinzufügen einer mutate und gsub Erklärung vor Ihrem anti-join.

tidy_books <- books %>% 
    unnest_tokens(word, text) %>% 
    mutate(word = gsub("\u2019", "'", word)) %>% 
    anti_join(stop_words) %>% 
    count(title, word, sort=TRUE) %>% 
    ungroup() 

Ergebnisse in:

enter image description here

+0

Diese kleinen quotes/Apostrophe sind so hinterhältig! Danke, dass Sie das herausgefunden haben und mir beigebracht haben, wie ich es beheben kann. –

+0

Wenn es geholfen hat, können Sie es als Antwort akzeptieren, damit andere es wissen. –

+0

Oh, ok - ich bin neu hier - werde tun! –