【问题标题】:Change text to lowercase in R keeping acronyms in uppercase in text mining在R中将文本更改为小写,在文本挖掘中保持首字母大写
【发布时间】:2019-10-01 04:39:24
【问题描述】:

如何使用 R 将全文更改为小写但保留大写首字母缩写词?我需要它来进行文本挖掘和使用 udpi 包。我当然可以使用大写,但无论如何要在使用小写时保留大写首字母缩写词?

tolower('NASA 是一家美国公司')。

tolower('NASA IS A US COMPANY')
tolower('NASA IS A US COMPANY')

Expected: NASA is a US company

Actual: nasa is a us company

【问题讨论】:

  • 使用tmmapstopwords?

标签: r


【解决方案1】:

我们可以做到: 测试是输入:

paste(lapply(strsplit(test," "),function(x) ifelse(x %in% toupper(tm::stopwords()),
                                              tolower(x),x))[[1]],collapse=" ")
[1] "NASA is a US COMPANY"

【讨论】:

    【解决方案2】:

    我编辑了

    Capitalize the first letter of both words in a two word string

    只是一点点。

    simpleCap <- function(x,abr) {
      s <- strsplit(x, " ")[[1]]
      loc = which(!s %in% abr)
      loc_abr = which(s %in% abr)
      tmp_s = s[!s %in% abr]
    
      paste(toupper(substring(tmp_s, 1,1)), tolower(substring(tmp_s, 2)),
            sep="", collapse=" ")
    
      result = character(length(s))
      result[loc] = strsplit(paste(toupper(substring(tmp_s, 1,1)), tolower(substring(tmp_s, 2)),
                                   sep="", collapse=" ")," ")[[1]]
      result[loc_abr] = abr
      result = paste(result,collapse = " ")
      return(result)
    }
    

    你必须管理一些像这样的缩写

    abr <- c("NASA", "US")
    

    之后你可以得到下面的结果

    simpleCap(abr= abr, 'NASA IS A US COMPANY')
    >[1] "NASA Is A US Company"
    

    【讨论】:

      【解决方案3】:

      这个怎么样?

      acronyms <- c('NASA','US')
      test <- 'NASA IS A US COMPANY'
      
      a <- tolower(test)
      b <- as.list(strsplit(a, " ")[[1]])
      
      for (i in 1:length(b)) {
        if (toupper(b[i]) %in% acronyms) {
          b[i] <- toupper(b[i])
        }
      }
      
      c <- paste(b, collapse=" ")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 2021-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多