File size: 2,236 Bytes
d840583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""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)