/
decode.R
668 lines (591 loc) · 22.9 KB
/
decode.R
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#' @name decode-method
#' @aliases decode
#' @rdname decode
setGeneric("decode", function(.Object, ...) standardGeneric("decode"))
setOldClass("Annotation")
setOldClass("AnnotatedPlainTextDocument")
setAs(from = "corpus", to = "Annotation", def = function(from){
# Implemented only for class 'corpus', 'subcorpus'-class will inherit from it.
if (!requireNamespace(package = "NLP", quietly = TRUE))
stop("Package 'NLP' required but not available")
word <- get_token_stream(
from@cpos,
corpus = from@corpus,
p_attribute = "word",
encoding = from@encoding
)
if ("pos" %in% p_attributes(from)){
cli_alert_info("using p_attribute 'pos' for detecting interpunctation")
# this is not robust if we have a subcorpus with one token only
pos <- get_token_stream(
from@cpos,
corpus = from@corpus,
p_attribute = "pos",
encoding = from@encoding
)
ws_after <- c(ifelse(pos %in% c("$.", "$,"), FALSE, TRUE)[-1], FALSE)
breaks <- unique(c(1L, grep("\\$\\.", pos), length(pos)))
} else {
cli_alert_info("using interpunctuation for sentence segmentation")
ws_after <- if (length(word) > 1L){
c(!grepl("^[\\.,;:\\!\\?]$", word)[2L:length(word)], FALSE)
} else {
FALSE
}
breaks <- unique(c(1L, grep("^[\\.:\\!\\?]$", word), length(word)))
}
word_with_ws <- paste(word, ifelse(ws_after, " ", ""), sep = "")
s <- paste(word_with_ws, collapse = "")
word_length <- sapply(word, nchar)
left_offset <- c(
1L,
(cumsum(sapply(word_with_ws, nchar)) + 1L)[1L:(length(word) - 1L)]
)
names(left_offset) <- word
right_offset <- left_offset + word_length - 1L
names(right_offset) <- word
cpos <- ranges_to_cpos(from@cpos)
w <- NLP::Annotation(
id = cpos,
rep.int("word", length(cpos)),
start = left_offset,
end = right_offset
)
right_offset <- left_offset + word_length
names(right_offset) <- word # repeats
m <- matrix(data = c(left_offset, right_offset), ncol = 2, byrow = FALSE)
f <- cut(x = 1L:length(word), breaks = breaks, include.lowest = TRUE)
chunks <- split(x = m, f = f)
sentence_left <- sapply(chunks, min)
sentence_right <- sapply(chunks, max) - 1L
s <- NLP::Annotation(
id = 1L:length(sentence_left),
rep.int("sentence", length(sentence_left)),
start = sentence_left,
end = sentence_right
)
c(w, s)
})
#' @importFrom RcppCWB region_matrix_to_struc_matrix
as.AnnotatedPlainTextDocument <- function(x, p_attributes = NULL, s_attributes = NULL, mw = NULL, stoplist = NULL, verbose = TRUE){
# is required to be a subcorpus object
stopifnot(
inherits(x, "subcorpus"),
length(mw) == length(unique(mw))
)
if (!requireNamespace(package = "NLP", quietly = TRUE))
stop("Package 'NLP' required but not available.")
if (verbose) cli_alert_info("decode p-attributes")
p_attrs <- if (is.null(p_attributes)) p_attributes(x) else p_attributes
ts <- decode(
x,
to = "data.table",
p_attribute = p_attrs,
s_attributes = character(),
verbose = verbose
)
if (!is.null(stoplist)) ts <- ts[!ts[["word"]] %in% stoplist]
if (verbose) cli_progress_step("generate plain text string")
ws_after <- if (nrow(ts) > 1L){
c(!grepl("^[.,;:!?]$", ts[["word"]])[2L:nrow(ts)], FALSE)
} else {
FALSE
}
word_with_ws <- paste(ts[["word"]], ifelse(ws_after, " ", ""), sep = "")
s <- stringi::stri_c(word_with_ws, collapse = "")
if (verbose) cli_progress_step("generate token-level annotation")
left_offset <- if (nrow(ts) > 1L){
c(1L, (cumsum(nchar(word_with_ws)) + 1L)[1L:(nrow(ts) - 1L)])
} else {
1L
}
right_offset <- left_offset + nchar(ts[["word"]]) - 1L
w <- NLP::Annotation(
id = ts[["cpos"]],
rep.int("word", nrow(ts)),
start = left_offset,
end = right_offset,
features = lapply(
split(ts[, p_attrs, with = FALSE], f = 1L:nrow(ts)),
as.data.frame
)
)
mw_annotations <- lapply(
mw,
function(s_attr){
if (verbose)
cli_progress_step(
"generate annotation for s-attribute {.val {s_attr}}"
)
struc_matrix <- RcppCWB::region_matrix_to_struc_matrix(
corpus = x@corpus,
registry = x@registry_dir,
region_matrix = x@cpos,
s_attribute = s_attr
)
# missing regions for s_attr within regions result in NAs
drop <- which(apply(struc_matrix, 1, function(row) any(is.na(row))))
if (length(drop) > 0L) struc_matrix <- struc_matrix[-drop,]
if (nrow(struc_matrix) == 0L) return(NULL)
strucs <- ranges_to_cpos(struc_matrix)
strucs_min <- unique(strucs[strucs >= 0L])
if (length(strucs_min) == 0L) return(NULL)
regions <- get_region_matrix(
corpus = x@corpus,
s_attribute = s_attr,
strucs = strucs_min,
registry = x@registry_dir
)
str <- RcppCWB::cl_struc2str(
corpus = x@corpus,
registry = x@registry_dir,
s_attribute = s_attr,
struc = strucs_min
)
Encoding(str) <- x@encoding
str <- as.nativeEnc(x = str, from = x@encoding)
data <- lapply(
1:nrow(regions),
function(i){
matching <- which(w$id %in% regions[i,1]:regions[i,2])
# If the region covers tokens that have been dropped due to the
# stoplist, no matches - omit these cases.
if (length(matching) > 0L){
txt <- stri_sub(
str = s,
from = w[min(matching)]$start,
to = w[max(matching)]$end
)
list(
kind = str[i],
text = txt,
constituents = regions[i,1]:regions[i,2],
start = w[min(matching)]$start,
end = w[max(matching)]$end
)
} else {
NULL
}
}
)
nomatch <- sapply(data, length) == 0
if (any(nomatch)) data <- data[-which(nomatch)]
NLP::Annotation(
id = seq_along(data),
type = rep.int(
if (is.null(names(mw))) s_attr else names(mw)[which(mw == s_attr)],
length(data)
),
start = unname(unlist(lapply(data, `[[`, "start"))),
end = unname(unlist(lapply(data, `[[`, "end"))),
features = lapply(data, `[`, c("kind", "text", "constituents"))
)
}
)
if (verbose) cli_progress_step("retrieve metadata")
meta <- lapply(
setNames(s_attributes, s_attributes),
function(s_attr){
struc <- cl_cpos2struc(
corpus = x@corpus,
registry = x@registry_dir,
s_attribute = s_attr,
cpos = x@cpos[1,1]
)
value <- cl_struc2str(
corpus = x@corpus,
registry = x@registry_dir,
s_attribute = s_attr,
struc = struc
)
Encoding(value) <- x@encoding
as.nativeEnc(x = value, from = x@encoding)
}
)
if (verbose) cli_progress_step("instantiate AnnotatedPlainTextDocument")
NLP::AnnotatedPlainTextDocument(
s = s,
a = c(w, do.call(c, mw_annotations)),
meta = meta
)
}
#' Decode corpus or subcorpus.
#'
#' Decode `corpus` or `subcorpus` and return class specified by argument `to`.
#'
#' The primary purpose of the method is type conversion. By obtaining the corpus
#' or subcorpus in the format specified by the argument `to`, the data can be
#' processed with tools that do not rely on the Corpus Workbench (CWB).
#' Supported output formats are `data.table` (which can be converted to a
#' `data.frame` or `tibble` easily) or an `Annotation` object as defined in the
#' package 'NLP'. Another purpose of decoding the corpus can be to rework it,
#' and to re-import it into the CWB (e.g. using the 'cwbtools'-package).
#'
#' An earlier version of the method included an option to decode a single
#' s-attribute, which is not supported any more. See the `s_attribute_decode()`
#' function of the package RcppCWB.
#'
#' @return The return value will correspond to the class specified by argument
#' `to`.
#'
#' @param .Object The `corpus` or `subcorpus` to decode.
#' @param to The class of the returned object, stated as a length-one
#' `character` vector.
#' @param s_attributes The structural attributes to decode. If `NULL` (default),
#' all structural attributes will be decoded.
#' @param p_attributes The positional attributes to decode. If `NULL` (default),
#' all positional attributes will be decoded.
#' @param stoplist A `character` vector with terms to be dropped.
#' @param decode A `logical` value, whether to decode token ids and struc ids to
#' character strings. If `FALSE`, the values of columns for p- and
#' s-attributes will be `integer` vectors. If `TRUE` (default), the respective
#' columns are `character` vectors.
#' @param mw A `character` vector with s-attributes with multiword expressions.
#' Used only if `to` is 'AnnotatedPlainTextDocument'.
#' @param ... Further arguments.
#' @param verbose A `logical` value, whether to output progess messages.
#' @exportMethod decode
#' @importFrom RcppCWB get_region_matrix
#' @seealso To decode a structural attribute, you can use the
#' \code{\link{s_attributes}}-method, setting argument `unique` as `FALSE` and
#' \code{\link[RcppCWB]{s_attribute_decode}}. See \code{\link{as.VCorpus}} to
#' decode a `partition_bundle` object, returning a `VCorpus` object.
#' @examples
#' use("polmineR")
#' use(pkg = "RcppCWB", corpus = "REUTERS")
#'
#' # Decode corpus as data.table
#' dt <- decode("REUTERS", to = "data.table")
#'
#' # Decode corpus selectively
#' dt <- decode(
#' "REUTERS",
#' to = "data.table",
#' p_attributes = "word",
#' s_attributes = "id"
#' )
#'
#' # Decode a subcorpus
#' dt <- corpus("REUTERS") %>%
#' subset(id %in% c("127", "144")) %>%
#' decode(s_attributes = "id", to = "data.table")
#'
#' # Decode partition
#' dt <- partition("REUTERS", places = "kuwait", regex = TRUE) %>%
#' decode(to = "data.table")
#'
#' # Previous versions of polmineR offered an option to decode a single
#' # s-attribute. This is how you could proceed to get a table with metadata.
#' dt <- partition("REUTERS", places = "kuwait", regex = TRUE) %>%
#' decode(s_attribute = "id", decode = FALSE, to = "data.table")
#' dt[, "word" := NULL]
#' dt[,{list(cpos_left = min(.SD[["cpos"]]), cpos_right = max(.SD[["cpos"]]))}, by = "id"]
#'
#' # Decode subcorpus as Annotation object
#' library(NLP)
#' p <- corpus("GERMAPARLMINI") %>%
#' subset(date == "2009-11-10" & speaker == "Angela Dorothea Merkel")
#'
#' s <- as(p, "String")
#' a <- as(p, "Annotation")
#'
#' # The beauty of having this NLP Annotation object is that you can now use
#' # the different annotators of the openNLP package. Here, just a short scenario
#' # how you can have a look at the tokenized words and the sentences.
#'
#' words <- s[a[a$type == "word"]]
#' sentences <- s[a[a$type == "sentence"]] # does not yet work perfectly for plenary protocols
#'
#' doc <- decode(p, to = "AnnotatedPlainTextDocument")
#' @rdname decode
#' @importFrom cli cli_progress_step
setMethod("decode", "corpus", function(.Object, to = c("data.table", "Annotation", "AnnotatedPlainTextDocument"), p_attributes = NULL, s_attributes = NULL, mw = NULL, stoplist = NULL, decode = TRUE, verbose = TRUE){
if (length(to) != 1L)
stop("`decode()` - argument 'to' required to have length 1")
if (!to %in% c("data.table", "Annotation", "AnnotatedPlainTextDocument")){
cli_alert_danger(
"Argument 'to' of `decode()` required to be 'data.table' or 'Annotation'."
)
stop()
}
if (to == "data.table"){
if (is.null(p_attributes)) p_attributes <- p_attributes(.Object)
if (!all(p_attributes %in% p_attributes(.Object)))
stop("Not all p_attributes provided are available.")
p_attribute_list <- lapply(
setNames(p_attributes, p_attributes),
function(p_attr){
if (verbose)
cli_progress_step(sprintf("decoding p-attribute: %s", p_attr))
get_token_stream(.Object, p_attribute = p_attr, decode = decode)
}
)
max_cpos <- size(.Object) - 1L
if (is.null(s_attributes)) s_attributes <- s_attributes(.Object)
if (length(s_attributes) > 0L){
if (!all(s_attributes %in% s_attributes(.Object)))
stop("Not all s_attributes provided are available.")
}
if (length(s_attributes) > 0L){
s_attribute_list <- lapply(
setNames(s_attributes, s_attributes),
function(s_attr){
if (verbose)
cli_progress_step(sprintf("decoding s-attribute: %s", s_attr))
struc <- cl_cpos2struc(
corpus = .Object@corpus,
registry = .Object@registry_dir,
s_attribute = s_attr,
cpos = 0L:max_cpos
)
if (decode && s_attr_has_values(s_attr, x = .Object)){
str <- cl_struc2str(
corpus = .Object@corpus,
registry = .Object@registry_dir,
s_attribute = s_attr,
struc = struc
)
Encoding(str) <- encoding(.Object)
return(as.nativeEnc(str, from = encoding(.Object)))
} else {
return(struc)
}
}
)
} else {
s_attribute_list <- list()
}
if (verbose) cli_progress_step("assembling data.table")
combined_list <- c(
list(cpos = 0L:max_cpos),
p_attribute_list,
s_attribute_list
)
y <- data.table::as.data.table(combined_list)
} else if (to == "Annotation"){
y <- as(.Object, "Annotation")
} else if (to == "AnnotatedPlainTextDocument"){
y <- as.AnnotatedPlainTextDocument(
x = as(.Object, "subcorpus"), # fn requires cpos slot
p_attributes = p_attributes,
s_attributes = s_attributes,
mw = mw,
stoplist = stoplist,
verbose = verbose
)
}
y
})
#' @exportMethod decode
#' @rdname decode
setMethod("decode", "character", function(.Object, to = c("data.table", "Annotation"), s_attributes = NULL, p_attributes = NULL, decode = TRUE, verbose = TRUE){
decode(corpus(.Object), to = to, s_attributes = s_attributes, p_attributes = p_attributes, decode = decode, verbose = verbose)
})
#' @exportMethod decode
#' @rdname decode
setMethod("decode", "slice", function(.Object, to = c("data.table", "Annotation", "AnnotatedPlainTextDocument"), s_attributes = NULL, p_attributes = NULL, mw = NULL, stoplist = NULL, decode = TRUE, verbose = TRUE){
if (to == "data.table"){
if (is.null(p_attributes)) p_attributes <- p_attributes(.Object)
if (!all(p_attributes %in% p_attributes(.Object)))
stop("Not all p_attributes provided are available.")
y <- data.table(cpos = ranges_to_cpos(.Object@cpos))
setkeyv(y, cols = "cpos") # col cpos used repeatedly to merge s-attributes
for (p_attr in p_attributes){
if (verbose) cli_progress_step("decoding p_attribute {.val {p_attr}}")
y[, (p_attr) := get_token_stream(.Object, p_attribute = p_attr, decode = decode)]
if (verbose) cli_progress_done()
}
if (is.null(s_attributes)){
if (verbose)
cli_alert_info(
"argument `s_attributes` missing - decode all s-attributes"
)
s_attributes <- s_attributes(.Object)
}
if (length(s_attributes) > 0L){
if (!all(s_attributes %in% s_attributes(.Object)))
stop("Not all s_attributes provided are available.")
for (i in 1L:length(s_attributes)){
if (verbose)
cli_progress_step("decoding s_attribute {.val {s_attributes[i]}}")
# Tested: strucs, regions and corpus positions will be identical if
# i > 2 and current s-attribute is sibling of previous sibling. But
# the cost of learning whether s-attributes are siblings consumes the
# performance gain, so we get strucs, regions and cpos anew.
struc_matrix <- RcppCWB::region_matrix_to_struc_matrix(
corpus = .Object@corpus,
s_attribute = s_attributes[i],
registry = .Object@registry_dir,
region_matrix = .Object@cpos
)
strucs <- suppressWarnings(RcppCWB::ranges_to_cpos(struc_matrix))
if (length(strucs) == 0L){
if (decode){
y[, (s_attributes[i]) := rep(NA_character_, times = nrow(y))]
} else {
y[, (s_attributes[i]) := rep(NA_integer_, times = nrow(y))]
}
next
}
regions <- RcppCWB::get_region_matrix(
corpus = .Object@corpus,
registry = .Object@registry_dir,
s_attribute = s_attributes[i],
strucs = strucs
)
cpos <- RcppCWB::ranges_to_cpos(regions)
decode <- decode && s_attr_has_values(s_attributes[i], x = .Object)
if (decode){
str <- cl_struc2str(
corpus = .Object@corpus,
registry = .Object@registry_dir,
s_attribute = s_attributes[i],
struc = strucs
)
Encoding(str) <- encoding(.Object)
}
values <- data.table(
cpos = cpos,
attr = do.call(
c,
mapply(
rep,
x = if (decode) str else strucs,
times = (regions[,2] - regions[,1]) + 1,
SIMPLIFY = FALSE
)
)
)
setnames(values, old = "attr", new = s_attributes[i])
y <- values[y, on = "cpos"]
if (verbose) cli_progress_done()
}
setcolorder(y, neworder = c("cpos", p_attributes, s_attributes))
}
} else if (to == "Annotation"){
y <- as(.Object, "Annotation")
} else if (to == "AnnotatedPlainTextDocument"){
return(callNextMethod())
} else {
stop("not implemented")
}
y
})
#' @rdname decode
setMethod("decode", "partition", function(.Object, to = "data.table", s_attributes = NULL, p_attributes = NULL, decode = TRUE, verbose = TRUE){
callNextMethod()
})
#' @exportMethod decode
#' @rdname decode
setMethod("decode", "subcorpus", function(.Object, to = c("data.table", "Annotation", "AnnotatedPlainTextDocument"), s_attributes = NULL, p_attributes = NULL, mw = NULL, stoplist = NULL, decode = TRUE, verbose = TRUE){
if (to == "AnnotatedPlainTextDocument"){
return(
as.AnnotatedPlainTextDocument(
x = .Object,
p_attributes = p_attributes,
s_attributes = s_attributes,
mw = mw,
stoplist = stoplist,
verbose = verbose
)
)
}
callNextMethod()
})
#' @details If \code{.Object} is an \code{integer} vector, it is assumed to be a
#' vector of integer ids of p-attributes. The \code{decode}-method will
#' translate token ids to string values as efficiently as possible. The
#' approach taken will depend on the corpus size and the share of the corpus
#' that is to be decoded. To decode a large number of integer ids, it is more
#' efficient to read the lexicon file from the data directory directly and to
#' index the lexicon with the ids rather than relying on
#' \code{RcppCWB::cl_id2str}. The internal decision rule is to use the lexicon
#' file when the corpus is larger than 10 000 000 million tokens and more than
#' 5 percent of the corpus are to be decoded. The encoding of the
#' \code{character} vector that is returned will be the coding of the locale
#' (usually ISO-8859-1 on Windows, and UTF-8 on macOS and Linux machines).
#' @param boost A length-one \code{logical} value, whether to speed up decoding
#' a long vector of token ids by directly by reading in the lexion file from
#' the data directory of a corpus. If \code{NULL} (default), the internal
#' decision rule is that \code{boost} will be \code{TRUE} if the corpus is
#' larger than 10 000 000 million tokens and more than 5 percent of the corpus
#' are to be decoded.
#' @param corpus A CWB indexed corpus, either a length-one \code{character} vector,
#' or a \code{corpus} object.
#' @exportMethod decode
#' @rdname decode
#' @examples
#'
#' # decode vector of token ids
#' y <- decode(0:20, corpus = "GERMAPARLMINI", p_attributes = "word")
#' @importFrom stringi stri_encode
setMethod("decode", "integer", function(.Object, corpus, p_attributes, boost = NULL){
stopifnot(
length(corpus) == 1L,
length(p_attributes) == 1L,
is.character(p_attributes)
)
corpus <- if (is.character(corpus)) corpus(corpus) else corpus
if (!inherits(corpus, "corpus"))
stop("Argument 'corpus' is required to be a corpus object.")
if (is.null(boost)){
boost <- if (corpus@size > 10000000L && length(cpos) >= (corpus@size * 0.05)) TRUE else FALSE
}
if (isTRUE(boost)){
lexfile <- fs::path(corpus@data_dir, sprintf("%s.lexicon", p_attributes))
lexicon <- readBin(con = lexfile, what = character(), n = file.info(lexfile)$size)
Encoding(lexicon) <- corpus@encoding
if (!identical(corpus@encoding, encoding())){
# lexicon <- stringi::stri_encode(lexicon, from = corpus@encoding, to = encoding()) # as.locale
lexicon <- iconv(lexicon, from = corpus@encoding, to = encoding())
Encoding(lexicon) <- encoding()
}
y <- lexicon[.Object + 1L]
} else if (isFALSE(boost)){
y <- RcppCWB::cl_id2str(
corpus = corpus@corpus, registry = corpus@registry_dir,
p_attribute = p_attributes, id = .Object
)
Encoding(y) <- corpus@encoding
if (!identical(corpus@encoding, encoding())){
# y <- stringi::stri_encode(y, from = corpus@encoding, to = encoding())
y <- iconv(y, from = corpus@encoding, to = encoding())
Encoding(y) <- encoding()
}
}
y
})
#' @details The \code{decode}-method for \code{data.table} objects will decode
#' token ids (column '`p-attribute`_id'), adding the corresponding string as a
#' new column. If a column "cpos" with corpus positions is present, ids are
#' derived for the corpus positions given first. If the \code{data.table}
#' neither has a column "cpos" nor columns with token ids (i.e. colummn name
#' ending with "_id"), the input \code{data.table} is returned unchanged. Note
#' that columns are added to the \code{data.table} in an in-place operation to
#' handle memory parsimoniously.
#' @examples
#' dt <- data.table::data.table(cpos = cpos("GERMAPARLMINI", query = "Liebe")[,1])
#' decode(dt, corpus = "GERMAPARLMINI", p_attributes = c("word", "pos"))
#' y <- dt[, .N, by = c("word", "pos")]
#' @rdname decode
setMethod("decode", "data.table", function(.Object, corpus, p_attributes){
corpus <- if (is.character(corpus)) corpus(corpus) else corpus
if (!inherits(corpus, "corpus")) stop("Argument 'corpus' is required to be a corpus object.")
if (isFALSE(all(p_attributes %in% p_attributes(corpus)))){
stop("At least one p-attribute is not available.")
}
if ("cpos" %in% colnames(.Object)){
for (p_attr in p_attributes){
p_attr_id <- paste(p_attr, "id", sep = "_")
ids <- cpos2id(corpus, p_attribute = p_attr, cpos = .Object[["cpos"]])
.Object[, (p_attr_id) := ids]
}
}
for (p_attr in p_attributes){
p_attr_id <- paste(p_attr, "id", sep = "_")
if (p_attr_id %in% colnames(.Object)){
.Object[, (p_attr) := decode(.Object[[p_attr_id]], corpus = corpus, p_attributes = p_attr)]
}
}
.Object
})