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

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

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