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 973 - (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 973 setMethod("asPlain", signature(object = "MailDocument"),
215 :     function(object, FUN, ...) as(object, "PlainTextDocument"))
216 : feinerer 757 setMethod("asPlain",
217 : feinerer 775 signature(object = "StructuredTextDocument"),
218 :     function(object, FUN, ...) {
219 : feinerer 946 new("PlainTextDocument", .Data = unlist(Content(object)),
220 :     Author = Author(object), DateTimeStamp = DateTimeStamp(object),
221 : feinerer 775 Description = Description(object), ID = ID(object), Origin = Origin(object),
222 : feinerer 826 Heading = Heading(object), Language = Language(object),
223 :     LocalMetaData = LocalMetaData(object))
224 : feinerer 775 })
225 : feinerer 49
226 : feinerer 854 setGeneric("tmFilter", function(object, ..., FUN = searchFullText, doclevel = TRUE) standardGeneric("tmFilter"))
227 : feinerer 946 setMethod("tmFilter", signature(object = "Corpus"),
228 :     function(object, ..., FUN = searchFullText, doclevel = TRUE)
229 :     object[tmIndex(object, ..., FUN = FUN, doclevel = doclevel)])
230 : feinerer 61
231 : feinerer 854 setGeneric("tmIndex", function(object, ..., FUN = searchFullText, doclevel = TRUE) standardGeneric("tmIndex"))
232 : feinerer 698 setMethod("tmIndex",
233 : feinerer 816 signature(object = "Corpus"),
234 : feinerer 854 function(object, ..., FUN = searchFullText, doclevel = TRUE) {
235 :     if (!is.null(attr(FUN, "doclevel")))
236 :     doclevel <- attr(FUN, "doclevel")
237 : feinerer 861 if (doclevel) {
238 :     if (clusterAvailable())
239 : feinerer 870 return(snow::parSapply(snow::getMPIcluster(), object, FUN, ..., DMetaData = DMetaData(object)))
240 : feinerer 861 else
241 :     return(sapply(object, FUN, ..., DMetaData = DMetaData(object)))
242 :     }
243 : feinerer 73 else
244 : feinerer 724 return(FUN(object, ...))
245 : feinerer 49 })
246 :    
247 : feinerer 698 setGeneric("appendElem", function(object, data, meta = NULL) standardGeneric("appendElem"))
248 :     setMethod("appendElem",
249 : feinerer 816 signature(object = "Corpus", data = "TextDocument"),
250 : feinerer 72 function(object, data, meta = NULL) {
251 : feinerer 886 if (DBControl(object)[["useDb"]] && require("filehash")) {
252 : feinerer 712 db <- dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
253 :     if (dbExists(db, ID(data)))
254 :     warning("document with identical ID already exists")
255 :     dbInsert(db, ID(data), data)
256 :     object@.Data[[length(object)+1]] <- ID(data)
257 :     }
258 :     else
259 :     object@.Data[[length(object)+1]] <- data
260 :     DMetaData(object) <- rbind(DMetaData(object), c(MetaID = CMetaData(object)@NodeID, meta))
261 : feinerer 52 return(object)
262 :     })
263 :    
264 : feinerer 946 prescindMeta <- function(object, meta) {
265 :     df <- DMetaData(object)
266 : feinerer 53
267 : feinerer 946 for (m in meta)
268 :     df <- cbind(df, structure(data.frame(I(meta(object, tag = m, type = "local"))), names = m))
269 : feinerer 69
270 : feinerer 946 df
271 :     }
272 :    
273 : feinerer 962 #setMethod("[",
274 :     # signature(x = "FCorpus", i = "ANY", j = "ANY", drop = "ANY"),
275 :     # function(x, i, j, ... , drop) {
276 :     # if (missing(i)) return(x)
277 :     #
278 :     # x@.Data <- x@.Data[i, ..., drop = FALSE]
279 :     # x
280 :     # })
281 : feinerer 946 setMethod("[",
282 :     signature(x = "PCorpus", i = "ANY", j = "ANY", drop = "ANY"),
283 :     function(x, i, j, ... , drop) {
284 :     if (missing(i)) return(x)
285 :    
286 :     x@.Data <- x@.Data[i, ..., drop = FALSE]
287 :     index <- x@DMetaData[[1 , "subset"]]
288 :     if (any(is.na(index))) x@DMetaData[[1 , "subset"]] <- i
289 :     else x@DMetaData[[1 , "subset"]] <- index[i]
290 :     x
291 : feinerer 73 })
292 : feinerer 53 setMethod("[",
293 : feinerer 963 signature(x = "VCorpus", i = "ANY", j = "ANY", drop = "ANY"),
294 : feinerer 53 function(x, i, j, ... , drop) {
295 : feinerer 946 if (missing(i)) return(x)
296 : feinerer 53
297 : feinerer 946 x@.Data <- x@.Data[i, ..., drop = FALSE]
298 :     DMetaData(x) <- DMetaData(x)[i, , drop = FALSE]
299 :     x
300 : feinerer 53 })
301 :    
302 : feinerer 63 setMethod("[<-",
303 : feinerer 946 signature(x = "PCorpus", i = "ANY", j = "ANY", value = "ANY"),
304 : feinerer 63 function(x, i, j, ... , value) {
305 : feinerer 946 db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
306 :     counter <- 1
307 :     for (id in x@.Data[i, ...]) {
308 :     if (identical(length(value), 1)) db[[id]] <- value
309 :     else db[[id]] <- value[[counter]]
310 :     counter <- counter + 1
311 : feinerer 724 }
312 : feinerer 946 x
313 : feinerer 63 })
314 : feinerer 946 setMethod("[<-",
315 : feinerer 963 signature(x = "VCorpus", i = "ANY", j = "ANY", value = "ANY"),
316 : feinerer 946 function(x, i, j, ... , value) {
317 :     x@.Data[i, ...] <- value
318 :     x
319 :     })
320 : feinerer 63
321 :     setMethod("[[",
322 : feinerer 946 signature(x = "PCorpus", i = "ANY", j = "ANY"),
323 : feinerer 63 function(x, i, j, ...) {
324 : feinerer 946 db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
325 :     filehash::dbFetch(db, x@.Data[[i]])
326 : feinerer 63 })
327 : feinerer 946 setMethod("[[",
328 : feinerer 963 signature(x = "VCorpus", i = "ANY", j = "ANY"),
329 : feinerer 946 function(x, i, j, ...) {
330 :     lazyTmMap <- meta(x, tag = "lazyTmMap", type = "corpus")
331 :     if (!is.null(lazyTmMap))
332 :     .Call("copyCorpus", x, materialize(x, i))
333 :     x@.Data[[i]]
334 :     })
335 : feinerer 63
336 :     setMethod("[[<-",
337 : feinerer 946 signature(x = "PCorpus", i = "ANY", j = "ANY", value = "ANY"),
338 : feinerer 63 function(x, i, j, ..., value) {
339 : feinerer 946 db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
340 :     index <- x@.Data[[i]]
341 :     db[[index]] <- value
342 :     x
343 :     })
344 :     setMethod("[[<-",
345 : feinerer 963 signature(x = "VCorpus", i = "ANY", j = "ANY", value = "ANY"),
346 : feinerer 946 function(x, i, j, ..., value) {
347 :     # Mark new objects as not active for lazy mapping
348 :     lazyTmMap <- meta(x, tag = "lazyTmMap", type = "corpus")
349 :     if (!is.null(lazyTmMap)) {
350 :     lazyTmMap$index[i] <- FALSE
351 :     meta(x, tag = "lazyTmMap", type = "corpus") <- lazyTmMap
352 : feinerer 712 }
353 : feinerer 946 # Set the value
354 :     x@.Data[[i, ...]] <- value
355 :    
356 :     x
357 : feinerer 63 })
358 :    
359 : feinerer 698 # Update \code{NodeID}s of a CMetaData tree
360 : feinerer 697 update_id <- function(object, id = 0, mapping = NULL, left.mapping = NULL, level = 0) {
361 : feinerer 698 # Traversal of (binary) CMetaData tree with setup of \code{NodeID}s
362 : feinerer 697 set_id <- function(object) {
363 :     object@NodeID <- id
364 :     id <<- id + 1
365 :     level <<- level + 1
366 : feinerer 71
367 : feinerer 697 if (length(object@children) > 0) {
368 :     mapping <<- cbind(mapping, c(object@children[[1]]@NodeID, id))
369 :     left <- set_id(object@children[[1]])
370 :     if (level == 1) {
371 :     left.mapping <<- mapping
372 :     mapping <<- NULL
373 :     }
374 :     mapping <<- cbind(mapping, c(object@children[[2]]@NodeID, id))
375 :     right <- set_id(object@children[[2]])
376 : feinerer 71
377 : feinerer 697 object@children <- list(left, right)
378 : feinerer 71 }
379 : feinerer 697 level <<- level - 1
380 : feinerer 71
381 : feinerer 697 return(object)
382 : feinerer 71 }
383 :    
384 : feinerer 952 list(root = set_id(object), left.mapping = left.mapping, right.mapping = mapping)
385 : feinerer 71 }
386 :    
387 : feinerer 53 setMethod("c",
388 : feinerer 816 signature(x = "Corpus"),
389 : feinerer 958 function(x, ..., meta = list(merge_date = as.POSIXlt(Sys.time(), tz = "GMT"), merger = Sys.getenv("LOGNAME")), recursive = FALSE) {
390 : feinerer 689 args <- list(...)
391 : feinerer 952 if (identical(length(args), 0)) return(x)
392 : feinerer 71
393 : feinerer 952 if (!all(sapply(args, inherits, class(x))))
394 : feinerer 958 stop("not all arguments are of the same corpus type")
395 : feinerer 720
396 : feinerer 958 if (inherits(x, "PCorpus"))
397 :     stop("concatenation of corpora with underlying databases is not supported")
398 :    
399 : feinerer 902 Reduce(c2, base::c(list(x), args))
400 : feinerer 689 })
401 :    
402 : feinerer 958 setGeneric("c2", function(x, y, ..., meta = list(merge_date = as.POSIXlt(Sys.time(), tz = "GMT"), merger = Sys.getenv("LOGNAME"))) standardGeneric("c2"))
403 : feinerer 962 #setMethod("c2", signature(x = "FCorpus", y = "FCorpus"),
404 :     # function(x, y, ..., meta = list(merge_date = as.POSIXlt(Sys.time(), tz = "GMT"), merger = Sys.getenv("LOGNAME"))) {
405 :     # new("FCorpus", .Data = c(as(x, "list"), as(y, "list")))
406 :     # })
407 : feinerer 963 setMethod("c2", signature(x = "VCorpus", y = "VCorpus"),
408 : feinerer 958 function(x, y, ..., meta = list(merge_date = as.POSIXlt(Sys.time(), tz = "GMT"), merger = Sys.getenv("LOGNAME"))) {
409 : feinerer 71 object <- x
410 :     # Concatenate data slots
411 :     object@.Data <- c(as(x, "list"), as(y, "list"))
412 :    
413 : feinerer 698 # Update the CMetaData tree
414 :     cmeta <- new("MetaDataNode", NodeID = 0, MetaData = meta, children = list(CMetaData(x), CMetaData(y)))
415 :     update.struct <- update_id(cmeta)
416 :     object@CMetaData <- update.struct$root
417 : feinerer 71
418 :     # Find indices to be updated for the left tree
419 :     indices.mapping <- NULL
420 :     for (m in levels(as.factor(DMetaData(x)$MetaID))) {
421 :     indices <- (DMetaData(x)$MetaID == m)
422 :     indices.mapping <- c(indices.mapping, list(m = indices))
423 :     names(indices.mapping)[length(indices.mapping)] <- m
424 :     }
425 :    
426 :     # Update the DMetaData data frames for the left tree
427 :     for (i in 1:ncol(update.struct$left.mapping)) {
428 :     map <- update.struct$left.mapping[,i]
429 :     x@DMetaData$MetaID <- replace(DMetaData(x)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
430 :     }
431 :    
432 :     # Find indices to be updated for the right tree
433 :     indices.mapping <- NULL
434 :     for (m in levels(as.factor(DMetaData(y)$MetaID))) {
435 :     indices <- (DMetaData(y)$MetaID == m)
436 :     indices.mapping <- c(indices.mapping, list(m = indices))
437 :     names(indices.mapping)[length(indices.mapping)] <- m
438 :     }
439 :    
440 :     # Update the DMetaData data frames for the right tree
441 :     for (i in 1:ncol(update.struct$right.mapping)) {
442 :     map <- update.struct$right.mapping[,i]
443 :     y@DMetaData$MetaID <- replace(DMetaData(y)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
444 :     }
445 :    
446 :     # Merge the DMetaData data frames
447 :     labels <- setdiff(names(DMetaData(y)), names(DMetaData(x)))
448 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(x)), ncol = length(labels), dimnames = list(row.names(DMetaData(x)), labels))
449 :     x.dmeta.aug <- cbind(DMetaData(x), na.matrix)
450 :     labels <- setdiff(names(DMetaData(x)), names(DMetaData(y)))
451 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(y)), ncol = length(labels), dimnames = list(row.names(DMetaData(y)), labels))
452 :     y.dmeta.aug <- cbind(DMetaData(y), na.matrix)
453 :     object@DMetaData <- rbind(x.dmeta.aug, y.dmeta.aug)
454 :    
455 : feinerer 952 object
456 : feinerer 72 })
457 : feinerer 689
458 : feinerer 72 setMethod("c",
459 :     signature(x = "TextDocument"),
460 : feinerer 958 function(x, ..., recursive = FALSE){
461 : feinerer 72 args <- list(...)
462 : feinerer 946 if (identical(length(args), 0)) return(x)
463 : feinerer 54
464 : feinerer 73 dmeta.df <- data.frame(MetaID = rep(0, length(list(x, ...))), stringsAsFactors = FALSE)
465 : feinerer 698 cmeta.node <- new("MetaDataNode",
466 : feinerer 72 NodeID = 0,
467 : feinerer 905 MetaData = list(create_date = as.POSIXlt(Sys.time(), tz = "GMT"), creator = Sys.getenv("LOGNAME")),
468 : feinerer 72 children = list())
469 :    
470 : feinerer 963 new("VCorpus", .Data = list(x, ...), DMetaData = dmeta.df, CMetaData = cmeta.node)
471 : feinerer 72 })
472 :    
473 : feinerer 54 setMethod("show",
474 : feinerer 816 signature(object = "Corpus"),
475 : feinerer 54 function(object){
476 : feinerer 70 cat(sprintf(ngettext(length(object),
477 : feinerer 894 "A corpus with %d text document\n",
478 :     "A corpus with %d text documents\n"),
479 : feinerer 70 length(object)))
480 : feinerer 54 })
481 :    
482 :     setMethod("summary",
483 : feinerer 816 signature(object = "Corpus"),
484 : feinerer 54 function(object){
485 :     show(object)
486 : feinerer 71 if (length(DMetaData(object)) > 0) {
487 : feinerer 698 cat(sprintf(ngettext(length(CMetaData(object)@MetaData),
488 : feinerer 77 "\nThe metadata consists of %d tag-value pair and a data frame\n",
489 :     "\nThe metadata consists of %d tag-value pairs and a data frame\n"),
490 : feinerer 698 length(CMetaData(object)@MetaData)))
491 : feinerer 54 cat("Available tags are:\n")
492 : feinerer 722 cat(strwrap(paste(names(CMetaData(object)@MetaData), collapse = " "), indent = 2, exdent = 2), "\n")
493 : feinerer 77 cat("Available variables in the data frame are:\n")
494 : feinerer 722 cat(strwrap(paste(names(DMetaData(object)), collapse = " "), indent = 2, exdent = 2), "\n")
495 : feinerer 54 }
496 :     })
497 : feinerer 55
498 : feinerer 938 inspect <- function(x) UseMethod("inspect", x)
499 : feinerer 946 inspect.PCorpus <- function(x) {
500 : feinerer 938 summary(x)
501 :     cat("\n")
502 : feinerer 946 db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
503 :     show(filehash::dbMultiFetch(db, unlist(x)))
504 : feinerer 938 }
505 : feinerer 962 #inspect.FCorpus <-
506 : feinerer 963 inspect.VCorpus <- function(x) {
507 : feinerer 946 summary(x)
508 :     cat("\n")
509 :     print(noquote(lapply(x, identity)))
510 :     }
511 : feinerer 65
512 :     # No metadata is checked
513 :     setGeneric("%IN%", function(x, y) standardGeneric("%IN%"))
514 : feinerer 946 setMethod("%IN%", signature(x = "TextDocument", y = "PCorpus"),
515 : feinerer 65 function(x, y) {
516 : feinerer 946 db <- filehash::dbInit(DBControl(y)[["dbName"]], DBControl(y)[["dbType"]])
517 :     any(sapply(y, function(x, z) {x %in% Content(z)}, x))
518 : feinerer 65 })
519 : feinerer 963 setMethod("%IN%", signature(x = "TextDocument", y = "VCorpus"),
520 : feinerer 946 function(x, y) x %in% y)
521 : feinerer 719
522 :     setMethod("lapply",
523 : feinerer 963 signature(X = "VCorpus"),
524 : feinerer 719 function(X, FUN, ...) {
525 : feinerer 946 lazyTmMap <- meta(X, tag = "lazyTmMap", type = "corpus")
526 :     if (!is.null(lazyTmMap))
527 :     .Call("copyCorpus", X, materialize(X))
528 :     base::lapply(X, FUN, ...)
529 : feinerer 719 })
530 : feinerer 946 setMethod("lapply",
531 :     signature(X = "PCorpus"),
532 :     function(X, FUN, ...) {
533 :     db <- filehash::dbInit(DBControl(X)[["dbName"]], DBControl(X)[["dbType"]])
534 :     lapply(filehash::dbMultiFetch(db, unlist(X)), FUN, ...)
535 :     })
536 : feinerer 719
537 :     setMethod("sapply",
538 : feinerer 963 signature(X = "VCorpus"),
539 : feinerer 719 function(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) {
540 : feinerer 946 lazyTmMap <- meta(X, tag = "lazyTmMap", type = "corpus")
541 :     if (!is.null(lazyTmMap))
542 :     .Call("copyCorpus", X, materialize(X))
543 :     base::sapply(X, FUN, ...)
544 : feinerer 719 })
545 : feinerer 946 setMethod("sapply",
546 :     signature(X = "PCorpus"),
547 :     function(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) {
548 :     db <- filehash::dbInit(DBControl(X)[["dbName"]], DBControl(X)[["dbType"]])
549 :     sapply(filehash::dbMultiFetch(db, unlist(X)), FUN, ...)
550 :     })
551 : feinerer 819
552 : feinerer 963 setAs("list", "VCorpus", function(from) {
553 : feinerer 836 cmeta.node <- new("MetaDataNode",
554 :     NodeID = 0,
555 : feinerer 905 MetaData = list(create_date = as.POSIXlt(Sys.time(), tz = "GMT"), creator = Sys.getenv("LOGNAME")),
556 : feinerer 836 children = list())
557 : feinerer 958 data <- vector("list", length(from))
558 : feinerer 836 counter <- 1
559 :     for (f in from) {
560 : feinerer 958 data[[counter]] <- new("PlainTextDocument",
561 :     .Data = f,
562 :     DateTimeStamp = as.POSIXlt(Sys.time(), tz = "GMT"),
563 :     ID = as.character(counter),
564 :     Language = "eng")
565 : feinerer 836 counter <- counter + 1
566 :     }
567 : feinerer 963 new("VCorpus", .Data = data,
568 : feinerer 946 DMetaData = data.frame(MetaID = rep(0, length(from)), stringsAsFactors = FALSE),
569 :     CMetaData = cmeta.node)
570 : feinerer 836 })
571 :    
572 : feinerer 819 setGeneric("writeCorpus", function(object, path = ".", filenames = NULL) standardGeneric("writeCorpus"))
573 :     setMethod("writeCorpus",
574 :     signature(object = "Corpus"),
575 :     function(object, path = ".", filenames = NULL) {
576 :     filenames <- file.path(path,
577 :     if (is.null(filenames)) sapply(object, function(x) sprintf("%s.txt", ID(x)))
578 :     else filenames)
579 :     i <- 1
580 :     for (o in object) {
581 : feinerer 859 writeLines(asPlain(o), filenames[i])
582 : feinerer 819 i <- i + 1
583 :     }
584 :     })

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