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

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