2017-12-06 25 views
1

テキストと絵文字の両方を含むFacebookからのコメント(Sprinkrのようなシステム経由で引っ張られている)があり、Rでさまざまな分析を実行しようとしていますが絵文字を正しく摂取することが難しくなります。例えばUnicode絵文字を正しく読み込むR

:私はこのような何かを含むメッセージ行を持つことになります(UTF-8でエンコード).CSVを持っている:

「これは正しいです、それは真実ではないと言うて下さい!?!?!今

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" 

、私は他のソースから理解して何から、私は変換する必要があります!!!私たちの家族は、私は、次のようにRにそれを摂取する「

を元リーセスピーナッツバターカップを食べますこのUTF-8をASCIIに変換して、他の絵文字リソースとリンクすることができます(l私は素晴らしい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" 

誰かが私に不足していることを私に照らしたり、別の絵文字マッピングリソースを見つける必要がありますか?ありがとう!

+0

'dput(fb.comments $ message [5])'を表示できますか? –

+0

'' 'dput(fb.commentsの$メッセージ[5]) 「これは正しいです!?!?!!!!私たちの家族だけ<9f><92><9a>元リーセスピーナッツバターカップを食べる真実ではないと言うてください<9f><92><9a> 0><9f><9a> \ n \ n "' '' –

+0

そしてmutate-iconvの前に? –

答えて

1

目標は本当に明確ではありませんが、私はemoji correctyを表現することをあきらめ、単にバイトとして表現することは最良の方法ではないと考えています。たとえば、絵文字を説明に変換したい場合は、次のようにすることができます:

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" 
+0

これはまさに目標でした。ありがとうございました。 –

関連する問題