【问题标题】:Why aren't these various encodings allowing me to properly display Portuguese?为什么这些不同的编码不能让我正确显示葡萄牙语?
【发布时间】:2018-01-03 15:29:25
【问题描述】:

我正在做一些涉及葡萄牙语文本的文本挖掘。我的一些自定义文本挖掘函数中还包含其他特殊字符。

我不是这方面的专家。当我的很多字符开始显示不正确时,我认为我需要更改文件编码。我试过了

  • ISO-8858-1
  • ISO-8858-7
  • UTF-8
  • WINDOWS-1252

它们都没有改善字符的显示。我需要不同的编码还是我做错了?

例如,当我尝试从 GitHub 读取此停用词列表时:

stop_words <- read.table("https://gist.githubusercontent.com/alopes/5358189/raw/2107d809cca6b83ce3d8e04dbd9463283025284f/stopwords.txt") 

他们是这样出来的:

tail(stop_words, 17)
206    tivéramos
207         tenha
208      tenhamos
209        tenham
210       tivesse
211   tivéssemos
212      tivessem
213         tiver
214      tivermos
215       tiverem
216         terei
217         terá
218       teremos
219        terão
220         teria
221     teríamos
222        teriam

我也试过stringsAsFactors = F

我不会说葡萄牙语,但我的直觉告诉我欧元和版权符号不在它们的字母表中。此外,它似乎正在将一些重音小写 e 更改为大写不同重音 A。

如果有帮助:

Sys.getlocale()

[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"

我还尝试更改语言环境stri_encode(stop_words$V1, "", "UTF-8")tail(enc2native(as.vector(stop_words[,1])),17)

【问题讨论】:

  • 我认为问题不在于葡萄牙语字母表。当我使用上面的代码从 GitHub 获取 stop_words 时,我可以看到字符格式正确。您如何更改文件编码?
  • @OriolMirosa 在更改系统默认编码(即 ISO-8859-1)之前,我遇到了问题。我尝试使用 RStudio(使用编码重新打开)更改它,然后重新提取数据。我还尝试使用 stringi 包更改它。我认为下面的答案是正确的,它以某种方式被双重编码,但我不知道为什么或如何解决它。
  • 你试过enc2utf8(as.vector(stop_words[,1]))enc2native(as.vector(stop_words[,1]))
  • @OriolMirosa 我没有尝试过,谢谢。看了你的评论,我才试了下,问题依旧。
  • 嗯...你在什么系统?你使用 RStudio 吗?您的 R 终端使用什么字体?您能在终端中看到波浪号和其他拉丁字符吗? (如果您的键盘是英文的,请按 alt+e,然后按 e 获得 'é')

标签: r text encoding character-encoding


【解决方案1】:

我是葡萄牙人,虽然我的编码是,但我遇到了同样的问题

Sys.getlocale()
[1] "LC_COLLATE=Portuguese_Portugal.1252;LC_CTYPE=Portuguese_Portugal.1252;LC_MONETARY=Portuguese_Portugal.1252;LC_NUMERIC=C;LC_TIME=Portuguese_Portugal.1252"

所以我在网上查了一下,在SO找到了这个提示。

stop_words2 <- sapply(stop_words, as.character)

成功了。但我使用read.table(..., stringsAsfactors = FALSE) 读取数据。

【讨论】:

  • 非常感谢。这对我不起作用,但我们可以为未来可能遇到与您的情况相同的问题/解决方案的读者保留答案。
  • @Hack-R:可能因为您的语言环境而无法正常工作。不能改吗?
【解决方案2】:

你似乎是双重编码为​​ utf-8。

这是 utf-8 中的字符图表:http://www.i18nqa.com/debug/utf8-debug.html
现在查看“实际”列。

如您所见,打印的字符似乎代表实际值而不是编码值。

临时解决办法是解码一层 utf-8。

更新:

安装 R 后,我尝试重现该问题。
这是我的控制台日志,有一个简单的解释:

首先,我复制粘贴了您的代码:

> stop_words <- read.table("https://gist.githubusercontent.com/alopes/5358189/raw/2107d809cca6b83ce3d8e04dbd9463283025284f/stopwords.txt")
> tail(stop_words, 17)
             V1
206  tivéramos
207       tenha
208    tenhamos
209      tenham
210     tivesse
211 tivéssemos
212    tivessem
213       tiver
214    tivermos
215     tiverem
216       terei
217       terá
218     teremos
219      terão
220       teria
221   teríamos
222      teriam

好的,所以它没有按原样工作,所以我在 read.table 函数的末尾添加了编码参数。 当我尝试使用小写 utf-8 时,结果如下:

> stop_words <- read.table("https://gist.githubusercontent.com/alopes/5358189/raw/2107d809cca6b83ce3d8e04dbd9463283025284f/stopwords.txt",encoding="utf-8")
> tail(stop_words, 17)
             V1
206  tivéramos
207       tenha
208    tenhamos
209      tenham
210     tivesse
211 tivéssemos
212    tivessem
213       tiver
214    tivermos
215     tiverem
216       terei
217       terá
218     teremos
219      terão
220       teria
221   teríamos
222      teriam

最后,我使用了带有大写字母的 UTF-8,现在它可以正常工作了:

> stop_words <- read.table("https://gist.githubusercontent.com/alopes/5358189/raw/2107d809cca6b83ce3d8e04dbd9463283025284f/stopwords.txt", encoding = "UTF-8")
> tail(stop_words, 17)
            V1
206  tivéramos
207      tenha
208   tenhamos
209     tenham
210    tivesse
211 tivéssemos
212   tivessem
213      tiver
214   tivermos
215    tiverem
216      terei
217       terá
218    teremos
219      terão
220      teria
221   teríamos
222     teriam

您可能忘记将编码参数放在 read.table 的末尾 或尝试使用小写而不是大写。我从中了解到的是,如果您未指定字符已在其中编码,R 会尝试将字符转换为 UTF-8。

【讨论】:

  • 我可以从图表中看出你是对的。我想弄清楚如何听从你的建议。如果你知道怎么做,你可以告诉我使用链接的 GitHub 文本吗?我看到了一些关于如何在 Python 中修复双重编码的示例,但不是 R。
  • 如果找不到答案,我可能会稍后再调查。
猜你喜欢
  • 2019-11-11
  • 2017-08-22
  • 2016-05-11
  • 2019-04-10
  • 1970-01-01
  • 2014-02-18
  • 2016-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多