Last active
December 29, 2015 06:09
-
-
Save jnv/7626913 to your computer and use it in GitHub Desktop.
Načetní postů z facebooku, tokenizace, sestavení korpusu a vytvoření wordcloudu.
Pro StuNoMe Digital Humanities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Rfacebook | |
# Instalace knihovny - staci spustit jenom jednou | |
install.packages("Rfacebook") | |
# Nacteni knihovny | |
library(Rfacebook) | |
# Token ziskany z https://developers.facebook.com/tools/explorer | |
token <- "..." | |
## Uzivatelske udaje | |
# Nacteni udaju o sobe | |
me <- getUsers("me", token=token) | |
# Nacteni site vazeb do matice | |
network <- getNetwork(token, format="adj.matrix") | |
## Nacteni 100 prispevku stranky | |
stunome <- getPage("stunome", token=token, n=100) | |
summary(stunome$likes_count) | |
# Post s maximem likes | |
stunome[which.max(stunome$likes_count),] | |
# Posty s nadprumernym poctem likes | |
uspesne <- subset(stunome, likes_count >= mean(stunome$likes_count)) | |
### Wordcloud | |
## Instalace zavislosti - staci spustit jenom jednou | |
install.packages("tm") | |
install.packages("wordcloud") | |
# Nacteni knihoven | |
library(tm) | |
library(wordcloud) | |
## Priprava korpusu | |
corpus <- Corpus(VectorSource(stunome$message)) | |
corpus <- tm_map(corpus, tolower) | |
corpus <- tm_map(corpus, removePunctuation) | |
corpus <- tm_map(corpus, removeNumbers) | |
# Matice slov | |
ap.tdm <- TermDocumentMatrix(corpus) | |
ap.m <- as.matrix(ap.tdm) | |
# Dataframe frekvenci | |
ap.v <- sort(rowSums(ap.m),decreasing=TRUE) | |
ap.d <- data.frame(word = names(ap.v),freq=ap.v) | |
## Wordcloud | |
require(RColorBrewer) | |
# Sestaveni barevne palety | |
# pro vyber palet viz http://blog.ggplot2.org/post/24607351280/choosing-colour-palettes-part-ii-educated-choices | |
pal2 <- brewer.pal(8,"Dark2") | |
# Povolit zapis obrazku do souboru | |
png("wordcloud_stunome.png", width=1024,height=768) | |
# Sestaveni wordcloudu | |
wordcloud(ap.d$word,ap.d$freq, scale=c(10,.2),min.freq=3, | |
max.words=150, random.order=FALSE, rot.per=.15, colors=pal2) | |
# Vypne zapis do souboru (device off) | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment