File size: 1,978 Bytes
37d5811
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
445aa77
37d5811
445aa77
37d5811
445aa77
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
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from transformers import pipeline
import streamlit as st

def translate_text(text):
    blob = TextBlob(text)
    return str(blob.translate(from_lang="pt", to="en"))

def sentiment_classification(sentence):
    sid_obj = SentimentIntensityAnalyzer()

    sentiment_dict = sid_obj.polarity_scores(sentence)
    negative = sentiment_dict['neg']
    neutral = sentiment_dict['neu']
    positive = sentiment_dict['pos']
    compound = sentiment_dict['compound']

    if sentiment_dict['compound'] >= 0.05 :
        overall_sentiment = "Positive"

    elif sentiment_dict['compound'] <= - 0.05 :
        overall_sentiment = "Negative"

    else :
        overall_sentiment = "Neutral"
  
    return overall_sentiment, sentiment_dict['compound']
 
def theme_classification(text):
    
    labels = ["Industrial Goods",
              "Communications",
              "Cyclic Consumption",
              "Non-cyclical Consumption",
              "Financial",
              "Basic Materials",
              #"Others",
              "Oil, Gas and Biofuels",
              "Health",
              #"Initial Sector",
              "Information Technology",
              "Public utility"]
    
    template = "The economic sector of this set of words is {}."

    classifier = pipeline("zero-shot-classification", model="joeddav/xlm-roberta-large-xnli")

    results = classifier(text, labels, hypothesis_template=template)

    index = results["scores"].index(max(results["scores"]))

    return results["labels"][index]

text = st.text_area("Coloque seu texto sobre mercado financeiro em português!")

if text:
   text_en = translate_text(text)
   st.write("Translation: {}".format(text_en))
   sentiment = sentiment_classification(text_en)
   st.write("Sentiment: {} - {}".format(sentiment[0], sentiment[1]))
   theme = theme_classification(text_en)
   st.write("Theme: {}".format(theme))