gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:59:04 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com