Skip to content

Instantly share code, notes, and snippets.

@leobarone
Last active June 10, 2017 02:13
Show Gist options
  • Save leobarone/77c910a9c722d14c03dc4dfed1505119 to your computer and use it in GitHub Desktop.
Save leobarone/77c910a9c722d14c03dc4dfed1505119 to your computer and use it in GitHub Desktop.
library(rvest)
library(stringr)
library(dplyr)
library(purrr)
library(tm)
library(wordcloud)
library(tidytext)
library(ggplot2)
url_obra_completa <- "http://machado.mec.gov.br/obra-completa-mainmenu-123"
url_tipo_obra <- url_obra_completa %>%
read_html() %>%
html_nodes(xpath = "//td[@class = 'list-title']/a") %>%
html_attr(name = "href")
tipo_obra <- url_obra_completa %>%
read_html() %>%
html_nodes(xpath = "//td[@class = 'list-title']/a") %>%
html_text()
url_tipo_obra <- paste0("http://machado.mec.gov.br/", url_tipo_obra)
tipo_obra <- trimws(tipo_obra)
tipo_obra <- iconv(tipo_obra, to = "Latin1")
obras <- data_frame()
for (i in 1:length(url_tipo_obra)){
print(i)
tabela <- url_tipo_obra[i] %>%
read_html() %>%
html_table()
titulos <- iconv(tabela[[1]]$X1, to = "Latin1")
titulos <- titulos[titulos != ""]
anos <- gsub(")", "", titulos)
anos <- substr(anos, nchar(anos) - 3, nchar(anos))
anos <- as.numeric(anos)
anos <- anos[titulos != "Dispersas (em ordem cronológica)"]
titulos <- titulos[titulos != "Dispersas (em ordem cronológica)"]
urls_novos <- url_tipo_obra[i] %>%
read_html() %>%
html_nodes(xpath = "//td/a") %>%
html_attr(name = "href") %>%
str_subset(".htm")
urls_novos <- paste0("http://machado.mec.gov.br/", urls_novos)
obras <- bind_rows(obras,
data_frame(titulo = titulos,
ano = anos,
url = urls_novos,
tipo = tipo_obra[i]))
}
get_texto_obra <- function(url_obra) {
print(url_obra)
texto <- url_obra %>%
read_html() %>%
html_nodes(xpath = "//p") %>%
html_text() %>%
str_replace_all("\n", " ") %>%
str_replace_all(" ", " ") %>%
str_replace_all(" ", " ") %>%
str_c(collapse = " ")
# inicio <- str_which(texto, "Publicado originalmente") + 1
# texto <- texto[inicio:length(texto) - 1]
texto
}
obras$textos <- map_chr(obras$url, get_texto_obra)
#### CORPUS COM PACOTE TM
limpa_corpus <- function(corpus){
stopwords_portugues <- c(stopwords("pt"), "capítulo")
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, content_transformer(tolower))
corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, removeWords, stopwords_portugues)
corpus
}
prepara_corpus <- function(vetor) {
v_source <- VectorSource(vetor)
v_corpus <- VCorpus(v_source)
v_corpus <- limpa_corpus(v_corpus)
v_corpus
}
remove_acentos <- function(x) iconv(x, to = "ASCII//TRANSLIT")
for (tipo in tipo_obra){
novo_corpus <- prepara_corpus(obras$textos[obras$tipo == tipo])
nome_corpus <- tolower(remove_acentos(tipo))
print(nome_corpus)
assign(nome_corpus, novo_corpus)
}
vetor_corpora <- tolower(remove_acentos(tipo_obra))
set.seed(20160609)
wordcloud(romance, max.words = 100, min.freq = 3)
wordcloud(conto, max.words = 100, min.freq = 3)
wordcloud(poesia, max.words = 100, min.freq = 3)
wordcloud(cronica, max.words = 100, min.freq = 3)
wordcloud(teatro, max.words = 100, min.freq = 3)
wordcloud(critica, max.words = 100, min.freq = 3)
wordcloud(cronica, max.words = 100, min.freq = 3)
wordcloud(traducao, max.words = 100, min.freq = 3)
wordcloud(miscelanea, max.words = 100, min.freq = 3)
### Frequencia de palavras com pacote tidytext
df_palavras <- obras %>%
select(titulo, textos, tipo) %>%
group_by(tipo) %>%
unnest_tokens(word, textos)
head(df_palavras)
stopwords_pt <- c(stopwords("pt"), "capítulos", "é")
stopwords_pt_df <- data.frame(word = stopwords_pt)
df_palavras <- df_palavras %>%
anti_join(stopwords_pt_df, by = "word")
palavras_chave <- c("negro", "negra", "preto", "preta", "ladino", "mucama", "escravidão", "alforria")
palavras_chave_df <- data.frame(word = palavras_chave)
df <- df_palavras %>%
semi_join(palavras_chave_df, by = "word")
df %>%
count(word, sort = TRUE) %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n, fill = tipo)) +
geom_col() +
xlab(NULL) +
coord_flip() +
facet_wrap(~tipo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment