【发布时间】:2017-05-04 12:36:21
【问题描述】:
我有一个名为“str”的字符串,它是通过加载 RDS 文件获得的。
此字符串包含在 R Studio 控制台中可以正常显示的法语口音。 但是,在此字符串上使用 ngram 包时,重音字符无法正确显示。
如果我直接在 R 中定义重音字符串,它就可以正常工作(参见下面代码中的“str2”)。
我该如何解决这个问题,例如,对我的原始字符串强制使用新编码。
str # console displays "crédit hypothécaire en juillet"
ng <- ngram(str, n = 2,sep= " ")
get.phrasetable(ng)
# ngrams freq prop
# 1 hypothécaire en 1 0.3333333
# 2 crédit hypothécaire 1 0.3333333
# 3 en juillet 1 0.3333333
str2 <- "crédit hypothécaire en juillet"
ng2 <- ngram(str2, n = 2,sep= " ")
get.phrasetable(ng2)
# ngrams freq prop
# 1 hypothécaire en 1 0.3333333
# 2 crédit hypothécaire 1 0.3333333
# 3 en juillet 1 0.3333333
编辑:
建议的链接 (handling special characters e.g. accents in R) 在经过验证的答案中没有为我的问题提供解决方案,因此这不是重复的问题,但确实提供了一些线索,请参阅下面的答案
【问题讨论】:
-
首先,不要使用 str,因为它是 R 中的内部函数。请参阅
?str了解我的意思。 -
@ErikSchutte 它在 ngram 包的所有示例中都使用过,所以我保持一致:)
标签: r utf-8 character-encoding n-gram