File size: 1,462 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
"""Sentiment prediction for new, unseen text."""

import pandas as pd

from config.constants import TEXT_COLUMN
from config.paths import DEFAULT_MODEL_PATH
from preprocessing.pipeline import preprocess_dataframe
from training.model_io import load_model

_default_model = None


def get_default_model(path=DEFAULT_MODEL_PATH):
    """Load (and cache) the saved best model."""
    global _default_model
    if _default_model is None:
        _default_model = load_model(path)
    return _default_model


def predict_sentiment(text, model=None, convert_emojis=True):
    """Predict the sentiment of a single piece of raw text.

    The text goes through the same cleaning chain the training corpus went
    through - emoticons to emojis, emojis to Arabic words, remove stopwords,
    remove non-Arabic characters, normalize, remove numbers, remove
    hashtags/mentions, remove URLs, remove punctuation, light-stem - and is
    then handed to the fitted TF-IDF + classifier pipeline.
    """
    if model is None:
        model = get_default_model()

    # Create a DataFrame with the input text
    text = [text]
    daf = pd.DataFrame({TEXT_COLUMN: text})

    # Preprocess the text
    daf = preprocess_dataframe(daf, text_column=TEXT_COLUMN,
                               convert_emojis=convert_emojis,
                               drop_small_sentences=False)

    # Make predictions
    predictions = model.predict(daf[TEXT_COLUMN])

    return predictions