有没有有效的方法可以删除文本中的标点符号,但保留连字符的表达方式,例如“容易发生事故”?
我使用以下函数来清理我的文本
clean.text = function(x)
{
# remove rt
x = gsub("rt ", "", x)
# remove at
x = gsub("@\\w+", "", x)
x = gsub("[[:punct:]]", "", x)
x = gsub("[[:digit:]]", "", x)
# remove http
x = gsub("http\\w+", "", x)
x = gsub("[ |\t]{2,}", "", x)
x = gsub("^ ", "", x)
x = gsub(" $", "", x)
x = str_replace_all(x, "[^[:alnum:][:space:]'-]", " ")
#return(x)
}
并将其应用于返回的连字符表达式
my_text <- "accident-prone"
new_text <- clean.text(text)
new_text
[1] "accidentprone"
而我想要的输出是
"accident-prone"
我已经参考过这个线程 https://stackoverflow.com/questions/31351811/regex-expression-to-exclude-hyphenated-words-in-r但没有发现它适合我的情况。一定有一些我还没有弄清楚的正则表达式的东西。如果有人能启发我,我将不胜感激。