【问题标题】:I want to replace double punctuations with space我想用空格替换双标点
【发布时间】:2018-08-23 20:07:03
【问题描述】:

我写了以下代码,但它不起作用

replacePunctuation <- function(x) {
                gsub("[[:punct:]]+" , " " , x)
                }

sms_data_corpus_clean <- tm_map(sms_data_corpus_clean, replacePunctuation)
**I installed and loaded the library tm

注意:目的是避免使用以下示例中的标点符号:

Are you still.....there?

使用

sms_data_corpus_clean <- tm_map(sms_data_corpus_clean, removePunctuation)

我们会得到这样连接的两个词

   Are you stillthere

【问题讨论】:

  • 我复制了你的函数问题中出现的然后X = "Are you still.....there?"当我运行replacePunctuation(X)时我得到[1]“你还在吗”的话不是加入。
  • 也许你可以试试gsub("[[:punct:]]{2,}"," ","Are you still.....there?") [1] "Are you still there?"

标签: r regex corpus punctuation


【解决方案1】:

我想你可以在这里使用stringrstr_replace函数——

library(stringr)

replacePunctuation <- function(x){
  # if there are multiple punctuations replace them
  str_replace(x, pattern = "[[:punct:]]{2,}", " ")
}

sample_data <- c("2 or more punctuations - Are you still.....there?", 
                   "only one punctuation - Are you still, here?")


replacePunctuation(sample_data)
#> [1] "2 or more punctuations - Are you still there?"
#> [2] "only one punctuation - Are you still, here?"

由 reprex 包 (v0.2.0) 于 2018 年 8 月 23 日创建。

【讨论】:

    猜你喜欢
    • 2016-04-23
    • 2018-08-05
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 2016-06-16
    • 2023-02-17
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多