-
Notifications
You must be signed in to change notification settings - Fork 2
/
wikimedia_detect_language.R
45 lines (37 loc) · 1.3 KB
/
wikimedia_detect_language.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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)
}