我正在處理法語單詞的資料。我希望 grepl 考慮這個詞,無論元音是否有口音。這是代碼的一部分:
在這里,我希望 grepl 找出所有是radiothérapieor 的單詞radiotherapie,即忽略重音
ifelse(grepl("Radiothe(é)rapie",mydata$word),"yes","no")
uj5u.com熱心網友回復:
ifelse(grepl("Radioth[eé]rapie", c("Radiotherapie", "Radiothérapie", "Radio")),"yes","no")
uj5u.com熱心網友回復:
那么,這樣做的蠻力方法是使用包含字母所有變體的字符類,例如
ifelse(grepl("Radioth[eé]rapie", mydata$word), "yes", "no")
uj5u.com熱心網友回復:
可能的解決方案:Latin-ASCII在使用之前轉換為grepl.
x <- c("radiothérapie", "radiotherapie")
grepl("radiotherapie", stringi::stri_trans_general(x,"Latin-ASCII"))
[1] TRUE TRUE
這應該適用于大多數(所有?)口音..
uj5u.com熱心網友回復:
您可以采用更通用的方法
string = "áb?dêfgà?p"
iconv(string, to='ASCII//TRANSLIT')
# [1] "abcdefgaop"
對于您的場景
x <- "Radiotherapie"
y <- c("Radiotherapie", "Radiothérapie", "Radio")
grepl(iconv(x, to='ASCII//TRANSLIT'), iconv(y, to='ASCII//TRANSLIT'))
# [1] TRUE TRUE FALSE
uj5u.com熱心網友回復:
這是一個使用greplwith的技巧regex:
在否定您的選擇的組內使用插入符號:
x <- c("radiothérapie", "radiotherapie")
grepl('radioth[é^e]rapie', x)
[1] TRUE TRUE
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/364209.html
