Twitter情感分析中的表情符号在R中的应用

19

如何处理/去除表情符号以便对推文进行情感分析排序?

出现以下错误:在sort.list(y)中出错:无效输入

谢谢

这就是推特上的表情符号进入r后的样子:

\xed��\xed�\u0083\xed��\xed��
\xed��\xed�\u008d\xed��\xed�\u0089 

3
请尝试使用iconv()函数进行操作。 - ndoogan
请查看?Encodings - IRTFM
1
我可以建议你弄清楚这些编码的含义。表情符号是一种语言形式,传达的意义可能无法在正式的文本语言中捕捉到。不确定你想要什么,但这些表情符号代表情感,一种以通常正式语言无法表达的方式来表示手势/面部表情的方式。再次使用这里的评论/解决方案,不是为了消除表情符号,而是为了弄清楚表情符号所传达的含义。 - Tyler Rinker
3个回答

22

这应该可以摆脱表情符号,使用 ndoogan 建议的 iconv

一些可复现的数据:

require(twitteR) 
# note that I had to register my twitter credentials first
# here's the method: https://dev59.com/Ymkw5IYBdhLWcg3waZxW
s <- searchTwitter('#emoticons', cainfo="cacert.pem") 

# convert to data frame
df <- do.call("rbind", lapply(s, as.data.frame))

# inspect, yes there are some odd characters in row five
head(df)

                                                                                                                                                text
1                                                                      ROFLOL: echte #emoticons [humor] http://t.co/0d6fA7RJsY via @tweetsmania  ;-)
2 “@teeLARGE: when tmobile get the iphone in 2 wks im killin everybody w/ emoticons &amp; \nall the other stuff i cant see on android!" \n#Emoticons
3                      E poi ricevi dei messaggi del genere da tua mamma xD #crazymum #iloveyou #emoticons #aiutooo #bestlike http://t.co/Yee1LB9ZQa
4                                                #emoticons I want to change my name to an #emoticon. Is it too soon? #prince http://t.co/AgmR5Lnhrk
5  I use emoticons too much. #addicted #admittingit #emoticons <ed><U+00A0><U+00BD><ed><U+00B8><U+00AC><ed><U+00A0><U+00BD><ed><U+00B8><U+0081> haha
6                                                                                         What you text What I see #Emoticons http://t.co/BKowBSLJ0s

这是关键行,将删除表情符号:

# Clean text to remove odd characters
df$text <- sapply(df$text,function(row) iconv(row, "latin1", "ASCII", sub=""))

现在再次检查,看看奇怪的字符是否已经消失(参见第5行)

head(df)    
                                                                                                                               text
1                                                                     ROFLOL: echte #emoticons [humor] http://t.co/0d6fA7RJsY via @tweetsmania  ;-)
2 @teeLARGE: when tmobile get the iphone in 2 wks im killin everybody w/ emoticons &amp; \nall the other stuff i cant see on android!" \n#Emoticons
3                     E poi ricevi dei messaggi del genere da tua mamma xD #crazymum #iloveyou #emoticons #aiutooo #bestlike http://t.co/Yee1LB9ZQa
4                                               #emoticons I want to change my name to an #emoticon. Is it too soon? #prince http://t.co/AgmR5Lnhrk
5                                                                                 I use emoticons too much. #addicted #admittingit #emoticons  haha
6                                                                                        What you text What I see #Emoticons http://t.co/BKowBSLJ0s

Ben- 非常感谢你,它解决了问题,终于! - Rhodo
不客气!如果您觉得回答对您有帮助,可以点赞(这是表达感谢的首选方式),并点击勾选符号(在上下箭头下方),表示该回答是最佳答案。这将有助于其他与您有相同问题的人(当有多个答案时,此过程更为重要,但在这种情况下,它更多是为了好玩)。 - Ben

2
我推荐使用以下函数:
ji_replace_all <- function (string, replacement)

需要安装这个包:
install_github (" hadley / emo ")

我需要从西班牙语的推文中删除表情符号。虽然我尝试了几种选项,但有些选项会弄乱我的文本。然而,以下神奇的功能完美地解决了我的问题:

library(emo)

text="#VIDEO ,Alguien sabe si en Afganistán hay cigarro?"

ji_replace_all(text,"")

结果如下:

"#VIDEO ,Alguien sabe si en Afganistán hay cigarro?"


1
您可以使用正则表达式来检测非字母字符并将其删除。示例代码:
rmNonAlphabet <- function(str) {
  words <- unlist(strsplit(str, " "))
  in.alphabet <- grep(words, pattern = "[a-z|0-9]", ignore.case = T)
  nice.str <- paste(words[in.alphabet], collapse = " ")
  nice.str
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接