r - 在 R 中的 tm_map(testfile, removeNumbers) 中使用过滤器?

标签 r tm

我正在使用 tm_map(testfile, removeNumbers) 删除文本文件的数字。但是,我需要保留 ipv4 和 ipv6 等词附带的数字。 如何使用 removeNumbers 函数删除其他号码,但保留 ipv4 和 ipv6 附带的号码?

这是我使用的代码:

test.txt = "this is a test file with numbers 1,2 and 3.
              The internet protocals ipv4 and ipv6"

library(tm)

test <- Corpus(DirSource('C:test'), readerControl = list(reader = readPlain))
test <- tm_map(test, removeNumbers)

inspect(test[1])

输出:

$test.txt

this is a test file with numbers , and . The internet protocals ipv and ipv

最佳答案

removeNumbers 将删除任何数字。你可以这样得到它的代码:

getS3method("removeNumbers","PlainTextDocument")
function (x) 
gsub("[[:digit:]]+", "", x)

您应该创建一个新函数来删除“单独的”数字或空格后的数字。

remove_alone_nbr <- 
function (x) 
  gsub('\\s*(?<!\\B|-)\\d+(?!\\B|-)\\s*', "", x,perl=TRUE)

然后如果你测试它:

inspect(tm_map(Corpus(VectorSource(test.txt)), remove_alone_nbr))

你得到:

this is a test file with numbers,and.
              The internet protocals ipv4 and ipv6

关于r - 在 R 中的 tm_map(testfile, removeNumbers) 中使用过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23864860/

相关文章:

r - 在 tm 包中,删除除字母数字字符之外的所有数字

r - 通过基于多个条件对行求和在 R 中创建一个向量

python - 将 pandas.TimeSeries 转换为 R.ts

轻松记忆起许多不同名称的变量

r - 每 15 分钟安排一次 R 脚本 (cronR)

r - 文档术语矩阵中的 "Difference"

r - 使用 R 的 Twitter 挖矿 (twitteR + tm) : error using tolower conversion

r - normalizePath(path.expand(path), winslash, mustWork) : path[1] ="NA": The system cannot find the file specified 中的错误

r - tm 合并语料库列表

r - corpus 包的 tolower 函数抛出错误