#' This function allows you to get synonym of a word thanks to Thesaurus.com.
#'
#' @param word Vecteur contenant le(s) mots recherché(s)
#' @param langue Choix de la langue ("en" ou "fr")
#' @param most_recommended Doit-on seulement chercher les synonymes les plus recommandés ou une liste plus exostives ? (TRUE ou FALSE)
#' @export
#' @examples
#' get_synonym("cat")
#'
#' Return larger list of synonym
#' get_synonym("cat", most_recommended = FALSE)
#' # For multiple research
#' lapply(c("cat","dog"), get_synonym)
get_synonym = function(word, langue, most_recommended = TRUE) {
# Depedencies
tidysession(lst = c("dplyr", "rvest", "stringr"))
if (langue == "en") {
# Get http path
page = paste0("https://www.thesaurus.com/browse/", word)
# Message
cat("Recherche: ", paste(word), "\n")
# Load web page
get = try(read_html(page) %>%
html_nodes(".css-0") %>%
xml_contents())
# Grep
syn = get[4] %>%
as.character() %>%
strsplit("data-emotion-css") %>%
unlist() %>%
.[3] %>%
strsplit("browse") %>%
unlist() %>%
.[-1] %>%
str_replace_all("[[:punct:]]", " ") %>%
trimws() %>%
word(1) %>%
paste(collapse = ", ")
if (most_recommended == FALSE) {
syn2 = get[4] %>%
as.character() %>%
strsplit("data-emotion-css") %>%
unlist() %>%
.[4] %>%
strsplit("browse") %>%
unlist() %>%
.[-1] %>%
str_replace_all("[[:punct:]]", " ") %>%
trimws() %>%
word(1) %>%
paste(collapse = ", ")
syn = paste(syn, syn2, sep = ", ")
}
# Print results
cat("Résultats: ", paste(syn), "\n")
# Return final list
return(syn)
}
else if (langue == "fr") {
# Get http path
page = paste0("http://www.synonymes.com/synonyme.php?mot=", word)
# Load web page
get = try(read_html(page) %>%
html_nodes(".defbox") %>%
xml_contents(),
silent = TRUE)
# Grep
syn = get[3] %>%
as.character() %>%
str_replace_all("[[:punct:]]+", " ") %>%
str_extract_all(">(.*?)<") %>%
unlist() %>%
str_remove_all("<|>") %>%
str_replace_all("\\s+", "") %>%
enframe() %>%
filter(!value == "") %>%
select(-name)
# Return final list
return(syn)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.