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 829 - (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 828 setGeneric("tmMap", function(object, FUN, lazy = FALSE, ...) standardGeneric("tmMap"))
157 :     ############################################
158 :     # Lazy mapping restrictions (at the moment):
159 :     # *) No database backend support
160 :     # *) No function composition
161 :     ############################################
162 : feinerer 698 setMethod("tmMap",
163 : feinerer 816 signature(object = "Corpus", FUN = "function"),
164 : feinerer 828 function(object, FUN, lazy = FALSE, ...) {
165 : feinerer 73 result <- object
166 : feinerer 689 # Note that text corpora are automatically loaded into memory via \code{[[}
167 : feinerer 719 if (DBControl(object)[["useDb"]]) {
168 :     db <- dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
169 : feinerer 728 i <- 1
170 :     for (id in unlist(object)) {
171 :     db[[id]] <- FUN(object[[i]], ..., DMetaData = DMetaData(object))
172 :     i <- i + 1
173 :     }
174 : feinerer 719 }
175 : feinerer 828 else {
176 :     if (lazy)
177 : feinerer 829 meta(result, tag = "lazyTmMap", type = "corpus") <-
178 :     list(index = rep(TRUE, length(result)),
179 :     fun = FUN,
180 :     args = ...)
181 : feinerer 828 else
182 :     result@.Data <- lapply(object, FUN, ..., DMetaData = DMetaData(object))
183 :     }
184 : feinerer 56 return(result)
185 : feinerer 49 })
186 :    
187 : feinerer 828 # Materialize lazy mappings
188 : feinerer 829 # ToDo: Clean up lazyTmMap markers (for the case that everything is materialized)
189 :     materialize <- function(corpus, range = seq_along(corpus)) {
190 : feinerer 828 lazyTmMap <- meta(corpus, tag = "lazyTmMap", type = "corpus")
191 : feinerer 829 if (!is.null(lazyTmMap)) {
192 :     for (i in range)
193 :     if (lazyTmMap$index[i]) {
194 :     corpus@.Data[[i]] <- lazyTmMap$fun(corpus@.Data[[i]], lazyTmMap$args, DMetaData = DMetaData(corpus))
195 :     lazyTmMap$index[i] <- FALSE
196 :     }
197 :     }
198 :     meta(corpus, tag = "lazyTmMap", type = "corpus") <- lazyTmMap
199 : feinerer 828 return(corpus)
200 :     }
201 :    
202 : feinerer 698 setGeneric("asPlain", function(object, FUN, ...) standardGeneric("asPlain"))
203 :     setMethod("asPlain",
204 : feinerer 62 signature(object = "PlainTextDocument"),
205 : feinerer 49 function(object, FUN, ...) {
206 :     return(object)
207 :     })
208 : feinerer 698 setMethod("asPlain",
209 : feinerer 62 signature(object = "XMLTextDocument", FUN = "function"),
210 : feinerer 49 function(object, FUN, ...) {
211 : feinerer 816 corpus <- Content(object)
212 : feinerer 49
213 :     # As XMLDocument is no native S4 class, restore valid information
214 :     class(corpus) <- "XMLDocument"
215 :     names(corpus) <- c("doc","dtd")
216 :    
217 : feinerer 61 return(FUN(xmlRoot(corpus), ...))
218 : feinerer 49 })
219 : feinerer 725 setMethod("asPlain",
220 : feinerer 757 signature(object = "Reuters21578Document"),
221 :     function(object, FUN, ...) {
222 :     FUN <- convertReut21578XMLPlain
223 : feinerer 816 corpus <- Content(object)
224 : feinerer 757
225 :     # As XMLDocument is no native S4 class, restore valid information
226 :     class(corpus) <- "XMLDocument"
227 :     names(corpus) <- c("doc","dtd")
228 :    
229 :     return(FUN(xmlRoot(corpus), ...))
230 :     })
231 :     setMethod("asPlain",
232 :     signature(object = "RCV1Document"),
233 :     function(object, FUN, ...) {
234 :     FUN <- convertRCV1Plain
235 : feinerer 816 corpus <- Content(object)
236 : feinerer 757
237 :     # As XMLDocument is no native S4 class, restore valid information
238 :     class(corpus) <- "XMLDocument"
239 :     names(corpus) <- c("doc","dtd")
240 :    
241 :     return(FUN(xmlRoot(corpus), ...))
242 :     })
243 :     setMethod("asPlain",
244 : feinerer 725 signature(object = "NewsgroupDocument"),
245 :     function(object, FUN, ...) {
246 : feinerer 816 new("PlainTextDocument", .Data = Content(object), Cached = TRUE, URI = "", Author = Author(object),
247 : feinerer 725 DateTimeStamp = DateTimeStamp(object), Description = Description(object), ID = ID(object),
248 : feinerer 826 Origin = Origin(object), Heading = Heading(object), Language = Language(object),
249 :     LocalMetaData = LocalMetaData(object))
250 : feinerer 725 })
251 : feinerer 775 setMethod("asPlain",
252 :     signature(object = "StructuredTextDocument"),
253 :     function(object, FUN, ...) {
254 : feinerer 816 new("PlainTextDocument", .Data = unlist(Content(object)), Cached = TRUE,
255 : feinerer 775 URI = "", Author = Author(object), DateTimeStamp = DateTimeStamp(object),
256 :     Description = Description(object), ID = ID(object), Origin = Origin(object),
257 : feinerer 826 Heading = Heading(object), Language = Language(object),
258 :     LocalMetaData = LocalMetaData(object))
259 : feinerer 775 })
260 : feinerer 49
261 : feinerer 698 setGeneric("tmFilter", function(object, ..., FUN = sFilter, doclevel = FALSE) standardGeneric("tmFilter"))
262 :     setMethod("tmFilter",
263 : feinerer 816 signature(object = "Corpus"),
264 : feinerer 698 function(object, ..., FUN = sFilter, doclevel = FALSE) {
265 : feinerer 73 if (doclevel)
266 :     return(object[sapply(object, FUN, ..., DMetaData = DMetaData(object))])
267 :     else
268 : feinerer 724 return(object[FUN(object, ...)])
269 : feinerer 61 })
270 :    
271 : feinerer 698 setGeneric("tmIndex", function(object, ..., FUN = sFilter, doclevel = FALSE) standardGeneric("tmIndex"))
272 :     setMethod("tmIndex",
273 : feinerer 816 signature(object = "Corpus"),
274 : feinerer 698 function(object, ..., FUN = sFilter, doclevel = FALSE) {
275 : feinerer 73 if (doclevel)
276 :     return(sapply(object, FUN, ..., DMetaData = DMetaData(object)))
277 :     else
278 : feinerer 724 return(FUN(object, ...))
279 : feinerer 49 })
280 :    
281 : feinerer 698 sFilter <- function(object, s, ...) {
282 : feinerer 73 con <- textConnection(s)
283 : feinerer 772 tokens <- scan(con, "character", quiet = TRUE)
284 : feinerer 73 close(con)
285 : feinerer 721 localMetaNames <- unique(names(sapply(object, LocalMetaData)))
286 :     localMetaTokens <- localMetaNames[localMetaNames %in% tokens]
287 : feinerer 729 n <- names(DMetaData(object))
288 :     tags <- c("Author", "DateTimeStamp", "Description", "ID", "Origin", "Heading", "Language", localMetaTokens)
289 :     query.df <- DMetaData(prescindMeta(object, tags))
290 :     if (DBControl(object)[["useDb"]])
291 :     DMetaData(object) <- DMetaData(object)[, setdiff(n, tags), drop = FALSE]
292 : feinerer 721 # Rename to avoid name conflicts
293 :     names(query.df)[names(query.df) == "Author"] <- "author"
294 :     names(query.df)[names(query.df) == "DateTimeStamp"] <- "datetimestamp"
295 :     names(query.df)[names(query.df) == "Description"] <- "description"
296 :     names(query.df)[names(query.df) == "ID"] <- "identifier"
297 :     names(query.df)[names(query.df) == "Origin"] <- "origin"
298 :     names(query.df)[names(query.df) == "Heading"] <- "heading"
299 :     names(query.df)[names(query.df) == "Language"] <- "language"
300 : feinerer 73 attach(query.df)
301 : feinerer 74 try(result <- rownames(query.df) %in% row.names(query.df[eval(parse(text = s)), ]))
302 : feinerer 73 detach(query.df)
303 :     return(result)
304 : feinerer 61 }
305 :    
306 : feinerer 698 setGeneric("appendElem", function(object, data, meta = NULL) standardGeneric("appendElem"))
307 :     setMethod("appendElem",
308 : feinerer 816 signature(object = "Corpus", data = "TextDocument"),
309 : feinerer 72 function(object, data, meta = NULL) {
310 : feinerer 712 if (DBControl(object)[["useDb"]]) {
311 :     db <- dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
312 :     if (dbExists(db, ID(data)))
313 :     warning("document with identical ID already exists")
314 :     dbInsert(db, ID(data), data)
315 :     object@.Data[[length(object)+1]] <- ID(data)
316 :     }
317 :     else
318 :     object@.Data[[length(object)+1]] <- data
319 :     DMetaData(object) <- rbind(DMetaData(object), c(MetaID = CMetaData(object)@NodeID, meta))
320 : feinerer 52 return(object)
321 :     })
322 :    
323 : feinerer 698 setGeneric("appendMeta", function(object, cmeta = NULL, dmeta = NULL) standardGeneric("appendMeta"))
324 :     setMethod("appendMeta",
325 : feinerer 816 signature(object = "Corpus"),
326 : feinerer 698 function(object, cmeta = NULL, dmeta = NULL) {
327 : feinerer 712 object@CMetaData@MetaData <- c(CMetaData(object)@MetaData, cmeta)
328 :     if (!is.null(dmeta)) {
329 : feinerer 719 DMetaData(object) <- cbind(DMetaData(object), eval(dmeta))
330 : feinerer 712 }
331 : feinerer 52 return(object)
332 :     })
333 : feinerer 53
334 : feinerer 698 setGeneric("removeMeta", function(object, cname = NULL, dname = NULL) standardGeneric("removeMeta"))
335 :     setMethod("removeMeta",
336 : feinerer 816 signature(object = "Corpus"),
337 : feinerer 698 function(object, cname = NULL, dname = NULL) {
338 : feinerer 729 if (!is.null(cname))
339 : feinerer 698 object@CMetaData@MetaData <- CMetaData(object)@MetaData[names(CMetaData(object)@MetaData) != cname]
340 : feinerer 729 if (!is.null(dname))
341 :     DMetaData(object) <- DMetaData(object)[, names(DMetaData(object)) != dname, drop = FALSE]
342 : feinerer 77 return(object)
343 :     })
344 : feinerer 69
345 : feinerer 698 setGeneric("prescindMeta", function(object, meta) standardGeneric("prescindMeta"))
346 :     setMethod("prescindMeta",
347 : feinerer 816 signature(object = "Corpus", meta = "character"),
348 : feinerer 73 function(object, meta) {
349 :     for (m in meta) {
350 : feinerer 719 if (m %in% c("Author", "DateTimeStamp", "Description", "ID", "Origin", "Heading", "Language")) {
351 : feinerer 75 local.m <- lapply(object, m)
352 :     local.m <- lapply(local.m, function(x) if (is.null(x)) return(NA) else return(x))
353 : feinerer 73 local.m <- unlist(local.m)
354 : feinerer 721 DMetaData(object) <- cbind(DMetaData(object), data.frame(m = local.m, stringsAsFactors = FALSE))
355 :     names(DMetaData(object))[which(names(DMetaData(object)) == "m")] <- m
356 : feinerer 75 }
357 :     else {
358 :     local.meta <- lapply(object, LocalMetaData)
359 :     local.m <- lapply(local.meta, "[[", m)
360 :     local.m <- lapply(local.m, function(x) if (is.null(x)) return(NA) else return(x))
361 :     if (length(local.m) == length(unlist(local.m)))
362 :     local.m <- unlist(local.m)
363 :     else
364 :     local.m <- I(local.m)
365 : feinerer 721 DMetaData(object) <- cbind(DMetaData(object), data.frame(m = local.m, stringsAsFactors = FALSE))
366 :     names(DMetaData(object))[which(names(DMetaData(object)) == "m")] <- m
367 : feinerer 75 }
368 : feinerer 73 }
369 :     return(object)
370 :     })
371 :    
372 : feinerer 53 setMethod("[",
373 : feinerer 816 signature(x = "Corpus", i = "ANY", j = "ANY", drop = "ANY"),
374 : feinerer 53 function(x, i, j, ... , drop) {
375 :     if(missing(i))
376 :     return(x)
377 :    
378 :     object <- x
379 :     object@.Data <- x@.Data[i, ..., drop = FALSE]
380 : feinerer 724 if (DBControl(object)[["useDb"]]) {
381 : feinerer 727 index <- object@DMetaData[[1 , "subset"]]
382 :     if (any(is.na(index)))
383 :     object@DMetaData[[1 , "subset"]] <- i
384 : feinerer 724 else
385 : feinerer 727 object@DMetaData[[1 , "subset"]] <- index[i]
386 : feinerer 724 }
387 : feinerer 729 else
388 :     DMetaData(object) <- DMetaData(x)[i, , drop = FALSE]
389 : feinerer 53 return(object)
390 :     })
391 :    
392 : feinerer 63 setMethod("[<-",
393 : feinerer 816 signature(x = "Corpus", i = "ANY", j = "ANY", value = "ANY"),
394 : feinerer 63 function(x, i, j, ... , value) {
395 :     object <- x
396 : feinerer 724 if (DBControl(object)[["useDb"]]) {
397 :     db <- dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
398 :     counter <- 1
399 :     for (id in object@.Data[i, ...]) {
400 :     if (length(value) == 1)
401 :     db[[id]] <- value
402 :     else {
403 :     db[[id]] <- value[[counter]]
404 :     }
405 :     counter <- counter + 1
406 :     }
407 :     }
408 :     else
409 :     object@.Data[i, ...] <- value
410 : feinerer 63 return(object)
411 :     })
412 :    
413 : feinerer 828 # ToDo: Implement on-demand materialization of lazy mappings
414 : feinerer 829 ############################################
415 :     # Lazy mapping restrictions (at the moment):
416 :     # *) No database backend support
417 :     ############################################
418 : feinerer 63 setMethod("[[",
419 : feinerer 816 signature(x = "Corpus", i = "ANY", j = "ANY"),
420 : feinerer 63 function(x, i, j, ...) {
421 : feinerer 712 if (DBControl(x)[["useDb"]]) {
422 :     db <- dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
423 :     result <- dbFetch(db, x@.Data[[i]])
424 :     return(loadDoc(result))
425 :     }
426 : feinerer 829 else {
427 :     # ToDo: Ensure that loadDoc is called and cached
428 :     .Call("copyCorpus", x, materialize(x, i))
429 : feinerer 712 return(loadDoc(x@.Data[[i]]))
430 : feinerer 829 }
431 : feinerer 63 })
432 :    
433 : feinerer 829 # ToDo: Mark set objects as not active for lazy mapping
434 : feinerer 63 setMethod("[[<-",
435 : feinerer 816 signature(x = "Corpus", i = "ANY", j = "ANY", value = "ANY"),
436 : feinerer 63 function(x, i, j, ..., value) {
437 :     object <- x
438 : feinerer 712 if (DBControl(object)[["useDb"]]) {
439 :     db <- dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
440 :     index <- object@.Data[[i]]
441 :     db[[index]] <- value
442 :     }
443 :     else
444 :     object@.Data[[i, ...]] <- value
445 : feinerer 63 return(object)
446 :     })
447 :    
448 : feinerer 698 # Update \code{NodeID}s of a CMetaData tree
449 : feinerer 697 update_id <- function(object, id = 0, mapping = NULL, left.mapping = NULL, level = 0) {
450 : feinerer 698 # Traversal of (binary) CMetaData tree with setup of \code{NodeID}s
451 : feinerer 697 set_id <- function(object) {
452 :     object@NodeID <- id
453 :     id <<- id + 1
454 :     level <<- level + 1
455 : feinerer 71
456 : feinerer 697 if (length(object@children) > 0) {
457 :     mapping <<- cbind(mapping, c(object@children[[1]]@NodeID, id))
458 :     left <- set_id(object@children[[1]])
459 :     if (level == 1) {
460 :     left.mapping <<- mapping
461 :     mapping <<- NULL
462 :     }
463 :     mapping <<- cbind(mapping, c(object@children[[2]]@NodeID, id))
464 :     right <- set_id(object@children[[2]])
465 : feinerer 71
466 : feinerer 697 object@children <- list(left, right)
467 : feinerer 71 }
468 : feinerer 697 level <<- level - 1
469 : feinerer 71
470 : feinerer 697 return(object)
471 : feinerer 71 }
472 :    
473 : feinerer 697 return(list(root = set_id(object), left.mapping = left.mapping, right.mapping = mapping))
474 : feinerer 71 }
475 :    
476 : feinerer 53 setMethod("c",
477 : feinerer 816 signature(x = "Corpus"),
478 : feinerer 689 function(x, ..., meta = list(merge_date = Sys.time(), merger = Sys.getenv("LOGNAME")), recursive = TRUE) {
479 :     args <- list(...)
480 : feinerer 720 if (length(args) == 0)
481 : feinerer 689 return(x)
482 : feinerer 71
483 : feinerer 816 if (!all(sapply(args, inherits, "Corpus")))
484 : feinerer 720 stop("not all arguments are text document collections")
485 : feinerer 730 if (DBControl(x)[["useDb"]] == TRUE || any(unlist(sapply(args, DBControl)["useDb", ])))
486 : feinerer 720 stop("concatenating text document collections with activated database is not supported")
487 :    
488 : feinerer 689 result <- x
489 :     for (c in args) {
490 :     result <- c2(result, c)
491 :     }
492 :     return(result)
493 :     })
494 :    
495 :     setGeneric("c2", function(x, y, ..., meta = list(merge_date = Sys.time(), merger = Sys.getenv("LOGNAME")), recursive = TRUE) standardGeneric("c2"))
496 :     setMethod("c2",
497 : feinerer 816 signature(x = "Corpus", y = "Corpus"),
498 : feinerer 689 function(x, y, ..., meta = list(merge_date = Sys.time(), merger = Sys.getenv("LOGNAME")), recursive = TRUE) {
499 : feinerer 71 object <- x
500 :     # Concatenate data slots
501 :     object@.Data <- c(as(x, "list"), as(y, "list"))
502 :    
503 : feinerer 720 # Set the DBControl slot
504 : feinerer 741 object@DBControl <- list(useDb = FALSE, dbName = "", dbType = "DB1")
505 : feinerer 720
506 : feinerer 698 # Update the CMetaData tree
507 :     cmeta <- new("MetaDataNode", NodeID = 0, MetaData = meta, children = list(CMetaData(x), CMetaData(y)))
508 :     update.struct <- update_id(cmeta)
509 :     object@CMetaData <- update.struct$root
510 : feinerer 71
511 :     # Find indices to be updated for the left tree
512 :     indices.mapping <- NULL
513 :     for (m in levels(as.factor(DMetaData(x)$MetaID))) {
514 :     indices <- (DMetaData(x)$MetaID == m)
515 :     indices.mapping <- c(indices.mapping, list(m = indices))
516 :     names(indices.mapping)[length(indices.mapping)] <- m
517 :     }
518 :    
519 :     # Update the DMetaData data frames for the left tree
520 :     for (i in 1:ncol(update.struct$left.mapping)) {
521 :     map <- update.struct$left.mapping[,i]
522 :     x@DMetaData$MetaID <- replace(DMetaData(x)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
523 :     }
524 :    
525 :     # Find indices to be updated for the right tree
526 :     indices.mapping <- NULL
527 :     for (m in levels(as.factor(DMetaData(y)$MetaID))) {
528 :     indices <- (DMetaData(y)$MetaID == m)
529 :     indices.mapping <- c(indices.mapping, list(m = indices))
530 :     names(indices.mapping)[length(indices.mapping)] <- m
531 :     }
532 :    
533 :     # Update the DMetaData data frames for the right tree
534 :     for (i in 1:ncol(update.struct$right.mapping)) {
535 :     map <- update.struct$right.mapping[,i]
536 :     y@DMetaData$MetaID <- replace(DMetaData(y)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
537 :     }
538 :    
539 :     # Merge the DMetaData data frames
540 :     labels <- setdiff(names(DMetaData(y)), names(DMetaData(x)))
541 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(x)), ncol = length(labels), dimnames = list(row.names(DMetaData(x)), labels))
542 :     x.dmeta.aug <- cbind(DMetaData(x), na.matrix)
543 :     labels <- setdiff(names(DMetaData(x)), names(DMetaData(y)))
544 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(y)), ncol = length(labels), dimnames = list(row.names(DMetaData(y)), labels))
545 :     y.dmeta.aug <- cbind(DMetaData(y), na.matrix)
546 :     object@DMetaData <- rbind(x.dmeta.aug, y.dmeta.aug)
547 :    
548 :     return(object)
549 : feinerer 72 })
550 : feinerer 689
551 : feinerer 72 setMethod("c",
552 :     signature(x = "TextDocument"),
553 :     function(x, ..., recursive = TRUE){
554 :     args <- list(...)
555 :     if(length(args) == 0)
556 :     return(x)
557 : feinerer 54
558 : feinerer 73 dmeta.df <- data.frame(MetaID = rep(0, length(list(x, ...))), stringsAsFactors = FALSE)
559 : feinerer 698 cmeta.node <- new("MetaDataNode",
560 : feinerer 72 NodeID = 0,
561 : feinerer 689 MetaData = list(create_date = Sys.time(), creator = Sys.getenv("LOGNAME")),
562 : feinerer 72 children = list())
563 :    
564 : feinerer 816 return(new("Corpus",
565 : feinerer 720 .Data = list(x, ...),
566 :     DMetaData = dmeta.df,
567 :     CMetaData = cmeta.node,
568 : feinerer 741 DBControl = list(useDb = FALSE, dbName = "", dbType = "DB1")))
569 : feinerer 72 })
570 :    
571 : feinerer 54 setMethod("length",
572 : feinerer 816 signature(x = "Corpus"),
573 : feinerer 54 function(x){
574 :     return(length(as(x, "list")))
575 :     })
576 :    
577 :     setMethod("show",
578 : feinerer 816 signature(object = "Corpus"),
579 : feinerer 54 function(object){
580 : feinerer 70 cat(sprintf(ngettext(length(object),
581 :     "A text document collection with %d text document\n",
582 :     "A text document collection with %d text documents\n"),
583 :     length(object)))
584 : feinerer 54 })
585 :    
586 :     setMethod("summary",
587 : feinerer 816 signature(object = "Corpus"),
588 : feinerer 54 function(object){
589 :     show(object)
590 : feinerer 71 if (length(DMetaData(object)) > 0) {
591 : feinerer 698 cat(sprintf(ngettext(length(CMetaData(object)@MetaData),
592 : feinerer 77 "\nThe metadata consists of %d tag-value pair and a data frame\n",
593 :     "\nThe metadata consists of %d tag-value pairs and a data frame\n"),
594 : feinerer 698 length(CMetaData(object)@MetaData)))
595 : feinerer 54 cat("Available tags are:\n")
596 : feinerer 722 cat(strwrap(paste(names(CMetaData(object)@MetaData), collapse = " "), indent = 2, exdent = 2), "\n")
597 : feinerer 77 cat("Available variables in the data frame are:\n")
598 : feinerer 722 cat(strwrap(paste(names(DMetaData(object)), collapse = " "), indent = 2, exdent = 2), "\n")
599 : feinerer 54 }
600 :     })
601 : feinerer 55
602 :     setGeneric("inspect", function(object) standardGeneric("inspect"))
603 :     setMethod("inspect",
604 : feinerer 816 signature("Corpus"),
605 : feinerer 55 function(object) {
606 :     summary(object)
607 :     cat("\n")
608 : feinerer 719 if (DBControl(object)[["useDb"]]) {
609 :     db <- dbInit(DBControl(object)[["dbName"]], DBControl(object)[["dbType"]])
610 :     show(dbMultiFetch(db, unlist(object)))
611 :     }
612 :     else
613 :     show(object@.Data)
614 : feinerer 55 })
615 : feinerer 65
616 :     # No metadata is checked
617 :     setGeneric("%IN%", function(x, y) standardGeneric("%IN%"))
618 :     setMethod("%IN%",
619 : feinerer 816 signature(x = "TextDocument", y = "Corpus"),
620 : feinerer 65 function(x, y) {
621 : feinerer 729 if (DBControl(y)[["useDb"]]) {
622 :     db <- dbInit(DBControl(y)[["dbName"]], DBControl(y)[["dbType"]])
623 : feinerer 816 result <- any(sapply(y, function(x, z) {x %in% Content(z)}, x))
624 : feinerer 729 }
625 :     else
626 :     result <- x %in% y
627 :     return(result)
628 : feinerer 65 })
629 : feinerer 719
630 :     setMethod("lapply",
631 : feinerer 816 signature(X = "Corpus"),
632 : feinerer 719 function(X, FUN, ...) {
633 :     if (DBControl(X)[["useDb"]]) {
634 :     db <- dbInit(DBControl(X)[["dbName"]], DBControl(X)[["dbType"]])
635 :     result <- lapply(dbMultiFetch(db, unlist(X)), FUN, ...)
636 :     }
637 :     else
638 :     result <- base::lapply(X, FUN, ...)
639 :     return(result)
640 :     })
641 :    
642 :     setMethod("sapply",
643 : feinerer 816 signature(X = "Corpus"),
644 : feinerer 719 function(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) {
645 :     if (DBControl(X)[["useDb"]]) {
646 :     db <- dbInit(DBControl(X)[["dbName"]], DBControl(X)[["dbType"]])
647 :     result <- sapply(dbMultiFetch(db, unlist(X)), FUN, ...)
648 :     }
649 :     else
650 :     result <- base::sapply(X, FUN, ...)
651 :     return(result)
652 :     })
653 : feinerer 819
654 :     setGeneric("writeCorpus", function(object, path = ".", filenames = NULL) standardGeneric("writeCorpus"))
655 :     setMethod("writeCorpus",
656 :     signature(object = "Corpus"),
657 :     function(object, path = ".", filenames = NULL) {
658 :     filenames <- file.path(path,
659 :     if (is.null(filenames)) sapply(object, function(x) sprintf("%s.txt", ID(x)))
660 :     else filenames)
661 :     i <- 1
662 :     for (o in object) {
663 :     writeLines(o, filenames[i])
664 :     i <- i + 1
665 :     }
666 :     })

root@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