【问题标题】:How to pull unique emojis from a character vector in R?如何从 R 中的字符向量中提取独特的表情符号?
【发布时间】:2021-02-28 05:12:17
【问题描述】:

假设我有一个名为 sampleData 的字符向量,其中包含表情符号

sampleData <- c('????????????????????????', '????❤️????????????????????????','????????❤️????')

如何从这个向量中提取每个独特的表情符号,以便最终结果只包含 ????????????❤️???????????????????

我尝试了以下方法,但没有成功。

sampleData <- stri_paste(sampleData, collapse = ' ')
unique(sampleData)

【问题讨论】:

  • 在所需的输出中:????不见了!

标签: r emoji


【解决方案1】:

我想我找到了解决您的问题的方法,但它非常复杂,因为表情符号是一种特殊的格式/编码,并且一次可以有很多习惯。要解决的主要问题是获取尽可能多的表情符号的列表/df,然后从那里开始工作。幸运的是,R 有这样的包:https://github.com/Rkabacoff/emoji2text 这个解决方案并不完美,但它可以完成您正在寻找的工作 - 您必须注意,由于表情符号的范围很广和可能的习俗,您可能需要根据需要清理或增加已注册表情符号的列表/df(你马上就会明白我的意思)

#remotes::install_github("Rkabacoff/emoji2text")
library(emoji2text)
library(dplyr)
library(tidyr)
library(stringr)


x <- emoji2text::emoji_to_text(sampleData, accents=TRUE) %>% # convert emojis to readable text inbetween  parenthesis
  dplyr::as_tibble() %>% # convert this to a tibble
  dplyr::mutate(SPLIT_EMOJIS = stringr::str_split(value, "\\)")) %>% # split in nested rows by the closing brackets
  tidyr::unnest(SPLIT_EMOJIS) %>% # unnest the nested rows
  dplyr::filter(nchar(SPLIT_EMOJIS) > 0) %>% # get rid of "" lines
  dplyr::mutate(SPLIT_EMOJIS = paste0(SPLIT_EMOJIS, ")")) %>% # put the closing braket back on
  dplyr::distinct(SPLIT_EMOJIS) %>% # get the unique emojis
  dplyr::inner_join(emoji2text::Emojis, by = c("SPLIT_EMOJIS" = "words")) %>% # join to the df of emojis to get the encoding and convert written text to encoding that can be printed
  dplyr::group_by(SPLIT_EMOJIS) %>% # heart can be small and black or large and red (this is what I meant by possible need to customize the list/df of registred emojis - you could just drop the black heart in advance i.e.)
  dplyr::slice_head(n = 1) %>%  #so we get the first entry (large and red)
  dplyr::pull(emoji) # pull out only the colum of correctly encoded emojis

cat(x) # print them
?? ? ? ? ❤️ ? ? ?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 2018-12-13
    • 2014-01-21
    • 2017-12-31
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多