SCM

SCM Repository

[tm] Annotation of /pkg/R/corpus.R
ViewVC logotype

Annotation of /pkg/R/corpus.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 982 - (view) (download)

1 : feinerer 17 # Author: Ingo Feinerer
2 :    
3 : feinerer 958 prepareReader <- function(readerControl, defaultReader = NULL, ...) {
4 :     if (is.null(readerControl$reader))
5 :     readerControl$reader <- defaultReader
6 :     if (is(readerControl$reader, "FunctionGenerator"))
7 :     readerControl$reader <- readerControl$reader(...)
8 : feinerer 950 if (is.null(readerControl$language))
9 :     readerControl$language <- "eng"
10 : feinerer 958 readerControl
11 :     }
12 : feinerer 950
13 : feinerer 962 ## Fast Corpus
14 :     ## - provides a prototype implementation of a more time and memory efficient representation of a corpus
15 :     ## - allows performance tests and comparisons to other corpus types
16 :     #FCorpus <- function(object, readerControl = list(language = "eng")) {
17 :     # readerControl <- prepareReader(readerControl)
18 :     #
19 :     # if (!object@Vectorized)
20 :     # stop("Source is not vectorized")
21 :     #
22 :     # tdl <- lapply(mapply(c, pGetElem(object), id = seq_len(object@Length), SIMPLIFY = FALSE),
23 :     # function(x) readSlim(x[c("content", "uri")],
24 :     # readerControl$language,
25 :     # as.character(x$id)))
26 :     #
27 :     # new("FCorpus", .Data = tdl)
28 :     #}
29 : feinerer 958
30 : feinerer 946 PCorpus <- function(object,
31 :     readerControl = list(reader = object@DefaultReader, language = "eng"),
32 :     dbControl = list(dbName = "", dbType = "DB1"),
33 :     ...) {
34 : feinerer 958 readerControl <- prepareReader(readerControl, object@DefaultReader, ...)
35 : feinerer 63
36 : feinerer 946 if (!filehash::dbCreate(dbControl$dbName, dbControl$dbType))
37 :     stop("error in creating database")
38 :     db <- filehash::dbInit(dbControl$dbName, dbControl$dbType)
39 : feinerer 712
40 : feinerer 946 # Allocate memory in advance if length is known
41 :     tdl <- if (object@Length > 0)
42 :     vector("list", as.integer(object@Length))
43 :     else
44 :     list()
45 : feinerer 869
46 : feinerer 946 counter <- 1
47 :     while (!eoi(object)) {
48 :     object <- stepNext(object)
49 :     elem <- getElem(object)
50 :     doc <- readerControl$reader(elem, readerControl$language, as.character(counter))
51 :     filehash::dbInsert(db, ID(doc), doc)
52 :     if (object@Length > 0) tdl[[counter]] <- ID(doc)
53 :     else tdl <- c(tdl, ID(doc))
54 :     counter <- counter + 1
55 :     }
56 : feinerer 63
57 : feinerer 946 df <- data.frame(MetaID = rep(0, length(tdl)), stringsAsFactors = FALSE)
58 :     filehash::dbInsert(db, "DMetaData", df)
59 :     dmeta.df <- data.frame(key = "DMetaData", subset = I(list(NA)))
60 : feinerer 712
61 : feinerer 946 cmeta.node <- new("MetaDataNode",
62 :     NodeID = 0,
63 :     MetaData = list(create_date = as.POSIXlt(Sys.time(), tz = "GMT"), creator = Sys.getenv("LOGNAME")),
64 :     children = list())
65 : feinerer 71
66 : feinerer 946 new("PCorpus", .Data = tdl, DMetaData = dmeta.df, CMetaData = cmeta.node, DBControl = dbControl)
67 :     }
68 : feinerer 21
69 : feinerer 946 # The "..." are additional arguments for the FunctionGenerator reader
70 : feinerer 963 VCorpus <- Corpus <- function(object,
71 : feinerer 946 readerControl = list(reader = object@DefaultReader, language = "eng"),
72 :     ...) {
73 : feinerer 958 readerControl <- prepareReader(readerControl, object@DefaultReader, ...)
74 : feinerer 49
75 : feinerer 946 # Allocate memory in advance if length is known
76 :     tdl <- if (object@Length > 0)
77 :     vector("list", as.integer(object@Length))
78 :     else
79 :     list()
80 : feinerer 72
81 : feinerer 946 if (object@Vectorized)
82 :     tdl <- lapply(mapply(c, pGetElem(object), id = seq_len(object@Length), SIMPLIFY = FALSE),
83 :     function(x) readerControl$reader(x[c("content", "uri")],
84 :     readerControl$language,
85 :     as.character(x$id)))
86 :     else {
87 :     counter <- 1
88 :     while (!eoi(object)) {
89 :     object <- stepNext(object)
90 :     elem <- getElem(object)
91 :     doc <- readerControl$reader(elem, readerControl$language, as.character(counter))
92 :     if (object@Length > 0)
93 :     tdl[[counter]] <- doc
94 :     else
95 :     tdl <- c(tdl, list(doc))
96 :     counter <- counter + 1
97 :     }
98 :     }
99 : feinerer 72
100 : feinerer 946 df <- data.frame(MetaID = rep(0, length(tdl)), stringsAsFactors = FALSE)
101 :     cmeta.node <- new("MetaDataNode",
102 :     NodeID = 0,
103 :     MetaData = list(create_date = as.POSIXlt(Sys.time(), tz = "GMT"), creator = Sys.getenv("LOGNAME")),
104 :     children = list())
105 : feinerer 72
106 : feinerer 963 new("VCorpus", .Data = tdl, DMetaData = df, CMetaData = cmeta.node)
107 : feinerer 946 }
108 : feinerer 72
109 : feinerer 830 setGeneric("tmMap", function(object, FUN, ..., lazy = FALSE) standardGeneric("tmMap"))
110 : feinerer 962 #setMethod("tmMap",
111 :     # signature(object = "FCorpus", FUN = "function"),
112 :     # function(object, FUN, ..., lazy = FALSE) {
113 :     # if (lazy)
114 :     # warning("lazy mapping is deactivated")
115 :     #
116 :     # new("FCorpus", .Data = lapply(object, FUN, ..., DMetaData = data.frame()))
117 :     # })
118 : feinerer 698 setMethod("tmMap",
119 : feinerer 963 signature(object = "VCorpus", FUN = "function"),
120 : feinerer 830 function(object, FUN, ..., lazy = FALSE) {
121 : feinerer 73 result <- object
122 : feinerer 946 # Lazy mapping
123 :     if (lazy) {
124 :     lazyTmMap <- meta(object, tag = "lazyTmMap", type = "corpus")
125 :     if (is.null(lazyTmMap)) {
126 :     meta(result, tag = "lazyTmMap", type = "corpus") <-
127 :     list(index = rep(TRUE, length(result)),
128 :     maps = list(function(x, DMetaData) FUN(x, ..., DMetaData = DMetaData)))
129 : feinerer 728 }
130 : feinerer 861 else {
131 : feinerer 946 lazyTmMap$maps <- c(lazyTmMap$maps, list(function(x, DMetaData) FUN(x, ..., DMetaData = DMetaData)))
132 :     meta(result, tag = "lazyTmMap", type = "corpus") <- lazyTmMap
133 : feinerer 861 }
134 : feinerer 828 }
135 : feinerer 946 else {
136 :     result@.Data <- if (clusterAvailable())
137 :     snow::parLapply(snow::getMPIcluster(), object, FUN, ..., DMetaData = DMetaData(object))
138 :     else
139 :     lapply(object, FUN, ..., DMetaData = DMetaData(object))
140 :     }
141 :     result
142 : feinerer 49 })
143 : feinerer 946 setMethod("tmMap",
144 :     signature(object = "PCorpus", FUN = "function"),
145 :     function(object, FUN, ..., lazy = FALSE) {
146 :     if (lazy)
147 :     warning("lazy mapping is deactived when using database backend")
148 :     db <- filehash::dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
149 :     i <- 1
150 :     for (id in unlist(object)) {
151 :     db[[id]] <- FUN(object[[i]], ..., DMetaData = DMetaData(object))
152 :     i <- i + 1
153 :     }
154 :     # Suggested by Christian Buchta
155 :     filehash::dbReorganize(db)
156 : feinerer 49
157 : feinerer 946 object
158 :     })
159 :    
160 : feinerer 828 # Materialize lazy mappings
161 : feinerer 833 # Improvements by Christian Buchta
162 : feinerer 829 materialize <- function(corpus, range = seq_along(corpus)) {
163 : feinerer 828 lazyTmMap <- meta(corpus, tag = "lazyTmMap", type = "corpus")
164 : feinerer 829 if (!is.null(lazyTmMap)) {
165 : feinerer 833 # Make valid and lazy index
166 :     idx <- (seq_along(corpus) %in% range) & lazyTmMap$index
167 :     if (any(idx)) {
168 : feinerer 946 res <- corpus@.Data[idx]
169 : feinerer 833 for (m in lazyTmMap$maps)
170 :     res <- lapply(res, m, DMetaData = DMetaData(corpus))
171 :     corpus@.Data[idx] <- res
172 :     lazyTmMap$index[idx] <- FALSE
173 :     }
174 : feinerer 829 }
175 : feinerer 830 # Clean up if everything is materialized
176 :     if (!any(lazyTmMap$index))
177 :     lazyTmMap <- NULL
178 : feinerer 829 meta(corpus, tag = "lazyTmMap", type = "corpus") <- lazyTmMap
179 : feinerer 946 corpus
180 : feinerer 828 }
181 :    
182 : feinerer 698 setGeneric("asPlain", function(object, FUN, ...) standardGeneric("asPlain"))
183 : feinerer 946 setMethod("asPlain", signature(object = "PlainTextDocument"),
184 :     function(object, FUN, ...) object)
185 : feinerer 698 setMethod("asPlain",
186 : feinerer 856 signature(object = "XMLTextDocument"),
187 : feinerer 49 function(object, FUN, ...) {
188 : feinerer 886 require("XML")
189 :    
190 : feinerer 816 corpus <- Content(object)
191 : feinerer 49
192 :     # As XMLDocument is no native S4 class, restore valid information
193 :     class(corpus) <- "XMLDocument"
194 :     names(corpus) <- c("doc","dtd")
195 :    
196 : feinerer 61 return(FUN(xmlRoot(corpus), ...))
197 : feinerer 49 })
198 : feinerer 725 setMethod("asPlain",
199 : feinerer 757 signature(object = "Reuters21578Document"),
200 :     function(object, FUN, ...) {
201 : feinerer 886 require("XML")
202 :    
203 : feinerer 757 FUN <- convertReut21578XMLPlain
204 : feinerer 816 corpus <- Content(object)
205 : feinerer 757
206 :     # As XMLDocument is no native S4 class, restore valid information
207 :     class(corpus) <- "XMLDocument"
208 :     names(corpus) <- c("doc","dtd")
209 :    
210 :     return(FUN(xmlRoot(corpus), ...))
211 :     })
212 : feinerer 946 setMethod("asPlain", signature(object = "RCV1Document"),
213 :     function(object, FUN, ...) convertRCV1Plain(object, ...))
214 : feinerer 757 setMethod("asPlain",
215 : feinerer 775 signature(object = "StructuredTextDocument"),
216 :     function(object, FUN, ...) {
217 : feinerer 946 new("PlainTextDocument", .Data = unlist(Content(object)),
218 :     Author = Author(object), DateTimeStamp = DateTimeStamp(object),
219 : feinerer 775 Description = Description(object), ID = ID(object), Origin = Origin(object),
220 : feinerer 826 Heading = Heading(object), Language = Language(object),
221 :     LocalMetaData = LocalMetaData(object))
222 : feinerer 775 })
223 : feinerer 49
224 : feinerer 854 setGeneric("tmFilter", function(object, ..., FUN = searchFullText, doclevel = TRUE) standardGeneric("tmFilter"))
225 : feinerer 946 setMethod("tmFilter", signature(object = "Corpus"),
226 :     function(object, ..., FUN = searchFullText, doclevel = TRUE)
227 :     object[tmIndex(object, ..., FUN = FUN, doclevel = doclevel)])
228 : feinerer 61
229 : feinerer 854 setGeneric("tmIndex", function(object, ..., FUN = searchFullText, doclevel = TRUE) standardGeneric("tmIndex"))
230 : feinerer 698 setMethod("tmIndex",
231 : feinerer 816 signature(object = "Corpus"),
232 : feinerer 854 function(object, ..., FUN = searchFullText, doclevel = TRUE) {
233 :     if (!is.null(attr(FUN, "doclevel")))
234 :     doclevel <- attr(FUN, "doclevel")
235 : feinerer 861 if (doclevel) {
236 :     if (clusterAvailable())
237 : feinerer 870 return(snow::parSapply(snow::getMPIcluster(), object, FUN, ..., DMetaData = DMetaData(object)))
238 : feinerer 861 else
239 :     return(sapply(object, FUN, ..., DMetaData = DMetaData(object)))
240 :     }
241 : feinerer 73 else
242 : feinerer 724 return(FUN(object, ...))
243 : feinerer 49 })
244 :    
245 : feinerer 698 setGeneric("appendElem", function(object, data, meta = NULL) standardGeneric("appendElem"))
246 :     setMethod("appendElem",
247 : feinerer 816 signature(object = "Corpus", data = "TextDocument"),
248 : feinerer 72 function(object, data, meta = NULL) {
249 : feinerer 886 if (DBControl(object)[["useDb"]] && require("filehash")) {
250 : feinerer 982 db <- filehash::dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
251 :     if (filehash::dbExists(db, ID(data)))
252 : feinerer 712 warning("document with identical ID already exists")
253 : feinerer 982 filehash::dbInsert(db, ID(data), data)
254 : feinerer 712 object@.Data[[length(object)+1]] <- ID(data)
255 :     }
256 :     else
257 :     object@.Data[[length(object)+1]] <- data
258 :     DMetaData(object) <- rbind(DMetaData(object), c(MetaID = CMetaData(object)@NodeID, meta))
259 : feinerer 52 return(object)
260 :     })
261 :    
262 : feinerer 946 prescindMeta <- function(object, meta) {
263 :     df <- DMetaData(object)
264 : feinerer 53
265 : feinerer 946 for (m in meta)
266 :     df <- cbind(df, structure(data.frame(I(meta(object, tag = m, type = "local"))), names = m))
267 : feinerer 69
268 : feinerer 946 df
269 :     }
270 :    
271 : feinerer 962 #setMethod("[",
272 :     # signature(x = "FCorpus", i = "ANY", j = "ANY", drop = "ANY"),
273 :     # function(x, i, j, ... , drop) {
274 :     # if (missing(i)) return(x)
275 :     #
276 :     # x@.Data <- x@.Data[i, ..., drop = FALSE]
277 :     # x
278 :     # })
279 : feinerer 946 setMethod("[",
280 :     signature(x = "PCorpus", i = "ANY", j = "ANY", drop = "ANY"),
281 :     function(x, i, j, ... , drop) {
282 :     if (missing(i)) return(x)
283 :    
284 :     x@.Data <- x@.Data[i, ..., drop = FALSE]
285 :     index <- x@DMetaData[[1 , "subset"]]
286 :     if (any(is.na(index))) x@DMetaData[[1 , "subset"]] <- i
287 :     else x@DMetaData[[1 , "subset"]] <- index[i]
288 :     x
289 : feinerer 73 })
290 : feinerer 53 setMethod("[",
291 : feinerer 963 signature(x = "VCorpus", i = "ANY", j = "ANY", drop = "ANY"),
292 : feinerer 53 function(x, i, j, ... , drop) {
293 : feinerer 946 if (missing(i)) return(x)
294 : feinerer 53
295 : feinerer 946 x@.Data <- x@.Data[i, ..., drop = FALSE]
296 :     DMetaData(x) <- DMetaData(x)[i, , drop = FALSE]
297 :     x
298 : feinerer 53 })
299 :    
300 : feinerer 63 setMethod("[<-",
301 : feinerer 946 signature(x = "PCorpus", i = "ANY", j = "ANY", value = "ANY"),
302 : feinerer 63 function(x, i, j, ... , value) {
303 : feinerer 946 db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
304 :     counter <- 1
305 :     for (id in x@.Data[i, ...]) {
306 :     if (identical(length(value), 1)) db[[id]] <- value
307 :     else db[[id]] <- value[[counter]]
308 :     counter <- counter + 1
309 : feinerer 724 }
310 : feinerer 946 x
311 : feinerer 63 })
312 : feinerer 946 setMethod("[<-",
313 : feinerer 963 signature(x = "VCorpus", i = "ANY", j = "ANY", value = "ANY"),
314 : feinerer 946 function(x, i, j, ... , value) {
315 :     x@.Data[i, ...] <- value
316 :     x
317 :     })
318 : feinerer 63
319 :     setMethod("[[",
320 : feinerer 946 signature(x = "PCorpus", i = "ANY", j = "ANY"),
321 : feinerer 63 function(x, i, j, ...) {
322 : feinerer 946 db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
323 :     filehash::dbFetch(db, x@.Data[[i]])
324 : feinerer 63 })
325 : feinerer 946 setMethod("[[",
326 : feinerer 963 signature(x = "VCorpus", i = "ANY", j = "ANY"),
327 : feinerer 946 function(x, i, j, ...) {
328 :     lazyTmMap <- meta(x, tag = "lazyTmMap", type = "corpus")
329 :     if (!is.null(lazyTmMap))
330 :     .Call("copyCorpus", x, materialize(x, i))
331 :     x@.Data[[i]]
332 :     })
333 : feinerer 63
334 :     setMethod("[[<-",
335 : feinerer 946 signature(x = "PCorpus", i = "ANY", j = "ANY", value = "ANY"),
336 : feinerer 63 function(x, i, j, ..., value) {
337 : feinerer 946 db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
338 :     index <- x@.Data[[i]]
339 :     db[[index]] <- value
340 :     x
341 :     })
342 :     setMethod("[[<-",
343 : feinerer 963 signature(x = "VCorpus", i = "ANY", j = "ANY", value = "ANY"),
344 : feinerer 946 function(x, i, j, ..., value) {
345 :     # Mark new objects as not active for lazy mapping
346 :     lazyTmMap <- meta(x, tag = "lazyTmMap", type = "corpus")
347 :     if (!is.null(lazyTmMap)) {
348 :     lazyTmMap$index[i] <- FALSE
349 :     meta(x, tag = "lazyTmMap", type = "corpus") <- lazyTmMap
350 : feinerer 712 }
351 : feinerer 946 # Set the value
352 :     x@.Data[[i, ...]] <- value
353 :    
354 :     x
355 : feinerer 63 })
356 :    
357 : feinerer 698 # Update \code{NodeID}s of a CMetaData tree
358 : feinerer 697 update_id <- function(object, id = 0, mapping = NULL, left.mapping = NULL, level = 0) {
359 : feinerer 698 # Traversal of (binary) CMetaData tree with setup of \code{NodeID}s
360 : feinerer 697 set_id <- function(object) {
361 :     object@NodeID <- id
362 :     id <<- id + 1
363 :     level <<- level + 1
364 : feinerer 71
365 : feinerer 697 if (length(object@children) > 0) {
366 :     mapping <<- cbind(mapping, c(object@children[[1]]@NodeID, id))
367 :     left <- set_id(object@children[[1]])
368 :     if (level == 1) {
369 :     left.mapping <<- mapping
370 :     mapping <<- NULL
371 :     }
372 :     mapping <<- cbind(mapping, c(object@children[[2]]@NodeID, id))
373 :     right <- set_id(object@children[[2]])
374 : feinerer 71
375 : feinerer 697 object@children <- list(left, right)
376 : feinerer 71 }
377 : feinerer 697 level <<- level - 1
378 : feinerer 71
379 : feinerer 697 return(object)
380 : feinerer 71 }
381 :    
382 : feinerer 952 list(root = set_id(object), left.mapping = left.mapping, right.mapping = mapping)
383 : feinerer 71 }
384 :    
385 : feinerer 53 setMethod("c",
386 : feinerer 816 signature(x = "Corpus"),
387 : feinerer 958 function(x, ..., meta = list(merge_date = as.POSIXlt(Sys.time(), tz = "GMT"), merger = Sys.getenv("LOGNAME")), recursive = FALSE) {
388 : feinerer 689 args <- list(...)
389 : feinerer 952 if (identical(length(args), 0)) return(x)
390 : feinerer 71
391 : feinerer 952 if (!all(sapply(args, inherits, class(x))))
392 : feinerer 958 stop("not all arguments are of the same corpus type")
393 : feinerer 720
394 : feinerer 958 if (inherits(x, "PCorpus"))
395 :     stop("concatenation of corpora with underlying databases is not supported")
396 :    
397 : feinerer 902 Reduce(c2, base::c(list(x), args))
398 : feinerer 689 })
399 :    
400 : feinerer 958 setGeneric("c2", function(x, y, ..., meta = list(merge_date = as.POSIXlt(Sys.time(), tz = "GMT"), merger = Sys.getenv("LOGNAME"))) standardGeneric("c2"))
401 : feinerer 962 #setMethod("c2", signature(x = "FCorpus", y = "FCorpus"),
402 :     # function(x, y, ..., meta = list(merge_date = as.POSIXlt(Sys.time(), tz = "GMT"), merger = Sys.getenv("LOGNAME"))) {
403 :     # new("FCorpus", .Data = c(as(x, "list"), as(y, "list")))
404 :     # })
405 : feinerer 963 setMethod("c2", signature(x = "VCorpus", y = "VCorpus"),
406 : feinerer 958 function(x, y, ..., meta = list(merge_date = as.POSIXlt(Sys.time(), tz = "GMT"), merger = Sys.getenv("LOGNAME"))) {
407 : feinerer 71 object <- x
408 :     # Concatenate data slots
409 :     object@.Data <- c(as(x, "list"), as(y, "list"))
410 :    
411 : feinerer 698 # Update the CMetaData tree
412 :     cmeta <- new("MetaDataNode", NodeID = 0, MetaData = meta, children = list(CMetaData(x), CMetaData(y)))
413 :     update.struct <- update_id(cmeta)
414 :     object@CMetaData <- update.struct$root
415 : feinerer 71
416 :     # Find indices to be updated for the left tree
417 :     indices.mapping <- NULL
418 :     for (m in levels(as.factor(DMetaData(x)$MetaID))) {
419 :     indices <- (DMetaData(x)$MetaID == m)
420 :     indices.mapping <- c(indices.mapping, list(m = indices))
421 :     names(indices.mapping)[length(indices.mapping)] <- m
422 :     }
423 :    
424 :     # Update the DMetaData data frames for the left tree
425 :     for (i in 1:ncol(update.struct$left.mapping)) {
426 :     map <- update.struct$left.mapping[,i]
427 :     x@DMetaData$MetaID <- replace(DMetaData(x)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
428 :     }
429 :    
430 :     # Find indices to be updated for the right tree
431 :     indices.mapping <- NULL
432 :     for (m in levels(as.factor(DMetaData(y)$MetaID))) {
433 :     indices <- (DMetaData(y)$MetaID == m)
434 :     indices.mapping <- c(indices.mapping, list(m = indices))
435 :     names(indices.mapping)[length(indices.mapping)] <- m
436 :     }
437 :    
438 :     # Update the DMetaData data frames for the right tree
439 :     for (i in 1:ncol(update.struct$right.mapping)) {
440 :     map <- update.struct$right.mapping[,i]
441 :     y@DMetaData$MetaID <- replace(DMetaData(y)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
442 :     }
443 :    
444 :     # Merge the DMetaData data frames
445 :     labels <- setdiff(names(DMetaData(y)), names(DMetaData(x)))
446 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(x)), ncol = length(labels), dimnames = list(row.names(DMetaData(x)), labels))
447 :     x.dmeta.aug <- cbind(DMetaData(x), na.matrix)
448 :     labels <- setdiff(names(DMetaData(x)), names(DMetaData(y)))
449 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(y)), ncol = length(labels), dimnames = list(row.names(DMetaData(y)), labels))
450 :     y.dmeta.aug <- cbind(DMetaData(y), na.matrix)
451 :     object@DMetaData <- rbind(x.dmeta.aug, y.dmeta.aug)
452 :    
453 : feinerer 952 object
454 : feinerer 72 })
455 : feinerer 689
456 : feinerer 72 setMethod("c",
457 :     signature(x = "TextDocument"),
458 : feinerer 958 function(x, ..., recursive = FALSE){
459 : feinerer 72 args <- list(...)
460 : feinerer 946 if (identical(length(args), 0)) return(x)
461 : feinerer 54
462 : feinerer 73 dmeta.df <- data.frame(MetaID = rep(0, length(list(x, ...))), stringsAsFactors = FALSE)
463 : feinerer 698 cmeta.node <- new("MetaDataNode",
464 : feinerer 72 NodeID = 0,
465 : feinerer 905 MetaData = list(create_date = as.POSIXlt(Sys.time(), tz = "GMT"), creator = Sys.getenv("LOGNAME")),
466 : feinerer 72 children = list())
467 :    
468 : feinerer 963 new("VCorpus", .Data = list(x, ...), DMetaData = dmeta.df, CMetaData = cmeta.node)
469 : feinerer 72 })
470 :    
471 : feinerer 54 setMethod("show",
472 : feinerer 816 signature(object = "Corpus"),
473 : feinerer 54 function(object){
474 : feinerer 70 cat(sprintf(ngettext(length(object),
475 : feinerer 894 "A corpus with %d text document\n",
476 :     "A corpus with %d text documents\n"),
477 : feinerer 70 length(object)))
478 : feinerer 54 })
479 :    
480 :     setMethod("summary",
481 : feinerer 816 signature(object = "Corpus"),
482 : feinerer 54 function(object){
483 :     show(object)
484 : feinerer 71 if (length(DMetaData(object)) > 0) {
485 : feinerer 698 cat(sprintf(ngettext(length(CMetaData(object)@MetaData),
486 : feinerer 77 "\nThe metadata consists of %d tag-value pair and a data frame\n",
487 :     "\nThe metadata consists of %d tag-value pairs and a data frame\n"),
488 : feinerer 698 length(CMetaData(object)@MetaData)))
489 : feinerer 54 cat("Available tags are:\n")
490 : feinerer 722 cat(strwrap(paste(names(CMetaData(object)@MetaData), collapse = " "), indent = 2, exdent = 2), "\n")
491 : feinerer 77 cat("Available variables in the data frame are:\n")
492 : feinerer 722 cat(strwrap(paste(names(DMetaData(object)), collapse = " "), indent = 2, exdent = 2), "\n")
493 : feinerer 54 }
494 :     })
495 : feinerer 55
496 : feinerer 938 inspect <- function(x) UseMethod("inspect", x)
497 : feinerer 946 inspect.PCorpus <- function(x) {
498 : feinerer 938 summary(x)
499 :     cat("\n")
500 : feinerer 946 db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
501 :     show(filehash::dbMultiFetch(db, unlist(x)))
502 : feinerer 938 }
503 : feinerer 962 #inspect.FCorpus <-
504 : feinerer 963 inspect.VCorpus <- function(x) {
505 : feinerer 946 summary(x)
506 :     cat("\n")
507 :     print(noquote(lapply(x, identity)))
508 :     }
509 : feinerer 65
510 :     # No metadata is checked
511 :     setGeneric("%IN%", function(x, y) standardGeneric("%IN%"))
512 : feinerer 946 setMethod("%IN%", signature(x = "TextDocument", y = "PCorpus"),
513 : feinerer 65 function(x, y) {
514 : feinerer 946 db <- filehash::dbInit(DBControl(y)[["dbName"]], DBControl(y)[["dbType"]])
515 :     any(sapply(y, function(x, z) {x %in% Content(z)}, x))
516 : feinerer 65 })
517 : feinerer 963 setMethod("%IN%", signature(x = "TextDocument", y = "VCorpus"),
518 : feinerer 946 function(x, y) x %in% y)
519 : feinerer 719
520 :     setMethod("lapply",
521 : feinerer 963 signature(X = "VCorpus"),
522 : feinerer 719 function(X, FUN, ...) {
523 : feinerer 946 lazyTmMap <- meta(X, tag = "lazyTmMap", type = "corpus")
524 :     if (!is.null(lazyTmMap))
525 :     .Call("copyCorpus", X, materialize(X))
526 :     base::lapply(X, FUN, ...)
527 : feinerer 719 })
528 : feinerer 946 setMethod("lapply",
529 :     signature(X = "PCorpus"),
530 :     function(X, FUN, ...) {
531 :     db <- filehash::dbInit(DBControl(X)[["dbName"]], DBControl(X)[["dbType"]])
532 :     lapply(filehash::dbMultiFetch(db, unlist(X)), FUN, ...)
533 :     })
534 : feinerer 719
535 :     setMethod("sapply",
536 : feinerer 963 signature(X = "VCorpus"),
537 : feinerer 719 function(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) {
538 : feinerer 946 lazyTmMap <- meta(X, tag = "lazyTmMap", type = "corpus")
539 :     if (!is.null(lazyTmMap))
540 :     .Call("copyCorpus", X, materialize(X))
541 :     base::sapply(X, FUN, ...)
542 : feinerer 719 })
543 : feinerer 946 setMethod("sapply",
544 :     signature(X = "PCorpus"),
545 :     function(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) {
546 :     db <- filehash::dbInit(DBControl(X)[["dbName"]], DBControl(X)[["dbType"]])
547 :     sapply(filehash::dbMultiFetch(db, unlist(X)), FUN, ...)
548 :     })
549 : feinerer 819
550 : feinerer 963 setAs("list", "VCorpus", function(from) {
551 : feinerer 836 cmeta.node <- new("MetaDataNode",
552 :     NodeID = 0,
553 : feinerer 905 MetaData = list(create_date = as.POSIXlt(Sys.time(), tz = "GMT"), creator = Sys.getenv("LOGNAME")),
554 : feinerer 836 children = list())
555 : feinerer 958 data <- vector("list", length(from))
556 : feinerer 836 counter <- 1
557 :     for (f in from) {
558 : feinerer 958 data[[counter]] <- new("PlainTextDocument",
559 :     .Data = f,
560 :     DateTimeStamp = as.POSIXlt(Sys.time(), tz = "GMT"),
561 :     ID = as.character(counter),
562 :     Language = "eng")
563 : feinerer 836 counter <- counter + 1
564 :     }
565 : feinerer 963 new("VCorpus", .Data = data,
566 : feinerer 946 DMetaData = data.frame(MetaID = rep(0, length(from)), stringsAsFactors = FALSE),
567 :     CMetaData = cmeta.node)
568 : feinerer 836 })
569 :    
570 : feinerer 819 setGeneric("writeCorpus", function(object, path = ".", filenames = NULL) standardGeneric("writeCorpus"))
571 :     setMethod("writeCorpus",
572 :     signature(object = "Corpus"),
573 :     function(object, path = ".", filenames = NULL) {
574 :     filenames <- file.path(path,
575 :     if (is.null(filenames)) sapply(object, function(x) sprintf("%s.txt", ID(x)))
576 :     else filenames)
577 :     i <- 1
578 :     for (o in object) {
579 : feinerer 859 writeLines(asPlain(o), filenames[i])
580 : feinerer 819 i <- i + 1
581 :     }
582 :     })

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business University of Wisconsin - Madison Powered By FusionForge