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

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

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