资料如下
[hide]
[/hide]
代码诸如:
[hide]
- ### R code from vignette source 'tm.Rnw'
- ### Encoding: UTF-8
- ###################################################
- ### code chunk number 1: Init
- ###################################################
- library("tm")
- data("crude")
- ###################################################
- ### code chunk number 2: Ovid
- ###################################################
- txt <- system.file("texts", "txt", package = "tm")
- (ovid <- Corpus(DirSource(txt, encoding = "UTF-8"),
- readerControl = list(language = "lat")))
- ###################################################
- ### code chunk number 3: VectorSource
- ###################################################
- docs <- c("This is a text.", "This another one.")
- Corpus(VectorSource(docs))
- ###################################################
- ### code chunk number 4: Reuters
- ###################################################
- reut21578 <- system.file("texts", "crude", package = "tm")
- reuters <- Corpus(DirSource(reut21578),
- readerControl = list(reader = readReut21578XML))
- ###################################################
- ### code chunk number 5: tm.Rnw:120-121 (eval = FALSE)
- ###################################################
- ## writeCorpus(ovid)
- ###################################################
- ### code chunk number 6: tm.Rnw:132-133
- ###################################################
- inspect(ovid[1:2])
- ###################################################
- ### code chunk number 7: tm.Rnw:137-138
- ###################################################
- identical(ovid[[2]], ovid[["ovid_2.txt"]])
- ###################################################
- ### code chunk number 8: tm.Rnw:156-157
- ###################################################
- reuters <- tm_map(reuters, as.PlainTextDocument)
- ###################################################
- ### code chunk number 9: tm.Rnw:165-166
- ###################################################
- reuters <- tm_map(reuters, stripWhitespace)
- ###################################################
- ### code chunk number 10: tm.Rnw:171-172
- ###################################################
- reuters <- tm_map(reuters, tolower)
- ###################################################
- ### code chunk number 11: Stopwords
- ###################################################
- reuters <- tm_map(reuters, removeWords, stopwords("english"))
- ###################################################
- ### code chunk number 12: Stemming
- ###################################################
- tm_map(reuters, stemDocument)
- ###################################################
- ### code chunk number 13: tm.Rnw:204-206
- ###################################################
- query <- "id == '237' & heading == 'INDONESIA SEEN AT CROSSROADS OVER ECONOMIC CHANGE'"
- tm_filter(reuters, FUN = sFilter, query)
- ###################################################
- ### code chunk number 14: DublinCore
- ###################################################
- DublinCore(crude[[1]], "Creator") <- "Ano Nymous"
- meta(crude[[1]])
- ###################################################
- ### code chunk number 15: tm.Rnw:237-241
- ###################################################
- meta(crude, tag = "test", type = "corpus") <- "test meta"
- meta(crude, type = "corpus")
- meta(crude, "foo") <- letters[1:20]
- meta(crude)
- ###################################################
- ### code chunk number 16: tm.Rnw:258-260
- ###################################################
- dtm <- DocumentTermMatrix(reuters)
- inspect(dtm[1:5,100:105])
- ###################################################
- ### code chunk number 17: tm.Rnw:269-270
- ###################################################
- findFreqTerms(dtm, 5)
- ###################################################
- ### code chunk number 18: tm.Rnw:275-276
- ###################################################
- findAssocs(dtm, "opec", 0.8)
- ###################################################
- ### code chunk number 19: tm.Rnw:288-289
- ###################################################
- inspect(removeSparseTerms(dtm, 0.4))
- ###################################################
- ### code chunk number 20: tm.Rnw:303-305
- ###################################################
- inspect(DocumentTermMatrix(reuters,
- list(dictionary = c("prices", "crude", "oil"))))
复制代码
- ### R code from vignette source 'extensions.Rnw'
- ###################################################
- ### code chunk number 1: Init
- ###################################################
- library("tm")
- library("XML")
- ###################################################
- ### code chunk number 2: extensions.Rnw:71-76
- ###################################################
- VecSource <- function(x) {
- s <- Source(length = length(x), names = names(x), class = "VectorSource")
- s$Content <- as.character(x)
- s
- }
- ###################################################
- ### code chunk number 3: extensions.Rnw:85-89
- ###################################################
- getElem.VectorSource <-
- function(x) list(content = x$Content[x$Position], uri = NA)
- pGetElem.VectorSource <-
- function(x) lapply(x$Content, function(y) list(content = y, uri = NA))
- ###################################################
- ### code chunk number 4: extensions.Rnw:114-117
- ###################################################
- readPlain <-
- function(elem, language, id)
- PlainTextDocument(elem$content, id = id, language = language)
- ###################################################
- ### code chunk number 5: extensions.Rnw:145-150
- ###################################################
- df <- data.frame(contents = c("content 1", "content 2", "content 3"),
- title = c("title 1", "title 2", "title 3"),
- authors= c("author 1" , "author 2" , "author 3" ),
- topics = c("topic 1", "topic 2", "topic 3"),
- stringsAsFactors = FALSE)
- ###################################################
- ### code chunk number 6: extensions.Rnw:156-157
- ###################################################
- names(attributes(PlainTextDocument()))
- ###################################################
- ### code chunk number 7: Mapping
- ###################################################
- m <- list(Content = "contents", Heading = "title",
- Author = "authors", Topic = "topics")
- ###################################################
- ### code chunk number 8: myReader
- ###################################################
- myReader <- readTabular(mapping = m)
- ###################################################
- ### code chunk number 9: extensions.Rnw:180-181
- ###################################################
- (corpus <- Corpus(DataframeSource(df), readerControl = list(reader = myReader)))
- ###################################################
- ### code chunk number 10: extensions.Rnw:186-188
- ###################################################
- corpus[[1]]
- meta(corpus[[1]])
- ###################################################
- ### code chunk number 11: CustomXMLFile
- ###################################################
- custom.xml <- system.file("texts", "custom.xml", package = "tm")
- print(readLines(custom.xml), quote = FALSE)
- ###################################################
- ### code chunk number 12: mySource
- ###################################################
- mySource <- function(x, encoding = "UTF-8")
- XMLSource(x, function(tree) XML::xmlChildren(XML::xmlRoot(tree)), myXMLReader, encoding)
- ###################################################
- ### code chunk number 13: myXMLReader
- ###################################################
- myXMLReader <- readXML(
- spec = list(Author = list("node", "/document/writer"),
- Content = list("node", "/document/description"),
- DateTimeStamp = list("function",
- function(x) as.POSIXlt(Sys.time(), tz = "GMT")),
- Description = list("attribute", "/document/@short"),
- Heading = list("node", "/document/caption"),
- ID = list("function", function(x) tempfile()),
- Origin = list("unevaluated", "My private bibliography"),
- Type = list("node", "/document/type")),
- doc = PlainTextDocument())
- ###################################################
- ### code chunk number 14: extensions.Rnw:301-302
- ###################################################
- corpus <- Corpus(mySource(custom.xml))
- ###################################################
- ### code chunk number 15: extensions.Rnw:306-308
- ###################################################
- corpus[[1]]
- meta(corpus[[1]])
复制代码
[/hide]
|