r - R 可以将表情符号字符转换为对应的文本吗?

标签 r unicode encoding

在我昨天的问题中,"Can R read html-encoded emoji characters?" ,用户rensa noted that :

As far as I'm aware, there's no solution to printing emoji in the R console: they always come out as "U0001f600" (or what have you). However, the packages I described above can help you plot emoji in some circumstances (I'm hoping to expand ggflags to display arbitrary full-colour emoji at some point). They can also help you search for emoji to get their codes, but they can't get names given the codes AFAIK. But maybe you could try importing the emoji list from emojilib into R and doing a join with your data frame, if you've extracted the emoji codes into a column, to get the English names.

这在 R 中看起来怎么样?

(注意:我发布这个问题的目的是立即回答它,而不是在上面链接的问题中发布这个问题,因为它与该问题无关,但仍然可能对其他人有用。)

最佳答案

下面的方法适用于将表情符号字符或 unicode 表示形式转换为名称。

我很高兴在 CC0 贡献下发布下面的代码片段(即,将此实现放入公共(public)领域以供免费重用)。

# Get (MIT-licensed) emojilib data:
emoji_json_file <- "https://raw.githubusercontent.com/muan/emojilib/master/emojis.json"
json_data <- rjson::fromJSON(paste(readLines(emoji_json_file), collapse = ""))

get_name_from_emoji <- function(emoji_unicode, emoji_data = json_data){
  emoji_evaluated <- stringi::stri_unescape_unicode(emoji_unicode)

  # names(json_data)
  vector_of_emoji_names_and_characters <- unlist(
    lapply(json_data, function(x){
      x$char
    })
  )

  name_of_emoji <- attr(
    which(vector_of_emoji_names_and_characters == emoji_evaluated)[1],
    "names"
  )

  name_of_emoji
}

get_name_from_emoji("\\U0001f917")
# [1] "hugs"
get_name_from_emoji("🤗") # An attempt actually pasting the hugs emoji in also works.
# [1] "hugs"

关于r - R 可以将表情符号字符转换为对应的文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48154928/

相关文章:

Python - 另一个编码问题。 Windows 8.1,都是最新的 python。 pip 抛出异常

r - 按序列计算和序列

r - 如何使用开始日期在 R 中创建滞后的结束日期?

r - 如何在不使用 for 循环的情况下用 NA 替换数据帧行中的连续零?

r - 使用 purrr 应用累积函数

python - Python 中的刽子手游戏

ios - 将 unicode 字符串转换为 nsstring

java - 如何替换java字符串中的unicode字符

ruby - 如何在Ruby中匹配中文单词?

audio - 仅从源mp4链接下载音频