我有一个包含“clear_message”列的数据框,并且创建了一个用于计算每行中所有单词的列。
history['word_count'] = history.clear_message.apply(lambda x: Counter(x.split(' ')))
例如,如果行消息是:Hello my name is Hello
那么他所在行的计数器将是Counter({'Hello': 2, 'is': 1, 'my': 1, 'name': 1})
问题
我的文本中有表情符号,我还想要一个表情符号计数器。
例如:
test = '????????????????????here sasdsa'
test_counter = Counter(test.split(' '))
输出是:
Counter({'sasdsa': 1, '????????????????????here': 1})
但我想要:
Counter({'sasdsa': 1, '????': 5, 'here':1})
显然问题是我正在使用split(' ')
.
我的想法是:
在表情符号前后添加一个空格。喜欢:
test = '???? ???? ???? ???? ???? here sasdsa'
然后使用 split,这样就可以了。
- 不确定这种方法是最好的。
- 不知道该怎么做。 (我确实知道如果
i
是一个表情符号,那么if i in emoji.UNICODE_EMOJI
将返回 true (emoji
包裹))。
我认为你在每个表情符号后面添加一个空格的想法是一个好方法。如果表情符号和下一个字符之间已经有空格,您还需要去除空格,但这很简单。就像是:
def emoji_splitter(text):
new_string = ""
for char in text:
if char in emoji.UNICODE_EMOJI:
new_string += " {} ".format(char)
else:
new_string += char
return [v for v in map(lambda x: x.strip(), new_string.split(" ")) if v != ""]
也许您可以通过使用滑动窗口来检查表情符号后面的空格并仅在必要时添加空格来改进这一点,但这会假设只有一个空格,因为此解决方案应考虑表情符号之间的 0 到 n 个空格。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)