Beispiel #1
0
class Dictionary:
    def __init__(self):
        self.sn = SenticNet()

    """
        Input : String 
        Output : "positive" or "negative"
    """

    def get_word_polarity(self, word, log=True):
        value = "empty"
        try:
            value = self.sn.polarity_value(word.lower())
        except:
            if log:
                print('An error occurred. Word: ' + word + ' is not known.')

        return value

    """
        Input : String
        Output : Int [-1 : 1]    
    """

    def get_word_polarity_numerical_value(self, word, log=True):
        value = "empty"
        try:
            value = self.sn.polarity_intense(word.lower())
        except:
            if log:
                print('An error occurred. Word: ' + word + ' is not known.')
        return value
Beispiel #2
0
def doc_sentiment(data):
    """
    """
    #Call SenticNet module
    sn = SenticNet()
    
    #Create positive and negative variables
    total_sentiment = 0
    
    #Calculate sentiment for all words in document
    for i in range(len(data)):
        #If words don't exist in SenticNet vocabulary they will return an error
        #We treat these words as if they have a sentiment of 0
        try:
            #Calculate sentiment of word
            sentiment = sn.polarity_value(data[i])
            #Update total sentiment
            total_sentiment += float(sentiment)
            
        except:
            None
    
    try:
        #If total sentiment = 0 division errors will occur
        #Calculate average sentiment for the document
        avg_sentiment = total_sentiment/len(data)
    except:
        avg_sentiment = 0
        
    if avg_sentiment >= 0:
        output = 1
    else:
        output = 0
    
    return output
Beispiel #3
0
class SenticNets(BaseEstimator, TransformerMixin):
    def __init__(self, vocab):
        self.vocab = vocab
        self.X_width = len(vocab)
        self.sn = SenticNet()

    def fit(self, X):
        return self

    def vector(self, X):
        X = X.split(' ')
        zeros = csr_matrix((1, self.X_width))
        for word in range(len(X)):
            if not X[word] in self.vocab:
                continue
            try:
                score = self.sn.polarity_value(X[word])
            except KeyError:
                continue
            zeros[0, self.vocab[X[word]]] = score
        return zeros

    def transform(self, X):
        self.zeros = csr_matrix((0, self.X_width))
        self.X_length = X.shape[0]
        for i in range(self.X_length):
            self.zeros = vstack([self.zeros, self.vector(X[i])])
        return self.zeros

    def fit_transform(self, X, y=None):
        self.fit(X)
        self.transform(X)
        return self.zeros
Beispiel #4
0
    def sentiment_avg(self, text):
        sn = SenticNet('pt')
        list_polarity = []
        qtd_words = len(text)
        temp = text.split()
        avg_n = 0
        for i in range(len(temp)):
            try:
                polarity_value = sn.polarity_value(
                    self.treatment_string(temp[i]))
                list_polarity.append(polarity_value)
            except:
                qtd_words -= 1
                i += 1

        avg_n = self.avg(list_polarity, qtd_words)
        if avg_n > 0.003 or avg_n < -0.003:
            return True
        else:
            return False
Beispiel #5
0
from senticnet.senticnet import SenticNet

sn = SenticNet()
print("polarity value:", sn.polarity_value("love"))
print("polarity intense:", sn.polarity_intense("love"))
print("moodtags:", ", ".join(sn.moodtags("love")))
print("semantics:", ", ".join(sn.semantics("love")))
print("\n".join([key + ": " + str(value) for key, value in sn.sentics("love").items()]))
Beispiel #6
0
# Each line of corpus must be equivalent to each document of the corpus
#boc_model=boc.BOCModel(doc_path="input corpus path")
boc_model = boc.BOCModel('text.txt')

#boc_model.context = text

# output can be saved with save_path parameter
boc_matrix, word2concept_list, idx2word_converter = boc_model.fit()

# SenitcNet lexicon lookup
from senticnet.senticnet import SenticNet

sn = SenticNet()

concept_info = sn.concept(text)
polarity_value = sn.polarity_value(text)
polarity_intense = sn.polarity_intense(text)
moodtags = sn.moodtags(text)
semantics = sn.semantics(text)
sentics = sn.sentics(text)

print('==================================')
print('test: ', text)
print('concept_info: ', concept_info)
print('polarity_value: ', polarity_value)
print('polarity_intense: ', polarity_intense)
print('moodtags: ', moodtags)
print('semantics: ', semantics)
print('sentics: ', sentics)
print('==================================')
words = dict(document_term_matrix_idf.apply(
    sum, axis=0))  ## this needs an dictionary object
wordcloud = WordCloud(
    max_font_size=40, max_words=50, background_color="white").fit_words(
        words)  #  fit_words() is used to plot wordcloud using dictionary.
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

#####  positive and negative words using user-built lexicons, plotting their wordclouds.
sn = SenticNet()
positive_words = []
negative_words = []
for word in vectorizer.get_feature_names():
    if word in sn.data:
        if sn.polarity_value(word) == 'positive':
            positive_words.append(word)
        if sn.polarity_value(word) == 'negative':
            negative_words.append(word)

len(positive_words)
len(negative_words)

positive_words = dict(document_term_matrix[positive_words].apply(sum, axis=0))
negative_words = dict(document_term_matrix[negative_words].apply(sum, axis=0))

#  positive words wordcloud using frequency of words
wordcloud = WordCloud(max_font_size=40, max_words=50,
                      background_color="white").fit_words(positive_words)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
from senticnet.senticnet import SenticNet

teste = []
sn = SenticNet('pt')
concept_info = sn.concept('amor')
polarity_value = sn.polarity_value('amor')
polarity_intense = sn.polarity_intense('amor')
moodtags = sn.moodtags('amor')
semantics = sn.semantics('amor')
sentics = sn.sentics('amor')

teste.append(concept_info)

print(teste)