0

我正在查看 Twitter 数据,然后将其输入 html 文档。文本通常包含特殊字符,例如未针对 html 正确编码的表情符号。例如推文:

如果#AvengersEndgame 和#Joker 都被提名为最佳影片,这将是漫威与DC 的第一次最佳影片竞赛。我认为这两部电影都值得点头,但仪式前的推特话语将是

会成为:

如果#AvengersEndgame 和#Joker 都被提名为最佳影片,这将是漫威与DC 的第一次最佳影片竞赛。我认为这两部电影都值得点头,但导致颁奖典礼的推特话语将是 🔥 🔥 🔥</p>

当输入 html 文档时。

手动工作我可以使用https://www.textfixer.com/html/html-character-encoding.php之类的工具将推文编码为:

如果#AvengersEndgame 和#Joker 都被提名为最佳影片,这将是漫威与DC 的第一次最佳影片竞赛。我认为这两部电影都值得点头,但导致颁奖典礼的推特话语将是“�”;“�”; "�";"�"; "�";"�";

然后我可以将其提供给 html 文档并显示表情符号。R 中是否有一个包或函数可以像上面的 web 工具一样接受文本和 html 对其进行编码?

4

1 回答 1

3

这是一个将非 ascii 字符编码为 HTML 实体的函数。

entity_encode <- function(x) {
  cp <- utf8ToInt(x)
  rr <- vector("character", length(cp))
  ucp <- cp>128
  rr[ucp] <- paste0("&#", as.character(cp[ucp]), ";")
  rr[!ucp] <- sapply(cp[!ucp], function(z) rawToChar(as.raw(z)))
  paste0(rr, collapse="")
}

这返回

[1] "If both #AvengersEndgame and #Joker are nominated for Best Picture, it will be Marvel vs DC for the first time in a Best Picture race. I think both films deserve the nod, but the Twitter discourse leading up to the ceremony will be &#128293; &#128293; &#128293;"

对于您的输入,但这些似乎是等效的编码。

于 2019-11-18T17:28:42.803 回答