簡體   English   中英

在R中的tm_map(testfile,removeNumbers)中使用Filter?

[英]Using Filter in tm_map(testfile, removeNumbers) in R?

我使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM