File size: 1,981 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
"""The single preprocessing pipeline shared by training and inference.

Order of the steps:
1. ``replace_emoticon_with_emojis``   (emoji step)
2. ``replace_emojis_with_text``       (emoji step)
3. ``remove_stop_words``
4. ``Removing_non_arabic``
5. ``normalizeArabic``
6. ``Removing_numbers``
7. ``remove_hashtags_and_mentions``
8. ``Removing_urls``
9. ``Removing_punctuations``
10. ``Arabic_Light_Stemmer``
"""

from config.constants import TEXT_COLUMN
from preprocessing.arabic_normalizer import Arabic_Light_Stemmer, normalizeArabic
from preprocessing.emoji_handler import (
    replace_emojis_with_text,
    replace_emoticon_with_emojis,
)
from preprocessing.text_cleaning import (
    Removing_non_arabic,
    Removing_numbers,
    Removing_punctuations,
    Removing_urls,
    remove_hashtags_and_mentions,
    remove_small_sentences,
    remove_stop_words,
)

#: Steps 1-2 - emoticon/emoji substitution.
EMOJI_STEPS = (
    replace_emoticon_with_emojis,
    replace_emojis_with_text,
)

#: Steps 3-10 - applied to both the training corpus and inference input.
CLEANING_STEPS = (
    remove_stop_words,
    Removing_non_arabic,
    normalizeArabic,
    Removing_numbers,
    remove_hashtags_and_mentions,
    Removing_urls,
    Removing_punctuations,
    Arabic_Light_Stemmer,
)


def clean_text(text, convert_emojis=True):
    """Run the full cleaning chain on a single string."""
    steps = (EMOJI_STEPS + CLEANING_STEPS) if convert_emojis else CLEANING_STEPS
    for step in steps:
        text = step(text)
    return text


def preprocess_dataframe(df, text_column=TEXT_COLUMN, convert_emojis=True,
                         drop_small_sentences=True):
    df[text_column] = df[text_column].apply(
        lambda text: clean_text(text, convert_emojis=convert_emojis)
    )
    if drop_small_sentences:
        # this function will convert the text which contains one or two words into null value
        remove_small_sentences(df, text_column=text_column)
    return df