def compute_sentiment(tweets_df): tweets_df["polarity"] = tweets_df["clean_tweets"].apply( lambda x: sentiment(x)[0]) tweets_df["subjectivity"] = tweets_df["clean_tweets"].apply( lambda x: sentiment(x)[1]) return tweets_df
def calculate_sentiment(row): if pypath not in sys.path: sys.path.insert(1, pypath) from pattern.text.nl import sentiment entry = row.asDict() entry['description_score'], entry['text_score'] = sentiment( entry['description']), sentiment(entry['text']) return entry
def get_analysis(self, sSent): """Return the sentiment-analysis of one sentence""" try: score = sentiment(sSent) return score except: # act upon error self.errHandle.DoError("SentiAna/get_analysis") return None
def getEntities(cleanSentence, tfidf, max): relevantWords = {} counter = 0 x = pl.sentiment(cleanSentence).assessments listSent = [] for x1 in x: w, a, b, c = x1 for p in w: listSent.append(p) for w in sorted(tfidf, key=tfidf.get, reverse=True): if w not in listSent and 2 < len(w) < 25: relevantWords[w] = ([], [], tfidf[w]) counter += 1 return relevantWords
def getSentiments(max_val_tf, relevantWords, text): sentiment_count = 0 for sentence in text: #passing each sentence the number of times a sentiment has occurred. do this before. sentiments_in_sentence = {} sentiments_in_sentence_list = [] for sentiment in pl.sentiment(sentence).assessments: sentiment_words, pol, obj, x = sentiment # only add sentiments with a bit of flavor if abs(pol) + abs(obj) > 0.1: # interestingness sentiments_in_sentence[" ".join( sentiment_words)] = sentiment # appending a tuple # assumption: if the sentiment is longer than 1 item, # the head of the first item will be the second item, etc. # This assumption is not always correct (case of conjunction) # TODO: fix conjunction case sentiments_in_sentence_list.append(sentiment_words[0]) sentiment_count += 1 if sentiments_in_sentence: # if there are interesting sentiments in the sentence, we want to do a recursion to match them with an entity/subject i, flag = 0, 0 for chunk in nlp( sentence ): # we are going through the nlp (spacy) tagged sentence if chunk.text == 'niet': # we found a negation flag = 1 if chunk.text in sentiments_in_sentence_list and chunk.dep_ not in [ 'case', 'mark', 'advcl' ] and chunk.pos_ not in ["VERB"]: sentiment_chunk = get_sentiments_from_text_chunk( sentiments_in_sentence, chunk.text) recursion_path_list = recurse_to_find_entity( chunk, 0, 0, 0, 0, []) missed_negation = find_missed_negation( i, sentiment_chunk[0]) context_fragment = missed_negation + sentiment_chunk[0] if 'naar' not in sentiment_chunk[ 0]: # hand-filtering out "naar", which keeps being classified as a sentiment word = "" for item in recursion_path_list: if item.isupper(): word = item.lower() if word not in context_fragment: # the word can't be in "context_fragment" already, that's how you get "ramp ramp chernobyl" context_fragment.append(word) if word != "": # if we found at least one potential noun to be the entity/subject relevantWords = attach_direct_list( relevantWords, recursion_path_list, context_fragment, word, sentiment_chunk) if flag == 1: # for the missed_negation i += 1 #print("SENTIMENT COUNT: ", sentiment_count) return relevantWords
def transform_text(text): from pattern.text.nl import sentiment polarity, subjectivity = sentiment(text) return numpy.array([polarity, subjectivity])