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

1 : feinerer 17 # Author: Ingo Feinerer
2 :    
3 : feinerer 985 .PCorpus <- function(x, cmeta, dmeta, dbcontrol) {
4 :     attr(x, "CMetaData") <- cmeta
5 :     attr(x, "DMetaData") <- dmeta
6 :     attr(x, "DBControl") <- dbcontrol
7 :     class(x) <- c("PCorpus", "Corpus", "list")
8 :     x
9 :     }
10 : feinerer 988 DBControl <- function(x) attr(x, "DBControl")
11 : feinerer 985
12 :     PCorpus <- function(x,
13 : feinerer 1073 readerControl = list(reader = x$DefaultReader, language = "en"),
14 : feinerer 946 dbControl = list(dbName = "", dbType = "DB1"),
15 :     ...) {
16 : feinerer 985 readerControl <- prepareReader(readerControl, x$DefaultReader, ...)
17 : feinerer 63
18 : feinerer 946 if (!filehash::dbCreate(dbControl$dbName, dbControl$dbType))
19 :     stop("error in creating database")
20 :     db <- filehash::dbInit(dbControl$dbName, dbControl$dbType)
21 : feinerer 712
22 : feinerer 946 # Allocate memory in advance if length is known
23 : feinerer 1074 tdl <- if (x$Length > 0)
24 : feinerer 985 vector("list", as.integer(x$Length))
25 : feinerer 946 else
26 :     list()
27 : feinerer 869
28 : feinerer 946 counter <- 1
29 : feinerer 985 while (!eoi(x)) {
30 :     x <- stepNext(x)
31 :     elem <- getElem(x)
32 : feinerer 1070 doc <- readerControl$reader(elem, readerControl$language, if (is.null(x$Names)) as.character(counter) else x$Names[counter])
33 : feinerer 946 filehash::dbInsert(db, ID(doc), doc)
34 : feinerer 985 if (x$Length > 0) tdl[[counter]] <- ID(doc)
35 : feinerer 946 else tdl <- c(tdl, ID(doc))
36 :     counter <- counter + 1
37 :     }
38 : feinerer 1064 names(tdl) <- x$Names
39 : feinerer 63
40 : feinerer 946 df <- data.frame(MetaID = rep(0, length(tdl)), stringsAsFactors = FALSE)
41 :     filehash::dbInsert(db, "DMetaData", df)
42 :     dmeta.df <- data.frame(key = "DMetaData", subset = I(list(NA)))
43 : feinerer 712
44 : feinerer 985 .PCorpus(tdl, .MetaDataNode(), dmeta.df, dbControl)
45 :     }
46 : feinerer 71
47 : feinerer 985 .VCorpus <- function(x, cmeta, dmeta) {
48 :     attr(x, "CMetaData") <- cmeta
49 :     attr(x, "DMetaData") <- dmeta
50 :     class(x) <- c("VCorpus", "Corpus", "list")
51 :     x
52 : feinerer 946 }
53 : feinerer 21
54 : feinerer 1021 # Register S3 corpus classes to be recognized by S4 methods. This is
55 :     # mainly a fix to be compatible with packages which were originally
56 :     # developed to cooperate with corresponding S4 tm classes. Necessary
57 :     # since tm's class architecture was changed to S3 since tm version 0.5.
58 :     setOldClass(c("VCorpus", "Corpus", "list"))
59 :    
60 : feinerer 946 # The "..." are additional arguments for the FunctionGenerator reader
61 : feinerer 985 VCorpus <- Corpus <- function(x,
62 : feinerer 1073 readerControl = list(reader = x$DefaultReader, language = "en"),
63 : feinerer 1063 ...) {
64 : feinerer 985 readerControl <- prepareReader(readerControl, x$DefaultReader, ...)
65 : feinerer 49
66 : feinerer 946 # Allocate memory in advance if length is known
67 : feinerer 985 tdl <- if (x$Length > 0)
68 :     vector("list", as.integer(x$Length))
69 : feinerer 946 else
70 :     list()
71 : feinerer 72
72 : feinerer 985 if (x$Vectorized)
73 : feinerer 987 tdl <- mapply(function(x, id) readerControl$reader(x, readerControl$language, id),
74 :     pGetElem(x),
75 : feinerer 1070 id = if (is.null(x$Names)) as.character(seq_len(x$Length)) else x$Names,
76 : feinerer 987 SIMPLIFY = FALSE)
77 : feinerer 946 else {
78 :     counter <- 1
79 : feinerer 985 while (!eoi(x)) {
80 :     x <- stepNext(x)
81 :     elem <- getElem(x)
82 : feinerer 1070 doc <- readerControl$reader(elem, readerControl$language, if (is.null(x$Names)) as.character(counter) else x$Names[counter])
83 : feinerer 985 if (x$Length > 0)
84 : feinerer 946 tdl[[counter]] <- doc
85 :     else
86 :     tdl <- c(tdl, list(doc))
87 :     counter <- counter + 1
88 :     }
89 :     }
90 : feinerer 1063 names(tdl) <- x$Names
91 : feinerer 946 df <- data.frame(MetaID = rep(0, length(tdl)), stringsAsFactors = FALSE)
92 : feinerer 985 .VCorpus(tdl, .MetaDataNode(), df)
93 :     }
94 : feinerer 72
95 : feinerer 985 `[.PCorpus` <- function(x, i) {
96 :     if (missing(i)) return(x)
97 :     index <- attr(x, "DMetaData")[[1 , "subset"]]
98 :     attr(x, "DMetaData")[[1 , "subset"]] <- if (is.numeric(index)) index[i] else i
99 :     dmeta <- attr(x, "DMetaData")
100 : feinerer 995 .PCorpus(NextMethod("["), CMetaData(x), dmeta, DBControl(x))
101 : feinerer 946 }
102 : feinerer 72
103 : feinerer 985 `[.VCorpus` <- function(x, i) {
104 :     if (missing(i)) return(x)
105 : feinerer 995 .VCorpus(NextMethod("["), CMetaData(x), DMetaData(x)[i, , drop = FALSE])
106 : feinerer 985 }
107 : feinerer 49
108 : feinerer 985 `[<-.PCorpus` <- function(x, i, value) {
109 :     db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
110 :     counter <- 1
111 :     for (id in unclass(x)[i]) {
112 : feinerer 1025 if (identical(length(value), 1L)) db[[id]] <- value
113 : feinerer 985 else db[[id]] <- value[[counter]]
114 :     counter <- counter + 1
115 : feinerer 829 }
116 : feinerer 985 x
117 : feinerer 828 }
118 :    
119 : feinerer 985 `[[.PCorpus` <- function(x, i) {
120 : feinerer 1102 if (is.character(i))
121 :     i <- match(i, meta(x, "ID", type = "local"))
122 : feinerer 985 db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
123 : feinerer 995 filehash::dbFetch(db, NextMethod("[["))
124 : feinerer 985 }
125 :     `[[.VCorpus` <- function(x, i) {
126 : feinerer 1102 if (is.character(i))
127 :     i <- match(i, meta(x, "ID", type = "local"))
128 : feinerer 985 lazyTmMap <- meta(x, tag = "lazyTmMap", type = "corpus")
129 :     if (!is.null(lazyTmMap))
130 :     .Call("copyCorpus", x, materialize(x, i))
131 : feinerer 995 NextMethod("[[")
132 : feinerer 985 }
133 : feinerer 886
134 : feinerer 985 `[[<-.PCorpus` <- function(x, i, value) {
135 :     db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
136 :     index <- unclass(x)[[i]]
137 :     db[[index]] <- value
138 :     x
139 : feinerer 946 }
140 : feinerer 985 `[[<-.VCorpus` <- function(x, i, value) {
141 :     # Mark new objects as not active for lazy mapping
142 :     lazyTmMap <- meta(x, tag = "lazyTmMap", type = "corpus")
143 :     if (!is.null(lazyTmMap)) {
144 :     lazyTmMap$index[i] <- FALSE
145 :     meta(x, tag = "lazyTmMap", type = "corpus") <- lazyTmMap
146 :     }
147 :     # Set the value
148 :     cl <- class(x)
149 : feinerer 995 y <- NextMethod("[[<-")
150 :     class(y) <- cl
151 :     y
152 : feinerer 985 }
153 : feinerer 946
154 : feinerer 1004 # Update NodeIDs of a CMetaData tree
155 :     .update_id <- function(x, id = 0, mapping = NULL, left.mapping = NULL, level = 0) {
156 :     # Traversal of (binary) CMetaData tree with setup of NodeIDs
157 : feinerer 985 set_id <- function(x) {
158 : feinerer 988 x$NodeID <- id
159 : feinerer 697 id <<- id + 1
160 :     level <<- level + 1
161 : feinerer 988 if (length(x$Children) > 0) {
162 :     mapping <<- cbind(mapping, c(x$Children[[1]]$NodeID, id))
163 :     left <- set_id(x$Children[[1]])
164 : feinerer 697 if (level == 1) {
165 :     left.mapping <<- mapping
166 :     mapping <<- NULL
167 :     }
168 : feinerer 988 mapping <<- cbind(mapping, c(x$Children[[2]]$NodeID, id))
169 :     right <- set_id(x$Children[[2]])
170 : feinerer 71
171 : feinerer 988 x$Children <- list(left, right)
172 : feinerer 71 }
173 : feinerer 697 level <<- level - 1
174 : feinerer 985 x
175 : feinerer 71 }
176 : feinerer 985 list(root = set_id(x), left.mapping = left.mapping, right.mapping = mapping)
177 : feinerer 71 }
178 :    
179 : feinerer 1004 # Find indices to be updated for a CMetaData tree
180 :     .find_indices <- function(x) {
181 :     indices.mapping <- NULL
182 :     for (m in levels(as.factor(DMetaData(x)$MetaID))) {
183 :     indices <- (DMetaData(x)$MetaID == m)
184 :     indices.mapping <- c(indices.mapping, list(m = indices))
185 :     names(indices.mapping)[length(indices.mapping)] <- m
186 :     }
187 :     indices.mapping
188 :     }
189 :    
190 : feinerer 985 c2 <- function(x, y, ...) {
191 :     # Update the CMetaData tree
192 :     cmeta <- .MetaDataNode(0, list(merge_date = as.POSIXlt(Sys.time(), tz = "GMT"), merger = Sys.getenv("LOGNAME")), list(CMetaData(x), CMetaData(y)))
193 : feinerer 1004 update.struct <- .update_id(cmeta)
194 : feinerer 71
195 : feinerer 985 new <- .VCorpus(c(unclass(x), unclass(y)), update.struct$root, NULL)
196 : feinerer 720
197 : feinerer 985 # Find indices to be updated for the left tree
198 : feinerer 1004 indices.mapping <- .find_indices(x)
199 : feinerer 958
200 : feinerer 985 # Update the DMetaData data frames for the left tree
201 :     for (i in 1:ncol(update.struct$left.mapping)) {
202 :     map <- update.struct$left.mapping[,i]
203 :     DMetaData(x)$MetaID <- replace(DMetaData(x)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
204 :     }
205 : feinerer 689
206 : feinerer 985 # Find indices to be updated for the right tree
207 : feinerer 1004 indices.mapping <- .find_indices(y)
208 : feinerer 71
209 : feinerer 985 # Update the DMetaData data frames for the right tree
210 :     for (i in 1:ncol(update.struct$right.mapping)) {
211 :     map <- update.struct$right.mapping[,i]
212 :     DMetaData(y)$MetaID <- replace(DMetaData(y)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
213 :     }
214 : feinerer 71
215 : feinerer 985 # Merge the DMetaData data frames
216 :     labels <- setdiff(names(DMetaData(y)), names(DMetaData(x)))
217 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(x)), ncol = length(labels), dimnames = list(row.names(DMetaData(x)), labels))
218 :     x.dmeta.aug <- cbind(DMetaData(x), na.matrix)
219 :     labels <- setdiff(names(DMetaData(x)), names(DMetaData(y)))
220 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(y)), ncol = length(labels), dimnames = list(row.names(DMetaData(y)), labels))
221 :     y.dmeta.aug <- cbind(DMetaData(y), na.matrix)
222 :     DMetaData(new) <- rbind(x.dmeta.aug, y.dmeta.aug)
223 : feinerer 71
224 : feinerer 985 new
225 :     }
226 : feinerer 71
227 : feinerer 985 c.Corpus <-
228 :     function(x, ..., recursive = FALSE)
229 :     {
230 :     args <- list(...)
231 : feinerer 71
232 : feinerer 1025 if (identical(length(args), 0L))
233 : feinerer 985 return(x)
234 : feinerer 71
235 : feinerer 985 if (!all(unlist(lapply(args, inherits, class(x)))))
236 :     stop("not all arguments are of the same corpus type")
237 : feinerer 71
238 : feinerer 985 if (inherits(x, "PCorpus"))
239 :     stop("concatenation of corpora with underlying databases is not supported")
240 : feinerer 689
241 : feinerer 1095 l <- base::c(list(x), args)
242 :     if (recursive)
243 :     Reduce(c2, l)
244 :     else {
245 :     l <- do.call("c", lapply(l, unclass))
246 :     .VCorpus(l,
247 :     cmeta = .MetaDataNode(),
248 :     dmeta = data.frame(MetaID = rep(0, length(l)), stringsAsFactors = FALSE))
249 :     }
250 : feinerer 985 }
251 : feinerer 54
252 : feinerer 985 c.TextDocument <- function(x, ..., recursive = FALSE) {
253 :     args <- list(...)
254 : feinerer 72
255 : feinerer 1025 if (identical(length(args), 0L))
256 : feinerer 985 return(x)
257 : feinerer 72
258 : feinerer 985 if (!all(unlist(lapply(args, inherits, class(x)))))
259 :     stop("not all arguments are text documents")
260 : feinerer 54
261 : feinerer 985 dmeta <- data.frame(MetaID = rep(0, length(list(x, ...))), stringsAsFactors = FALSE)
262 :     .VCorpus(list(x, ...), .MetaDataNode(), dmeta)
263 :     }
264 : feinerer 55
265 : feinerer 985 print.Corpus <- function(x, ...) {
266 :     cat(sprintf(ngettext(length(x),
267 :     "A corpus with %d text document\n",
268 :     "A corpus with %d text documents\n"),
269 :     length(x)))
270 :     invisible(x)
271 :     }
272 :    
273 : feinerer 988 summary.Corpus <- function(object, ...) {
274 :     print(object)
275 :     if (length(DMetaData(object)) > 0) {
276 :     cat(sprintf(ngettext(length(attr(CMetaData(object), "MetaData")),
277 : feinerer 985 "\nThe metadata consists of %d tag-value pair and a data frame\n",
278 :     "\nThe metadata consists of %d tag-value pairs and a data frame\n"),
279 : feinerer 988 length(CMetaData(object)$MetaData)))
280 : feinerer 985 cat("Available tags are:\n")
281 : feinerer 988 cat(strwrap(paste(names(CMetaData(object)$MetaData), collapse = " "), indent = 2, exdent = 2), "\n")
282 : feinerer 985 cat("Available variables in the data frame are:\n")
283 : feinerer 988 cat(strwrap(paste(names(DMetaData(object)), collapse = " "), indent = 2, exdent = 2), "\n")
284 : feinerer 985 }
285 :     }
286 :    
287 : feinerer 938 inspect <- function(x) UseMethod("inspect", x)
288 : feinerer 946 inspect.PCorpus <- function(x) {
289 : feinerer 938 summary(x)
290 :     cat("\n")
291 : feinerer 946 db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
292 :     show(filehash::dbMultiFetch(db, unlist(x)))
293 : feinerer 938 }
294 : feinerer 963 inspect.VCorpus <- function(x) {
295 : feinerer 946 summary(x)
296 :     cat("\n")
297 :     print(noquote(lapply(x, identity)))
298 :     }
299 : feinerer 65
300 : feinerer 985 lapply.PCorpus <- function(X, FUN, ...) {
301 :     db <- filehash::dbInit(DBControl(X)[["dbName"]], DBControl(X)[["dbType"]])
302 :     lapply(filehash::dbMultiFetch(db, unlist(X)), FUN, ...)
303 :     }
304 :     lapply.VCorpus <- function(X, FUN, ...) {
305 :     lazyTmMap <- meta(X, tag = "lazyTmMap", type = "corpus")
306 :     if (!is.null(lazyTmMap))
307 :     .Call("copyCorpus", X, materialize(X))
308 :     base::lapply(X, FUN, ...)
309 :     }
310 : feinerer 719
311 : feinerer 985 writeCorpus <- function(x, path = ".", filenames = NULL) {
312 :     filenames <- file.path(path,
313 :     if (is.null(filenames)) unlist(lapply(x, function(x) sprintf("%s.txt", ID(x))))
314 :     else filenames)
315 :     i <- 1
316 :     for (o in x) {
317 :     writeLines(as.PlainTextDocument(o), filenames[i])
318 :     i <- i + 1
319 : feinerer 836 }
320 : feinerer 985 }

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