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

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