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

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