Scrapping Data Using R. case by data review Marina Bay Sands…
By: Ryan Reza Fadillah
Tags: R, Sentiment Analysis, Tripadvisor, Data Science, Hotel Reviews

Scrapping Data Using R

Hi Data Enthusiast !!!

Materi kali ini kita akan menganalisis data review Marina Bay Sands Hotel, Singapore. Jadi kita ingin mengetahui bagaimana sih secara garis besaranya review pengjung terhadap hotel tersebut.

Review hotel bisa kalian dapatkan di website TripAdvisor dan kalian juga perlu mengunduh SelectorGadget untuk melihat posisi review yang akan diambil.

Pada materi ini package yang kalian butuhkan sebagai berikut,

install.packages(xml2)

install.packages(rvest)

install.packages(tm)

install.packages(SnowballC)

install.packages(wordcloud)

install.packages(RColorBrewer)

install.packages(stringr)

library(xml2)

library(rvest)

library(tm)

library(SnowballC)

library(wordcloud)

library(RColorBrewer)

library(stringr)

Lalu copy-kan link halaman yang memuat review hotel yang akan kita scraping dan pindahkan link tersebut kedalam R



marina marina https://www.tripadvisor.com/Hotel_Review-g294265-d1770798-Reviews-Marina_Bay_Sands-Singapore.html ")marina

nampak hasilnya seperti gambar dibawah,

Selanjutnya sorot salah satu komentar yang ada dengan menggunakan SelectorGadget,

Lalu masukkan script letak review ke dalam R,

review<-html_nodes(marina,".location-review-review-list-parts-ExpandableReview__reviewText--gOmRC")

reviewtext<-html_text(review)

reviewtext

nampak hasilnya seperti gambar di bawah,

Selanjutnya, kita akan membersihkan huruf yang tidak diperlukan misal “

” dan lainnya dan simpan data yang telah dibersihkan dalam csv

reviewtextbaru<-gsub(“

”,””,reviewtext)

reviewtextbaru

write.csv(reviewtextbaru,”D:\\BIML\\reviewtextmarina.csv”)

Selanjutnya, buka file csv yang telah tersimpan dan lihat jika ada baris yang tidak memuat data maka bisa kita hapuskan. Contohnya dibawah ini pada baris pertama tidak memuat data yang berarti jadi bisa kalian hapus

Selanjutnya, panggil kembali data csv yang telah dibersihkan kedalam R,

dokumen<-readLines("D:\\BIML\\reviewtextmarina.csv")

dokumen

nampak hasilnya,

Lalu buatlah Corpus dan str untuk dokumen di atas,

dokumen <- VCorpus(VectorSource(dokumen))

dokumen

str(dokumen)

Kemudian, cobalah dari kelima dokumen yang ada,

dokumen[[2]]$content

maka akan nampak seperti gambar di bawah,

Setelah itu, buatlah matriks kata- kata dalam dokumen dengan menggunakan script di bawah ini,

dokumenDTM<-DocumentTermMatrix(dokumen,control=list(tolower=TRUE,

removeNumbers=TRUE,

stopwords=TRUE,

removePunctuation=TRUE,

stemming=TRUE))

dokumenDTM

maka akan nampak,

Bisa dilihat jika dari 5 dokumen ada 262 kata yang berbeda, maka kita gunakan

inspect(dokumenDTM)

maka akan menghasilkan,

Pada gambar di atas bisa kita ketahui jika, pada dokumen ke-1 kata “citi” muncul 2 kali, “hotel” 8 kali, dst. Kemudian kita dapat menampilkan semua kata- kata yang ada,

dokumenDTM$dimnames$Terms

maka akan menghasilkan,

> dokumenDTM$dimnames$Terms [1] "« cé" "access" "adjac" "adult" "advic"

[6] "again" "airport" "alcohol" "all" "almost"

[11] "also" "although" "alway" "amaz" "amidst"

[16] "anoth" "appreci" "around" "arriv" "art"

[21] "ask" "ate" "attach" "attent" "attract"

[26] "balconi" "bar" "bath" "bathroom" "bay"

[31] "beauti" "bed" "begin" "best" "better"

[36] "beverag" "bill" "binocular" "bit" "breakfast"

[41] "bucket" "budget" "busi" "bustl" "came"

[46] "can" "card" "carri" "centr" "check"

[51] "checkout" "cheer" "children" "christma" "citi"

[56] "club »" "cold" "colder" "comfort" "coursemb"

[61] "crowd" "day" "decemb" "design" "disappoint"

[66] "dollar" "done" "drink" "drop" "els"

[71] "encount" "end" "enjoy" "enorm" "ensur"

[76] "entir" "especi" "european" "everyth" "exceed"

[81] "excel" "expect" "experi" "experienc" "express"

[86] "extra" "extrem" "face" "fantast" "feel"

[91] "felt" "final" "first" "flight" "floor"

[96] "food" "forev" "friend" "full" "funni"

[101] "garden" "get" "girl" "give" "glad"

[106] "good" "great" "harbor" "harbour" "help"

[111] "high" "hoard" "holiday" "holiday…" "hotel"

[116] "hour" "housekeep" "howev" "huge" "imposs"

[121] "in…" "includ" "incred" "infin" "inroom"

[126] "item" "itself" "jacket" "jacuzzi" "key"

[131] "keycard" "kind" "lack" "larg" "least"

[136] "let" "level" "lifetim" "light" "list"

[141] "live" "long" "look" "lustr" "luxuri"

[146] "made" "magnific" "main" "makeup" "mani"

[151] "mattress" "menu" "mini" "moment" "much"

[156] "museum" "need" "nice" "night" "noisi"

[161] "now" "okay" "once…" "onli" "outlet"

[166] "outsid" "overal" "overlook" "panoram" "pay"

[171] "peopl" "perfect" "person" "phone" "place"

[176] "plane" "pleasant" "polit" "pool" "poolsid"

[181] "posit" "price" "privaci" "probabl" "profess"

[186] "rather" "readi" "realli" "recommend" "relax"

[191] "remark" "resort" "restaur" "return" "room"

[196] "say" "scienc" "secur" "see" "self"

[201] "selfi" "servic" "ship" "shop" "singapor"

[206] "size" "skypark" "snack" "special" "splash"

[211] "spotter" "staff" "stand" "star" "stare"

[216] "stay" "stick" "stiletto" "still" "store"

[221] "stun" "sunni" "superb" "support" "swift"

[226] "swim" "televis" "there" "thermal" "thing"

[231] "think" "time" "top" "towel" "trendi"

[236] "tri" "unattract" "uncomfort" "understand" "upmarket"

[241] "use" "varieti" "vi »" "view" "visit"

[246] "wait" "want" "warm" "watch" "wave"

[251] "well" "went" "winter" "wise" "wish"

[256] "with" "without" "wonder" "worth" "wouldn’t"

[261] "wrong" "young"

[256] "with" "without" "wonder" "worth" "wouldn’t"

[261] "wrong" "young"

Selanjutnya run script dibawah ini,

dokumenfreq<-findFreqTerms(dokumenDTM,3)

dokumenfreq

dokumenfreq<-findFreqTerms(dokumenDTM,3)

dokumenfreq

maka akan menghasilkan,

dokumenfreq<-findFreqTerms(dokumenDTM,3)

dokumenfreq [1] "also" "bay" "check" "citi" "enjoy" "food"

[7] "friend" "garden" "get" "good" "high" "hotel"

[13]"incred" "infin" "made" "much" "night" "pay"

[19]"pool" "room" "singapor" "size" "staff" "stay"

[25]"view" "visit" dokumenfreq<-findFreqTerms(dokumenDTM,4)

dokumenfreq [1] "bay" "check" "citi" "get" "good" "hotel"

[7] "night" "pool" "room" "singapor" "staff" "stay"

[13]"view" "visit"

(dokumenDTM,3) artinya kata — kata yang mempunyai frekuensi 3 kali muncul atau lebih, sedangkan (dokumenDTM,4) rtinya kata — kata yang mempunyai frekuensi 4 kali muncul atau lebih.

dokkudtm <- TermDocumentMatrix(dokumen)

maka akan menghasilkan,

Selanjutkan, buatlah matriks nya,

em <- as.matrix(dokkudtm)

em

ve <- sort(rowSums(em),decreasing=TRUE)

ve

Setelah muncul matriksnya, kita akan melihat 6 data terbesar,

de <- data.frame(word = names(ve),freq=ve)

head(de, 15)

str(de)

de$word

maka akan menghasilkan,

[1] the and you was

[5] hotel pool for room

[9] view are but staff

[13] that this were with

[17] all from has our

[21] singapore would your bay

[25] city get good not

[29] stay there check food

[33] have high infinity made

[37] much night once out

[41] pool, than views when

[45] which while after also

[49] bar best bill busy

[53] can did don't enjoy

[57] especially everything friendly full

[61] gardens had incredible list

[65] nice only own paying

[69] people place pool. price

[73] really say selfie service

[77] shopping star stunning their

[81] top very visit warm

[85] well what without worth

[89] !""" "1,""i "2,""at "3,""check

[93] "4,""we "5,""okay, (snack (with

[97] « cé 11am. 12.5 57th

[101] about access added adjacent

[105] adult advice again. airport.

[109] alcoholic all, almost also,

[113] although always amazing amidst

[117] another appreciate around arrived

[121] art ask ate attached

[125] attentive. attraction balcony. bath

[129] bath. bathroom beautiful.""" beds

[133] been before begin better

[137] beverages binoculars) bit both

[141] breakfast bucket budget bustle

[145] came cannot card carry

[149] centre centre, check-out checking

[153] cheerful children christmas club »

[157] cold colder comfortable could

[161] course,mbs crowds day, december

[165] designer didn't disappointed doesn't

[169] dollar done drinks. dropped

[173] each else. encountered end

[177] enjoyed enormous ensured entire

[181] european exceedingly excellent. expected

[185] expected, experience, experience. experienced

[189] express extra extremely face

[193] fantastic feel felt final

[197] first flight floor, forever.

[201] friends funny gardens, girls

[205] give gives glad great

[209] harbor harbor. harbour harbour.

[213] having helpful helpful. hoards

[217] holiday holiday.…""" hotel, hotel.

[221] hours. housekeeping how however

[225] huge impossible in-room in.…"""

[229] including incredibly isn't it,

[233] item its itself, jacket

[237] jacuzzis key keycard kind,

[241] lack large least. let

[245] level level. lifetime lights.

[249] live long. look looking

[253] lustre luxuries luxury magnificent,

[257] main makeup many mattress

[261] me. menu mini moment

[265] most museum need nights

[269] noisy. now on. once.…"""

[273] only. other outlets outside

[277] overall overall, overlooked panoramic

[281] pay perfect person phones

[285] plane pleasant polite pool?

[289] poolside positives. privacy probably

[293] profession. rather ready. recommend

[297] relaxing remarkably resort restaurants.

[301] return. rooms science security

[305] see self ships singapore.

[309] size size, sized) skypark

[313] some special. splashed spotters

[317] standing stare stay, stay.

[321] sticks stilettos. still stores

[325] sunnies superb support swift

[329] swim swimming television them

[333] then there, thermals thing

[337] think those time time,

[341] too towels trendy trying

[345] unattractive uncomfortable. understand upmarket

[349] used. using variety vi »

[353] visit. visiting waiting want

[357] watch waved went winter

[361] wise wise. wish wondered

[365] wouldn't wouldn’t wrong, you've

[369] young

369 Levels: !""" "1,""i "2,""at "3,""check "4,""we "5,""okay, ... your

Setalh itu, kita akan membuat wordcloud dari kata- kata di atas menggunakan script di bawah ini,

wordcloud(words = de$word, freq = de$freq, min.freq = 1,

max.words=50, random.order=FALSE, rot.per=0.35,

colors=brewer.pal(8, "Dark2"))

maka akan menghasilkan,

Dari hasil wordcloud diatas, terlihat masih ada kata “the”, “was”, dll yang artinya kurang bagus. Jika kita ingin mengetahui asosiasi kata- kata yang sering muncul, misal kata “good”, “great”, “nice” maka gunakanlah perintah berikut,

vee<-as.list(findAssocs(dokkudtm, terms =c("good", "amaizing","nice","delicious"), corlimit = c(0.15,0.15,0.15,0.15,0.15,0.15)))

dan akan menghasilkan,

$good

food high once views which

0.87 0.87 0.87 0.87 0.87

the "3,""check (snack (with added

0.86 0.80 0.80 0.80 0.80

adjacent adult airport. alcoholic also,

0.80 0.80 0.80 0.80 0.80

although amidst appreciate art ate

0.80 0.80 0.80 0.80 0.80

balcony. bar better beverages bill

0.80 0.80 0.80 0.80 0.80

binoculars) bit budget bustle centre

0.80 0.80 0.80 0.80 0.800.80

harbour hotel. however in-room in.…"""

0.80 0.80 0.80 0.80 0.80

item its lack large level.

0.80 0.80 0.80 0.80 0.80

lights. look looking lustre magnificent,

0.80 0.80 0.80 0.80 0.80

mattress me. mini museum noisy.

0.80 0.80 0.80 0.80 0.80

only outlets overall, plane price $great

"5,""okay, 11am. 12.5 advice after

1.00 1.00 1.00 1.00 1.00

again. all, almost also always

1.00 1.00 1.00 1.00 1.00

another around attached bath. bathroom

1.00 1.00 1.00 1.00 1.00

beds begin both came cannot

1.00 1.00 1.00 1.00 1.00

centre, check-out cold colder comfortable

1.00 1.00 1.00 1.00 1.00

could day, didn't disappointed doesn't

1.00 1.00 1.00 1.00 1.00

dollar don't done else. encountered

1.00 1.00 1.00 1.00 1.00

end european everything expected expected,

1.00 1.00 1.00 1.00 1.00

experience, experience. experienced extra face

1.00 1.00 1.00 1.00 1.00

fantastic feel felt flight forever.

1.00 1.00 1.00 1.00 1.00

glad having hoards holiday holiday.…""" $nice "5,""okay, 11am. 12.5 advice after

1.00 1.00 1.00 1.00 1.0

again. all, almost also always

1.00 1.00 1.00 1.00 1.00

another around attached bath. bathroom

1.00 1.00 1.00 1.00 1.00

beds begin both came cannot

1.00 1.00 1.00 1.00 1.00

centre, check-out cold colder comfortable

1.00 1.00 1.00 1.00 1.00

could day, didn't disappointed doesn't

1.00 1.00 1.00 1.00 1.00

dollar don't done else. encountered

1.00 1.00 1.00 1.00 1.00

end european everything expected expected,

1.00 1.00 1.00 1.00 1.00

experience, experience. experienced extra face

1.00 1.00 1.00 1.00 1.00

fantastic feel felt flight forever.

1.00 1.00 1.00 1.00 1.00

glad having hoards holiday holiday.…"""

1.00 1.00 1.00 1.00 1.00

hotel, hours. huge impossible including

1.00 1.00 1.00 1.00 1.00

incredibly isn't it, itself, jacket

1.00 1.00 1.00 1.00 1.00

let live long. most much

1.00 1.00 1.00 1.00 1.00

nights now on. only. other

1.00 1.00 1.00 1.00 1.00

outside pay paying pleasant pool,

1.00 1.00 1.00 1.00 1.00

positives. probably resort restaurants. rooms

1.00 1.00 1.00 1.00 1.00

Referensi