【发布时间】:2021-09-21 04:09:02
【问题描述】:
我正在尝试分隔文本字符串。我在尝试实现这一目标时遇到了一些问题。我试过的如下。
import emoji
text = "#samplesenti @emojitweets i ❤❤❤ sentiment " analysis " http://senti.com/pic_01.jpg "
def extract_text_and_emoji(text=text):
global allchars, emoji_list
# remove all tagging and links, not need for sentiments
remove_keys = ('@', 'http://', '&', '#')
clean_text = ' '.join(txt for txt in text.split() if not txt.startswith(remove_keys))
# print(clean_text)
# setup the input, get the characters and the emoji lists
allchars = [str for str in text]
emoji_list = [c for c in allchars if c in emoji.UNICODE_EMOJI]
# extract text
clean_text = ' '.join([str for str in clean_text.split() if not any(i in str for i in emoji_list)])
# extract emoji
clean_emoji = ''.join([str for str in text.split() if any(i in str for i in emoji_list)])
return (clean_text, clean_emoji)
allchars, emoji_list = 0, 0
(clean_text, clean_emoji) = extract_text_and_emoji()
print('\nAll Char:', allchars)
print('\nAll Emoji:', emoji_list)
print('\n', clean_text)
print('\n', clean_emoji)
我希望将其发送到我的控制台:
All Char: ['#', 's', 'a', 'm', 'p', 'l', 'e', 's', 'e', 'n', 't', 'i', ' ', '@', 'e', 'm', 'o', 'j', 'i', 't', 'w', 'e', 'e', 't', 's', ' ', 'i', ' ', '❤', '❤', '❤', ' ', 's', 'e', 'n', 't', 'i', 'm', 'e', 'n', 't', ' ', '&', 'q', 'u', 'o', 't', ';', ' ', 'a', 'n', 'a', 'l', 'y', 's', 'i', 's', ' ', '&', 'q', 'u', 'o', 't', ';', ' ', 'h', 't', 't', 'p', ':', '/', '/', 's', 'e', 'n', 't', 'i', '.', 'c', 'o', 'm', '/', 'p', 'i', 'c', '_', '0', '1', '.', 'j', 'p', 'g', ' ']
All Emoji: ['❤', '❤', '❤']
i sentiment analysis
❤❤❤
但我得到了这个:
All Char: ['#', 's', 'a', 'm', 'p', 'l', 'e', 's', 'e', 'n', 't', 'i', ' ', '@', 'e', 'm', 'o', 'j', 'i', 't', 'w', 'e', 'e', 't', 's', ' ', 'i', ' ', '❤', '❤', '❤', ' ', 's', 'e', 'n', 't', 'i', 'm', 'e', 'n', 't', ' ', '&', 'q', 'u', 'o', 't', ';', ' ', 'a', 'n', 'a', 'l', 'y', 's', 'i', 's', ' ', '&', 'q', 'u', 'o', 't', ';', ' ', 'h', 't', 't', 'p', ':', '/', '/', 's', 'e', 'n', 't', 'i', '.', 'c', 'o', 'm', '/', 'p', 'i', 'c', '_', '0', '1', '.', 'j', 'p', 'g', ' ']
All Emoji: []
i ❤❤❤ sentiment analysis
【问题讨论】:
标签: python python-3.x list emoji