cloudyr / aws.comprehend

Compare 806fcbb ... +12 ... 5343eef

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.

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.

@@ -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
}

@@ -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
}

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Learn more Showing 4 files with coverage changes found.

New file R/bind_and_index.R
New
Loading file...
Changes in R/detect_entities.R
-1
+1
Loading file...
Changes in R/detect_sentiment.R
-1
+1
Loading file...
Changes in R/detect_medical_entities.R
-3
+3
Loading file...
Files Coverage
R +54.70% 54.70%
Project Totals (9 files) 54.70%
Loading