搜索
人大经济论坛 附件下载

附件下载

所在主题:
文件名:  Rwordseg_Vignette_CN.pdf
资料下载链接地址: https://bbs.pinggu.org/a-1719649.html
附件大小:
315.88 KB   举报本内容


资料如下


[hide]


[/hide]




代码诸如:


[hide]
  1. ### R code from vignette source 'tm.Rnw'
  2. ### Encoding: UTF-8

  3. ###################################################
  4. ### code chunk number 1: Init
  5. ###################################################
  6. library("tm")
  7. data("crude")


  8. ###################################################
  9. ### code chunk number 2: Ovid
  10. ###################################################
  11. txt <- system.file("texts", "txt", package = "tm")
  12. (ovid <- Corpus(DirSource(txt, encoding = "UTF-8"),
  13. readerControl = list(language = "lat")))


  14. ###################################################
  15. ### code chunk number 3: VectorSource
  16. ###################################################
  17. docs <- c("This is a text.", "This another one.")
  18. Corpus(VectorSource(docs))


  19. ###################################################
  20. ### code chunk number 4: Reuters
  21. ###################################################
  22. reut21578 <- system.file("texts", "crude", package = "tm")
  23. reuters <- Corpus(DirSource(reut21578),
  24. readerControl = list(reader = readReut21578XML))


  25. ###################################################
  26. ### code chunk number 5: tm.Rnw:120-121 (eval = FALSE)
  27. ###################################################
  28. ## writeCorpus(ovid)


  29. ###################################################
  30. ### code chunk number 6: tm.Rnw:132-133
  31. ###################################################
  32. inspect(ovid[1:2])


  33. ###################################################
  34. ### code chunk number 7: tm.Rnw:137-138
  35. ###################################################
  36. identical(ovid[[2]], ovid[["ovid_2.txt"]])


  37. ###################################################
  38. ### code chunk number 8: tm.Rnw:156-157
  39. ###################################################
  40. reuters <- tm_map(reuters, as.PlainTextDocument)


  41. ###################################################
  42. ### code chunk number 9: tm.Rnw:165-166
  43. ###################################################
  44. reuters <- tm_map(reuters, stripWhitespace)


  45. ###################################################
  46. ### code chunk number 10: tm.Rnw:171-172
  47. ###################################################
  48. reuters <- tm_map(reuters, tolower)


  49. ###################################################
  50. ### code chunk number 11: Stopwords
  51. ###################################################
  52. reuters <- tm_map(reuters, removeWords, stopwords("english"))


  53. ###################################################
  54. ### code chunk number 12: Stemming
  55. ###################################################
  56. tm_map(reuters, stemDocument)


  57. ###################################################
  58. ### code chunk number 13: tm.Rnw:204-206
  59. ###################################################
  60. query <- "id == '237' & heading == 'INDONESIA SEEN AT CROSSROADS OVER ECONOMIC CHANGE'"
  61. tm_filter(reuters, FUN = sFilter, query)


  62. ###################################################
  63. ### code chunk number 14: DublinCore
  64. ###################################################
  65. DublinCore(crude[[1]], "Creator") <- "Ano Nymous"
  66. meta(crude[[1]])


  67. ###################################################
  68. ### code chunk number 15: tm.Rnw:237-241
  69. ###################################################
  70. meta(crude, tag = "test", type = "corpus") <- "test meta"
  71. meta(crude, type = "corpus")
  72. meta(crude, "foo") <- letters[1:20]
  73. meta(crude)


  74. ###################################################
  75. ### code chunk number 16: tm.Rnw:258-260
  76. ###################################################
  77. dtm <- DocumentTermMatrix(reuters)
  78. inspect(dtm[1:5,100:105])


  79. ###################################################
  80. ### code chunk number 17: tm.Rnw:269-270
  81. ###################################################
  82. findFreqTerms(dtm, 5)


  83. ###################################################
  84. ### code chunk number 18: tm.Rnw:275-276
  85. ###################################################
  86. findAssocs(dtm, "opec", 0.8)


  87. ###################################################
  88. ### code chunk number 19: tm.Rnw:288-289
  89. ###################################################
  90. inspect(removeSparseTerms(dtm, 0.4))


  91. ###################################################
  92. ### code chunk number 20: tm.Rnw:303-305
  93. ###################################################
  94. inspect(DocumentTermMatrix(reuters,
  95. list(dictionary = c("prices", "crude", "oil"))))
复制代码


  1. ### R code from vignette source 'extensions.Rnw'

  2. ###################################################
  3. ### code chunk number 1: Init
  4. ###################################################
  5. library("tm")
  6. library("XML")


  7. ###################################################
  8. ### code chunk number 2: extensions.Rnw:71-76
  9. ###################################################
  10. VecSource <- function(x) {
  11. s <- Source(length = length(x), names = names(x), class = "VectorSource")
  12. s$Content <- as.character(x)
  13. s
  14. }


  15. ###################################################
  16. ### code chunk number 3: extensions.Rnw:85-89
  17. ###################################################
  18. getElem.VectorSource <-
  19. function(x) list(content = x$Content[x$Position], uri = NA)
  20. pGetElem.VectorSource <-
  21. function(x) lapply(x$Content, function(y) list(content = y, uri = NA))


  22. ###################################################
  23. ### code chunk number 4: extensions.Rnw:114-117
  24. ###################################################
  25. readPlain <-
  26. function(elem, language, id)
  27. PlainTextDocument(elem$content, id = id, language = language)


  28. ###################################################
  29. ### code chunk number 5: extensions.Rnw:145-150
  30. ###################################################
  31. df <- data.frame(contents = c("content 1", "content 2", "content 3"),
  32. title = c("title 1", "title 2", "title 3"),
  33. authors= c("author 1" , "author 2" , "author 3" ),
  34. topics = c("topic 1", "topic 2", "topic 3"),
  35. stringsAsFactors = FALSE)


  36. ###################################################
  37. ### code chunk number 6: extensions.Rnw:156-157
  38. ###################################################
  39. names(attributes(PlainTextDocument()))


  40. ###################################################
  41. ### code chunk number 7: Mapping
  42. ###################################################
  43. m <- list(Content = "contents", Heading = "title",
  44. Author = "authors", Topic = "topics")


  45. ###################################################
  46. ### code chunk number 8: myReader
  47. ###################################################
  48. myReader <- readTabular(mapping = m)


  49. ###################################################
  50. ### code chunk number 9: extensions.Rnw:180-181
  51. ###################################################
  52. (corpus <- Corpus(DataframeSource(df), readerControl = list(reader = myReader)))


  53. ###################################################
  54. ### code chunk number 10: extensions.Rnw:186-188
  55. ###################################################
  56. corpus[[1]]
  57. meta(corpus[[1]])


  58. ###################################################
  59. ### code chunk number 11: CustomXMLFile
  60. ###################################################
  61. custom.xml <- system.file("texts", "custom.xml", package = "tm")
  62. print(readLines(custom.xml), quote = FALSE)


  63. ###################################################
  64. ### code chunk number 12: mySource
  65. ###################################################
  66. mySource <- function(x, encoding = "UTF-8")
  67. XMLSource(x, function(tree) XML::xmlChildren(XML::xmlRoot(tree)), myXMLReader, encoding)


  68. ###################################################
  69. ### code chunk number 13: myXMLReader
  70. ###################################################
  71. myXMLReader <- readXML(
  72. spec = list(Author = list("node", "/document/writer"),
  73. Content = list("node", "/document/description"),
  74. DateTimeStamp = list("function",
  75. function(x) as.POSIXlt(Sys.time(), tz = "GMT")),
  76. Description = list("attribute", "/document/@short"),
  77. Heading = list("node", "/document/caption"),
  78. ID = list("function", function(x) tempfile()),
  79. Origin = list("unevaluated", "My private bibliography"),
  80. Type = list("node", "/document/type")),
  81. doc = PlainTextDocument())


  82. ###################################################
  83. ### code chunk number 14: extensions.Rnw:301-302
  84. ###################################################
  85. corpus <- Corpus(mySource(custom.xml))


  86. ###################################################
  87. ### code chunk number 15: extensions.Rnw:306-308
  88. ###################################################
  89. corpus[[1]]
  90. meta(corpus[[1]])
复制代码







[/hide]




    熟悉论坛请点击新手指南
下载说明
1、论坛支持迅雷和网际快车等p2p多线程软件下载,请在上面选择下载通道单击右健下载即可。
2、论坛会定期自动批量更新下载地址,所以请不要浪费时间盗链论坛资源,盗链地址会很快失效。
3、本站为非盈利性质的学术交流网站,鼓励和保护原创作品,拒绝未经版权人许可的上传行为。本站如接到版权人发出的合格侵权通知,将积极的采取必要措施;同时,本站也将在技术手段和能力范围内,履行版权保护的注意义务。
(如有侵权,欢迎举报)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

GMT+8, 2025-12-31 07:18