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 995 - (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 698 # Update \code{NodeID}s of a CMetaData tree
144 : feinerer 985 update_id <- function(x, id = 0, mapping = NULL, left.mapping = NULL, level = 0) {
145 : feinerer 698 # Traversal of (binary) CMetaData tree with setup of \code{NodeID}s
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 985 c2 <- function(x, y, ...) {
169 :     # Update the CMetaData tree
170 :     cmeta <- .MetaDataNode(0, list(merge_date = as.POSIXlt(Sys.time(), tz = "GMT"), merger = Sys.getenv("LOGNAME")), list(CMetaData(x), CMetaData(y)))
171 :     update.struct <- update_id(cmeta)
172 : feinerer 71
173 : feinerer 985 new <- .VCorpus(c(unclass(x), unclass(y)), update.struct$root, NULL)
174 : feinerer 720
175 : feinerer 985 # Find indices to be updated for the left tree
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 : feinerer 958
183 : feinerer 985 # Update the DMetaData data frames for the left tree
184 :     for (i in 1:ncol(update.struct$left.mapping)) {
185 :     map <- update.struct$left.mapping[,i]
186 :     DMetaData(x)$MetaID <- replace(DMetaData(x)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
187 :     }
188 : feinerer 689
189 : feinerer 985 # Find indices to be updated for the right tree
190 :     indices.mapping <- NULL
191 :     for (m in levels(as.factor(DMetaData(y)$MetaID))) {
192 :     indices <- (DMetaData(y)$MetaID == m)
193 :     indices.mapping <- c(indices.mapping, list(m = indices))
194 :     names(indices.mapping)[length(indices.mapping)] <- m
195 :     }
196 : feinerer 71
197 : feinerer 985 # Update the DMetaData data frames for the right tree
198 :     for (i in 1:ncol(update.struct$right.mapping)) {
199 :     map <- update.struct$right.mapping[,i]
200 :     DMetaData(y)$MetaID <- replace(DMetaData(y)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
201 :     }
202 : feinerer 71
203 : feinerer 985 # Merge the DMetaData data frames
204 :     labels <- setdiff(names(DMetaData(y)), names(DMetaData(x)))
205 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(x)), ncol = length(labels), dimnames = list(row.names(DMetaData(x)), labels))
206 :     x.dmeta.aug <- cbind(DMetaData(x), na.matrix)
207 :     labels <- setdiff(names(DMetaData(x)), names(DMetaData(y)))
208 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(y)), ncol = length(labels), dimnames = list(row.names(DMetaData(y)), labels))
209 :     y.dmeta.aug <- cbind(DMetaData(y), na.matrix)
210 :     DMetaData(new) <- rbind(x.dmeta.aug, y.dmeta.aug)
211 : feinerer 71
212 : feinerer 985 new
213 :     }
214 : feinerer 71
215 : feinerer 985 c.Corpus <-
216 :     function(x, ..., recursive = FALSE)
217 :     {
218 :     args <- list(...)
219 : feinerer 71
220 : feinerer 985 if (identical(length(args), 0))
221 :     return(x)
222 : feinerer 71
223 : feinerer 985 if (!all(unlist(lapply(args, inherits, class(x)))))
224 :     stop("not all arguments are of the same corpus type")
225 : feinerer 71
226 : feinerer 985 if (inherits(x, "PCorpus"))
227 :     stop("concatenation of corpora with underlying databases is not supported")
228 : feinerer 689
229 : feinerer 985 Reduce(c2, base::c(list(x), args))
230 :     }
231 : feinerer 54
232 : feinerer 985 c.TextDocument <- function(x, ..., recursive = FALSE) {
233 :     args <- list(...)
234 : feinerer 72
235 : feinerer 985 if (identical(length(args), 0))
236 :     return(x)
237 : feinerer 72
238 : feinerer 985 if (!all(unlist(lapply(args, inherits, class(x)))))
239 :     stop("not all arguments are text documents")
240 : feinerer 54
241 : feinerer 985 dmeta <- data.frame(MetaID = rep(0, length(list(x, ...))), stringsAsFactors = FALSE)
242 :     .VCorpus(list(x, ...), .MetaDataNode(), dmeta)
243 :     }
244 : feinerer 55
245 : feinerer 985 print.Corpus <- function(x, ...) {
246 :     cat(sprintf(ngettext(length(x),
247 :     "A corpus with %d text document\n",
248 :     "A corpus with %d text documents\n"),
249 :     length(x)))
250 :     invisible(x)
251 :     }
252 :    
253 : feinerer 988 summary.Corpus <- function(object, ...) {
254 :     print(object)
255 :     if (length(DMetaData(object)) > 0) {
256 :     cat(sprintf(ngettext(length(attr(CMetaData(object), "MetaData")),
257 : feinerer 985 "\nThe metadata consists of %d tag-value pair and a data frame\n",
258 :     "\nThe metadata consists of %d tag-value pairs and a data frame\n"),
259 : feinerer 988 length(CMetaData(object)$MetaData)))
260 : feinerer 985 cat("Available tags are:\n")
261 : feinerer 988 cat(strwrap(paste(names(CMetaData(object)$MetaData), collapse = " "), indent = 2, exdent = 2), "\n")
262 : feinerer 985 cat("Available variables in the data frame are:\n")
263 : feinerer 988 cat(strwrap(paste(names(DMetaData(object)), collapse = " "), indent = 2, exdent = 2), "\n")
264 : feinerer 985 }
265 :     }
266 :    
267 : feinerer 938 inspect <- function(x) UseMethod("inspect", x)
268 : feinerer 946 inspect.PCorpus <- function(x) {
269 : feinerer 938 summary(x)
270 :     cat("\n")
271 : feinerer 946 db <- filehash::dbInit(DBControl(x)[["dbName"]], DBControl(x)[["dbType"]])
272 :     show(filehash::dbMultiFetch(db, unlist(x)))
273 : feinerer 938 }
274 : feinerer 963 inspect.VCorpus <- function(x) {
275 : feinerer 946 summary(x)
276 :     cat("\n")
277 :     print(noquote(lapply(x, identity)))
278 :     }
279 : feinerer 65
280 : feinerer 985 lapply.PCorpus <- function(X, FUN, ...) {
281 :     db <- filehash::dbInit(DBControl(X)[["dbName"]], DBControl(X)[["dbType"]])
282 :     lapply(filehash::dbMultiFetch(db, unlist(X)), FUN, ...)
283 :     }
284 :     lapply.VCorpus <- function(X, FUN, ...) {
285 :     lazyTmMap <- meta(X, tag = "lazyTmMap", type = "corpus")
286 :     if (!is.null(lazyTmMap))
287 :     .Call("copyCorpus", X, materialize(X))
288 :     base::lapply(X, FUN, ...)
289 :     }
290 : feinerer 719
291 : feinerer 985 writeCorpus <- function(x, path = ".", filenames = NULL) {
292 :     filenames <- file.path(path,
293 :     if (is.null(filenames)) unlist(lapply(x, function(x) sprintf("%s.txt", ID(x))))
294 :     else filenames)
295 :     i <- 1
296 :     for (o in x) {
297 :     writeLines(as.PlainTextDocument(o), filenames[i])
298 :     i <- i + 1
299 : feinerer 836 }
300 : 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