"""Emoji and emoticon handling.""" import re import emoji import regex from config.emoji_map import emojis, emoticons_to_emoji def extract_emoji(text): """Return the list of emoji grapheme clusters contained in ``text``.""" emoji_list = [] data = regex.findall(r'\X', text) for word in data: if any(emoji.distinct_emoji_list(char) for char in word): emoji_list.append(word) return emoji_list def emoji_counter(sentence): """Return the number of emojis in ``sentence`` (used for the EDA table).""" return emoji.emoji_count(sentence) def replace_emoticon_with_emojis(message): """Replace whitespace-delimited ASCII emoticons with their emoji equivalent.""" separate_words = message.split(' ') modified_message = "" for word in separate_words: modified_message += emoticons_to_emoji.get(word, word) + " " return modified_message.strip() # Remove trailing space def replace_emojis_with_text(message): """Replace each known emoji with the Arabic word(s) describing it.""" separate_words = regex.findall(r'\X', message) modified_message = "" for word in separate_words: if any(emoji.distinct_emoji_list(char) for char in word): modified_message += " " + emojis.get(word, word) + " " else: modified_message += emojis.get(word, word) + "" return modified_message def remove_emoji(string): """Strip emoji characters from ``string`` (not part of the active pipeline).""" emoji_pattern = re.compile("[" u"\U0001F600-\U0001F64F" # emoticons u"\U0001F300-\U0001F5FF" # symbols & pictographs u"\U0001F680-\U0001F6FF" # transport & map symbols u"\U0001F1E0-\U0001F1FF" # flags (iOS) u"\U00002702-\U000027B0" u"\U000024C2-\U0001F251" "]+", flags=re.UNICODE) return emoji_pattern.sub(r'', string).strip() def space_between_emojis(s): """Pad every emoji with spaces (not part of the active pipeline).""" return ''.join((' '+c+' ') if c in emoji.UNICODE_EMOJI['en'] else c for c in s)