cloudyr / aws.comprehend
Showing 8 of 42 files from the diff.
Newly tracked file
R/bind_and_index.R created.
Other files ignored by Codecov
tests/testthat.R has changed.
DESCRIPTION has changed.
NEWS.md has changed.

@@ -0,0 +1,42 @@
Loading
1 +
#' Bind and index a ResultList
2 +
#'
3 +
#' Turn a list of data.frames (of different lengths and potentially empty)
4 +
#' into a single indexed data.frame. Useful to process a ResultList from `comprehendHTTP`.
5 +
#'
6 +
#' @param index Vector of indices
7 +
#' @param df_list List of data.frames to bind and index. Should NOT be a data.frame.
8 +
#'
9 +
#' @details `index` and `df_list` should be the same length. An error is raised otherwise.
10 +
#'
11 +
#' bind_and_index(1:2, list(data.frame(col = "a"), data.frame(col = "b")))
12 +
#'
13 +
#' bind_and_index(1:3, list(
14 +
#'      data.frame(col = "a"),
15 +
#'      data.frame(),
16 +
#'      data.frame(c("b", "c"))))
17 +
#'
18 +
bind_and_index <- function(index, df_list) {
19 +
20 +
  if (length(index) != length(df_list)) {
21 +
    stop("Index and df_list must be the same length.")
22 +
  }
23 +
24 +
  indexed <- mapply(function(i, df) {
25 +
    if (nrow(df) > 0) {
26 +
      # Flatten and add Index as first col
27 +
      cbind(Index = i, flatten(df))
28 +
    } else {
29 +
      df
30 +
    }
31 +
  }, index, df_list, SIMPLIFY = FALSE)
32 +
33 +
  do.call(rbind, indexed)
34 +
}
35 +
36 +
37 +
#' Flatten embedded data.frames (1 level max)
38 +
#' @param df data.frame to flatten
39 +
#'
40 +
flatten <- function(df) {
41 +
  as.data.frame(as.list(df), stringsAsFactors = FALSE)
42 +
}

@@ -25,14 +25,11 @@
Loading
25 25
    if (length(text) > 1L) {
26 26
        bod <- list(TextList = text)
27 27
        out <- comprehendHTTP(action = "BatchDetectDominantLanguage", body = bod, ...)
28 -
        
29 -
        # build response data frame
30 -
        x <- out$ResultList
31 -
        x <- cbind(Index = x$Index, do.call("rbind", x$Languages))
28 +
        x <- bind_and_index(out$ResultList$Index, out$ResultList$Languages)
32 29
        return(structure(x, ErrorList = out$ErrorList))
33 30
    } else {
34 31
        bod <- list(Text = text)
35 32
        out <- comprehendHTTP(action = "DetectDominantLanguage", body = bod, ...)
36 -
        return(out$Languages)
33 +
        return(cbind(Index = 0, out$Languages))
37 34
    }
38 35
}

@@ -22,5 +22,5 @@
Loading
22 22
                      "2" = "DetectEntitiesV2")
23 23
  bod <- list(Text = text, LanguageCode = language)
24 24
  out <- comprehendHTTP(action = operation, body = bod, service = "comprehendmedical", ...)
25 -
  return(cbind(Index = 1, out$Entities))
25 +
  return(cbind(Index = 0, out$Entities))
26 26
}

@@ -8,7 +8,7 @@
Loading
8 8
#' \dontrun{
9 9
#'   # simple example
10 10
#'   detect_sentiment("I have never been happier. This is the best day ever.")
11 -
#'   
11 +
#'
12 12
#'   txt <-c("I have never been happier. This is the best day ever.",
13 13
#'           "I have always been happier. This is the worst day ever.")
14 14
#'   detect_sentiment(txt)
@@ -24,12 +24,16 @@
Loading
24 24
        bod <- list(TextList = text, LanguageCode = language)
25 25
        out <- comprehendHTTP(action = "BatchDetectSentiment", body = bod, ...)
26 26
        # build response data frame
27 -
        x <- out$ResultList
28 -
        x <- cbind(x[c("Index", "Sentiment")], x[["SentimentScore"]])
29 -
        return(structure(x, ErrorList = out$ErrorList))
27 +
        formatted <- cbind(out$ResultList[c("Index", "Sentiment")],
28 +
                           out$ResultList[["SentimentScore"]])
29 +
        return(structure(formatted, ErrorList = out$ErrorList))
30 30
    } else {
31 31
        bod <- list(Text = text, LanguageCode = language)
32 32
        out <- comprehendHTTP(action = "DetectSentiment", body = bod, ...)
33 -
        return(cbind(Index = 1, as.data.frame(c(list(Sentiment = out$Sentiment), out$SentimentScore))))
33 +
        formatted <- cbind(Index = 0,
34 +
                           Sentiment = out$Sentiment,
35 +
                           flatten(out$SentimentScore),
36 +
                           stringsAsFactors = FALSE)
37 +
        return(formatted)
34 38
    }
35 39
}

@@ -23,13 +23,11 @@
Loading
23 23
    if (length(text) > 1L) {
24 24
        bod <- list(TextList = text, LanguageCode = language)
25 25
        out <- comprehendHTTP(action = "BatchDetectKeyPhrases", body = bod, ...)
26 -
        # build response data frame
27 -
        x <- out$ResultList
28 -
        x <- cbind(Index = x$Index, do.call("rbind", x$KeyPhrases))
26 +
        x <- bind_and_index(out$ResultList$Index, out$ResultList$KeyPhrases)
29 27
        return(structure(x, ErrorList = out$ErrorList))
30 28
    } else {
31 29
        bod <- list(Text = text, LanguageCode = language)
32 30
        out <- comprehendHTTP(action = "DetectKeyPhrases", body = bod, ...)
33 -
        return(cbind(Index = 1, out$KeyPhrases))
31 +
        return(cbind(Index = 0, out$KeyPhrases))
34 32
    }
35 33
}

@@ -24,12 +24,11 @@
Loading
24 24
        bod <- list(TextList = text, LanguageCode = language)
25 25
        out <- comprehendHTTP(action = "BatchDetectEntities", body = bod, ...)
26 26
        # build response data frame
27 -
        x <- out$ResultList
28 -
        x <- cbind(Index = rep(x$Index, unlist(lapply(x$Entities, nrow))), do.call("rbind", x$Entities))
27 +
        x <- bind_and_index(out$ResultList$Index, out$ResultList$Entities)
29 28
        return(structure(x, ErrorList = out$ErrorList))
30 29
    } else {
31 30
        bod <- list(Text = text, LanguageCode = language)
32 31
        out <- comprehendHTTP(action = "DetectEntities", body = bod, ...)
33 -
        return(cbind(Index = 1, out$Entities))
32 +
        return(cbind(Index = 0, out$Entities))
34 33
    }
35 34
}

@@ -23,21 +23,13 @@
Loading
23 23
    if (length(text) > 1L) {
24 24
      bod <- list(TextList = text, LanguageCode = language)
25 25
      out <- comprehendHTTP(action = "BatchDetectSyntax", body = bod, ...)
26 -
      # build response data frame
27 -
      res_list <- mapply(out$ResultList$Index, out$ResultList$SyntaxTokens,
28 -
                  FUN = function(index, df) {
29 -
                    # fixing nested 'PartOfSpeech' df
30 -
                    df <- as.data.frame(as.list(df))
31 -
                    # rep handles case where nrow(df) is 0
32 -
                    cbind(Index = rep(index, nrow(df)), df)
33 -
                  }, SIMPLIFY = FALSE, USE.NAMES = FALSE)
34 -
      res <- do.call(rbind, res_list)
35 -
      return(structure(res, ErrorList = out$ErrorList))
26 +
      x <- bind_and_index(out$ResultList$Index, out$ResultList$SyntaxTokens)
27 +
      return(structure(x, ErrorList = out$ErrorList))
36 28
    } else {
37 29
      bod <- list(Text = text, LanguageCode = language)
38 30
      out <- comprehendHTTP(action = "DetectSyntax", body = bod, ...)
39 -
      # fixing nested 'PartOfSpeech' df
40 -
      x <- as.data.frame(as.list(out$SyntaxTokens))
41 -
      return(cbind(Index = 1, x))
31 +
      # Fix nested 'PartOfSpeech' df. `flatten` is defined in bind_and_index.R.
32 +
      x <- flatten(out$SyntaxTokens)
33 +
      return(cbind(Index = 0, x))
42 34
    }
43 35
  }

@@ -22,5 +22,5 @@
Loading
22 22
) {
23 23
    bod <- list(Text = text, LanguageCode = language)
24 24
    out <- comprehendHTTP(action = "DetectPHI", body = bod, service = "comprehendmedical", ...)
25 -
    return(cbind(Index = 1, out$Entities))
25 +
    return(cbind(Index = 0, out$Entities))
26 26
}
Files Coverage
R 54.70%
Project Totals (9 files) 54.70%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading