2017-11-09 5 views
1

これは奇妙なパズルです。私はgutenbergr - Alice in WonderlandとUlyssesの2つのテキストをダウンロードしました。 stop_wordsはAliceから消えますが、まだUlyssesにあります。 anti_joinを フィルタ(!word%%stop_words $ word)に置き換えても、この問題は解決されませんでした。R tidytext stop_wordsがgutenbergrから一貫してフィルタリングされないダウンロード

ユリシーズからstop_wordsを取得するにはどうすればよいですか?

ありがとうございました!

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() 

答えて

1

トークン化ユリシーズに掘りのビットの後、テキストは「それはだ」実際の代わりにアポストロフィの右側の単一引用符を使用しています。 stop_wordstidytextにアポストロフィを使用します。右一重引用符をアポストロフィで置き換える必要があります。

私がすることでこれを見つけた:8217をグーグルで

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

hereに私をリードしています。そこから、C++/Javaソース\u2019を取得し、anti-joinの前にmutategsubのステートメントを追加するのと同じくらい簡単です。中

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

結果:

enter image description here

+0

は、それらのほとんどの引用符/アポストロフィはとても卑劣です!これを理解し、それを修正する方法を教えてくれてありがとう。 –

+0

助けがあれば、それを答えとして受け入れて他の人に知らせることができます。 –

+0

オハイオ州、オクラホマ - 私はここに新しいです - するでしょう! –

関連する問題