def analyse_sentiment(text): pol = polarity(text) if pol < -0.1: sentiment = "negative" elif pol > 0.1: sentiment = "positive" else: sentiment = "neutral" return sentiment
def pattern_assessor(x, *args): from pattern.web import plaintext from pattern.en import polarity, subjectivity key = args[0] pt = plaintext(x) sss = {} sss.update({'polarity': polarity(pt), 'subjectivity': subjectivity(pt)}) #return sss[key] return sss
def sentiment_new(text, terms): ''' INPUT string, string OUTPUT float ''' # Parse sentences def Sentences(paragraph): sentenceEnders = re.compile('[.!?]') sentenceList = sentenceEnders.split(paragraph) return sentenceList # Identify sentences with key terms def to_filter(text, terms): if any(word in text for word in terms): return text else: return np.nan target = [] # Retrieve sentences that include key terms for i in Sentences(text): if to_filter(i, terms) == None: pass else: target.append(i) s_max = 0 comment_score = 0 # Take the most extreme score at the sentence level and use that for the comment for i in target: if abs(sentiment(i)) > abs(s_max): comment_score = polarity(i) s_max = polarity(i) return comment_score
def sentiment_new(text, terms): """ INPUT string, string OUTPUT float """ # Parse sentences def Sentences(paragraph): sentenceEnders = re.compile("[.!?]") sentenceList = sentenceEnders.split(paragraph) return sentenceList # Identify sentences with key terms def to_filter(text, terms): if any(word in text for word in terms): return text else: return np.nan target = [] # Retrieve sentences that include key terms for i in Sentences(text): if to_filter(i, terms) == None: pass else: target.append(i) s_max = 0 comment_score = 0 # Take the most extreme score at the sentence level and use that for the comment for i in target: if abs(sentiment(i)) > abs(s_max): comment_score = polarity(i) s_max = polarity(i) return comment_score
def test_polarity(self): # Assert that en.polarity() yields en.sentiment()[0]. s = "A great day!" self.assertTrue(en.polarity(s) == en.sentiment(s)[0]) print("pattern.en.polarity()")
def test_polarity(self): # Assert that en.polarity() yields en.sentiment()[0]. s = "A great day!" self.assertTrue(en.polarity(s) == en.sentiment(s)[0]) print "pattern.en.polarity()"
def sentiment(text): ''' INPUT string OUTPUT float ''' return polarity(text)
print conjugate('purred', '3sg') print PAST in tenses('purred') # 'p' in tenses() also works. print (PAST, 1, PL) in tenses('purred') print 'Quantification' print quantify(['goose', 'goose', 'duck', 'chicken', 'chicken', 'chicken']) print quantify('carrot', amount=90) print quantify({'carrot': 100, 'parrot': 20}) print 'ngrams' print ngrams("I am eating a pizza.", n=2) #parse s = parse('I eat pizza with a fork.') pprint(s) #tag for word, t in tag('The cat felt happy.'): print word +' is ' +t s = "The movie attempts to be surreal by incorporating various time paradoxes, but it's presented in such a ridiculous way it's seriously boring." print sentiment(s) print polarity(s) print subjectivity(s) #The modality() function returns a value between -1.0 and +1.0, expressing the degree of certainty s2 = "Some amino acids tend to be acidic while others may be basic." # weaseling se = Sentence(parse(s, chunks=False, lemmata=True)) print modality(se)
ACCESS_TOKEN_KEY = '----' ACCESS_TOKEN_SECRET = '---' api = TwitterAPI(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET) r = api.request('statuses/filter', { 'track': TRACK_TERM, 'locations': TRACK_LOC }) ### MongoDB Calling client = MongoClient("mongodb://127.0.0.1:3001/meteor") db = client['meteor'] for item in r.get_iterator(): if 'text' in item: a = item['text'] p = polarity(a) s = subjectivity(a) if (p != 0.0): post = { "track": "newyork", "tweet": a, "tweetdate": datetime.datetime.utcnow(), "polarity": p, "subjectivity": s } db.tweets_newyork.insert_one(post).inserted_id print("NewYork") #print(a)
def sentiment(text): """ INPUT string OUTPUT float """ return polarity(text)
from pattern.web import Twitter, plaintext 2 from pattern.db import Datasheet 3 from pattern.en import polarity 4 5 class GatherTweets: 6 # a file called companies.txt contains a list of companies to search Twitter for 7 file = open("companies.txt") 8 csv = Datasheet() 9 # traverse companies.txt and retrieve tweets and assign polarity to them 10 for line in file: 11 for tweet in Twitter().search(line.rstrip('\n').rstrip('0xD')): 12 tweet_desc = plaintext(tweet.description) 13 tweet_pol = polarity(tweet.description) 14 csv.append([tweet.date, tweet_desc, tweet_pol]) 15 # save the tweet date, tweet, and the polairty of the tweet in gathered_tweets.csv 16 csv.save('gathered_tweets.csv')
from pattern.en import sentiment, polarity print polarity('DMC Latam 2013 was awesome!') print polarity('Bruno is beautiful') print polarity('I hate brocoli')
# a text is objective or subjective, positive or negative. # The sentiment analysis lexicon bundled in Pattern focuses on adjectives. # It contains adjectives that occur frequently in customer reviews, # hand-tagged with values for polarity and subjectivity. # The polarity() function measures positive vs. negative, as a number between -1.0 and +1.0. # The subjectivity() function measures objective vs. subjective, as a number between 0.0 and 1.0. # The sentiment() function returns an averaged (polarity, subjectivity)-tuple for a given string. for word in ("amazing", "horrible", "public"): print word, sentiment(word) text = "The movie attempts to be surreal by incorporating time travel and various time paradoxes, but it's presented in such a ridiculous way it's seriously boring." print print sentiment(text) print print polarity(text) print print subjectivity(text) # The input string can be: # - a string, # - a Synset (see pattern.en.wordnet), # - a parsed Sentence, Text, Chunk or Word (see pattern.en), # - a Document (see pattern.vector). # The positive() function returns True if the string's polarity >= threshold. # The threshold can be lowered or raised, # but overall for strings with multiple words +0.1 yields the best results. print print "good:", positive("good", threshold=0.1) print " bad:", positive("bad")
def get_polarity(self, sentence): """ given sentence provide the polarity {1,-1} """ return polarity(sentence)
print lemma('running') print conjugate('purred', '3sg') print PAST in tenses('purred') # 'p' in tenses() also works. print(PAST, 1, PL) in tenses('purred') print 'Quantification' print quantify(['goose', 'goose', 'duck', 'chicken', 'chicken', 'chicken']) print quantify('carrot', amount=90) print quantify({'carrot': 100, 'parrot': 20}) print 'ngrams' print ngrams("I am eating a pizza.", n=2) #parse s = parse('I eat pizza with a fork.') pprint(s) #tag for word, t in tag('The cat felt happy.'): print word + ' is ' + t s = "The movie attempts to be surreal by incorporating various time paradoxes, but it's presented in such a ridiculous way it's seriously boring." print sentiment(s) print polarity(s) print subjectivity(s) #The modality() function returns a value between -1.0 and +1.0, expressing the degree of certainty s2 = "Some amino acids tend to be acidic while others may be basic." # weaseling se = Sentence(parse(s, chunks=False, lemmata=True)) print modality(se)