Analyst Perempuan Paling Cantik di Negeriku Indonesia - Dewa 19 with Worldcloud
author :
Hieronimus Bryan Eiffel 5052231003
Danish Rafie Ekaputra 5052231024
Generating World-cloud in R
A word cloud is a visually prominent presentation of “keywords” that appear frequently in text data. The rendering of keywords forms a cloud-like color picture, so that you can appreciate the main text data at a glance.
In R, word cloud can be made with help of wordcloud library.
The core of the wordcloud library is the WordCloud class, and all functions are encapsulated in the WordCloud class. When using, you need to instantiate a WordCloud object, and call its generate(text) method to convert the text into a word cloud.
Parameter pada wordcloud() dan wordcloud2() Pada wordlcoud() terdapat parameter-parameter dasar dalam membuat Bubble Word seperti:
- Word : berisi kata kata yang ingin divisualisasikan dalam wordcloud
- Freq : vektor berisi frekuensi atau bobot dari setiap kata dalam word
- Max.word: jumlah maksimal kata yang akan ditampilkan dalam wordcloud
- Random.order: pengaturan logika kata kata yang akan ditampilkan secara acak atau berdasarkan frekuensi
- Colors: vektor warna yang akan digunakan untuk kata kata dalam wordcloud Sedangkan untuk wordcloud2, merupakan pengembangan dari wordcloud() yang bisa diinstal menggunakan third-party software (GitHub). Parameter yang ada di wordlouc2() kurang lebih sama namun ada beberapa parameter khusus seperti:
- figPath : Path atau URL gambar yang akan digunakan sebagai bentuk wordcloud
- fileFlagColor : warna yang akan digunakan untuk mengisi area dalam bentuk wordcloud yang akan dibuat
- fileFlagColourPaletteLenghtCap : digunakan untuk menentukan maksimun warna dalam pallete yang akan digunakan
- colourRampPalette: digunakan untuk membuat palet warna yang akan digunakan untuk kata kata dalam wordcloud. Dalam wordcloud2(), pengguna bisa untuk membuat wordcloud dengan bentuk yang lebih kompleks serta menyediakan opsi untuk kustomisasi warna menggunakan colour pallete
1. Create a text file
In the following examples, we want process the “Perempuan Paling Cantik di Negeriku Indonesia” from “Dewa-19”.
2. Install and load the required packages
Type the R code below, to install and load the required packages.
library(tm)## Loading required package: NLP
library(SnowballC)
library(magick)## Linking to ImageMagick 6.9.12.93
## Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fftw, ghostscript, x11
library(wordcloud)## Loading required package: RColorBrewer
library(wordcloud2)
library(RColorBrewer)
library(ggplot2)##
## Attaching package: 'ggplot2'
## The following object is masked from 'package:NLP':
##
## annotate
text <- readLines("perempuanpalingcantik.txt")3. Text mining
The text is loaded using Corpus() function from text
mining (tm) package. Corpus is a list of a document (in our
case, we only have one document).
Load the data as a corpus and Inspect the content of the document
docs <- Corpus(VectorSource(text))
inspect(docs)## <<SimpleCorpus>>
## Metadata: corpus specific: 1, document level (indexed): 0
## Content: documents: 28
##
## [1] Merah darahku bulat tekadku
## [2] Setelah aku tatap wajahmu
## [3] Berkobar seluruh jiwa dan ragaku
## [4] Untuk perjuangkan cinta yang ku yakini
## [5] Putih tulangku semangat cintaku
## [6] Setelah aku raba tanganmu
## [7] Rasakan kulitmu yang selembut salju
## [8] Serentak bergelora darah mudaku
## [9] Kamu adalah perempuan paling cantik
## [10] Di negeriku indonesia kamulah yang nomor satu
## [11] Aku tak akan bisa sukai lagi perempuan yang lainnya
## [12] Revolusi cinta matiku
## [13] Telah bergema ke seluruh negeri
## [14] Ini adalah tonggak sejarah hidupku
## [15] Karena ku yakin kamu adalah takdirku
## [16] Dengan tegasnya ku nyatakan
## [17] Kamulah akhir perjuanganku
## [18] Kuburkan cinta cinta yang sudah sudah
## [19] Kemerdekaan aku kamu yang ku tunggu
## [20] Kamu adalah perempuan paling cantik
## [21] Di negeriku indonesia kamulah yang nomor satu
## [22] Aku tak akan bisa sukai lagi perempuan yang lainnya
## [23] Kamu adalah perempuan paling cantik
## [24] Di negeriku indonesia kamulah yang nomor satu
## [25] Aku tak akan bisa sukai lagi perempuan yang lainnya
## [26] Kamu adalah perempuan paling cantik
## [27] Di negeriku indonesia kamulah yang nomor satu
## [28] Aku tak akan bisa sukai lagi perempuan yang lainnya
4. Text transformation
Transformation is performed using tm_map() function to
replace, for example, special characters from the text.
Replacing “/”, “@” and “|” with space.
toSpace <- content_transformer(function (x , pattern ) gsub(pattern, " ", x))
docs <- tm_map(docs, toSpace, "/")
docs <- tm_map(docs, toSpace, "@")
docs <- tm_map(docs, toSpace, "\\|")5. Cleaning the text
The tm_map() function is used to remove unnecessary
white space, to convert the text to lower case, to remove common
stopwords like “the” or “we”.
The information value of stopwords is near zero due to the
fact that they are so common in a language. Removing this kind of words
is useful before further analyses. For ‘stopwords’, supported languages
are danish, dutch, english,
finnish, french, german,
hungarian, italian, norwegian,
portuguese, russian, spanish and
swedish. Language names are case sensitive.
You could also remove numbers and punctuation with removeNumbers and removePunctuation arguments.
Another important preprocessing step is to make a text stemming which reduces words to their root form. In other words, this process removes suffixes from words to make it simple and to get the common origin. For example, a stemming process reduces the words “moving”, “moved” and “movement” to the root word, “move”.
# Convert the text to lower case
docs <- tm_map(docs, content_transformer(tolower))## Warning in tm_map.SimpleCorpus(docs, content_transformer(tolower)):
## transformation drops documents
# Remove numbers
docs <- tm_map(docs, removeNumbers)## Warning in tm_map.SimpleCorpus(docs, removeNumbers): transformation drops
## documents
# Remove english common stopwords
docs <- tm_map(docs, removeWords, stopwords("english"))## Warning in tm_map.SimpleCorpus(docs, removeWords, stopwords("english")):
## transformation drops documents
# Remove your own stop word
# specify your stopwords as a character vector
docs <- tm_map(docs, removeWords, c("yang", "aku", "adalah", "kamu", "kamulah", "akan", "bisa","lagi", "lainnya", "tak","setelah")) ## Warning in tm_map.SimpleCorpus(docs, removeWords, c("yang", "aku", "adalah", :
## transformation drops documents
# Remove punctuations
docs <- tm_map(docs, removePunctuation)## Warning in tm_map.SimpleCorpus(docs, removePunctuation): transformation drops
## documents
# Eliminate extra white spaces
docs <- tm_map(docs, stripWhitespace)## Warning in tm_map.SimpleCorpus(docs, stripWhitespace): transformation drops
## documents
# Text stemming
# docs <- tm_map(docs, stemDocument)6. Build a term-document matrix
Document matrix is a table containing the frequency of the words.
Column names are words and row names are documents. The function
TermDocumentMatrix() from text mining package can be used
as follow.
dtm <- TermDocumentMatrix(docs)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
head(d, 10)## word freq
## perempuan perempuan 8
## cinta cinta 4
## cantik cantik 4
## paling paling 4
## indonesia indonesia 4
## negeriku negeriku 4
## nomor nomor 4
## satu satu 4
## sukai sukai 4
## seluruh seluruh 2
# Select the top 10 most frequent words
top10 <- head(d, 10)
# Create a bar plot for the top 10 word frequencies
ggplot(data = top10, aes(x = reorder(word, -freq), y = freq)) +
geom_bar(stat = "identity", fill = "steelblue") +
theme_minimal() +
labs(title = "Top 10 Word Frequency Bar Plot",
x = "Words",
y = "Frequency") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))7. Generate the word-cloud
The importance of words can be illustrated as a word cloud as follow,
using wordcloud function.
set.seed(0324)
wordcloud(words = d$word, freq = d$freq, min.freq = 0,
max.words=200, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(8, "Dark2"))``wordcloud2`` package, with a slightly different design and fun applications. This package is a bit more fun to use, allowing us to do some more advanced visualizations. For instance, you can choose your word-cloud to appear in a specific shape or even letter.
wordcloud2(data=d, size=0.5, color='random-dark')wordcloud_image <- wordcloud2(data = d, figPath = 'LogoITS.png', size = 0.5, color = "random-light", backgroundColor = "white")
wordcloud_imagewordcloud_image1 <- wordcloud2(data = d, figPath = 'logosainsdata1.png', size = 0.5, color = "random-light", backgroundColor = "white")
wordcloud_image1wordcloud_image1 <- wordcloud2(data = d, figPath = 'logosainsdata.png', size = 10, color = "random-light", backgroundColor = "white")
wordcloud_image1