我正在使用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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句