3

我现在使用该stringi软件包有一段时间了,一切正常。

我最近想将一些正则表达式放在一个函数中,并将该函数存储在一个单独的文件中。如果函数是从脚本加载的,那么代码就可以正常工作,但是当它被获取时,我没有得到预期的结果。

这是重现该问题的代码:

clean <- function(text){
  stri_replace_all_regex(str = text, 
                         pattern = "(?i)[^a-zàâçéèêëîïôûùüÿñæœ0-9,\\.\\?!']",
                         replacement = " ")
}
text <- "A sample text with some french accent é, è, â, û and some special characters |, [, ( that needs to be cleaned."
clean(text) # OK
[1] "A sample text with some french accent é, è, â, û and some special characters  ,  ,   that needs to be cleaned."
source(clean.r)
clean(text) # KO
[1] "A sample text with some french accent  ,  ,  ,   and some special characters  ,  ,   that needs to be cleaned."

我想删除所有不是字母、重音字母和标点符号?!,..

如果函数直接加载到脚本中,代码就可以正常工作。如果它是采购的,那么它会给出不同的结果。

我也试过使用stringr,我也有同样的问题。我的文件以 UTF-8 编码保存。

我不明白为什么会这样,非常感谢任何帮助。

谢谢你。

R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252   
[3] LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
[5] LC_TIME=French_France.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stringi_1.1.5     data.table_1.10.4

loaded via a namespace (and not attached):
[1] compiler_3.4.1 tools_3.4.1    yaml_2.1.14 
4

1 回答 1

0

尝试先将文本转换为 ASCII。这将改变字符,并且当您在 R 中获取函数时可能允许相同的行为。

+1 费利佩·阿尔瓦伦加 https://stackoverflow.com/a/45941699/2069472

text <- "Ábcdêãçoàúü"
iconv(text, to = "ASCII//TRANSLIT")
于 2018-08-03T14:55:00.190 回答