Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从tweets中提取表情符号_R_String_Twitter_Encoding_Emoji - Fatal编程技术网

R 从tweets中提取表情符号

R 从tweets中提取表情符号,r,string,twitter,encoding,emoji,R,String,Twitter,Encoding,Emoji,我正在从贴有标签的推特数据中提取特征,用于预测虚假推特。我花了很多时间在各种GitHub方法、R库、stackoverflow帖子上,但不知何故,我找不到一种“直接”的方法来提取与表情符号相关的特征,例如表情符号的数量,推特上是否包含表情符号(1/0),甚至是特定表情符号的出现(这可能在假/真新闻中更常见)。 我不确定显示可复制代码是否有意义 例如,“Ore”库提供的函数可以收集对象中的所有tweet并提取表情,但在尝试从提取中创建功能时,格式是有问题的(至少对我来说),如上所述。下面的示例使用

我正在从贴有标签的推特数据中提取特征,用于预测虚假推特。我花了很多时间在各种GitHub方法、R库、stackoverflow帖子上,但不知何故,我找不到一种“直接”的方法来提取与表情符号相关的特征,例如表情符号的数量,推特上是否包含表情符号(1/0),甚至是特定表情符号的出现(这可能在假/真新闻中更常见)。 我不确定显示可复制代码是否有意义

例如,“Ore”库提供的函数可以收集对象中的所有tweet并提取表情,但在尝试从提取中创建功能时,格式是有问题的(至少对我来说),如上所述。下面的示例使用whatsapp文本示例。我将添加来自kaggle的twitter数据,使其具有一定的可复制性。 Twitter数据集:

#将其保存到“_chat.txt”(需要登录)
# https://www.kaggle.com/sarthaknautiyal/whatsappsample
图书馆(ore)
图书馆(dplyr)

emoji_src我在我的包
rwhatsapp
中为此编写了一个函数

由于您的示例是whatsapp数据集,因此可以直接使用该软件包进行测试(通过
remotes::install\u github(“JBGruber/rwhatsapp”)
安装)

读取行中的df警告(x,encoding=encoding,…):找到不完整的最后一行 #>在“\u chat.txt”上 df #>#A tibble:392 x 6 #>时间作者文本源表情符号表情符号名称 #> #> 1 2015-06-25 01:42:12 : ‎毗瑟奴高德…/家园/约翰… #> 2 2015-06-25 01:42:12 : ‎你被添加/家/约翰… #>3 2016-12-18 01:57:38沙汉:/home/约翰… #>4 2016-12-21 21:54:46潘卡吉…:/home/johan… #>5 2016-12-21 21:57:45沙汉:哇/家/约翰… #>6 2016-12-21 22:48:51 Sakshi:/home/johan… #>7 2016-12-21 22:49:00 Sakshi:/home/johan…
#>8 2016-12-21 22:50:12 Neha Wip…:谢谢你的努力!你认为这种方法适用于Twitter数据和格式吗?我正试图在这个过程中创建预测因子来检测假新闻。这是我的主要观点。如果tweet的格式与
类似,或者如果你可以将数据以
的形式带出
(使用
最新的标记
),这将起作用。我将在稍后或明天研究它,并提供反馈。你实际上可以通过
rwhatsapp:::rwa_add_emoji(df)使用我的功能
在任何包含
文本列的data.frame上。为了说明这一点,我稍微改变了我的答案。现在应该可以处理您的数据了。
rwhatsapp:::rwa_add_emoji(df)
在我的情况下可以工作,但它只能区分“商标”,其余的行是空的。
# save this to '_chat.txt` (it require a login)
# https://www.kaggle.com/sarthaknautiyal/whatsappsample

library(ore)
library(dplyr)

emoji_src <- "https://raw.githubusercontent.com/laurenancona/twimoji/gh-pages/twitterEmojiProject/emoticon_conversion_noGraphic.csv"
emoji_fil <- basename(emoji_src)
if (!file.exists(emoji_fil)) download.file(emoji_src, emoji_fil)

emoji <- read.csv(emoji_fil, header=FALSE, stringsAsFactors = FALSE)
emoji_regex <- sprintf("(%s)", paste0(emoji$V2, collapse="|"))
compiled <- ore(emoji_regex)

chat <- readLines("_chat.txt", encoding = "UTF-8", warn = FALSE)

which(grepl(emoji_regex, chat, useBytes = TRUE))
##   [1]   8   9  10  11  13  19  20  22  23  62  65  69  73  74  75  82  83  84  87  88  90  91
##  [23]  92  93  94  95 107 108 114 115 117 119 122 123 124 125 130 135 139 140 141 142 143 144
##  [45] 146 147 150 151 153 157 159 161 162 166 169 171 174 177 178 183 184 189 191 192 195 196
##  [67] 199 200 202 206 207 209 220 221 223 224 225 226 228 229 234 235 238 239 242 244 246 247
##  [89] 248 249 250 251 253 259 260 262 263 265 274 275 280 281 282 286 287 288 291 292 293 296
## [111] 302 304 305 307 334 335 343 346 348 351 354 355 356 358 361 362 382 389 390 391 396 397
## [133] 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
## [155] 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 442 451 452
## [177] 454 459 463 465 466 469 471 472 473 474 475 479 482 484 485 486 488 490 492 493 496 503
## [199] 505 506 507 509 517 518 519 525 526 527 528 531 535 540 543 545 548 549 557 558 559 560
## [221] 566 567 571 572 573 574 576 577 578 580 587 589 591 592 594 597 600 601 603 608 609 625
## [243] 626 627 637 638 639 640 641 643 645 749 757 764

chat_emoji_lines <- chat[which(grepl(emoji_regex, chat, useBytes = TRUE))]

found_emoji <- ore.search(compiled, chat_emoji_lines, all=TRUE)
emoji_matches <- matches(found_emoji)

str(emoji_matches, 1)
## List of 254
##  $ : chr [1:4] "\U0001f600" "\U0001f600" "\U0001f44d" "\U0001f44d"
##  $ : chr "\U0001f648"
##  $ : chr [1:2] "\U0001f44d" "\U0001f44d"
##  $ : chr "\U0001f602"
##  $ : chr [1:3] "\U0001f602" "\U0001f602" "\U0001f602"
##  $ : chr [1:4] "\U0001f44c" "\U0001f44c" "\U0001f44c" "\U0001f44c"
##  $ : chr [1:6] "\U0001f602" "\U0001f602" "\U0001f602" "\U0001f602" ...
##  $ : chr "\U0001f600"
##  $ : chr [1:5] "\U0001f604" "\U0001f604" "\U0001f604" "\U0001f603" ...
##  $ : chr "\U0001f44d"
## ...

data_frame(
  V2 = flatten_chr(emoji_matches) %>% 
    map(charToRaw) %>% 
    map(as.character) %>% 
    map(toupper) %>% 
    map(~sprintf("\\x%s", .x)) %>% 
    map_chr(paste0, collapse="")
) %>% 
  left_join(emoji) %>% 
  count(V3, sort=TRUE)
## # A tibble: 89 x 2
##                                                    V3     n
##                                                 <chr> <int>
##  1                             face with tears of joy   110
##  2                     smiling face with smiling eyes    50
##  3         face with stuck-out tongue and winking eye    43
##  4                                       musical note    42
##  5                                      birthday cake    35
##  6                    grinning face with smiling eyes    26
##  7 face with stuck-out tongue and tightly-closed eyes    24
##  8                                      grinning face    21
##  9                                            bouquet    17
## 10                                     thumbs up sign    17
## # ... with 79 more rows