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 74 - (view) (download)
Original Path: trunk/R/textmin/R/textdoccol.R

1 : feinerer 17 # Author: Ingo Feinerer
2 :    
3 : feinerer 65 # The "..." are additional arguments for the function_generator parser
4 : feinerer 63 setGeneric("TextDocCol", function(object, parser = plaintext_parser, ...) standardGeneric("TextDocCol"))
5 : feinerer 49 setMethod("TextDocCol",
6 : feinerer 63 signature(object = "Source"),
7 : feinerer 67 function(object, parser = plaintext_parser, ...) {
8 : feinerer 63 if (inherits(parser, "function_generator"))
9 :     parser <- parser(...)
10 :    
11 :     tdl <- list()
12 :     counter <- 1
13 :     while (!eoi(object)) {
14 : feinerer 66 object <- step_next(object)
15 :     elem <- get_elem(object)
16 : feinerer 63 # If there is no Load on Demand support
17 :     # we need to load the corpus into memory at startup
18 :     if (object@LoDSupport)
19 :     load <- object@Load
20 :     else
21 :     load <- TRUE
22 :     tdl <- c(tdl, list(parser(elem, object@LoDSupport, load, as.character(counter))))
23 :     counter <- counter + 1
24 :     }
25 :    
26 : feinerer 73 dmeta.df <- data.frame(MetaID = rep(0, length(tdl)), stringsAsFactors = FALSE)
27 : feinerer 71 dcmeta.node <- new("MetaDataNode",
28 :     NodeID = 0,
29 :     MetaData = list(create_date = date(), creator = Sys.getenv("LOGNAME")),
30 :     children = list())
31 :    
32 :     return(new("TextDocCol", .Data = tdl, DMetaData = dmeta.df, DCMetaData = dcmeta.node))
33 : feinerer 21 })
34 :    
35 : feinerer 63 setGeneric("DirSource", function(directory, load = FALSE) standardGeneric("DirSource"))
36 :     setMethod("DirSource",
37 :     signature(directory = "character"),
38 :     function(directory, load = FALSE) {
39 :     new("DirSource", LoDSupport = TRUE, FileList = dir(directory, full.names = TRUE),
40 :     Position = 0, Load = load)
41 :     })
42 : feinerer 60
43 : feinerer 67 setGeneric("CSVSource", function(object) standardGeneric("CSVSource"))
44 : feinerer 63 setMethod("CSVSource",
45 : feinerer 65 signature(object = "character"),
46 : feinerer 67 function(object) {
47 :     object <- substitute(file(object))
48 :     con <- eval(object)
49 : feinerer 65 content <- scan(con, what = "character")
50 :     close(con)
51 :     new("CSVSource", LoDSupport = FALSE, URI = object,
52 :     Content = content, Position = 0)
53 : feinerer 63 })
54 : feinerer 67 setMethod("CSVSource",
55 :     signature(object = "ANY"),
56 :     function(object) {
57 :     object <- substitute(object)
58 :     con <- eval(object)
59 :     content <- scan(con, what = "character")
60 :     close(con)
61 :     new("CSVSource", LoDSupport = FALSE, URI = object,
62 :     Content = content, Position = 0)
63 :     })
64 : feinerer 60
65 : feinerer 67 setGeneric("ReutersSource", function(object) standardGeneric("ReutersSource"))
66 : feinerer 65 setMethod("ReutersSource",
67 :     signature(object = "character"),
68 : feinerer 67 function(object) {
69 :     object <- substitute(file(object))
70 :     con <- eval(object)
71 : feinerer 65 corpus <- paste(readLines(con), "\n", collapse = "")
72 :     close(con)
73 :     tree <- xmlTreeParse(corpus, asText = TRUE)
74 : feinerer 63 content <- xmlRoot(tree)$children
75 : feinerer 65
76 :     new("ReutersSource", LoDSupport = FALSE, URI = object,
77 : feinerer 63 Content = content, Position = 0)
78 :     })
79 : feinerer 67 setMethod("ReutersSource",
80 :     signature(object = "ANY"),
81 :     function(object) {
82 :     object <- substitute(object)
83 :     con <- eval(object)
84 :     corpus <- paste(readLines(con), "\n", collapse = "")
85 :     close(con)
86 :     tree <- xmlTreeParse(corpus, asText = TRUE)
87 :     content <- xmlRoot(tree)$children
88 : feinerer 63
89 : feinerer 67 new("ReutersSource", LoDSupport = FALSE, URI = object,
90 :     Content = content, Position = 0)
91 :     })
92 :    
93 : feinerer 66 setGeneric("step_next", function(object) standardGeneric("step_next"))
94 :     setMethod("step_next",
95 : feinerer 63 signature(object = "DirSource"),
96 :     function(object) {
97 :     object@Position <- object@Position + 1
98 :     object
99 :     })
100 : feinerer 66 setMethod("step_next",
101 : feinerer 63 signature(object = "CSVSource"),
102 :     function(object) {
103 :     object@Position <- object@Position + 1
104 :     object
105 :     })
106 : feinerer 66 setMethod("step_next",
107 : feinerer 65 signature(object = "ReutersSource"),
108 : feinerer 63 function(object) {
109 :     object@Position <- object@Position + 1
110 :     object
111 :     })
112 :    
113 : feinerer 66 setGeneric("get_elem", function(object) standardGeneric("get_elem"))
114 :     setMethod("get_elem",
115 : feinerer 63 signature(object = "DirSource"),
116 :     function(object) {
117 : feinerer 67 filename <- object@FileList[object@Position]
118 : feinerer 72 list(content = readLines(filename),
119 : feinerer 67 uri = substitute(file(filename)))
120 : feinerer 63 })
121 : feinerer 66 setMethod("get_elem",
122 : feinerer 63 signature(object = "CSVSource"),
123 :     function(object) {
124 :     list(content = object@Content[object@Position],
125 : feinerer 65 uri = object@URI)
126 : feinerer 63 })
127 : feinerer 66 setMethod("get_elem",
128 : feinerer 65 signature(object = "ReutersSource"),
129 : feinerer 63 function(object) {
130 : feinerer 65 # Construct a character representation from the XMLNode
131 :     con <- textConnection("virtual.file", "w")
132 :     saveXML(object@Content[[object@Position]], con)
133 :     close(con)
134 :    
135 :     list(content = virtual.file, uri = object@URI)
136 : feinerer 63 })
137 :    
138 :     setGeneric("eoi", function(object) standardGeneric("eoi"))
139 :     setMethod("eoi",
140 :     signature(object = "DirSource"),
141 :     function(object) {
142 :     if (length(object@FileList) <= object@Position)
143 :     return(TRUE)
144 :     else
145 :     return(FALSE)
146 :     })
147 :     setMethod("eoi",
148 :     signature(object = "CSVSource"),
149 :     function(object) {
150 :     if (length(object@Content) <= object@Position)
151 :     return(TRUE)
152 :     else
153 :     return(FALSE)
154 :     })
155 :     setMethod("eoi",
156 : feinerer 65 signature(object = "ReutersSource"),
157 : feinerer 63 function(object) {
158 :     if (length(object@Content) <= object@Position)
159 :     return(TRUE)
160 :     else
161 :     return(FALSE)
162 :     })
163 :    
164 :     plaintext_parser <- function(...) {
165 :     function(elem, lodsupport, load, id) {
166 :     if (!lodsupport || (lodsupport && load)) {
167 : feinerer 65 doc <- new("PlainTextDocument", .Data = elem$content, URI = elem$uri, Cached = TRUE,
168 : feinerer 63 Author = "", DateTimeStamp = date(), Description = "", ID = id, Origin = "", Heading = "")
169 :     }
170 :     else {
171 : feinerer 65 doc <- new("PlainTextDocument", URI = elem$uri, Cached = FALSE,
172 : feinerer 63 Author = "", DateTimeStamp = date(), Description = "", ID = id, Origin = "", Heading = "")
173 :     }
174 :    
175 :     return(doc)
176 : feinerer 60 }
177 :     }
178 : feinerer 63 class(plaintext_parser) <- "function_generator"
179 : feinerer 60
180 : feinerer 66 reut21578xml_parser <- function(...) {
181 : feinerer 63 function(elem, lodsupport, load, id) {
182 : feinerer 65 corpus <- paste(elem$content, "\n", collapse = "")
183 :     tree <- xmlTreeParse(corpus, asText = TRUE)
184 : feinerer 63 node <- xmlRoot(tree)
185 : feinerer 60
186 : feinerer 65 # Mask as list to bypass S4 checks
187 :     class(tree) <- "list"
188 :    
189 : feinerer 63 # The <AUTHOR></AUTHOR> tag is unfortunately NOT obligatory!
190 :     if (!is.null(node[["TEXT"]][["AUTHOR"]]))
191 :     author <- xmlValue(node[["TEXT"]][["AUTHOR"]])
192 :     else
193 :     author <- ""
194 : feinerer 60
195 : feinerer 63 datetimestamp <- xmlValue(node[["DATE"]])
196 :     description <- ""
197 :     id <- xmlAttrs(node)[["NEWID"]]
198 : feinerer 40
199 : feinerer 63 # The <TITLE></TITLE> tag is unfortunately NOT obligatory!
200 :     if (!is.null(node[["TEXT"]][["TITLE"]]))
201 :     heading <- xmlValue(node[["TEXT"]][["TITLE"]])
202 :     else
203 :     heading <- ""
204 : feinerer 41
205 : feinerer 63 topics <- unlist(xmlApply(node[["TOPICS"]], function(x) xmlValue(x)), use.names = FALSE)
206 : feinerer 41
207 : feinerer 63 if (!lodsupport || (lodsupport && load)) {
208 : feinerer 65 doc <- new("XMLTextDocument", .Data = tree, URI = elem$uri, Cached = TRUE, Author = author,
209 : feinerer 63 DateTimeStamp = datetimestamp, Description = "", ID = id, Origin = "Reuters-21578 XML",
210 :     Heading = heading, LocalMetaData = list(Topics = topics))
211 :     } else {
212 : feinerer 65 doc <- new("XMLTextDocument", URI = elem$uri, Cached = FALSE, Author = author,
213 : feinerer 63 DateTimeStamp = datetimestamp, Description = "", ID = id, Origin = "Reuters-21578 XML",
214 :     Heading = heading, LocalMetaData = list(Topics = topics))
215 :     }
216 : feinerer 40
217 : feinerer 63 return(doc)
218 : feinerer 60 }
219 :     }
220 : feinerer 66 class(reut21578xml_parser) <- "function_generator"
221 : feinerer 41
222 : feinerer 63 rcv1_parser <- function(...) {
223 :     function(elem, lodsupport, load, id) {
224 : feinerer 65 corpus <- paste(elem$content, "\n", collapse = "")
225 :     tree <- xmlTreeParse(corpus, asText = TRUE)
226 : feinerer 63 node <- xmlRoot(tree)
227 : feinerer 41
228 : feinerer 65 # Mask as list to bypass S4 checks
229 :     class(tree) <- "list"
230 :    
231 : feinerer 63 datetimestamp <- xmlAttrs(node)[["date"]]
232 :     id <- xmlAttrs(node)[["itemid"]]
233 :     heading <- xmlValue(node[["title"]])
234 : feinerer 40
235 : feinerer 63 if (!lodsupport || (lodsupport && load)) {
236 : feinerer 65 doc <- new("XMLTextDocument", .Data = tree, URI = elem$uri, Cached = TRUE, Author = "",
237 : feinerer 63 DateTimeStamp = datetimestamp, Description = "", ID = id, Origin = "Reuters Corpus Volume 1 XML",
238 :     Heading = heading)
239 :     } else {
240 : feinerer 65 doc <- new("XMLTextDocument", URI = elem$uri, Cached = FALSE, Author = "",
241 : feinerer 63 DateTimeStamp = datetimestamp, Description = "", ID = id, Origin = "Reuters Corpus Volume 1 XML",
242 :     Heading = heading)
243 :     }
244 : feinerer 40
245 : feinerer 63 return(doc)
246 : feinerer 60 }
247 : feinerer 40 }
248 : feinerer 63 class(rcv1_parser) <- "function_generator"
249 : feinerer 40
250 : feinerer 66 newsgroup_parser <- function(...) {
251 : feinerer 63 function(elem, lodsupport, load, id) {
252 : feinerer 65 mail <- elem$content
253 : feinerer 63 author <- gsub("From: ", "", grep("^From:", mail, value = TRUE))
254 :     datetimestamp <- gsub("Date: ", "", grep("^Date:", mail, value = TRUE))
255 :     origin <- gsub("Path: ", "", grep("^Path:", mail, value = TRUE))
256 :     heading <- gsub("Subject: ", "", grep("^Subject:", mail, value = TRUE))
257 :     newsgroup <- gsub("Newsgroups: ", "", grep("^Newsgroups:", mail, value = TRUE))
258 : feinerer 60
259 : feinerer 63 if (!lodsupport || (lodsupport && load)) {
260 : feinerer 65 # The header is separated from the body by a blank line.
261 :     # Reference: \url{http://en.wikipedia.org/wiki/E-mail#Internet_e-mail_format}
262 :     for (index in seq(along = mail)) {
263 :     if (mail[index] == "")
264 :     break
265 :     }
266 : feinerer 63 content <- mail[(index + 1):length(mail)]
267 : feinerer 60
268 : feinerer 65 doc <- new("NewsgroupDocument", .Data = content, URI = elem$uri, Cached = TRUE,
269 : feinerer 63 Author = author, DateTimeStamp = datetimestamp,
270 : feinerer 65 Description = "", ID = id, Origin = origin,
271 : feinerer 63 Heading = heading, Newsgroup = newsgroup)
272 :     } else {
273 : feinerer 65 doc <- new("NewsgroupDocument", URI = elem$uri, Cached = FALSE, Author = author, DateTimeStamp = datetimestamp,
274 :     Description = "", ID = id, Origin = origin, Heading = heading, Newsgroup = newsgroup)
275 : feinerer 63 }
276 :    
277 :     return(doc)
278 : feinerer 60 }
279 :     }
280 : feinerer 66 class(newsgroup_parser) <- "function_generator"
281 : feinerer 60
282 : feinerer 23 # Parse a <newsitem></newsitem> element from a well-formed RCV1 XML file
283 : feinerer 65 rcv1_to_plain <- function(node, ...) {
284 : feinerer 42 datetimestamp <- xmlAttrs(node)[["date"]]
285 : feinerer 60 id <- xmlAttrs(node)[["itemid"]]
286 : feinerer 36 origin <- "Reuters Corpus Volume 1 XML"
287 : feinerer 18 corpus <- unlist(xmlApply(node[["text"]], xmlValue), use.names = FALSE)
288 :     heading <- xmlValue(node[["title"]])
289 : feinerer 17
290 : feinerer 65 new("PlainTextDocument", .Data = corpus, Cached = TRUE, URI = "", Author = "", DateTimeStamp = datetimestamp,
291 : feinerer 60 Description = "", ID = id, Origin = "Reuters Corpus Volume 1 XML", Heading = heading)
292 : feinerer 19 }
293 : feinerer 23
294 :     # Parse a <REUTERS></REUTERS> element from a well-formed Reuters-21578 XML file
295 : feinerer 66 reut21578xml_to_plain <- function(node, ...) {
296 : feinerer 36 # The <AUTHOR></AUTHOR> tag is unfortunately NOT obligatory!
297 :     if (!is.null(node[["TEXT"]][["AUTHOR"]]))
298 :     author <- xmlValue(node[["TEXT"]][["AUTHOR"]])
299 :     else
300 :     author <- ""
301 :    
302 : feinerer 42 datetimestamp <- xmlValue(node[["DATE"]])
303 : feinerer 36 description <- ""
304 : feinerer 60 id <- xmlAttrs(node)[["NEWID"]]
305 : feinerer 23
306 : feinerer 36 origin <- "Reuters-21578 XML"
307 : feinerer 23
308 :     # The <BODY></BODY> tag is unfortunately NOT obligatory!
309 :     if (!is.null(node[["TEXT"]][["BODY"]]))
310 :     corpus <- xmlValue(node[["TEXT"]][["BODY"]])
311 :     else
312 :     corpus <- ""
313 :    
314 :     # The <TITLE></TITLE> tag is unfortunately NOT obligatory!
315 :     if (!is.null(node[["TEXT"]][["TITLE"]]))
316 :     heading <- xmlValue(node[["TEXT"]][["TITLE"]])
317 :     else
318 :     heading <- ""
319 :    
320 : feinerer 49 topics <- unlist(xmlApply(node[["TOPICS"]], function(x) xmlValue(x)), use.names = FALSE)
321 :    
322 : feinerer 65 new("PlainTextDocument", .Data = corpus, Cached = TRUE, URI = "", Author = author, DateTimeStamp = datetimestamp,
323 : feinerer 49 Description = description, ID = id, Origin = origin, Heading = heading, LocalMetaData = list(Topics = topics))
324 : feinerer 23 }
325 : feinerer 49
326 : feinerer 66 setGeneric("load_doc", function(object, ...) standardGeneric("load_doc"))
327 :     setMethod("load_doc",
328 : feinerer 62 signature(object = "PlainTextDocument"),
329 : feinerer 65 function(object, ...) {
330 : feinerer 61 if (!Cached(object)) {
331 : feinerer 67 con <- eval(URI(object))
332 : feinerer 65 corpus <- readLines(con)
333 :     close(con)
334 : feinerer 56 Corpus(object) <- corpus
335 : feinerer 60 Cached(object) <- TRUE
336 : feinerer 56 return(object)
337 :     } else {
338 :     return(object)
339 :     }
340 :     })
341 : feinerer 66 setMethod("load_doc",
342 : feinerer 62 signature(object = "XMLTextDocument"),
343 : feinerer 65 function(object, ...) {
344 : feinerer 61 if (!Cached(object)) {
345 : feinerer 67 con <- eval(URI(object))
346 : feinerer 65 corpus <- paste(readLines(con), "\n", collapse = "")
347 :     close(con)
348 :     doc <- xmlTreeParse(corpus, asText = TRUE)
349 : feinerer 49 class(doc) <- "list"
350 : feinerer 56 Corpus(object) <- doc
351 : feinerer 60 Cached(object) <- TRUE
352 : feinerer 49 return(object)
353 :     } else {
354 :     return(object)
355 :     }
356 :     })
357 : feinerer 66 setMethod("load_doc",
358 : feinerer 62 signature(object = "NewsgroupDocument"),
359 : feinerer 65 function(object, ...) {
360 : feinerer 61 if (!Cached(object)) {
361 : feinerer 67 con <- eval(URI(object))
362 : feinerer 65 mail <- readLines(con)
363 :     close(con)
364 : feinerer 60 Cached(object) <- TRUE
365 : feinerer 65 for (index in seq(along = mail)) {
366 :     if (mail[index] == "")
367 :     break
368 :     }
369 : feinerer 56 Corpus(object) <- mail[(index + 1):length(mail)]
370 :     return(object)
371 :     } else {
372 :     return(object)
373 :     }
374 :     })
375 : feinerer 49
376 : feinerer 72 setGeneric("tm_update", function(object, origin, parser = plaintext_parser, ...) standardGeneric("tm_update"))
377 :     # Update is only supported for directories
378 :     # At the moment no other LoD devices are available anyway
379 :     setMethod("tm_update",
380 :     signature(object = "TextDocCol", origin = "DirSource"),
381 :     function(object, origin, parser = plaintext_parser, ...) {
382 :     if (inherits(parser, "function_generator"))
383 :     parser <- parser(...)
384 :    
385 :     object.filelist <- unlist(lapply(object, function(x) {as.character(URI(x))[2]}))
386 :     new.files <- setdiff(origin@FileList, object.filelist)
387 :    
388 :     for (filename in new.files) {
389 :     elem <- list(content = readLines(filename),
390 :     uri = substitute(file(filename)))
391 :     object <- append_doc(object, parser(elem, TRUE, origin@Load, filename), NA)
392 :     }
393 :    
394 :     return(object)
395 :     })
396 :    
397 : feinerer 54 setGeneric("tm_transform", function(object, FUN, ...) standardGeneric("tm_transform"))
398 :     setMethod("tm_transform",
399 : feinerer 62 signature(object = "TextDocCol", FUN = "function"),
400 : feinerer 49 function(object, FUN, ...) {
401 : feinerer 73 result <- object
402 :     result@.Data <- lapply(object, FUN, ..., DMetaData = DMetaData(object))
403 : feinerer 56 return(result)
404 : feinerer 49 })
405 :    
406 : feinerer 66 setGeneric("as.plaintext_doc", function(object, FUN, ...) standardGeneric("as.plaintext_doc"))
407 :     setMethod("as.plaintext_doc",
408 : feinerer 62 signature(object = "PlainTextDocument"),
409 : feinerer 49 function(object, FUN, ...) {
410 :     return(object)
411 :     })
412 : feinerer 66 setMethod("as.plaintext_doc",
413 : feinerer 62 signature(object = "XMLTextDocument", FUN = "function"),
414 : feinerer 49 function(object, FUN, ...) {
415 : feinerer 61 if (!Cached(object))
416 : feinerer 66 object <- load_doc(object)
417 : feinerer 49
418 : feinerer 56 corpus <- Corpus(object)
419 : feinerer 49
420 :     # As XMLDocument is no native S4 class, restore valid information
421 :     class(corpus) <- "XMLDocument"
422 :     names(corpus) <- c("doc","dtd")
423 :    
424 : feinerer 61 return(FUN(xmlRoot(corpus), ...))
425 : feinerer 49 })
426 :    
427 : feinerer 67 setGeneric("tm_tolower", function(object, ...) standardGeneric("tm_tolower"))
428 :     setMethod("tm_tolower",
429 :     signature(object = "PlainTextDocument"),
430 :     function(object, ...) {
431 :     if (!Cached(object))
432 :     object <- load_doc(object)
433 :    
434 :     Corpus(object) <- tolower(object)
435 :     return(object)
436 :     })
437 :    
438 :     setGeneric("strip_whitespace", function(object, ...) standardGeneric("strip_whitespace"))
439 :     setMethod("strip_whitespace",
440 :     signature(object = "PlainTextDocument"),
441 :     function(object, ...) {
442 :     if (!Cached(object))
443 :     object <- load_doc(object)
444 :    
445 :     Corpus(object) <- gsub("[[:space:]]+", " ", object)
446 :     return(object)
447 :     })
448 :    
449 : feinerer 66 setGeneric("stem_doc", function(object, ...) standardGeneric("stem_doc"))
450 :     setMethod("stem_doc",
451 : feinerer 62 signature(object = "PlainTextDocument"),
452 : feinerer 61 function(object, ...) {
453 :     if (!Cached(object))
454 : feinerer 66 object <- load_doc(object)
455 : feinerer 56
456 : feinerer 49 require(Rstem)
457 :     splittedCorpus <- unlist(strsplit(object, " ", fixed = TRUE))
458 : feinerer 67 stemmedCorpus <- wordStem(splittedCorpus)
459 : feinerer 56 Corpus(object) <- paste(stemmedCorpus, collapse = " ")
460 :     return(object)
461 : feinerer 49 })
462 :    
463 : feinerer 66 setGeneric("remove_words", function(object, stopwords, ...) standardGeneric("remove_words"))
464 :     setMethod("remove_words",
465 : feinerer 60 signature(object = "PlainTextDocument", stopwords = "character"),
466 : feinerer 61 function(object, stopwords, ...) {
467 :     if (!Cached(object))
468 : feinerer 66 object <- load_doc(object)
469 : feinerer 56
470 : feinerer 49 require(Rstem)
471 :     splittedCorpus <- unlist(strsplit(object, " ", fixed = TRUE))
472 :     noStopwordsCorpus <- splittedCorpus[!splittedCorpus %in% stopwords]
473 : feinerer 56 Corpus(object) <- paste(noStopwordsCorpus, collapse = " ")
474 :     return(object)
475 : feinerer 49 })
476 :    
477 : feinerer 73 setGeneric("tm_filter", function(object, ..., FUN = s_filter, doclevel = FALSE) standardGeneric("tm_filter"))
478 : feinerer 54 setMethod("tm_filter",
479 : feinerer 61 signature(object = "TextDocCol"),
480 : feinerer 73 function(object, ..., FUN = s_filter, doclevel = FALSE) {
481 :     if (doclevel)
482 :     return(object[sapply(object, FUN, ..., DMetaData = DMetaData(object))])
483 :     else
484 :     return(object[FUN(object, ...)])
485 : feinerer 61 })
486 :    
487 : feinerer 73 setGeneric("tm_index", function(object, ..., FUN = s_filter, doclevel = FALSE) standardGeneric("tm_index"))
488 : feinerer 61 setMethod("tm_index",
489 :     signature(object = "TextDocCol"),
490 : feinerer 73 function(object, ..., FUN = s_filter, doclevel = FALSE) {
491 :     if (doclevel)
492 :     return(sapply(object, FUN, ..., DMetaData = DMetaData(object)))
493 :     else
494 :     return(FUN(object, ...))
495 : feinerer 49 })
496 :    
497 : feinerer 73 s_filter <- function(object, s, ...) {
498 :     query.df <- DMetaData(object)
499 :     con <- textConnection(s)
500 :     tokens <- scan(con, "character")
501 :     close(con)
502 :     local.meta <- lapply(object, LocalMetaData)
503 :     local.used.meta <- lapply(local.meta, function(x) names(x) %in% tokens)
504 :     l.meta <- NULL
505 :     for (i in 1:length(object)) {
506 :     l.meta <- c(l.meta, list(local.meta[[i]][local.used.meta[[i]]]))
507 :     }
508 : feinerer 74 # Load local meta data from text documents into data frame
509 :     for (i in 1:length(l.meta)) {
510 :     l.meta[[i]] <- c(l.meta[[i]], list(author = Author(object[[i]])))
511 :     l.meta[[i]] <- c(l.meta[[i]], list(datetimestamp = DateTimeStamp(object[[i]])))
512 :     l.meta[[i]] <- c(l.meta[[i]], list(description = Description(object[[i]])))
513 :     l.meta[[i]] <- c(l.meta[[i]], list(identifier = ID(object[[i]])))
514 :     l.meta[[i]] <- c(l.meta[[i]], list(origin = Origin(object[[i]])))
515 :     l.meta[[i]] <- c(l.meta[[i]], list(heading = Heading(object[[i]])))
516 :     }
517 : feinerer 73 # TODO: Handle entries (\code{m} with length greater 1, i.e., lists)
518 :     for (i in 1:length(l.meta)) {
519 : feinerer 74 for (j in 1:length(l.meta[[i]])) {
520 :     m <- l.meta[[i]][[j]]
521 :     m.name <- names(l.meta[[i]][j])
522 :     if (!(m.name %in% names(query.df))) {
523 : feinerer 73 before <- rep(NA, i - 1)
524 :     after <- rep(NA, length(l.meta) - i)
525 :     insert <- c(before, m, after)
526 :     query.df <- cbind(query.df, insert, stringsAsFactors = FALSE)
527 : feinerer 74 names(query.df)[length(query.df)] <- m.name
528 : feinerer 73 }
529 :     else {
530 :     if (is.null(m))
531 :     m <- NA
532 :     #if (length(m) > 1)
533 :     # query.df[i,names(l.meta[[i]])] <- list(m)
534 :     #else
535 : feinerer 74 query.df[i,m.name] <- m
536 : feinerer 73 }
537 : feinerer 61 }
538 :     }
539 : feinerer 73 attach(query.df)
540 : feinerer 74 try(result <- rownames(query.df) %in% row.names(query.df[eval(parse(text = s)), ]))
541 : feinerer 73 detach(query.df)
542 :     return(result)
543 : feinerer 61 }
544 :    
545 : feinerer 73 #s_filter <- function(object, s, ..., DMetaData) {
546 :     # b <- TRUE
547 :     # for (tag in names(s)) {
548 :     # if (tag %in% names(LocalMetaData(object))) {
549 :     # b <- b && any(grep(s[[tag]], LocalMetaData(object)[[tag]]))
550 :     # } else if (tag %in% names(DMetaData)){
551 :     # b <- b && any(grep(s[[tag]], DMetaData[[tag]]))
552 :     # } else {
553 :     # b <- b && any(grep(s[[tag]], eval(call(tag, object))))
554 :     # }
555 :     # }
556 :     # return(b)
557 :     #}
558 :    
559 : feinerer 63 setGeneric("fulltext_search_filter", function(object, pattern, ...) standardGeneric("fulltext_search_filter"))
560 :     setMethod("fulltext_search_filter",
561 : feinerer 61 signature(object = "PlainTextDocument", pattern = "character"),
562 :     function(object, pattern, ...) {
563 :     if (!Cached(object))
564 : feinerer 66 object <- load_doc(object)
565 : feinerer 61
566 :     return(any(grep(pattern, Corpus(object))))
567 :     })
568 :    
569 : feinerer 66 setGeneric("attach_data", function(object, data) standardGeneric("attach_data"))
570 : feinerer 71 setGeneric("attach_metadata", function(object, name, metadata) standardGeneric("attach_metadata"))
571 :    
572 : feinerer 72 setGeneric("append_doc", function(object, data, meta = NULL) standardGeneric("append_doc"))
573 : feinerer 71 setMethod("append_doc",
574 : feinerer 72 signature(object = "TextDocCol", data = "TextDocument"),
575 :     function(object, data, meta = NULL) {
576 : feinerer 71 object@.Data <- c(object@.Data, list(data))
577 : feinerer 72 if (length(meta) > 0)
578 :     object@DMetaData <- rbind(object@DMetaData, c(MetaID = DCMetaData(object)@NodeID, meta))
579 : feinerer 52 return(object)
580 :     })
581 :    
582 : feinerer 72 setGeneric("append_meta", function(object, dcmeta = list(), dmeta = NULL) standardGeneric("append_meta"))
583 : feinerer 71 setMethod("append_meta",
584 : feinerer 72 signature(object = "TextDocCol"),
585 :     function(object, dcmeta = list(), dmeta = NULL) {
586 : feinerer 71 object@DCMetaData@MetaData <- c(object@DCMetaData@MetaData, dcmeta)
587 : feinerer 72 if (length(dmeta) > 0)
588 :     object@DMetaData <- cbind(object@DMetaData, dmeta)
589 : feinerer 52 return(object)
590 :     })
591 : feinerer 53
592 : feinerer 69 setGeneric("remove_metadata", function(object, name) standardGeneric("remove_metadata"))
593 : feinerer 71 #setMethod("remove_metadata",
594 :     # signature(object = "TextDocCol"),
595 :     # function(object, name) {
596 :     # object@DMetaData <- DMetaData(object)[names(DMetaData(object)) != name]
597 :     # return(object)
598 :     # })
599 : feinerer 69
600 :     setGeneric("modify_metadata", function(object, name, metadata) standardGeneric("modify_metadata"))
601 : feinerer 71 #setMethod("modify_metadata",
602 :     # signature(object = "TextDocCol"),
603 :     # function(object, name, metadata) {
604 :     # object@DMetaData[[name]] <- metadata
605 :     # return(object)
606 :     # })
607 : feinerer 69
608 : feinerer 74 # TODO: Handle metadata in document slots
609 : feinerer 73 setGeneric("prescind_meta", function(object, meta) standardGeneric("prescind_meta"))
610 :     setMethod("prescind_meta",
611 :     signature(object = "TextDocCol", meta = "character"),
612 :     function(object, meta) {
613 :     for (m in meta) {
614 :     local.meta <- lapply(object, LocalMetaData)
615 :     local.m <- lapply(local.meta, "[[", m)
616 :     local.m <- lapply(local.m, function(x) if (is.null(x)) return(NA) else return(x))
617 :     if (length(local.m) == length(unlist(local.m)))
618 :     local.m <- unlist(local.m)
619 :     else
620 :     local.m <- I(local.m)
621 :     object@DMetaData <- cbind(DMetaData(object), data.frame(m = local.m), stringsAsFactors = FALSE)
622 :     names(object@DMetaData)[length(object@DMetaData)] <- m
623 :     }
624 :     return(object)
625 :     })
626 :    
627 : feinerer 53 setMethod("[",
628 :     signature(x = "TextDocCol", i = "ANY", j = "ANY", drop = "ANY"),
629 :     function(x, i, j, ... , drop) {
630 :     if(missing(i))
631 :     return(x)
632 :    
633 :     object <- x
634 :     object@.Data <- x@.Data[i, ..., drop = FALSE]
635 : feinerer 71 object@DMetaData <- DMetaData(object)[i, ]
636 : feinerer 53 return(object)
637 :     })
638 :    
639 : feinerer 63 setMethod("[<-",
640 :     signature(x = "TextDocCol", i = "ANY", j = "ANY", value = "ANY"),
641 :     function(x, i, j, ... , value) {
642 :     object <- x
643 :     object@.Data[i, ...] <- value
644 :     return(object)
645 :     })
646 :    
647 :     setMethod("[[",
648 :     signature(x = "TextDocCol", i = "ANY", j = "ANY"),
649 :     function(x, i, j, ...) {
650 :     return(x@.Data[[i, ...]])
651 :     })
652 :    
653 :     setMethod("[[<-",
654 :     signature(x = "TextDocCol", i = "ANY", j = "ANY", value = "ANY"),
655 :     function(x, i, j, ..., value) {
656 :     object <- x
657 :     object@.Data[[i, ...]] <- value
658 :     return(object)
659 :     })
660 :    
661 : feinerer 71 # Update \code{NodeID}s of a DCMetaData tree
662 :     # TODO: Avoid global variables outside of update_id function
663 :     update_id <- function(object) {
664 :     id <<- 0
665 :     mapping <<- left.mapping <<- NULL
666 :     level <<- 0
667 :     return(list(root = set_id(object), left.mapping = left.mapping, right.mapping = mapping))
668 :     }
669 :    
670 :     # Traversal of (binary) DCMetaData tree with setup of \code{NodeID}s
671 :     set_id <- function(object) {
672 :     object@NodeID <- id
673 :     id <<- id + 1
674 :     level <<- level + 1
675 :    
676 :     if (length(object@children) > 0) {
677 :     mapping <<- cbind(mapping, c(object@children[[1]]@NodeID, id))
678 :     left <- set_id(object@children[[1]])
679 :     if (level == 1) {
680 :     left.mapping <<- mapping
681 :     mapping <<- NULL
682 :     }
683 :     mapping <<- cbind(mapping, c(object@children[[2]]@NodeID, id))
684 :     right <- set_id(object@children[[2]])
685 :    
686 :     object@children <- list(left, right)
687 :     }
688 :     level <<- level - 1
689 :    
690 :     return(object)
691 :     }
692 :    
693 : feinerer 53 setMethod("c",
694 :     signature(x = "TextDocCol"),
695 : feinerer 71 function(x, y, ..., meta = list(merge_date = date(), merger = Sys.getenv("LOGNAME")), recursive = TRUE) {
696 :     if (!inherits(y, "TextDocCol"))
697 :     stop("invalid argument")
698 :    
699 :     object <- x
700 :     # Concatenate data slots
701 :     object@.Data <- c(as(x, "list"), as(y, "list"))
702 :    
703 :     # Update the DCMetaData tree
704 :     dcmeta <- new("MetaDataNode", NodeID = 0, MetaData = meta, children = list(DCMetaData(x), DCMetaData(y)))
705 :     update.struct <- update_id(dcmeta)
706 :     object@DCMetaData <- update.struct$root
707 :    
708 :     # Find indices to be updated for the left tree
709 :     indices.mapping <- NULL
710 :     for (m in levels(as.factor(DMetaData(x)$MetaID))) {
711 :     indices <- (DMetaData(x)$MetaID == m)
712 :     indices.mapping <- c(indices.mapping, list(m = indices))
713 :     names(indices.mapping)[length(indices.mapping)] <- m
714 :     }
715 :    
716 :     # Update the DMetaData data frames for the left tree
717 :     for (i in 1:ncol(update.struct$left.mapping)) {
718 :     map <- update.struct$left.mapping[,i]
719 :     x@DMetaData$MetaID <- replace(DMetaData(x)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
720 :     }
721 :    
722 :     # Find indices to be updated for the right tree
723 :     indices.mapping <- NULL
724 :     for (m in levels(as.factor(DMetaData(y)$MetaID))) {
725 :     indices <- (DMetaData(y)$MetaID == m)
726 :     indices.mapping <- c(indices.mapping, list(m = indices))
727 :     names(indices.mapping)[length(indices.mapping)] <- m
728 :     }
729 :    
730 :     # Update the DMetaData data frames for the right tree
731 :     for (i in 1:ncol(update.struct$right.mapping)) {
732 :     map <- update.struct$right.mapping[,i]
733 :     y@DMetaData$MetaID <- replace(DMetaData(y)$MetaID, indices.mapping[[as.character(map[1])]], map[2])
734 :     }
735 :    
736 :     # Merge the DMetaData data frames
737 :     labels <- setdiff(names(DMetaData(y)), names(DMetaData(x)))
738 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(x)), ncol = length(labels), dimnames = list(row.names(DMetaData(x)), labels))
739 :     x.dmeta.aug <- cbind(DMetaData(x), na.matrix)
740 :     labels <- setdiff(names(DMetaData(x)), names(DMetaData(y)))
741 :     na.matrix <- matrix(NA, nrow = nrow(DMetaData(y)), ncol = length(labels), dimnames = list(row.names(DMetaData(y)), labels))
742 :     y.dmeta.aug <- cbind(DMetaData(y), na.matrix)
743 :     object@DMetaData <- rbind(x.dmeta.aug, y.dmeta.aug)
744 :    
745 :     return(object)
746 : feinerer 72 })
747 :     setMethod("c",
748 :     signature(x = "TextDocument"),
749 :     function(x, ..., recursive = TRUE){
750 :     args <- list(...)
751 :     if(length(args) == 0)
752 :     return(x)
753 : feinerer 54
754 : feinerer 73 dmeta.df <- data.frame(MetaID = rep(0, length(list(x, ...))), stringsAsFactors = FALSE)
755 : feinerer 72 dcmeta.node <- new("MetaDataNode",
756 :     NodeID = 0,
757 :     MetaData = list(create_date = date(), creator = Sys.getenv("LOGNAME")),
758 :     children = list())
759 :    
760 :     return(new("TextDocCol", .Data = list(x, ...), DMetaData = dmeta.df, DCMetaData = dcmeta.node))
761 :     })
762 :    
763 : feinerer 54 setMethod("length",
764 :     signature(x = "TextDocCol"),
765 :     function(x){
766 :     return(length(as(x, "list")))
767 :     })
768 :    
769 :     setMethod("show",
770 :     signature(object = "TextDocCol"),
771 :     function(object){
772 : feinerer 70 cat(sprintf(ngettext(length(object),
773 :     "A text document collection with %d text document\n",
774 :     "A text document collection with %d text documents\n"),
775 :     length(object)))
776 : feinerer 54 })
777 :    
778 :     setMethod("summary",
779 :     signature(object = "TextDocCol"),
780 :     function(object){
781 :     show(object)
782 : feinerer 71 if (length(DMetaData(object)) > 0) {
783 :     cat(sprintf(ngettext(length(DMetaData(object)),
784 : feinerer 70 "\nThe global metadata consists of %d tag-value pair\n",
785 :     "\nThe global metadata consists of %d tag-value pairs\n"),
786 : feinerer 71 length(DMetaData(object))))
787 : feinerer 54 cat("Available tags are:\n")
788 : feinerer 71 cat(names(DMetaData(object)), "\n")
789 : feinerer 54 }
790 :     })
791 : feinerer 55
792 :     setGeneric("inspect", function(object) standardGeneric("inspect"))
793 :     setMethod("inspect",
794 : feinerer 65 signature("TextDocCol"),
795 : feinerer 55 function(object) {
796 :     summary(object)
797 :     cat("\n")
798 :     show(as(object, "list"))
799 :     })
800 : feinerer 65
801 :     # No metadata is checked
802 :     setGeneric("%IN%", function(x, y) standardGeneric("%IN%"))
803 :     setMethod("%IN%",
804 :     signature(x = "TextDocument", y = "TextDocCol"),
805 :     function(x, y) {
806 :     x %in% y
807 :     })

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