SCM

SCM Repository

[tm] Diff of /pkg/R/corpus.R
ViewVC logotype

Diff of /pkg/R/corpus.R

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/R/trunk/R/textdoccol.R revision 18, Sat Nov 5 19:00:05 2005 UTC pkg/R/corpus.R revision 1333, Fri Apr 18 10:38:46 2014 UTC
# Line 1  Line 1 
1  # Author: Ingo Feinerer  # Author: Ingo Feinerer
2    
3  # S4 class definition  PCorpus <-
4  # Text document collection  function(x,
5  # TODO: Define proper S4 term-document matrix           readerControl = list(reader = x$defaultreader, language = "en"),
6  setClass("textdoccol", representation(docs = "list",           dbControl = list(dbName = "", dbType = "DB1"))
7                                        tdm = "matrix"))  {
8        stopifnot(inherits(x, "Source"))
9  # Accessor function  
10  if (!isGeneric("docs")) {      readerControl <- prepareReader(readerControl, x$defaultreader)
11      if (is.function("docs"))  
12          fun <- docs      if (is.function(readerControl$init))
13            readerControl$init()
14    
15        if (is.function(readerControl$exit))
16            on.exit(readerControl$exit())
17    
18        if (!filehash::dbCreate(dbControl$dbName, dbControl$dbType))
19            stop("error in creating database")
20        db <- filehash::dbInit(dbControl$dbName, dbControl$dbType)
21    
22        # Allocate memory in advance if length is known
23        tdl <- if (x$length > 0) vector("list", as.integer(x$length)) else list()
24    
25        counter <- 1
26        while (!eoi(x)) {
27            x <- stepNext(x)
28            elem <- getElem(x)
29            id <- if (is.null(x$names) || is.na(x$names))
30                as.character(counter)
31      else      else
32          fun <- function(object) standardGeneric("docs")              x$names[counter]
33      setGeneric("docs", fun)          doc <- readerControl$reader(elem, readerControl$language, id)
34            filehash::dbInsert(db, meta(doc, "id"), doc)
35            if (x$length > 0) tdl[[counter]] <- meta(doc, "id")
36            else tdl <- c(tdl, meta(doc, "id"))
37            counter <- counter + 1
38        }
39        if (!is.null(x$names) && !is.na(x$names))
40            names(tdl) <- x$names
41    
42        structure(list(content = tdl,
43                       meta = CorpusMeta(),
44                       dmeta = data.frame(row.names = seq_along(tdl)),
45                       dbcontrol = dbControl),
46                  class = c("PCorpus", "Corpus"))
47  }  }
 setMethod("docs", "textdoccol", function(object) object@docs)  
48    
49  setGeneric("textdoccol", function(docs) standardGeneric("textdoccol"))  VCorpus <-
50  # Read in XML text documents  function(x, readerControl = list(reader = x$defaultreader, language = "en"))
51  # Reuters Corpus Volume 1 (RCV1)  {
52  setMethod("textdoccol", "character", function(docs) {      stopifnot(inherits(x, "Source"))
53      require(XML)  
54        readerControl <- prepareReader(readerControl, x$defaultreader)
     tree <- xmlTreeParse(docs)  
     root <- xmlRoot(tree)  
   
     # TODO: At each loop node points to the current newsitem  
     node <- root  
   
     # TODO: Implement lacking fields.  
     # For this we need the full RCV1 XML set to know where to find those things  
     author <- "Not yet implemented"  
     timestamp <- xmlAttrs(node)[["date"]]  
     description <- "Not yet implemented"  
     id <- as.integer(xmlAttrs(node)[["itemid"]])  
     origin <- "Not yet implemented"  
     corpus <- unlist(xmlApply(node[["text"]], xmlValue), use.names = FALSE)  
55    
56      heading <- xmlValue(node[["title"]])      if (is.function(readerControl$init))
57            readerControl$init()
58    
59      doc <- new("textdocument", author = author, timestamp = timestamp, description = description,      if (is.function(readerControl$exit))
60                 id = id, origin = origin, corpus = corpus, heading = heading)          on.exit(readerControl$exit())
61    
62      new("textdoccol", docs = list(doc), tdm = matrix())      # Allocate memory in advance if length is known
63  })      tdl <- if (x$length > 0) vector("list", as.integer(x$length)) else list()
64    
65        if (x$vectorized)
66            tdl <- mapply(function(elem, id)
67                              readerControl$reader(elem, readerControl$language, id),
68                          pGetElem(x),
69                          id = if (is.null(x$names) || is.na(x$names))
70                              as.character(seq_len(x$length))
71                          else x$names,
72                          SIMPLIFY = FALSE)
73        else {
74            counter <- 1
75            while (!eoi(x)) {
76                x <- stepNext(x)
77                elem <- getElem(x)
78                id <- if (is.null(x$names) || is.na(x$names))
79                    as.character(counter)
80                else
81                    x$names[counter]
82                doc <- readerControl$reader(elem, readerControl$language, id)
83                if (x$length > 0)
84                    tdl[[counter]] <- doc
85                else
86                    tdl <- c(tdl, list(doc))
87                counter <- counter + 1
88            }
89        }
90        if (!is.null(x$names) && !is.na(x$names))
91            names(tdl) <- x$names
92    
93        structure(list(content = tdl,
94                       meta = CorpusMeta(),
95                       dmeta = data.frame(row.names = seq_along(tdl))),
96                  class = c("VCorpus", "Corpus"))
97    }
98    
99    `[.PCorpus` <-
100    function(x, i)
101    {
102        if (!missing(i)) {
103            x$content <- x$content[i]
104            x$dmeta <- x$dmeta[i, , drop = FALSE]
105        }
106        x
107    }
108    
109    `[.VCorpus` <-
110    function(x, i)
111    {
112        if (!missing(i)) {
113            x$content <- x$content[i]
114            x$dmeta <- x$dmeta[i, , drop = FALSE]
115            if (!is.null(x$lazy))
116                x$lazy$index <- x$lazy$index[i]
117        }
118        x
119    }
120    
121    .map_name_index <-
122    function(x, i)
123    {
124        if (is.character(i))
125            match(i, if (is.null(names(x))) meta(x, "id", "local") else names(x))
126        else
127            i
128    }
129    
130    `[[.PCorpus` <-
131    function(x, i)
132    {
133        i <- .map_name_index(x, i)
134        db <- filehash::dbInit(x$dbcontrol[["dbName"]], x$dbcontrol[["dbType"]])
135        filehash::dbFetch(db, x$content[[i]])
136    }
137    `[[.VCorpus` <-
138    function(x, i)
139    {
140        i <- .map_name_index(x, i)
141        if (!is.null(x$lazy))
142            .Call(copyCorpus, x, materialize(x, i))
143        x$content[[i]]
144    }
145    
146    `[[<-.PCorpus` <-
147    function(x, i, value)
148    {
149        i <- .map_name_index(x, i)
150        db <- filehash::dbInit(x$dbcontrol[["dbName"]], x$dbcontrol[["dbType"]])
151        db[[x$content[[i]]]] <- value
152        x
153    }
154    `[[<-.VCorpus` <-
155    function(x, i, value)
156    {
157        i <- .map_name_index(x, i)
158        # Mark new objects as inactive for lazy mapping
159        if (!is.null(x$lazy))
160            x$lazy$index[i] <- FALSE
161        x$content[[i]] <- value
162        x
163    }
164    
165    outer_union <-
166    function(x, y, ...)
167    {
168        if (nrow(x) > 0L)
169            x[, setdiff(names(y), names(x))] <- NA
170        if (nrow(y) > 0L)
171            y[, setdiff(names(x), names(y))] <- NA
172        res <- rbind(x, y)
173        if (ncol(res) == 0L)
174            res <- data.frame(row.names = seq_len(nrow(x) + nrow(y)))
175        res
176    }
177    
178    c.VCorpus <-
179    function(..., recursive = FALSE)
180    {
181        args <- list(...)
182        x <- args[[1L]]
183    
184        if (length(args) == 1L)
185            return(x)
186    
187        if (!all(unlist(lapply(args, inherits, class(x)))))
188            stop("not all arguments are of the same corpus type")
189    
190        structure(list(content = do.call("c", lapply(args, content)),
191                       meta = structure(do.call("c",
192                         lapply(args, function(a) meta(a, type = "corpus"))),
193                                        class = "CorpusMeta"),
194                       dmeta = Reduce(outer_union, lapply(args, meta))),
195                  class = c("VCorpus", "Corpus"))
196    }
197    
198    as.list.PCorpus <- as.list.VCorpus <-
199    function(x, ...)
200        content(x)
201    
202    content.VCorpus <-
203    function(x)
204    {
205        if (!is.null(x$lazy))
206            .Call(copyCorpus, x, materialize(x))
207        x$content
208    }
209    
210    content.PCorpus <-
211    function(x)
212    {
213        db <- filehash::dbInit(x$dbcontrol[["dbName"]], x$dbcontrol[["dbType"]])
214        filehash::dbMultiFetch(db, unlist(x$content))
215    }
216    
217    length.PCorpus <- length.VCorpus <-
218    function(x)
219        length(x$content)
220    
221    print.PCorpus <- print.VCorpus <-
222    function(x, ...)
223    {
224        writeLines(sprintf("<<%s (documents: %d, metadata (corpus/indexed): %d/%d)>>",
225                           class(x)[1],
226                           length(x),
227                           length(meta(x, type = "corpus")),
228                           ncol(meta(x, type = "indexed"))))
229        invisible(x)
230    }
231    
232    inspect <-
233    function(x)
234        UseMethod("inspect", x)
235    inspect.PCorpus <- inspect.VCorpus <-
236    function(x)
237    {
238        print(x)
239        cat("\n")
240        print(noquote(content(x)))
241        invisible(x)
242    }
243    
244    writeCorpus <-
245    function(x, path = ".", filenames = NULL)
246    {
247        filenames <- file.path(path,
248          if (is.null(filenames))
249              sprintf("%s.txt", as.character(meta(x, "id", "local")))
250          else filenames)
251    
252        stopifnot(length(x) == length(filenames))
253    
254        mapply(function(doc, f) writeLines(as.character(doc), f), x, filenames)
255    
256        invisible(x)
257    }

Legend:
Removed from v.18  
changed lines
  Added in v.1333

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