我有一些推文,我想检测其中的数字表情。对于这个任务,我想使用textclean包的hash_emoticons词典。
hash_emoticons[1:5]
x y
1: #-) partied all night
2: %) drunk
3: %-) drunk
4: ',:-l scepticism
5: ',:-| scepticism如果我将它与标准函数一起使用,我会得到这个错误:
library(stringr)
str_detect(Tweets$text, hash_emoticons$x)
longer object length is not a multiple of shorter object lengthError in
stri_detect_regex(string, pattern, opts_regex = opts(pattern)):
Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)你知道怎么解决这个问题吗?
发布于 2019-01-07 01:56:08
以下是直接使用stringi包完成此操作的一种方法。但是,您需要更仔细地解释/考虑一些边界注意事项。
# Generate some data
xxx <- tibble(Text = c("asdasd", ":o)", "hej :o) :o) :-*"))您想要计算每个字符串中使用的表情符号的数量,因此您需要考虑为每个表情图标检测字符串。str_detect()将返回任何表情符号,但不返回数字,因此我们使用stri_count_fixed()。
例如
library("stringi")
library("textclean")
# Run through each emoticon
# see if it matches each tweet
# Compute the number of hits
rowSums(sapply(lexicon::hash_emoticons$x, function(i) {
stringi::stri_count_fixed(xxx$Text, pattern=i)
}))它会返回
[1] 0 2 5现在,如果你看一下输入字符串,你会看到4个表情符号。元素:o)将匹配两个表情符号:o和:o),这就是第二个元素为2的原因。相反,字符串hej :o) :o) :-*将返回5,因为它匹配:o两次,匹配:o)两次,匹配:-*一次。
https://stackoverflow.com/questions/54063880
复制相似问题