关于 r:tm 语料库:tm_map 函数不会改变语料库 | 珊瑚贝

tm Corpus: tm_map function does not change the corpus


我是 R 中 tm 包的新手。我正在尝试使用 tm_map 函数创建文档术语矩阵,但显然传递给 tm_map(Corpus, function, lazy=TRUE) 的函数并未应用于语料库。具体来说,文档不会转换为小写。 R Studio 不显示任何错误或警告。
我在这里搞砸了什么吗?这可能是一些问题吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
library(tm)
setwd(“…”)

filenames <- list.files(getwd(), pattern=”*.txt”)
files <- lapply(filenames, readLines)

docs <- Corpus(VectorSource(files))
writeLines(as.character(docs[[30]]))

docs <- tm_map(docs, function(x) iconv(enc2utf8(x$content), sub =””), lazy=TRUE)

#to lower case
docs <- tm_map(docs, content_transformer(tolower), lazy=TRUE)
writeLines(as.character(docs[[30]]))

感谢您的建议!


这是一个简单的修复。将转换为小写的代码移到 iconv(…) 之前。

这行得通:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
library(tm)
setwd(“”)

# Read in Files
filenames <- list.files(getwd(), pattern=”*.txt”)
files <- lapply(filenames, readLines)
docs <- Corpus(VectorSource(files))
writeLines(as.character(docs[[30]]))

# Lower Case
docs <- tm_map(docs, content_transformer(tolower), lazy=TRUE)

# Convert
docs <- tm_map(docs, function(x) iconv(enc2utf8(x$content), sub =””))
writeLines(as.character(docs[[30]]))


来源:https://www.codenong.com/42365214/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9446.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?