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

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge