gpt4 book ai didi

regex - 如何删除非字母字符并将所有字母转换为 R 中的小写?

转载 作者:行者123 更新时间:2023-12-01 22:57:14 25 4
gpt4 key购买 nike

在以下字符串中:

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

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

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

最佳答案

您可以使用 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" )有一个乱码对应物。

关于regex - 如何删除非字母字符并将所有字母转换为 R 中的小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com