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

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