2016-11-17 6 views
1

Wordcloudsを作成するときは、すべての単語を小文字にするのが最も一般的です。しかし、私は、単語の大文字を単語で表示するようにしたい。単語を大文字にした後も、ワードクラウドは小文字の単語を表示します。なぜどんなアイデア?すべての単語をWordcloudの大文字にするR

再現可能コード:

library(tm) 
    library(wordcloud) 

data <- data.frame(text = c("Creativity is the art of being ‘productive’ by using 
      the available resources in a skillful manner. 
      Scientifically speaking, creativity is part of 
      our consciousness and we can be creative – 
      if we know – ’what goes on in our mind during 
      the process of creation’. 
      Let us now look at 6 examples of creativity which blows the mind.")) 

text <- paste(data$text, collapse = " ") 

# I am using toupper() to force the words to become uppercase. 
text <- toupper(text) 

source <- VectorSource(text) 
corpus <- VCorpus(source, list(language = "en")) 

# This is my function for cleaning the text     
clean_corpus <- function(corpus){ 
      corpus <- tm_map(corpus, removePunctuation) 
      corpus <- tm_map(corpus, removeNumbers) 
      corpus <- tm_map(corpus, stripWhitespace) 
      corpus <- tm_map(corpus, removeWords, c(stopwords("en"))) 
      return(corpus) 
} 

clean_corp <- clean_corpus(corpus) 
data_tdm <- TermDocumentMatrix(clean_corp) 
data_m <- as.matrix(data_tdm) 

commonality.cloud(data_m, colors = c("#224768", "#ffc000"), max.words = 50) 

これは舞台裏TermDocumentMatrix(clean_corp)TermDocumentMatrix(clean_corp, control = list(tolower = TRUE))を行っているためです次の出力

enter image description here

答えて

3

に生成します。 TermDocumentMatrix(clean_corp, control = list(tolower = FALSE))に設定すると、単語は大文字のままになります。あるいは、後でマトリックスの行名を調整することもできます:rownames(data_m) <- toupper(rownames(data_m))

+0

おかげで、クリーニング、特にストップワードの削除が小文字になるので、クリーニング後にrownamesを上にするほうが理にかなっていると思いますか? – FilipW

+0

そのようにして、小文字に変換してから大文字に変換します。だから私は制御引数を指定するだけです。 – lukeA

関連する問題