File size: 897 Bytes
93109de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Module to classify text into positive or negative sentiments
"""
import sys
import tensorflow as tf
from models.models import load_sentiments_model

sentiments_model = load_sentiments_model()
MAX_NEG = 0.4
MIN_POS = 0.6


def classify_sentiment(input_text: str) -> str:
    """
    Receives a string and classifies it in positive, negative or none
    """
    result = tf.sigmoid(sentiments_model(tf.constant([input_text])))
    if result < MAX_NEG:
        return "negative"
    elif result > MIN_POS:
        return "positive"
    else:
        return "-"


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print(
            f"Usage: python {sys.argv[0]} <text to classify>")
        sys.exit(1)
    # Get the input string from command line argument
    input_text = sys.argv[1]
    sentiment = classify_sentiment(input_text)
    print("Sentiment of the sentence: ", sentiment)