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 722 - (view) (download)
Original Path: trunk/tm/R/textdoccol.R

1 : feinerer 17 # Author: Ingo Feinerer
2 :    
3 : feinerer 722 # The "..." are additional arguments for the FunctionGenerator reader
4 : feinerer 712 setGeneric("TextDocCol", function(object,
5 : feinerer 722 readerControl = list(reader = readPlain, language = "en_US", load = FALSE),
6 : feinerer 712 dbControl = list(useDb = FALSE, dbName = "", dbType = "DB1"),
7 :     ...) standardGeneric("TextDocCol"))
8 : feinerer 49 setMethod("TextDocCol",
9 : feinerer 63 signature(object = "Source"),
10 : feinerer 717 function(object,
11 : feinerer 722 readerControl = list(reader = readPlain, language = "en_US", load = FALSE),
12 : feinerer 717 dbControl = list(useDb = FALSE, dbName = "", dbType = "DB1"),
13 :     ...) {
14 : feinerer 722 if (attr(readerControl$reader, "FunctionGenerator"))
15 :     readerControl$reader <- readerControl$reader(...)
16 : feinerer 63
17 : feinerer 712 if (dbControl$useDb) {
18 :     if (!dbCreate(dbControl$dbName, dbControl$dbType))
19 :     stop("error in creating database")
20 :     db <- dbInit(dbControl$dbName, dbControl$dbType)
21 :     }
22 :    
23 : feinerer 63 tdl <- list()
24 :     counter <- 1
25 :     while (!eoi(object)) {
26 : feinerer 698 object <- stepNext(object)
27 :     elem <- getElem(object)
28 : feinerer 63 # If there is no Load on Demand support
29 :     # we need to load the corpus into memory at startup
30 : feinerer 694 if (!object@LoDSupport)
31 : feinerer 722 readerControl$load <- TRUE
32 :     doc <- readerControl$reader(elem, readerControl$load, readerControl$language, as.character(counter))
33 : feinerer 712 if (dbControl$useDb) {
34 :     dbInsert(db, ID(doc), doc)
35 :     tdl <- c(tdl, ID(doc))
36 :     }
37 :     else
38 :     tdl <- c(tdl, list(doc))
39 : feinerer 63 counter <- counter + 1
40 :     }
41 :    
42 : feinerer 712 df <- data.frame(MetaID = rep(0, length(tdl)), stringsAsFactors = FALSE)
43 :     if (dbControl$useDb) {
44 :     dbInsert(db, "DMetaData", df)
45 :     dmeta.df <- data.frame(key = "DMetaData")
46 :     dbDisconnect(db)
47 :     }
48 :     else
49 :     dmeta.df <- df
50 :    
51 : feinerer 698 cmeta.node <- new("MetaDataNode",
52 : feinerer 71 NodeID = 0,
53 : feinerer 689 MetaData = list(create_date = Sys.time(), creator = Sys.getenv("LOGNAME")),
54 : feinerer 71 children = list())
55 :    
56 : feinerer 712 return(new("TextDocCol", .Data = tdl, DMetaData = dmeta.df, CMetaData = cmeta.node, DBControl = dbControl))
57 : feinerer 21 })
58 :    
59 : feinerer 698 setGeneric("loadDoc", function(object, ...) standardGeneric("loadDoc"))
60 :     setMethod("loadDoc",
61 : feinerer 62 signature(object = "PlainTextDocument"),
62 : feinerer 65 function(object, ...) {
63 : feinerer 61 if (!Cached(object)) {
64 : feinerer 67 con <- eval(URI(object))
65 : feinerer 65 corpus <- readLines(con)
66 :     close(con)
67 : feinerer 56 Corpus(object) <- corpus
68 : feinerer 60 Cached(object) <- TRUE
69 : feinerer 56 return(object)
70 :     } else {
71 :     return(object)
72 :     }
73 :     })
74 : feinerer 698 setMethod("loadDoc",
75 : feinerer 62 signature(object = "XMLTextDocument"),
76 : feinerer 65 function(object, ...) {
77 : feinerer 61 if (!Cached(object)) {
78 : feinerer 67 con <- eval(URI(object))
79 : feinerer 65 corpus <- paste(readLines(con), "\n", collapse = "")
80 :     close(con)
81 :     doc <- xmlTreeParse(corpus, asText = TRUE)
82 : feinerer 49 class(doc) <- "list"
83 : feinerer 56 Corpus(object) <- doc
84 : feinerer 60 Cached(object) <- TRUE
85 : feinerer 49 return(object)
86 :     } else {
87 :     return(object)
88 :     }
89 :     })
90 : feinerer 698 setMethod("loadDoc",
91 : feinerer 62 signature(object = "NewsgroupDocument"),
92 : feinerer 65 function(object, ...) {
93 : feinerer 61 if (!Cached(object)) {
94 : feinerer 67 con <- eval(URI(object))
95 : feinerer 65 mail <- readLines(con)
96 :     close(con)
97 : feinerer 60 Cached(object) <- TRUE
98 : feinerer 65 for (index in seq(along = mail)) {
99 :     if (mail[index] == "")
100 :     break
101 :     }
102 : feinerer 56 Corpus(object) <- mail[(index + 1):length(mail)]
103 :     return(object)
104 :     } else {
105 :     return(object)
106 :     }
107 :     })
108 : feinerer 49
109 : feinerer 719 setGeneric("tmUpdate", function(object,
110 :     origin,
111 : feinerer 722 readerControl = list(reader = readPlain, language = "en_US", load = FALSE),
112 : feinerer 719 ...) standardGeneric("tmUpdate"))
113 : feinerer 72 # Update is only supported for directories
114 :     # At the moment no other LoD devices are available anyway
115 : feinerer 698 setMethod("tmUpdate",
116 : feinerer 72 signature(object = "TextDocCol", origin = "DirSource"),
117 : feinerer 719 function(object, origin,
118 : feinerer 722 readerControl = list(reader = readPlain, language = "en_US", load = FALSE),
119 : feinerer 719 ...) {
120 : feinerer 722 if (inherits(readerControl$reader, "FunctionGenerator"))
121 :     readerControl$reader <- readerControl$reader(...)
122 : feinerer 72
123 :     object.filelist <- unlist(lapply(object, function(x) {as.character(URI(x))[2]}))
124 :     new.files <- setdiff(origin@FileList, object.filelist)
125 :    
126 :     for (filename in new.files) {
127 :     elem <- list(content = readLines(filename),
128 :     uri = substitute(file(filename)))
129 : feinerer 722 object <- appendElem(object, readerControl$reader(elem, readerControl$load, readerControl$language, filename))
130 : feinerer 72 }
131 :    
132 :     return(object)
133 :     })
134 :    
135 : feinerer 698 setGeneric("tmMap", function(object, FUN, ...) standardGeneric("tmMap"))
136 :     setMethod("tmMap",
137 : feinerer 62 signature(object = "TextDocCol", FUN = "function"),
138 : feinerer 49 function(object, FUN, ...) {
139 : feinerer 73 result <- object
140 : feinerer 689 # Note that text corpora are automatically loaded into memory via \code{[[}
141 : feinerer 719 if (DBControl(object)[["useDb"]]) {
142 :     db <- dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
143 :     new <- lapply(object, FUN, ..., DMetaData = DMetaData(object))
144 :     ids <- lapply(object, ID)
145 :     # Avoidance of explicit loop is probably more efficient
146 :     for (i in length(new)) {
147 :     db[[ids[i]]] <- new[[i]]
148 :     }
149 :     dbDisconnect(db)
150 :     }
151 :     else
152 :     result@.Data <- lapply(object, FUN, ..., DMetaData = DMetaData(object))
153 : feinerer 56 return(result)
154 : feinerer 49 })
155 :    
156 : feinerer 698 setGeneric("asPlain", function(object, FUN, ...) standardGeneric("asPlain"))
157 :     setMethod("asPlain",
158 : feinerer 62 signature(object = "PlainTextDocument"),
159 : feinerer 49 function(object, FUN, ...) {
160 :     return(object)
161 :     })
162 : feinerer 698 setMethod("asPlain",
163 : feinerer 62 signature(object = "XMLTextDocument", FUN = "function"),
164 : feinerer 49 function(object, FUN, ...) {
165 : feinerer 56 corpus <- Corpus(object)
166 : feinerer 49
167 :     # As XMLDocument is no native S4 class, restore valid information
168 :     class(corpus) <- "XMLDocument"
169 :     names(corpus) <- c("doc","dtd")
170 :    
171 : feinerer 61 return(FUN(xmlRoot(corpus), ...))
172 : feinerer 49 })
173 :    
174 : feinerer 698 setGeneric("tmTolower", function(object, ...) standardGeneric("tmTolower"))
175 :     setMethod("tmTolower",
176 : feinerer 67 signature(object = "PlainTextDocument"),
177 :     function(object, ...) {
178 :     Corpus(object) <- tolower(object)
179 :     return(object)
180 :     })
181 :    
182 : feinerer 698 setGeneric("stripWhitespace", function(object, ...) standardGeneric("stripWhitespace"))
183 :     setMethod("stripWhitespace",
184 : feinerer 67 signature(object = "PlainTextDocument"),
185 :     function(object, ...) {
186 :     Corpus(object) <- gsub("[[:space:]]+", " ", object)
187 :     return(object)
188 :     })
189 :    
190 : feinerer 713 setGeneric("stemDoc", function(object, language = "english", ...) standardGeneric("stemDoc"))
191 : feinerer 698 setMethod("stemDoc",
192 : feinerer 62 signature(object = "PlainTextDocument"),
193 : feinerer 713 function(object, language = "english", ...) {
194 : feinerer 49 splittedCorpus <- unlist(strsplit(object, " ", fixed = TRUE))
195 : feinerer 713 stemmedCorpus <- if (require("Rstem"))
196 :     Rstem::wordStem(splittedCorpus, language)
197 :     else
198 :     SnowballStemmer(splittedCorpus, Weka_control(S = language))
199 : feinerer 56 Corpus(object) <- paste(stemmedCorpus, collapse = " ")
200 :     return(object)
201 : feinerer 49 })
202 :    
203 : feinerer 722 setGeneric("removePunctuation", function(object, ...) standardGeneric("removePunctuation"))
204 :     setMethod("removePunctuation",
205 :     signature(object = "PlainTextDocument"),
206 :     function(object, ...) {
207 :     Corpus(object) <- gsub("[[:punct:]]+", "", Corpus(object))
208 :     return(object)
209 :     })
210 :    
211 : feinerer 698 setGeneric("removeWords", function(object, stopwords, ...) standardGeneric("removeWords"))
212 :     setMethod("removeWords",
213 : feinerer 60 signature(object = "PlainTextDocument", stopwords = "character"),
214 : feinerer 61 function(object, stopwords, ...) {
215 : feinerer 49 splittedCorpus <- unlist(strsplit(object, " ", fixed = TRUE))
216 :     noStopwordsCorpus <- splittedCorpus[!splittedCorpus %in% stopwords]
217 : feinerer 56 Corpus(object) <- paste(noStopwordsCorpus, collapse = " ")
218 :     return(object)
219 : feinerer 49 })
220 :    
221 : feinerer 698 setGeneric("tmFilter", function(object, ..., FUN = sFilter, doclevel = FALSE) standardGeneric("tmFilter"))
222 :     setMethod("tmFilter",
223 : feinerer 61 signature(object = "TextDocCol"),
224 : feinerer 698 function(object, ..., FUN = sFilter, doclevel = FALSE) {
225 : feinerer 73 if (doclevel)
226 :     return(object[sapply(object, FUN, ..., DMetaData = DMetaData(object))])
227 :     else
228 : feinerer 712 return(object[FUN(object, ...)]) # TODO: Check that FUN knows about the database
229 : feinerer 61 })
230 :    
231 : feinerer 698 setGeneric("tmIndex", function(object, ..., FUN = sFilter, doclevel = FALSE) standardGeneric("tmIndex"))
232 :     setMethod("tmIndex",
233 : feinerer 61 signature(object = "TextDocCol"),
234 : feinerer 698 function(object, ..., FUN = sFilter, doclevel = FALSE) {
235 : feinerer 73 if (doclevel)
236 :     return(sapply(object, FUN, ..., DMetaData = DMetaData(object)))
237 :     else
238 : feinerer 712 return(FUN(object, ...)) # TODO: Check that FUN knows about the database
239 : feinerer 49 })
240 :    
241 : feinerer 698 sFilter <- function(object, s, ...) {
242 : feinerer 73 con <- textConnection(s)
243 :     tokens <- scan(con, "character")
244 :     close(con)
245 : feinerer 721 localMetaNames <- unique(names(sapply(object, LocalMetaData)))
246 :     localMetaTokens <- localMetaNames[localMetaNames %in% tokens]
247 :     query.df <- DMetaData(prescindMeta(object,
248 :     c("Author", "DateTimeStamp", "Description", "ID",
249 :     "Origin", "Heading", "Language", localMetaTokens)))
250 :     # Rename to avoid name conflicts
251 :     names(query.df)[names(query.df) == "Author"] <- "author"
252 :     names(query.df)[names(query.df) == "DateTimeStamp"] <- "datetimestamp"
253 :     names(query.df)[names(query.df) == "Description"] <- "description"
254 :     names(query.df)[names(query.df) == "ID"] <- "identifier"
255 :     names(query.df)[names(query.df) == "Origin"] <- "origin"
256 :     names(query.df)[names(query.df) == "Heading"] <- "heading"
257 :     names(query.df)[names(query.df) == "Language"] <- "language"
258 : feinerer 73 attach(query.df)
259 : feinerer 74 try(result <- rownames(query.df) %in% row.names(query.df[eval(parse(text = s)), ]))
260 : feinerer 73 detach(query.df)
261 :     return(result)
262 : feinerer 61 }
263 :    
264 : feinerer 698 setGeneric("searchFullText", function(object, pattern, ...) standardGeneric("searchFullText"))
265 :     setMethod("searchFullText",
266 : feinerer 61 signature(object = "PlainTextDocument", pattern = "character"),
267 :     function(object, pattern, ...) {
268 :     return(any(grep(pattern, Corpus(object))))
269 :     })
270 :    
271 : feinerer 698 setGeneric("appendElem", function(object, data, meta = NULL) standardGeneric("appendElem"))
272 :     setMethod("appendElem",
273 : feinerer 72 signature(object = "TextDocCol", data = "TextDocument"),
274 :     function(object, data, meta = NULL) {
275 : feinerer 712 if (DBControl(object)[["useDb"]]) {
276 :     db <- dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
277 :     if (dbExists(db, ID(data)))
278 :     warning("document with identical ID already exists")
279 :     dbInsert(db, ID(data), data)
280 :     dbDisconnect(db)
281 :     object@.Data[[length(object)+1]] <- ID(data)
282 :     }
283 :     else
284 :     object@.Data[[length(object)+1]] <- data
285 :     DMetaData(object) <- rbind(DMetaData(object), c(MetaID = CMetaData(object)@NodeID, meta))
286 : feinerer 52 return(object)
287 :     })
288 :    
289 : feinerer 698 setGeneric("appendMeta", function(object, cmeta = NULL, dmeta = NULL) standardGeneric("appendMeta"))
290 :     setMethod("appendMeta",
291 : feinerer 72 signature(object = "TextDocCol"),
292 : feinerer 698 function(object, cmeta = NULL, dmeta = NULL) {
293 : feinerer 712 object@CMetaData@MetaData <- c(CMetaData(object)@MetaData, cmeta)
294 :     if (!is.null(dmeta)) {
295 : feinerer 719 DMetaData(object) <- cbind(DMetaData(object), eval(dmeta))
296 : feinerer 712 }
297 : feinerer 52 return(object)
298 :     })
299 : feinerer 53
300 : feinerer 698 setGeneric("removeMeta", function(object, cname = NULL, dname = NULL) standardGeneric("removeMeta"))
301 :     setMethod("removeMeta",
302 : feinerer 77 signature(object = "TextDocCol"),
303 : feinerer 698 function(object, cname = NULL, dname = NULL) {
304 :     if (!is.null(cname)) {
305 :     object@CMetaData@MetaData <- CMetaData(object)@MetaData[names(CMetaData(object)@MetaData) != cname]
306 : feinerer 77 }
307 :     if (!is.null(dname)) {
308 : feinerer 712 DMetaData(object) <- DMetaData(object)[names(DMetaData(object)) != dname]
309 : feinerer 77 }
310 :     return(object)
311 :     })
312 : feinerer 69
313 : feinerer 721 # WARNING: If dbUse the augmented dataframe is stored (watch out since sFilter calls this method)
314 : feinerer 698 setGeneric("prescindMeta", function(object, meta) standardGeneric("prescindMeta"))
315 :     setMethod("prescindMeta",
316 : feinerer 73 signature(object = "TextDocCol", meta = "character"),
317 :     function(object, meta) {
318 :     for (m in meta) {
319 : feinerer 719 if (m %in% c("Author", "DateTimeStamp", "Description", "ID", "Origin", "Heading", "Language")) {
320 : feinerer 75 local.m <- lapply(object, m)
321 :     local.m <- lapply(local.m, function(x) if (is.null(x)) return(NA) else return(x))
322 : feinerer 73 local.m <- unlist(local.m)
323 : feinerer 721 DMetaData(object) <- cbind(DMetaData(object), data.frame(m = local.m, stringsAsFactors = FALSE))
324 :     names(DMetaData(object))[which(names(DMetaData(object)) == "m")] <- m
325 : feinerer 75 }
326 :     else {
327 :     local.meta <- lapply(object, LocalMetaData)
328 :     local.m <- lapply(local.meta, "[[", m)
329 :     local.m <- lapply(local.m, function(x) if (is.null(x)) return(NA) else return(x))
330 :     if (length(local.m) == length(unlist(local.m)))
331 :     local.m <- unlist(local.m)
332 :     else
333 :     local.m <- I(local.m)
334 : feinerer 721 DMetaData(object) <- cbind(DMetaData(object), data.frame(m = local.m, stringsAsFactors = FALSE))
335 :     names(DMetaData(object))[which(names(DMetaData(object)) == "m")] <- m
336 : feinerer 75 }
337 : feinerer 73 }
338 :     return(object)
339 :     })
340 :    
341 : feinerer 712 # WARNING: DMetaData is changed (since both use the same database)
342 : feinerer 53 setMethod("[",
343 :     signature(x = "TextDocCol", i = "ANY", j = "ANY", drop = "ANY"),
344 :     function(x, i, j, ... , drop) {
345 :     if(missing(i))
346 :     return(x)
347 :    
348 :     object <- x
349 :     object@.Data <- x@.Data[i, ..., drop = FALSE]
350 : feinerer 715 df <- as.data.frame(DMetaData(x)[i, ])
351 :     names(df) <- names(DMetaData(x))
352 :     DMetaData(object) <- df
353 : feinerer 53 return(object)
354 :     })
355 :    
356 : feinerer 712 # TODO
357 : feinerer 63 setMethod("[<-",
358 :     signature(x = "TextDocCol", i = "ANY", j = "ANY", value = "ANY"),
359 :     function(x, i, j, ... , value) {
360 :     object <- x
361 :     object@.Data[i, ...] <- value
362 :     return(object)
363 :     })
364 :    
365 :     setMethod("[[",
366 :     signature(x = "TextDocCol", i = "ANY", j = "ANY"),
367 :     function(x, i, j, ...) {
368 : feinerer 712 if (DBControl(x)[["useDb"]]) {
369 :     db <- dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
370 :     result <- dbFetch(db, x@.Data[[i]])
371 :     dbDisconnect(db)
372 :     return(loadDoc(result))
373 :     }
374 :     else
375 :     return(loadDoc(x@.Data[[i]]))
376 : feinerer 63 })
377 :    
378 :     setMethod("[[<-",
379 :     signature(x = "TextDocCol", i = "ANY", j = "ANY", value = "ANY"),
380 :     function(x, i, j, ..., value) {
381 :     object <- x
382 : feinerer 712 if (DBControl(object)[["useDb"]]) {
383 :     db <- dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
384 :     index <- object@.Data[[i]]
385 :     db[[index]] <- value
386 :     dbDisconnect(db)
387 :     }
388 :     else
389 :     object@.Data[[i, ...]] <- value
390 : feinerer 63 return(object)
391 :     })
392 :    
393 : feinerer 698 # Update \code{NodeID}s of a CMetaData tree
394 : feinerer 697 update_id <- function(object, id = 0, mapping = NULL, left.mapping = NULL, level = 0) {
395 : feinerer 698 # Traversal of (binary) CMetaData tree with setup of \code{NodeID}s
396 : feinerer 697 set_id <- function(object) {
397 :     object@NodeID <- id
398 :     id <<- id + 1
399 :     level <<- level + 1
400 : feinerer 71
401 : feinerer 697 if (length(object@children) > 0) {
402 :     mapping <<- cbind(mapping, c(object@children[[1]]@NodeID, id))
403 :     left <- set_id(object@children[[1]])
404 :     if (level == 1) {
405 :     left.mapping <<- mapping
406 :     mapping <<- NULL
407 :     }
408 :     mapping <<- cbind(mapping, c(object@children[[2]]@NodeID, id))
409 :     right <- set_id(object@children[[2]])
410 : feinerer 71
411 : feinerer 697 object@children <- list(left, right)
412 : feinerer 71 }
413 : feinerer 697 level <<- level - 1
414 : feinerer 71
415 : feinerer 697 return(object)
416 : feinerer 71 }
417 :    
418 : feinerer 697 return(list(root = set_id(object), left.mapping = left.mapping, right.mapping = mapping))
419 : feinerer 71 }
420 :    
421 : feinerer 53 setMethod("c",
422 :     signature(x = "TextDocCol"),
423 : feinerer 689 function(x, ..., meta = list(merge_date = Sys.time(), merger = Sys.getenv("LOGNAME")), recursive = TRUE) {
424 :     args <- list(...)
425 : feinerer 720 if (length(args) == 0)
426 : feinerer 689 return(x)
427 : feinerer 71
428 : feinerer 720 if (!all(sapply(args, inherits, "TextDocCol")))
429 :     stop("not all arguments are text document collections")
430 :     if (DBControl(x)$useDb == TRUE || any(unlist(sapply(args, DBControl)["useDb", ])))
431 :     stop("concatenating text document collections with activated database is not supported")
432 :    
433 : feinerer 689 result <- x
434 :     for (c in args) {
435 :     result <- c2(result, c)
436 :     }
437 :     return(result)
438 :     })
439 :    
440 :     setGeneric("c2", function(x, y, ..., meta = list(merge_date = Sys.time(), merger = Sys.getenv("LOGNAME")), recursive = TRUE) standardGeneric("c2"))
441 :     setMethod("c2",
442 :     signature(x = "TextDocCol", y = "TextDocCol"),
443 :     function(x, y, ..., meta = list(merge_date = Sys.time(), merger = Sys.getenv("LOGNAME")), recursive = TRUE) {
444 : feinerer 71 object <- x
445 :     # Concatenate data slots
446 :     object@.Data <- c(as(x, "list"), as(y, "list"))
447 :    
448 : feinerer 720 # Set the DBControl slot
449 :     object@DBControl <- list(useDb = FALSE, dbName = "", dbType = "DB1")
450 :    
451 : feinerer 698 # Update the CMetaData tree
452 :     cmeta <- new("MetaDataNode", NodeID = 0, MetaData = meta, children = list(CMetaData(x), CMetaData(y)))
453 :     update.struct <- update_id(cmeta)
454 :     object@CMetaData <- update.struct$root
455 : feinerer 71
456 :     # Find indices to be updated for the left tree
457 :     indices.mapping <- NULL
458 :     for (m in levels(as.factor(DMetaData(x)$MetaID))) {
459 :     indices <- (DMetaData(x)$MetaID == m)
460 :     indices.mapping <- c(indices.mapping, list(m = indices))
461 :     names(indices.mapping)[length(indices.mapping)] <- m
462 :     }
463 :    
464 :     # Update the DMetaData data frames for the left tree
465 :     for (i in 1:ncol(update.struct$left.mapping)) {
466 :     map <- update.struct$left.mapping[,i]
467 :     x@DMetaData$MetaID <- replace(DMetaData(x)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
468 :     }
469 :    
470 :     # Find indices to be updated for the right tree
471 :     indices.mapping <- NULL
472 :     for (m in levels(as.factor(DMetaData(y)$MetaID))) {
473 :     indices <- (DMetaData(y)$MetaID == m)
474 :     indices.mapping <- c(indices.mapping, list(m = indices))
475 :     names(indices.mapping)[length(indices.mapping)] <- m
476 :     }
477 :    
478 :     # Update the DMetaData data frames for the right tree
479 :     for (i in 1:ncol(update.struct$right.mapping)) {
480 :     map <- update.struct$right.mapping[,i]
481 :     y@DMetaData$MetaID <- replace(DMetaData(y)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
482 :     }
483 :    
484 :     # Merge the DMetaData data frames
485 :     labels <- setdiff(names(DMetaData(y)), names(DMetaData(x)))
486 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(x)), ncol = length(labels), dimnames = list(row.names(DMetaData(x)), labels))
487 :     x.dmeta.aug <- cbind(DMetaData(x), na.matrix)
488 :     labels <- setdiff(names(DMetaData(x)), names(DMetaData(y)))
489 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(y)), ncol = length(labels), dimnames = list(row.names(DMetaData(y)), labels))
490 :     y.dmeta.aug <- cbind(DMetaData(y), na.matrix)
491 :     object@DMetaData <- rbind(x.dmeta.aug, y.dmeta.aug)
492 :    
493 :     return(object)
494 : feinerer 72 })
495 : feinerer 689
496 : feinerer 72 setMethod("c",
497 :     signature(x = "TextDocument"),
498 :     function(x, ..., recursive = TRUE){
499 :     args <- list(...)
500 :     if(length(args) == 0)
501 :     return(x)
502 : feinerer 54
503 : feinerer 73 dmeta.df <- data.frame(MetaID = rep(0, length(list(x, ...))), stringsAsFactors = FALSE)
504 : feinerer 698 cmeta.node <- new("MetaDataNode",
505 : feinerer 72 NodeID = 0,
506 : feinerer 689 MetaData = list(create_date = Sys.time(), creator = Sys.getenv("LOGNAME")),
507 : feinerer 72 children = list())
508 :    
509 : feinerer 720 return(new("TextDocCol",
510 :     .Data = list(x, ...),
511 :     DMetaData = dmeta.df,
512 :     CMetaData = cmeta.node,
513 :     DBControl = list(useDb = FALSE, dbName = "", dbType = "DB1")))
514 : feinerer 72 })
515 :    
516 : feinerer 54 setMethod("length",
517 :     signature(x = "TextDocCol"),
518 :     function(x){
519 :     return(length(as(x, "list")))
520 :     })
521 :    
522 :     setMethod("show",
523 :     signature(object = "TextDocCol"),
524 :     function(object){
525 : feinerer 70 cat(sprintf(ngettext(length(object),
526 :     "A text document collection with %d text document\n",
527 :     "A text document collection with %d text documents\n"),
528 :     length(object)))
529 : feinerer 54 })
530 :    
531 :     setMethod("summary",
532 :     signature(object = "TextDocCol"),
533 :     function(object){
534 :     show(object)
535 : feinerer 71 if (length(DMetaData(object)) > 0) {
536 : feinerer 698 cat(sprintf(ngettext(length(CMetaData(object)@MetaData),
537 : feinerer 77 "\nThe metadata consists of %d tag-value pair and a data frame\n",
538 :     "\nThe metadata consists of %d tag-value pairs and a data frame\n"),
539 : feinerer 698 length(CMetaData(object)@MetaData)))
540 : feinerer 54 cat("Available tags are:\n")
541 : feinerer 722 cat(strwrap(paste(names(CMetaData(object)@MetaData), collapse = " "), indent = 2, exdent = 2), "\n")
542 : feinerer 77 cat("Available variables in the data frame are:\n")
543 : feinerer 722 cat(strwrap(paste(names(DMetaData(object)), collapse = " "), indent = 2, exdent = 2), "\n")
544 : feinerer 54 }
545 :     })
546 : feinerer 55
547 :     setGeneric("inspect", function(object) standardGeneric("inspect"))
548 :     setMethod("inspect",
549 : feinerer 65 signature("TextDocCol"),
550 : feinerer 55 function(object) {
551 :     summary(object)
552 :     cat("\n")
553 : feinerer 719 if (DBControl(object)[["useDb"]]) {
554 :     db <- dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
555 :     show(dbMultiFetch(db, unlist(object)))
556 :     dbDisconnect(db)
557 :     }
558 :     else
559 :     show(object@.Data)
560 : feinerer 55 })
561 : feinerer 65
562 :     # No metadata is checked
563 :     setGeneric("%IN%", function(x, y) standardGeneric("%IN%"))
564 :     setMethod("%IN%",
565 :     signature(x = "TextDocument", y = "TextDocCol"),
566 :     function(x, y) {
567 :     x %in% y
568 :     })
569 : feinerer 719
570 :     setMethod("lapply",
571 :     signature(X = "TextDocCol"),
572 :     function(X, FUN, ...) {
573 :     if (DBControl(X)[["useDb"]]) {
574 :     db <- dbInit(DBControl(X)[["dbName"]], DBControl(X)[["dbType"]])
575 :     result <- lapply(dbMultiFetch(db, unlist(X)), FUN, ...)
576 :     dbDisconnect(db)
577 :     }
578 :     else
579 :     result <- base::lapply(X, FUN, ...)
580 :     return(result)
581 :     })
582 :    
583 :     setMethod("sapply",
584 :     signature(X = "TextDocCol"),
585 :     function(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) {
586 :     if (DBControl(X)[["useDb"]]) {
587 :     db <- dbInit(DBControl(X)[["dbName"]], DBControl(X)[["dbType"]])
588 :     result <- sapply(dbMultiFetch(db, unlist(X)), FUN, ...)
589 :     dbDisconnect(db)
590 :     }
591 :     else
592 :     result <- base::sapply(X, FUN, ...)
593 :     return(result)
594 :     })

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