Repository navigation
Expand file tree
/
Copy pathextract.R
More file actions
546 lines (464 loc) · 15.3 KB
/
Copy pathextract.R
File metadata and controls
546 lines (464 loc) · 15.3 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#' Get text
#'
#' @description Extracts the text OCRed by Document AI (DAI)
#'
#' @param object either a HTTP response object from
#' \code{dai_sync()} or the path to a JSON file from
#' \code{dai_async()}.
#' @param type one of "sync" or "async", depending on
#' the function used to process the original document.
#' @param save_to_file boolean; whether to save the text as a .txt file
#' @param dest_dir folder path for the .txt output file if \code{save_to_file = TRUE}
#' @param outfile_stem string to form the stem of the
#' .txt output file
#' @return a string (if \code{save_to_file = FALSE})
#' @export
#'
#' @examples
#' \dontrun{
#' text <- get_text(dai_sync("file.pdf"))
#'
#' text <- get_text("file.json", type = "async", save_to_file = TRUE)
#' }
get_text <- function(object,
type = "sync",
save_to_file = FALSE,
dest_dir = getwd(),
outfile_stem = NULL) {
# checks
if (!(length(type) == 1) || !(type %in% c("sync", "async"))) {
stop("Invalid type parameter.")
}
if (!(is.logical(save_to_file) && length(save_to_file) == 1 && !is.na(save_to_file))) {
stop("Invalid save_to_file argument. Must be either TRUE or FALSE.")
}
if (!(length(dest_dir) == 1) || !(is.character(dest_dir))) {
stop("Invalid dest_dir argument. Must be a valid folder path.")
}
if (!(length(outfile_stem) <= 1)) {
stop("Invalid outfile_stem argument. Must be NULL or a string.")
}
if ((length(outfile_stem) == 1) && !(is.character(outfile_stem))) {
stop("Invalid outfile_stem argument. Must be NULL or a string.")
}
dest_dir <- normalizePath(dest_dir, winslash = "/")
if (type == "sync") {
if (!(inherits(object, "response"))) {
stop("Invalid object: not a valid HTTP response. Did you supply a JSON filepath without type = 'async'?")
}
parsed <- httr::content(object, as = "parsed")
if (!("pages" %in% names(parsed$document) || "pages" %in% names(parsed))) {
stop("Input not recognized. Is it from dai_async?")
}
if (!("text" %in% names(parsed$document) || "text" %in% names(parsed))) {
warning("DAI found no text. The document may be blank.")
text <- ""
}
# get text
if ("text" %in% names(parsed$document)) {
text <- parsed$document$text
} else {
text <- parsed$text
}
} else if (type == "async") {
if (!(is.character(object) && length(object) == 1)) {
stop("Invalid object: must be a single character string filepath.")
}
if (!(is_json(object))) {
stop("Invalid object: file is not a .json file or does not exist. Is the file in your working directory?")
}
parsed <- jsonlite::fromJSON(object)
if (!("pages" %in% names(parsed))) {
stop("JSON not in right format. Is it from DAI?")
}
if (!("text" %in% names(parsed))) {
warning("DAI found no text. The document may be blank.")
text <- ""
}
if ("text" %in% names(parsed)) {
text <- parsed$text
}
}
# save to file if requested
if (isTRUE(save_to_file) && !(is.null(outfile_stem))) {
write(text, file.path(dest_dir, paste0(outfile_stem, ".txt")))
} else if (isTRUE(save_to_file) && (is.null(outfile_stem))) {
if (type == "sync") {
stem <- "output"
} else {
stem <- gsub("\\.json", "", basename(object))
}
write(text, file.path(dest_dir, paste0(stem, ".txt")))
}
text
}
#' Get tables
#'
#' @description Extracts tables identified by a Document AI
#' form parser processor.
#' @param object either a HTTP response object from
#' \code{dai_sync()} or the path to a JSON file from
#' \code{dai_async()}.
#' @param type one of "sync" or "async", depending on
#' the function used to process the original document.
#' @return a list of data frames
#' @export
#'
#' @examples
#' \dontrun{
#'
#' tables <- get_tables(dai_sync("file.pdf"))
#'
#' tables <- get_tables("file.json", type = "async")
#' }
get_tables <- function(
object,
type = "sync"
) {
if (!(length(type) == 1) || !(type %in% c("sync", "async"))) {
stop("Invalid type parameter.")
}
if (type == "sync") {
# checks
if (!(inherits(object, "response"))) {
stop("Invalid object: not a valid HTTP response. Did you supply a JSON filepath without type = 'async'?")
}
parsed <- httr::content(object, as = "parsed")
if (!("pages" %in% names(parsed) || "pages" %in% names(parsed$document))) {
stop("The supplied object is not from a successful HTTP request.")
}
if (!("text" %in% names(parsed) || "text" %in% names(parsed$document))) {
stop("DAI found no text. Was the page blank?")
}
# compile list of table entries
if ("pages" %in% names(parsed$document)) {
table_list_raw <- purrr::map(parsed$document$pages, ~ .x$tables)
} else {
table_list_raw <- purrr::map(parsed$pages, ~ .x$tables)
}
if (all(sapply(table_list_raw, is.null))) {
stop("DAI found no tables in the document.")
}
table_list <- purrr::flatten(table_list_raw)
text <- get_text(object)
purrr::map(table_list, ~ resp_build_table(.x, text))
} else if (type == "async") {
# checks
if (!(is.character(object) && length(object) == 1)) {
stop("Invalid object: must be a single character string filepath.")
}
if (!(is_json(object))) {
stop("Invalid object: file is not a .json file or does not exist. Is the file in your working directory?")
}
parsed <- jsonlite::fromJSON(object)
if (!("pages" %in% names(parsed))) {
stop("JSON not in right format. Is it from DAI?")
}
if (!("text" %in% names(parsed))) {
stop("DAI found no text. Was the document blank?")
}
if (!("tables" %in% names(parsed$pages))) {
stop("DAI found no tables in the document.")
}
table_list_raw <- parsed$pages$tables
table_list_by_page <- purrr::map(table_list_raw, file_get_table_objects)
table_list <- purrr::flatten(table_list_by_page)
text <- get_text(object, type = "async")
purrr::map(table_list, ~ file_build_table(.x, text))
}
}
#' Get entities
#'
#' @description Extracts entities Document AI (DAI) identified by a Document AI
#' form parser processor.
#'
#' @param object either a HTTP response object from
#' \code{dai_sync()} or the path to a JSON file from
#' \code{dai_async()}.
#' @param type one of "sync" or "async", depending on
#' the function used to process the original document.
#' @return a list of dataframes, one per page
#' @export
#'
#' @examples
#' \dontrun{
#' entities <- get_entities(dai_sync("file.pdf"))
#'
#' entities <- get_entities("file.json", type = "async")
#' }
get_entities <- function(
object,
type = "sync"
) {
if (!(length(type) == 1) || !(type %in% c("sync", "async"))) {
stop("Invalid type parameter.")
}
if (type == "sync") {
# checks
if (!(inherits(object, "response"))) {
stop("Invalid object: not a valid HTTP response. Did you supply a JSON filepath without type = 'async'?")
}
parsed <- httr::content(object, as = "parsed")
if (!("pages" %in% names(parsed) || "pages" %in% names(parsed$document))) {
stop("The supplied object is not from a successful HTTP request.")
}
if (!("text" %in% names(parsed) || "text" %in% names(parsed$document))) {
stop("DAI found no text. Was the page blank?")
}
parsed <- httr::content(object)
if (!("entities" %in% names(parsed$document))) {
message("Document AI identified no entities in the document.")
return(NULL)
} else {
entity_pages <- parsed$document$entities
purrr::map(entity_pages, build_sync_entity_df)
}
} else if (type == "async") {
# checks
if (!(is.character(object) && length(object) == 1)) {
stop("Invalid object: must be a single character string filepath.")
}
if (!(is_json(object))) {
stop("Invalid object: file is not a .json file or does not exist. Is the file in your working directory?")
}
parsed <- jsonlite::fromJSON(object)
if (!("pages" %in% names(parsed))) {
stop("JSON not in right format. Is it from DAI?")
}
if (!("text" %in% names(parsed))) {
stop("DAI found no text. Was the document blank?")
}
if (!("entities" %in% names(parsed))) {
message("Document AI identified no entities in the document.")
return(NULL)
} else {
parsed <- jsonlite::fromJSON(object)
entity_pages <- parsed$entities$properties
purrr::map(entity_pages, build_async_entity_df)
}
}
}
#' Get cell text from response
#'
#' @description Helper function to get the text of an individual cell
#' @param cell a list from a parsed Document AI response object
#' @param text a string
#'
#' @noRd
resp_get_cell_text <- function(
cell,
text
) {
anchors <- cell$layout$textAnchor
if (length(anchors) == 0) {
txt <- ""
} else {
indices <- cell$layout$textAnchor$textSegments
txt <- character()
for (i in indices) {
if (is.null(i$startIndex)) {
line_start <- 1
} else {
line_start <- i$startIndex
}
line_end <- i$endIndex
line_txt <- substr(text, line_start, line_end)
txt <- paste(txt, line_txt, sep = "\n")
}
}
txt
}
#' Compile cells into row from response
#'
#' @description Helper function to compile cell entries into a row vector
#' @param elem a list from a parsed Document AI response object
#' @param text a string
#'
#' @noRd
resp_get_row_vector <- function(
elem,
text
) {
cells <- elem$cells
purrr::map_chr(cells, ~ resp_get_cell_text(.x, text))
}
#' Build table from rows in response
#'
#' @description Helper function to build a table from row vectors
#' @param table a list from a parsed Document AI response object
#' @param text a string
#'
#' @noRd
resp_build_table <- function(
table,
text
) {
headers_list <- table$headerRows
rows_list <- table$bodyRows
headervectors <- purrr::map(headers_list, ~ resp_get_row_vector(.x, text))
rowvectors <- purrr::map(rows_list, ~ resp_get_row_vector(.x, text))
table <- data.frame(matrix(nrow = 0, ncol = 6))
for (i in rowvectors) {
table <- rbind(table, as.data.frame(t(i)))
}
stats::setNames(table, headervectors[[1]])
}
#' Get table objects from file
#'
#' @description Helper function to extract and reorganize
#' table-related elements from a parsed JSON file
#' @param page a list from a parsed JSON file from Document AI
#' @param text a string
#'
#' @noRd
file_get_table_objects <- function(page) {
if (is.null(page)) {
return(NULL)
} else {
pagewise_list_of_header_objs <- page$headerRows
pagewise_list_of_row_objs <- page$bodyRows
table_objects <- list()
for (i in seq_along(pagewise_list_of_header_objs)) {
table_object <- list(list(
headerRows = pagewise_list_of_header_objs[[i]],
bodyRows = pagewise_list_of_row_objs[[i]]
))
table_objects <- append(table_objects, table_object)
}
table_objects
}
}
#' Get cell text from file
#'
#' @description Helper function to get the text of an individual cell
#' @param cell a list from a parsed JSON file from Document AI
#' @param text a string
#'
#' @noRd
file_get_cell_text <- function(
cell,
text
) {
if (is.null(cell)) {
txt <- ""
} else {
txt <- character()
for (i in seq_len(nrow(cell))) {
if (is.null(cell[i, "startIndex"])) {
line_start <- 1
} else {
line_start <- cell[i, "startIndex"]
}
line_end <- cell[i, "endIndex"]
line_txt <- substr(text, line_start, line_end)
txt <- paste(txt, line_txt, sep = "\n")
}
}
txt
}
#' Compile cells into rows from file
#'
#' @description Helper function to compile cell entries into a row vector
#' @param elem a list from a parsed JSON file from Document AI
#' @param text a string
#'
#' @noRd
file_get_row_vector <- function(
elem,
text
) {
cells <- elem$layout$textAnchor$textSegments
purrr::map_chr(cells, ~ file_get_cell_text(.x, text))
}
#' Build table from rows from file
#'
#' @description Helper function to build a table from row vectors
#' @param table_object a list from a parsed JSON file from Document AI
#' @param text a string
#'
#' @noRd
file_build_table <- function(
table_object,
text
) {
headers_list <- table_object$headerRows$cells
rows_list <- table_object$bodyRows$cells
headervectors <- purrr::map(headers_list, ~ file_get_row_vector(.x, text))
rowvectors <- purrr::map(rows_list, ~ file_get_row_vector(.x, text))
table <- data.frame(matrix(nrow = 0, ncol = 6))
if (length(rowvectors) == 0) {
table <- as.data.frame(t(headervectors[[1]]))
} else {
for (i in seq_along(rowvectors)) {
if (is.null(rowvectors[[i]])) {
if (i == 1) {
table_width <- length(rowvectors[[i + 1]])
} else {
table_width <- length(rowvectors[[i - 1]])
}
rowvectors[[i]] <- rep("", times = table_width)
table <- rbind(table, as.data.frame(t(rowvectors[[i]])))
} else {
table <- rbind(table, as.data.frame(t(rowvectors[[i]])))
}
}
table <- stats::setNames(table, headervectors[[1]])
}
table
}
#' Build dataframe of entities from response
#'
#' @description Helper function to build dataframe of entities
#' @param lst a list from a parsed HTTP object from Document AI
#'
#' @noRd
build_sync_entity_df <- function(lst) {
props <- lst$properties
ids <- as.numeric(unlist(purrr::map(props, ~ .x$id)))
mentionTexts <- purrr::map_chr(props, ~ .x$mentionText)
types <- purrr::map_chr(props, ~ .x$type)
confs <- as.numeric(unlist(purrr::map(props, ~ .x$confidence)))
start_inds <- as.numeric(unlist(purrr::map(props, ~ .x$textAnchor$textSegments[[1]]$startIndex)))
end_inds <- as.numeric(unlist(purrr::map(props, ~ .x$textAnchor$textSegments[[1]]$endIndex)))
lefts <- as.numeric(unlist(purrr::map(props, ~ .x$pageAnchor$pageRefs[[2]][[1]][[1]][[1]]$x)))
rights <- as.numeric(unlist(purrr::map(props, ~ .x$pageAnchor$pageRefs[[2]][[1]][[1]][[2]]$x)))
tops <- as.numeric(unlist(purrr::map(props, ~ .x$pageAnchor$pageRefs[[2]][[1]][[1]][[1]]$y)))
bottoms <- as.numeric(unlist(purrr::map(props, ~ .x$pageAnchor$pageRefs[[2]][[1]][[1]][[4]]$y)))
data.frame(
id = ids,
mentionText = mentionTexts,
type = types,
confidence = confs,
start_ind = start_inds,
end_ind = end_inds,
left = lefts,
right = rights,
top = tops,
bottom = bottoms
)
}
#' Build dataframe of entities from file
#'
#' @description Helper function to build dataframe of entities
#' @param lst a list from a parsed JSON file from Document AI
#'
#' @noRd
build_async_entity_df <- function(x) {
anchors <- x$pageAnchor$pageRefs
lefts <- as.numeric(unlist(purrr::map(anchors, ~ .x$boundingPoly$normalizedVertices[[2]]$x[1])))
rights <- as.numeric(unlist(purrr::map(anchors, ~ .x$boundingPoly$normalizedVertices[[2]]$x[2])))
tops <- as.numeric(unlist(purrr::map(anchors, ~ .x$boundingPoly$normalizedVertices[[2]]$y[1])))
bottoms <- as.numeric(unlist(purrr::map(anchors, ~ .x$boundingPoly$normalizedVertices[[2]]$y[3])))
data.frame(
id = as.numeric(x$id),
mentionText = x$mentionText,
type = x$type,
confidence = as.numeric(x$confidence),
start_ind = as.numeric(unlist(purrr::map(x$textAnchor$textSegments, ~ .x$startIndex))),
end_ind = as.numeric(unlist(purrr::map(x$textAnchor$textSegments, ~ .x$endIndex))),
left = lefts,
right = rights,
top = tops,
bottom = bottoms
)
}