【问题标题】:Reading in Unicode Emoji correctly into R将 Unicode 表情符号正确读入 R
【发布时间】:2018-05-20 10:25:57
【问题描述】:

我有一组来自 Facebook 的 cmets(通过像 Sprinkr 这样的系统提取),其中包含文本和表情符号,我正在尝试在 R 中对它们进行各种分析,但在摄取表情符号时遇到了困难字符正确。

例如:我有一个 .csv(以 UTF-8 编码),它的消息行包含如下内容:

“这是正确的!?!?!请说这是不正确的!我们家只吃原汁原味的Reeses花生酱杯????????????”

然后我通过以下方式将其摄取到 R 中:

library(tidyverse)
library(janitor)
raw.fb.comments <- read_csv("data.csv",
                            locale = locale(encoding="UTF-8"))
fb.comments <- raw.fb.comments %>%
  clean_names() %>%
  filter(senderscreenname != "Reese's") %>% 
  select(c(message,messagetype,sentiment)) %>%
  mutate(type = "Facebook")
fb.comments$message[5]
[1] "IS THIS CORRECT!?!?! Please say it isn't true!!! Our family only eats the original Reeses Peanut Butter Cups\xf0\u009f\u0092\u009a\xf0\u009f\u0092\u009a\xf0\u009f\u0092\u009a\n\n"

现在,根据我从其他来源了解到的情况,我需要将这个 UTF-8 转换为 ASCII,然后我可以用它来将它与其他表情符号资源链接起来(比如美妙的 emojidictionary)。为了使连接工作,我需要把它变成 R 编码,像这样:

<e2><9d><a4><ef><b8><8f>

但是,添加正常步骤(使用iconv)并没有让我到达那里:

fb.comments <- raw.fb.comments %>%
  clean_names() %>%
  filter(senderscreenname != "Reese's") %>% 
  select(c(message,messagetype,sentiment)) %>%
  mutate(type = "Facebook") %>%
  mutate(message = iconv(message, from="UTF-8", to="ascii",sub="byte"))
fb.comments$message[5]
[1] "IS THIS CORRECT!?!?! Please say it isn't true!!! Our family only eats the original Reeses Peanut Butter Cups<f0><9f><92><9a><f0><9f><92><9a><f0><9f><92><9a>\n\n"

那里的任何人都可以告诉我我缺少什么,或者我需要找到不同的表情符号映射资源吗?谢谢!

【问题讨论】:

  • 你能显示dput(fb.comments$message[5])吗?
  • dput(fb.comments$message[5]) "IS THIS CORRECT!?!?! Please say it isn't true!!! Our family only eats the original Reeses Peanut Butter Cups&lt;f0&gt;&lt;9f&gt;&lt;92&gt;&lt;9a&gt;&lt;f0&gt;&lt;9f&gt;&lt;92&gt;&lt;9a&gt;0&gt;&lt;9f&gt;&lt;92&gt;&lt;9a&gt;\n\n"
  • 在 mutate-iconv 之前?
  • 啊,是的!对不起! dput(fb.comments$message[5]) "IS THIS CORRECT!?!?! Please say it isn't true!!! Our family only eats the original Reeses Peanut Butter Cups&lt;f0&gt;&lt;U+009F&gt;&lt;U+0092&gt;&lt;U+009A&gt;&lt;f0&gt;&lt;U+009F&gt;&lt;U+0092&gt;&lt;U+009A&gt;&lt;f0&gt;&lt;U+009F&gt;&lt;U+0092&gt;&lt;U+009A&gt;\n\n"

标签: r text unicode utf-8 emoji


【解决方案1】:

目标不是很明确,但我怀疑放弃表示表情符号的正确性并将其表示为字节并不是最好的方法。例如,如果您希望将表情符号转换为他们的描述,您可以执行以下操作:

x <- "IS THIS CORRECT!?!?! Please say it isn't true!!! Our family only eats the original Reeses Peanut Butter Cups???"

## read emoji info and get rid of documentation lines
readLines("https://unicode.org/Public/emoji/5.0/emoji-test.txt",
          encoding="UTF-8") %>%
    stri_subset_regex(pattern = "^[^#]") %>%
    stri_subset_regex(pattern = ".+") -> emoji

## get the emoji characters and clean them up
emoji %>%
    stri_extract_all_regex(pattern = "# *.{1,2} *") %>%
    stri_replace_all_fixed(pattern = c("*", "#"),
                           replacement = "",
                           vectorize_all=FALSE) %>%
    stri_trim_both() -> emoji.chars

## get the emoji character descriptions
emoji %>%
    stri_extract_all_regex(pattern = "#.*$") %>%
    stri_replace_all_regex(pattern = "# *.{1,2} *",
                           replacement = "") %>%
    stri_trim_both() -> emoji.descriptions


## replace emoji characters with their descriptions.
stri_replace_all_regex(x,
                       pattern = emoji.chars,
                       replacement = emoji.descriptions,
                       vectorize_all=FALSE)

## [1] "IS THIS CORRECT!?!?! Please say it isn't true!!! Our family only eats the original Reeses Peanut Butter Cupsgreen heartgreen heartgreen heart"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-26
    • 2016-10-05
    • 1970-01-01
    • 2015-10-18
    • 2015-01-15
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    相关资源
    最近更新 更多