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
|
AnnotatedPlainTextDocument <-
function(s, annotations, meta = list())
{
s <- as.String(s)
## Be nice.
if(is.Annotation(annotations))
annotations <- list(annotations)
else {
annotations <- as.list(annotations)
if(!length(annotations) ||
!all(sapply(annotations, is.Annotation)))
stop("argument 'annotations' must give a positive number of Annotation objects")
}
doc <- list(content = s, meta = meta, annotations = annotations)
class(doc) <- c("AnnotatedPlainTextDocument",
"PlainTextDocument",
"TextDocument")
doc
}
format.AnnotatedPlainTextDocument <-
function(x, ...)
{
annotations <- x$annotations
c(.format_TextDocument(x),
sprintf("Annotations: %d, length(s): %s",
length(annotations),
paste(sapply(annotations, length), collapse = "/")),
sprintf("Content: chars: %d",
nchar(x$content)))
}
## print.AnnotatedPlainTextDocument <-
## function(x, ...)
## {
## annotations <- x$annotations
## writeLines(sprintf("<<AnnotatedPlainTextDocument (annotations: %d, length(s): %s)>>",
## length(annotations),
## paste(sapply(annotations, length),
## collapse = "/")))
## invisible(x)
## }
content.AnnotatedPlainTextDocument <-
function(x)
x$content
`content<-.AnnotatedPlainTextDocument` <-
function(x, value)
stop("content modification is not possible for AnnotatedPlainTextDocument objects")
## meta.AnnotatedPlainTextDocument <-
## function(x, tag = NULL, ...)
## if(is.null(tag)) x$meta else x$meta[[tag]]
## `meta<-.AnnotatedPlainTextDocument` <-
## function(x, tag = NULL, ..., value)
## {
## if(is.null(tag))
## x$meta <- value
## else
## x$meta[[tag]] <- value
## x
## }
as.character.AnnotatedPlainTextDocument <-
function(x, ...)
x$content
annotations <-
function(x)
{
if(!inherits(x, "AnnotatedPlainTextDocument"))
stop("argument 'x' must be an AnnotatedPlainTextDocument object")
x$annotations
}
## NLTK style functions for high level access
words.AnnotatedPlainTextDocument <-
function(x, which = 1L, ...)
{
if(!inherits(x, "AnnotatedPlainTextDocument"))
stop("argument 'x' must be an AnnotatedPlainTextDocument object")
s <- x$content
a <- annotations(x)[[which]]
## Could check for word token annotations ...
s[a[a$type == "word"]]
}
sents.AnnotatedPlainTextDocument <-
function(x, which = 1L, ...)
{
if(!inherits(x, "AnnotatedPlainTextDocument"))
stop("argument 'x' must be an AnnotatedPlainTextDocument object")
s <- x$content
a <- annotations(x)[[which]]
.sents_from_annotation_and_text(a, s)
}
.sents_from_annotation_and_text <-
function(a, s)
{
## Could check for sentence and word token annotations ...
s[annotations_in_spans(a[a$type == "word"],
a[a$type == "sentence"])]
}
paras.AnnotatedPlainTextDocument <-
function(x, which = 1L, ...)
{
if(!inherits(x, "AnnotatedPlainTextDocument"))
stop("argument 'x' must be an AnnotatedPlainTextDocument object")
s <- x$content
a <- annotations(x)[[which]]
## Could check for paragraph annotations ...
lapply(annotations_in_spans(a, a[a$type == "paragraph"]),
.sents_from_annotation_and_text, s)
}
tagged_words.AnnotatedPlainTextDocument <-
function(x, which = 1L, map = NULL, ...)
{
if(!inherits(x, "AnnotatedPlainTextDocument"))
stop("argument 'x' must be an AnnotatedPlainTextDocument object")
s <- x$content
a <- annotations(x)[[which]]
## Could check for word token annotations ...
a <- a[a$type == "word"]
if(!is.null(map))
a <- .map_POS_tags_Annotation(a, map)
.tagged_words_from_annotation_and_text(a, s)
}
.tagged_words_from_annotation_and_text <-
function(a, s)
{
## Could check for POS tag features ...
pos <- sapply(a$features, `[[`, "POS")
Tagged_Token(s[a], pos)
}
tagged_sents.AnnotatedPlainTextDocument <-
function(x, which = 1L, map = NULL, ...)
{
if(!inherits(x, "AnnotatedPlainTextDocument"))
stop("argument 'x' must be an AnnotatedPlainTextDocument object")
s <- x$content
a <- annotations(x)[[which]]
if(!is.null(map))
a <- .map_POS_tags_Annotation(a, map)
.tagged_sents_from_annotation_and_text(a, s)
}
.tagged_sents_from_annotation_and_text <-
function(a, s)
{
## Could check for word and sentence token annotations ...
lapply(annotations_in_spans(a[a$type == "word"],
a[a$type == "sentence"]),
.tagged_words_from_annotation_and_text, s)
}
tagged_paras.AnnotatedPlainTextDocument <-
function(x, which = 1L, map = NULL, ...)
{
if(!inherits(x, "AnnotatedPlainTextDocument"))
stop("argument 'x' must be an AnnotatedPlainTextDocument object")
s <- x$content
a <- annotations(x)[[which]]
if(!is.null(map))
a <- .map_POS_tags_Annotation(a, map)
## Could check for paragraph annotations ...
lapply(annotations_in_spans(a, a[a$type == "paragraph"]),
.tagged_sents_from_annotation_and_text, s)
}
parsed_sents.AnnotatedPlainTextDocument <-
function(x, which = 1L, ...)
{
if(!inherits(x, "AnnotatedPlainTextDocument"))
stop("argument 'x' must be an AnnotatedPlainTextDocument object")
a <- annotations(x)[[which]]
.parsed_sents_from_annotation(a)
}
.parsed_sents_from_annotation <-
function(a)
{
## Could check for sentence token annotations and parse features ...
ptexts <- sapply(a[a$type == "sentence"]$features, `[[`, "parse")
lapply(ptexts, Tree_parse)
}
parsed_paras.AnnotatedPlainTextDocument <-
function(x, which = 1L, ...)
{
if(!inherits(x, "AnnotatedPlainTextDocument"))
stop("argument 'x' must be an AnnotatedPlainTextDocument object")
a <- annotations(x)[[which]]
## Could check for paragraph annotations ...
lapply(annotations_in_spans(a, a[a$type == "paragraph"]),
.parsed_sents_from_annotation)
}
chunked_sents.AnnotatedPlainTextDocument <-
function(x, which = 1L, ...)
{
if(!inherits(x, "AnnotatedPlainTextDocument"))
stop("argument 'x' must be an AnnotatedPlainTextDocument object")
s <- x$content
a <- annotations(x)[[which]]
## Require annotations with POS and chunk_tag features, as obtained
## e.g. with the Apache OpenNLP POS tag and chunk annotators. We
## could alternatively use annotations with parse features and
## flatten the parse trees.
## Could check for word and sentence token annotations ...
lapply(annotations_in_spans(a[a$type == "word"],
a[a$type == "sentence"]),
function(a) {
## Could check for POS and chunk tag features ...
ptags <- sapply(a$features, `[[`, "POS")
ctags <- sapply(a$features, `[[`, "chunk_tag")
words <- s[a]
chunk_tree_from_chunk_info(words, ptags, ctags)
})
}
.map_POS_tags_Annotation <-
function(x, map)
{
map <- POS_tag_mapper(map, meta(x, "POS_tagset"))
x$features <-
lapply(x$features,
function(e) {
if(!is.null(pos <- e$POS))
e$POS <- map(pos)
e
})
x
}
|