【问题标题】:How to remove non-alphabetic characters and convert all letter to lowercase in R?如何删除非字母字符并将R中的所有字母转换为小写?
【发布时间】:2015-06-28 01:47:28
【问题描述】:

在以下字符串中:

"I may opt for a yam for Amy, May, and Tommy."

如何删除非字母字符并将所有字母转换为小写并对R中每个单词中的字母进行排序?

同时,我尝试对句子中的单词进行排序并删除重复项。

【问题讨论】:

  • 你能告诉我们what have you tried到目前为止吗?
  • 你能提供一个示例字符串和预期的输出吗?要转换为小写,只需使用tolower
  • “对每个单词中的字母进行排序”?

标签: regex r


【解决方案1】:

你可以使用stringi

library(stringi)
unique(stri_sort(stri_trans_tolower(stri_extract_all_words(txt, simplify = TRUE))))

这给出了:

## [1] "a"     "amy"   "and"   "for"   "i"     "may"   "opt"   "tommy" "yam" 

更新

正如@DavidArenburg 提到的,我忽略了您问题的“对单词中的字母进行排序”部分。您没有提供所需的输出,也没有立即想到应用程序,但是,假设您想确定哪些单词具有匹配的对应项(字符串距离为 0):

unique(stri_sort(stri_trans_tolower(stri_extract_all_words(txt, simplify = TRUE)))) %>%
  stringdistmatrix(., ., useNames = "strings", method = "qgram") %>%

#       a amy and for i may opt tommy yam
# a     0   2   2   4 2   2   4     6   2
# amy   2   0   4   6 4   0   6     4   0
# and   2   4   0   6 4   4   6     8   4
# for   4   6   6   0 4   6   4     6   6
# i     2   4   4   4 0   4   4     6   4
# may   2   0   4   6 4   0   6     4   0
# opt   4   6   6   4 4   6   0     4   6
# tommy 6   4   8   6 6   4   4     0   4
# yam   2   0   4   6 4   0   6     4   0

  apply(., 1, function(x) sum(x == 0, na.rm=TRUE)) 

# a   amy   and   for     i   may   opt tommy   yam 
# 1     3     1     1     1     3     1     1     3 

每行包含多个 0 ("amy", "may", "yam") 的单词有一个加扰对应词。

【讨论】:

  • 我现在倾向于使用stringr,因为它在底层使用stringi,但是那个功能,但stri_extract_all_words看起来真的很方便。我可能不得不重新使用stringi
  • 是的。 stringr 更简单,但我发现stringi 更灵活。
  • @hrbrmstr 我认为你们都忽略了“对每个单词中的字母进行排序”部分
  • 但这意味着什么?
  • @DavidArenburg OP 确实要求对单词中的字母进行排序。这对我来说毫无意义。该职位是一些准系统。我认为如果 OP 提供了所需的输出,他们的问题会更加清楚,因为他们要求的内容没有明显的直接应用。
【解决方案2】:
str <- "I may opt for a yam for Amy, May, and Tommy."

## Clean the words (just keep letters and convert to lowercase)
words <- strsplit(tolower(gsub("[^A-Za-z ]", "", str)), " ")[[1]]

## split the words into characters and sort them
sortedWords <- sapply(words, function(word) sort(unlist(strsplit(word, ""))))

## Join the sorted letters back together
sapply(sortedWords, paste, collapse="")

# i     may     opt     for       a     yam     for     amy     may     and 
# "i"   "amy"   "opt"   "for"     "a"   "amy"   "for"   "amy"   "amy"   "adn" 
# tommy 
# "mmoty" 

## If you want to convert result back to string
do.call(paste, lapply(sortedWords, paste, collapse=""))
# [1] "i amy opt for a amy for amy amy adn mmoty"

【讨论】:

    【解决方案3】:

    stringr 可让您以 R 和 C 速度处理所有字符集,magrittr 可让您使用适合您需求的管道习语:

    library(stringr)
    library(magrittr)
    
    txt <- "I may opt for a yam for Amy, May, and Tommy."
    
    txt %>% 
      str_to_lower %>%                                            # lowercase
      str_replace_all("[[:punct:][:digit:][:cntrl:]]", "") %>%    # only alpha
      str_replace_all("[[:space:]]+", " ") %>%                    # single spaces
      str_split(" ") %>%                                          # tokenize
      extract2(1) %>%                                             # str_split returns a list
      sort %>%                                                    # sort
      unique                                                      # unique words
    
      ## [1] "a"     "amy"   "and"   "for"   "i"     "may"   "opt"   "tommy" "yam"  
    

    【讨论】:

    • 我想知道txt %&gt;% str_to_lower %&gt;% str_replace_all("[^[:alpha:] ']", "") %&gt;% str_split(" +") %&gt;% extract2(1) %&gt;% sort %&gt;% unique是否可能更简洁。
    【解决方案4】:

    我维护的 qdap 包有 bag_o_words 函数非常适合这个:

    txt <- "I may opt for a yam for Amy, May, and Tommy."
    
    library(qdap)
    
    unique(sort(bag_o_words(txt)))
    
    ## [1] "a"     "amy"   "and"   "for"   "i"     "may"   "opt"   "tommy" "yam"
    

    【讨论】:

      猜你喜欢
      • 2013-09-17
      • 2017-08-26
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      相关资源
      最近更新 更多