Skip to content

Commit

Permalink
feat: translate to morse code
Browse files Browse the repository at this point in the history
  • Loading branch information
tin900 committed Apr 17, 2024
1 parent 1b89a2a commit ee24ff9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(qcri_get_domains)
export(qcri_get_language_pairs)
export(qcri_translate_text)
export(translate_file)
export(translate_to_morse)
export(wikimedia_detect_language)
export(wikipedia_get_language_names)
export(wmcloud_translate)
Expand Down
43 changes: 43 additions & 0 deletions R/translate_to_morse.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' Translate Text to Morse Code using the FunTranslations API
#'
#' This function takes a string of text as input and translates it to Morse code using the FunTranslations API.
#'
#' @param text A character string containing the text to be translated to Morse code.
#' @param api_key (optional) Your FunTranslations API key, if you have a paid subscription.
#' @return A list containing the translated Morse code text and other metadata.
#' @export
translate_to_morse <- function(text, api_key = NULL) {
url <- "https://api.funtranslations.com/translate/morse.json"
payload <- list(text = text)

# Add API key if provided
if (!is.null(api_key)) {
headers <- c("X-FunTranslations-Api-Secret" = api_key)
} else {
headers <- NULL
}

# Make the API request
response <- httr::POST(url, body = payload, httr::add_headers(headers))

# Check the response status
if (httr::status_code(response) == 200) {
# Parse the JSON response
result <- jsonlite::fromJSON(httr::content(response, "text"))

# Extract the relevant information
morse_code <- result$contents$translated
text_translated <- result$contents$text
translation_type <- result$contents$translation

# Return the results as a list
return(list(
morse_code = morse_code,
text_translated = text_translated,
translation_type = translation_type
))
} else {
# Handle error cases
stop(paste("Error:", httr::status_code(response), httr::content(response, "text")))
}
}
5 changes: 5 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ reference:
- wikimedia_detect_language
- wikipedia_get_language_names
- wmcloud_translate

- title: Funtranslation Methods
desc: Methods using Funtranslation services for morse code
contents:
- translate_to_morse
19 changes: 19 additions & 0 deletions man/translate_to_morse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ee24ff9

Please sign in to comment.