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