-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0c0a88
commit 1cedf8f
Showing
9 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#' Detect the language of a text | ||
#' | ||
#' This function sends a POST request to the Wikimedia Language ID API with the specified text, | ||
#' parses the JSON response, and returns the detected language. | ||
#' | ||
#' @param text The text whose language is to be detected. | ||
#' | ||
#' @return The detected language. | ||
#' | ||
#' @examples | ||
#' # Detect the language of a text | ||
#' wikimedia_detect_language("Hallo, wereld") | ||
#' | ||
#' @export | ||
wikimedia_detect_language <- function(text) { | ||
# Define the URL of the API | ||
url <- "https://api.wikimedia.org/service/lw/inference/v1/models/langid:predict" | ||
|
||
# Create a list of parameters to send in the POST request | ||
body <- list( | ||
text = text | ||
) | ||
|
||
# Convert the list to a JSON string | ||
json_body <- jsonlite::toJSON(body, auto_unbox = TRUE) | ||
|
||
# Set the content type of the request to 'application/json' | ||
headers <- c("Content-Type" = "application/json") | ||
|
||
# Send the POST request and get the response | ||
response <- httr::POST(url, body = json_body, httr::add_headers(headers)) | ||
|
||
# Check if the request was successful | ||
if (httr::status_code(response) != 200) { | ||
stop("Request failed with status ", httr::status_code(response)) | ||
} | ||
|
||
# Parse the response | ||
result <- httr::content(response, "parsed") | ||
|
||
# Extract the detected language from the response | ||
language <- result$wikicode | ||
|
||
return(language) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#' Get language names | ||
#' | ||
#' This function sends a GET request to the Wikipedia API and returns the language names as a dataframe. | ||
#' | ||
#' @return A dataframe of language names. | ||
#' | ||
#' @examples | ||
#' # Get language names | ||
#' wikipedia_get_language_names() | ||
#' | ||
#' @export | ||
wikipedia_get_language_names <- function() { | ||
# Define the URL of the API | ||
url <- "https://en.wikipedia.org/w/api.php?action=query&liprop=autonym|name&meta=languageinfo&uselang=en&format=json&origin=*" | ||
|
||
# Send the GET request and get the response | ||
response <- httr::GET(url) | ||
|
||
# Check if the request was successful | ||
if (httr::status_code(response) != 200) { | ||
stop("Request failed with status ", httr::status_code(response)) | ||
} | ||
|
||
# Parse the response | ||
result <- httr::content(response, "parsed") | ||
|
||
# Extract the language names from the response | ||
language_names <- result$query$languageinfo | ||
|
||
# Get the keys of the language_names list | ||
language_tags <- names(language_names) | ||
|
||
# Combine the language_tags, names, and autonyms into a dataframe | ||
df <- data.frame( | ||
language_tag = language_tags, | ||
name = sapply(language_names, function(x) x$name), | ||
autonym = sapply(language_names, function(x) x$autonym), | ||
row.names = NULL | ||
) | ||
|
||
return(df) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#' Translate text using WMCloud | ||
#' | ||
#' This function sends a POST request to the WMCloud translation API with the specified parameters, | ||
#' parses the JSON response, and returns the translated text. | ||
#' | ||
#' @param text The text to translate. | ||
#' @param target_language The target language for the translation (default is "en"). | ||
#' @param source_language The source language of the text (default is "en"). | ||
#' @param format The format of the content ("html", "json", "markdown", "text", "svg", "webpage"). | ||
#' @param model The model to use for the translation (only "nllb200-600M" is currently known to work). | ||
#' | ||
#' @return The translated text. | ||
#' @export | ||
wmcloud_translate <- function(text, | ||
target_language = "en", | ||
source_language = "en", | ||
format = "text", | ||
model = "nllb200-600M") { | ||
# Define the URL of the API | ||
url <- "https://translate.wmcloud.org/api/translate" | ||
|
||
# List of valid formats | ||
valid_formats <- c("html", "json", "markdown", "text", "svg", "webpage") | ||
|
||
# List of valid models | ||
valid_models <- c("nllb200-600M") # Add more models here | ||
|
||
# Check if format and model are valid | ||
if (!format %in% valid_formats) { | ||
stop(paste("Invalid format. Must be one of:", paste(valid_formats, collapse = ", "))) | ||
} | ||
if (!model %in% valid_models) { | ||
stop(paste("Invalid model. Must be one of:", paste(valid_models, collapse = ", "))) | ||
} | ||
|
||
# Create a list of parameters to send in the POST request | ||
body <- list( | ||
source_language = source_language, | ||
target_language = target_language, | ||
format = format, | ||
model = model, | ||
content = text | ||
) | ||
|
||
# Convert the list to a JSON string | ||
json_body <- jsonlite::toJSON(body, auto_unbox = TRUE) | ||
|
||
# Set the content type of the request to 'application/json' | ||
headers <- c("Content-Type" = "application/json") | ||
|
||
# Send the POST request and get the response | ||
response <- httr::POST(url, body = json_body, httr::add_headers(headers)) | ||
|
||
# Check if the request was successful | ||
if (httr::status_code(response) != 200) { | ||
stop("Request failed with status ", httr::status_code(response)) | ||
} | ||
|
||
# Parse the response | ||
result <- httr::content(response, "parsed") | ||
|
||
# Extract the translated text from the response | ||
translation <- result$translation | ||
|
||
return(translation) | ||
} | ||
|
||
|
||
|
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.