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 952 - (view) (download)

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

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