Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Park committed Jun 14, 2017
2 parents b968b2b + a39ca46 commit 1b94b42
Show file tree
Hide file tree
Showing 28 changed files with 159 additions and 47 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rosetteApi
Type: Package
Title: Rosette API
Version: 1.5.0
Version: 1.7.0
Date: 2016-12-12
Author: Sam Hausmann [aut, cre],
Chris Park [ctb],
Expand Down
48 changes: 41 additions & 7 deletions R/Api.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ api <- function(user_key, endpoint, parameters=FALSE, custom_headers=NULL, url_p
"name-similarity"= error_check(
post_endpoint(user_key, check_names(parameters, "name-similarity"), "name-similarity", url, custom_headers, url_parameters)
),
"name-deduplication"= error_check(
post_endpoint(user_key, check_deduplication(parameters, "name-deduplication"), "name-deduplication", url, custom_headers, url_parameters)
),
"relationships"= error_check(
post_endpoint(user_key, check_content_parameters(parameters), "relationships", url, custom_headers, url_parameters)
),
Expand All @@ -74,6 +77,9 @@ api <- function(user_key, endpoint, parameters=FALSE, custom_headers=NULL, url_p
"text-embedding"= error_check(
post_endpoint(user_key, check_content_parameters(parameters), "text-embedding", url, custom_headers, url_parameters)
),
"transliteration"= error_check(
post_endpoint(user_key, check_content_parameters(parameters), "transliteration", url, custom_headers, url_parameters)
),
"syntax/dependencies"= error_check(
post_endpoint(user_key, check_content_parameters(parameters), "syntax/dependencies", url, custom_headers, url_parameters)
),
Expand All @@ -87,7 +93,7 @@ api <- function(user_key, endpoint, parameters=FALSE, custom_headers=NULL, url_p
#' Provides the binding Version
#' @return current binding version
get_binding_version <- function() {
bindingVersion <- "1.5.0"
bindingVersion <- "1.7.0"
return(bindingVersion)
}

Expand Down Expand Up @@ -138,6 +144,19 @@ check_names <- function(parameters, endpoint) {
}
}

#' check if the required request parameters for name deduplication are correct
#' @param parameters - parameters list to be passed to specified Rosette API endpoint
#' @param endpoint - Rosette API endpoint to be utilized
#' @return Returns list of verified parameters to be sent to Rosette API
check_deduplication <- function(parameters, endpoint) {
params <- parameters
if (!("names" %in% names(params))) {
stop("Must supply a list of names to deduplicate")
} else {
return(parameters)
}
}

#' create a multipart
#' @param parameters - parameters list to be passed to specified Rosette API endpoint
#' @return Returns a multipart
Expand Down Expand Up @@ -225,6 +244,19 @@ serialize_name_parameters <- function(parameters) {
return(jsonlite::toJSON(serialized_params, auto_unbox = TRUE))
}

#' serialize Rosette API parameters
#' @param parameters - parameters list to be passed to name-deduplication
#' @return Returns the serialized parameters for the Rosette API
serialize_name_deduplication_parameters <- function(parameters) {
serialized_params <- list()
for (param in names(parameters)) {
if (param == "names" || param == "threshold") {
serialized_params[[param]] <- parameters[[param]]
}
}
return(jsonlite::toJSON(serialized_params, auto_unbox = TRUE))
}

#' Helper to check for file submission
#' @param parameters - JSON parameters
#' @return true if multipart
Expand All @@ -250,23 +282,25 @@ post_endpoint <- function(user_key, parameters, endpoint, url, custom_headers=NU
content_type <- "application/json"
if (endpoint == "name-translation" || endpoint == "name-similarity") {
request_body <- serialize_name_parameters(parameters)
} else if (endpoint == "name-deduplication") {
request_body <- serialize_name_deduplication_parameters(parameters)
} else {
request_body <- serialize_parameters(parameters)
}
}

if (is.null(url_parameters)) {
response <- httr::POST(
paste(url, endpoint, sep=""),
paste(url, endpoint, sep=""),
encode = encoding,
httr::add_headers(get_headers(user_key, content_type = content_type, custom_headers = custom_headers)),
httr::add_headers(get_headers(user_key, content_type = content_type, custom_headers = custom_headers)),
body = request_body
)
} else {
response <- httr::POST(
paste(url, endpoint, sep=""),
paste(url, endpoint, sep=""),
encode = encoding,
httr::add_headers(get_headers(user_key, content_type = content_type, custom_headers = custom_headers)),
httr::add_headers(get_headers(user_key, content_type = content_type, custom_headers = custom_headers)),
body = request_body,
query = url_parameters
)
Expand All @@ -284,12 +318,12 @@ post_endpoint <- function(user_key, parameters, endpoint, url, custom_headers=NU
get_endpoint <- function(user_key, endpoint, url, custom_headers=NULL, url_parameters=NULL) {
if (is.null(url_parameters)) {
response <- httr::GET(
paste(url, endpoint, sep=""),
paste(url, endpoint, sep=""),
httr::add_headers(get_headers(user_key, custom_headers))
)
} else {
response <- httr::GET(
paste(url, endpoint, sep=""),
paste(url, endpoint, sep=""),
httr::add_headers(get_headers(user_key, custom_headers)),
query = url_parameters
)
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@ print(jsonlite::toJSON(result$content, pretty = TRUE)
```
## API Parameters
| Parameter | Endpoint | Required
| ------------- |------------- |-------------
| content | categories, entities, language, morphology, relationships, sentences, sentiment, tokens, syntax_dependencies | Either content or contentUri required |
| contentUri | categories, entities, language, morphology, relationships, sentences, sentiment, tokens, syntax_dependencies | Either content or contentUri required |
| ------------- |------------- |-------------
| content | categories, entities, language, morphology, relationships, sentences, sentiment, tokens, syntax/dependencies, transliteration (Yes) | Either content or contentUri required |
| contentUri | categories, entities, language, morphology, relationships, sentences, sentiment, tokens, syntax/dependencies | Either content or contentUri required |
| language | categories, entities, language, morphology, relationships, sentences, sentiment, tokens, name similarity | No |
| documentFile | categories, entities, language, morphology, relationships, sentences, sentiment, tokens | No |
| name1 | name similarity | Yes |
| name2 | name similarity| Yes |
| name | name translation | Yes |
| targetLanguage | name translation | Yes |
| targetLanguage | name translation, transliteration (No) | Yes |
| entityType | name translation | No |
| sourceLanguageOfOrigin | name translation | No |
| sourceLanguageOfUse | name translation | No |
| sourceScript | name translation | No |
| targetScript | name translation | No |
| sourceScript | name translation, transliteration | No |
| sourceLanguage | transliteration | Yes |
| targetScript | name translation, transliteration | No |
| targetScheme | name translation | No |
| options | relationships | No |
| accuracyMode | relationships | Yes |
Expand Down
2 changes: 1 addition & 1 deletion examples/categories.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ parameters[[ "contentUri" ]] <- categories_url_data
if (is.na(opt$url)){
result <- api(opt$key, "categories", parameters)
} else {
result <- api(opt$key, "categories", parameters, NULL, opt$url)
result <- api(opt$key, "categories", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/entities.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ url_parameters <- list(output = "rosette")
if (is.na(opt$url)){
result <- api(opt$key, "entities", parameters, url_parameters = url_parameters)
} else {
result <- api(opt$key, "entities", parameters, NULL, opt$url)
result <- api(opt$key, "entities", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/info.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ opt = parse_args(opt_parser)
if (is.na(opt$url)){
result <- api(opt$key, "info")
} else {
result <- api(opt$key, "info", NULL, NULL, opt$url)
result <- api(opt$key, "info", FALSE, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/language.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ custom_headers <- c("X-RosetteAPI-App" = "r-application-test")
if (is.na(opt$url)){
result <- api(opt$key, "language", parameters = parameters, custom_headers = custom_headers)
} else {
result <- api(opt$key, "language", parameters = parameters, custom_headers = headers, url = opt$url)
result <- api(opt$key, "language", parameters = parameters, custom_headers = custom_headers, NULL, url = opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/morphology_complete.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ parameters[[ "morphology" ]] <- "complete"
if (is.na(opt$url)){
result <- api(opt$key, "morphology", parameters)
} else {
result <- api(opt$key, "morphology", parameters, NULL, opt$url)
result <- api(opt$key, "morphology", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/morphology_compound-components.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ parameters[[ "morphology" ]] <- "compound-components"
if (is.na(opt$url)){
result <- api(opt$key, "morphology", parameters)
} else {
result <- api(opt$key, "morphology", parameters, NULL, opt$url)
result <- api(opt$key, "morphology", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/morphology_han-readings.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ parameters[[ "morphology" ]] <- "han-readings"
if (is.na(opt$url)){
result <- api(opt$key, "morphology", parameters)
} else {
result <- api(opt$key, "morphology", parameters, NULL, opt$url)
result <- api(opt$key, "morphology", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/morphology_lemmas.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ parameters[[ "morphology" ]] <- "lemmas"
if (is.na(opt$url)){
result <- api(opt$key, "morphology", parameters)
} else {
result <- api(opt$key, "morphology", parameters, NULL, opt$url)
result <- api(opt$key, "morphology", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/morphology_parts-of-speech.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ parameters[[ "morphology" ]] <- "parts-of-speech"
if (is.na(opt$url)){
result <- api(opt$key, "morphology", parameters)
} else {
result <- api(opt$key, "morphology", parameters, NULL, opt$url)
result <- api(opt$key, "morphology", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
29 changes: 29 additions & 0 deletions examples/name_deduplication.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
source("../R/Api.R")
library(jsonlite)
library("optparse")

option_list = list( make_option(c("-k", "--key"), action="store", default=NA, type='character',
help="Rosette API key"), make_option(c("-u", "--url"), action="store", default=NA, type='character',
help="Rosette API url"))
opt_parser = OptionParser(option_list=option_list)
opt = parse_args(opt_parser)

name_dedupe_data <- "John Smith,Johnathon Smith,Fred Jones"


text <- unlist(strsplit(name_dedupe_data, ","))
targetLanguage <- rep("eng", length(text))
targetScript <- rep("Latn", length(text))

names = data.frame(text, targetLanguage, targetScript)
parameters <- list()
parameters[[ "names" ]] <- names
parameters[[ "threshold" ]] <- 0.75

if (is.na(opt$url)){
result <- api(opt$key, "name-deduplication", parameters)
} else {
result <- api(opt$key, "name-deduplication", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/name_similarity.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ parameters[[ "name2" ]] <- matched_name_data2
if (is.na(opt$url)){
result <- api(opt$key, "name-similarity", parameters)
} else {
result <- api(opt$key, "name-similarity", parameters, NULL, opt$url)
result <- api(opt$key, "name-similarity", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/name_translation.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ parameters[[ "targetScript" ]] <- "Latn"
if (is.na(opt$url)){
result <- api(opt$key, "name-translation", parameters)
} else {
result <- api(opt$key, "name-translation", parameters, NULL, opt$url)
result <- api(opt$key, "name-translation", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/ping.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ opt = parse_args(opt_parser)
if (is.na(opt$url)){
result <- api(opt$key, "ping")
} else {
result <- api(opt$key, "ping", NULL, NULL, opt$url)
result <- api(opt$key, "ping", FALSE, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/relationships.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ parameters[[ "content" ]] <- relationships_text_data
if (is.na(opt$url)){
result <- api(opt$key, "relationships", parameters)
} else {
result <- api(opt$key, "relationships", parameters, NULL, opt$url)
result <- api(opt$key, "relationships", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/sentences.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ parameters[[ "content" ]] <- sentences_data
if (is.na(opt$url)){
result <- api(opt$key, "sentences", parameters)
} else {
result <- api(opt$key, "sentences", parameters, NULL, opt$url)
result <- api(opt$key, "sentences", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/sentiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ parameters[[ "documentFile" ]] <- "true"
if (is.na(opt$url)){
result <- api(opt$key, "sentiment", parameters)
} else {
result <- api(opt$key, "sentiment", parameters, NULL, opt$url)
result <- api(opt$key, "sentiment", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
3 changes: 1 addition & 2 deletions examples/syntax_dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ syntax_dependencies_data <- "Yoshinori Ohsumi, a Japanese cell biologist, was aw

parameters <- list()
parameters[[ "content" ]] <- syntax_dependencies_data
parameters[[ "genre" ]] <- "social-media"

if (is.na(opt$url)){
result <- api(opt$key, "syntax/dependencies", parameters)
} else {
result <- api(opt$key, "syntax/dependencies", parameters, NULL, opt$url)
result <- api(opt$key, "syntax/dependencies", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/text_embedding.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ parameters[[ "content" ]] <- embeddings_data
if (is.na(opt$url)){
result <- api(opt$key, "text-embedding", parameters)
} else {
result <- api(opt$key, "text-embedding", parameters, NULL, opt$url)
result <- api(opt$key, "text-embedding", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
2 changes: 1 addition & 1 deletion examples/tokens.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ parameters[[ "content" ]] <- tokens_data
if (is.na(opt$url)){
result <- api(opt$key, "tokens", parameters)
} else {
result <- api(opt$key, "tokens", parameters, NULL, opt$url)
result <- api(opt$key, "tokens", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
22 changes: 22 additions & 0 deletions examples/transliteration.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
source("../R/Api.R")
library(jsonlite)
library("optparse")

option_list = list( make_option(c("-k", "--key"), action="store", default=NA, type='character',
help="Rosette API key"), make_option(c("-u", "--url"), action="store", default=NA, type='character',
help="Rosette API url"))
opt_parser = OptionParser(option_list=option_list)
opt = parse_args(opt_parser)

transliteration_data <- "معمر محمد أبو منيار القذاف"

parameters <- list()
parameters[[ "content" ]] <- transliteration_data

if (is.na(opt$url)){
result <- api(opt$key, "transliteration", parameters)
} else {
result <- api(opt$key, "transliteration", parameters, NULL, NULL, opt$url)
}
print(jsonlite::toJSON(result$header, pretty = TRUE))
print(jsonlite::toJSON(result$content, pretty = TRUE))
Loading

0 comments on commit 1b94b42

Please sign in to comment.