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

revision 22, Sat Nov 19 16:58:34 2005 UTC revision 32, Thu Dec 15 13:13:54 2005 UTC
# Line 3  Line 3 
3  # S4 class definition  # S4 class definition
4  # Text document collection  # Text document collection
5  setClass("textdoccol",  setClass("textdoccol",
          representation(tdm = "termdocmatrix"),  
6           contains = c("list"))           contains = c("list"))
7    
 # Accessor functions as described in "S4 Classes in 15 pages, more or less"  
   
 if (!isGeneric("tdm")) {  
     if (is.function("tdm"))  
         fun <- tdm  
     else  
         fun <- function(object) standardGeneric("tdm")  
     setGeneric("tdm", fun)  
 }  
 setMethod("tdm", "textdoccol", function(object) object@tdm)  
   
8  # Constructors  # Constructors
9    
10  setGeneric("textdoccol", function(object, ...) standardGeneric("textdoccol"))  setGeneric("textdoccol", function(object, ...) standardGeneric("textdoccol"))
11  setMethod("textdoccol",  setMethod("textdoccol",
12            c("character", "character", "logical", "logical",  "character",            c("character", "character", "logical", "logical"),
13              "logical", "character", "integer", "integer", "logical"),            function(object, inputType = "RCV1", stripWhiteSpace = FALSE, toLower = FALSE) {
           function(object, inputType = "RCV1", stripWhiteSpace = FALSE, toLower = FALSE, weighting = "tf",  
                    stemming = FALSE, language = "english", minWordLength = 3, minDocFreq = 1, stopwords = NULL) {  
14    
15                # Add a new type for each unique input source format                # Add a new type for each unique input source format
16                type <- match.arg(inputType,c("RCV1","CSV"))                type <- match.arg(inputType,c("RCV1","CSV","REUT21578"))
17                switch(type,                switch(type,
18                       # Read in text documents in XML Reuters Corpus Volume 1 (RCV1) format                       # Read in text documents in XML Reuters Corpus Volume 1 (RCV1) format
19                         # For the moment the first argument is still a single file
20                         # This will be changed to a directory as soon as we have the full RCV1 data set
21                       "RCV1" = {                       "RCV1" = {
                          require(XML)  
   
22                           tree <- xmlTreeParse(object)                           tree <- xmlTreeParse(object)
23                           tdcl <- new("textdoccol", .Data = xmlApply(xmlRoot(tree), parseNewsItem, stripWhiteSpace, toLower))                           tdcl <- new("textdoccol", .Data = xmlApply(xmlRoot(tree), parseNewsItem, stripWhiteSpace, toLower))
24                       },                       },
25                       # Text in CSV format (as e.g. exported from an Excel sheet)                       # Text in a special CSV format (as e.g. exported from an Excel sheet)
26                         # For details on the file format see data/Umfrage.csv
27                         # The first argument has to be a single file
28                       "CSV" = {                       "CSV" = {
29                           m <- as.matrix(read.csv(object))                           m <- as.matrix(read.csv(object))
30                           l <- vector("list", dim(m)[1])                           l <- vector("list", dim(m)[1])
# Line 57  Line 45 
45                                   description = description, id = id, origin = origin, heading = heading)                                   description = description, id = id, origin = origin, heading = heading)
46                           }                           }
47                           tdcl <- new("textdoccol", .Data = l)                           tdcl <- new("textdoccol", .Data = l)
48                       }                       },
49                       )                       # Read in text documents in Reuters-21578 XML (not SGML) format
50                         # Typically the first argument will be a directory where we can
51                tdcl@tdm <- termdocmatrix(tdcl, weighting, stemming, language, minWordLength, minDocFreq, stopwords)                       # find the files reut2-000.xml ... reut2-021.xml
52                         "REUT21578" = {
53                             tdl <- sapply(dir(object,
54                                               pattern = ".xml",
55                                               full.names = TRUE),
56                                           function(file) {
57                                               tree <- xmlTreeParse(file)
58                                               xmlApply(xmlRoot(tree), parseReuters, stripWhiteSpace, toLower)
59                                           })
60    
61                             tdcl <- new("textdoccol", .Data = tdl)
62                         })
63                tdcl                tdcl
64            })            })
65    
66  # Parse a <newsitem></newsitem> element from a valid RCV1 XML file  # Parse a <newsitem></newsitem> element from a well-formed RCV1 XML file
67  parseNewsItem <- function(node, stripWhiteSpace = FALSE, toLower = FALSE) {  parseNewsItem <- function(node, stripWhiteSpace = FALSE, toLower = FALSE) {
68      author <- "Not yet implemented"      author <- "Not yet implemented"
69      timestamp <- xmlAttrs(node)[["date"]]      timestamp <- xmlAttrs(node)[["date"]]
# Line 83  Line 81 
81    
82      new("textdocument", .Data = corpus, author = author, timestamp = timestamp,      new("textdocument", .Data = corpus, author = author, timestamp = timestamp,
83          description = description, id = id, origin = origin, heading = heading)          description = description, id = id, origin = origin, heading = heading)
84    }
85    
86    # Parse a <REUTERS></REUTERS> element from a well-formed Reuters-21578 XML file
87    parseReuters <- function(node, stripWhiteSpace = FALSE, toLower = FALSE) {
88        author <- "Not yet implemented"
89        timestamp <- xmlValue(node[["DATE"]])
90        description <- "Not yet implemented"
91        id <- as.integer(xmlAttrs(node)[["NEWID"]])
92    
93        origin <- "Not yet implemented"
94    
95        # The <BODY></BODY> tag is unfortunately NOT obligatory!
96        if (!is.null(node[["TEXT"]][["BODY"]]))
97            corpus <- xmlValue(node[["TEXT"]][["BODY"]])
98        else
99            corpus <- ""
100    
101        if (stripWhiteSpace)
102            corpus <- gsub("[[:space:]]+", " ", corpus)
103        if (toLower)
104            corpus <- tolower(corpus)
105    
106        # The <TITLE></TITLE> tag is unfortunately NOT obligatory!
107        if (!is.null(node[["TEXT"]][["TITLE"]]))
108            heading <- xmlValue(node[["TEXT"]][["TITLE"]])
109        else
110            heading <- ""
111    
112        new("textdocument", .Data = corpus, author = author, timestamp = timestamp,
113            description = description, id = id, origin = origin, heading = heading)
114  }  }

Legend:
Removed from v.22  
changed lines
  Added in v.32

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