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