Пример #1
0
def main():
    with open('secret.json', 'r') as fp:
        credentials = json.load(fp)

    client = UserClient(**credentials)

    response = client.api.search.tweets.get(q=sys.argv[1], count=150)

    # Need to break down hierarchy (example 'user' column)
    tweets = pd.DataFrame(response.data['statuses'])

    # Doesnt seem to the best classifier - its quite shit actually
    # Maybe movie reviews for a training corpus isn't the most ideal
    # representation of tweets?
    # A lot of false positives and a few false negatives
    print('Training classifier...')
    classifier = NaiveBayesAnalyzer()
    classifier.train()  # Train on a Movie Review Corpus

    print('Performing Sentiment Analysis...')
    counter = Counter()

    for text in tweets['text']:
        result = classifier.analyze(text)

        counter[result.classification] += 1
        print '%s: %s' % (result.classification, text)

    print 'Total: ', counter
Пример #2
0
    def comparision_piechart(username):##function declaration to show number of positive and negative comments and plot a pie-chart
    media_id = fetch_post_id(username)
    request_url = (BASE_URL + 'media/%s/comments/?access_token=%s') % (media_id, app_access_token)
    print 'GET request url : %s' % (request_url)
    comment_info = requests.get(request_url).json()
    if comment_info['meta']['code'] == 200:
        if len(comment_info['data']):
            for x in range(0, len(comment_info['data'])):
                comment_id = comment_info['data'][x]['id']
                comment_text = comment_info['data'][x]['text']
                blob = TextBlob(comment_text, analyzer=NaiveBayesAnalyzer())
                if (blob.sentiment.p_neg > blob.sentiment.p_pos):
                    print 'Negative comment : %s' % (comment_text)



            for y in range(0, len(comment_info['data'])):
                comment_id = comment_info['data'][y]['id']
                comment_text = comment_info['data'][y]['text']
                blob = TextBlob(comment_text, analyzer=NaiveBayesAnalyzer())
                if (blob.sentiment.p_neg < blob.sentiment.p_pos):
                    print 'positive comment : %s' % (comment_text)
                    a = y + 1  # positive comments
                    b = x - y  # negative comments
                    print "No. of Positive comments: %s" % (a)
                    print "No. of negative comments: %s" % (b)
                    c = a + b
                    print "Total no. of comments: %s" %(c)
                    
                    
                   
                    #commands to plot pychart
                    labels = 'Positive ', 'Negative'
                    sizes = [a, b]
                    colors = ['blanchedalmond', 'aliceblue']
                    explode = (0.1, 0)  # explode 1st slice
                    plt.pie(sizes, explode=explode, labels=labels, colors=colors,
                            autopct='%1.1f%%', shadow=True, startangle=140)

                    plt.axis('equal')
                    plt.show()


        else:
            print 'Comments not found on the post!'
    else:
        print 'Status code other than 200 received!'
Пример #3
0
	def getFeatures(rev):

		wordpattern = re.compile('\w+')
		capspattern = re.compile('([A-Z])+\w')
		exclaimpattern = re.compile('!')

		rev = rev.decode('utf-8', 'ignore')


		revCharLength = len(rev)

		words = wordpattern.findall(rev)
		revWordsLength = len(words)

		revUniqueWordLength = len(set(words))


		revCapCount = len(capspattern.findall(rev))

		revExclaimCount = len(exclaimpattern.findall(rev))


		revAdjCount = 0

		revPosTokens = nltk.pos_tag(nltk.word_tokenize(rev))

		for _, pos in revPosTokens:
			if pos == 'JJ' or pos == 'VBP':
				revAdjCount += 1


		## Sentiment Classifiers:
		# revSentAgg = sentClassify(rev)
		## overall production sentiment classifier
		blob = TextBlob(rev, analyzer=NaiveBayesAnalyzer())
		blobSent = blob.sentiment

		# print blobSent

		if blobSent[0] == 'pos':
			revSent = 1 * blobSent[1]
		elif blobSent[0] == 'neg':
			revSent = -1 * blobSent[2]
		else:
			revSent = 0




		return [
			revCharLength,
			revWordsLength,
			revUniqueWordLength,
			revCapCount,
			revExclaimCount,
			revAdjCount,
			revSentAgg,
			revSent
		]
Пример #4
0
def hashtag_analysis():
    # get user details
    usr_name = raw_input("Enter Username.")
    user_id = user_info(usr_name)
    # get user id
    url_hash = Base_url + 'users/%s/media/recent/?access_token=%s' % (
        user_id, ACCESS_TOKEN)
    print url_hash
    media = requests.get(url_hash).json()
    # if connection successful
    if media['meta']['code'] == 200:
        # get all post caption
        for i in range(0, len(media['data'])):
            hash_id = media['data'][i]['id']
            # get media hashtags
            hash_url = Base_url + 'media/%s?access_token=%s' % (hash_id,
                                                                ACCESS_TOKEN)
            print hash_url
            # get request for hash_url
            hash_request = requests.get(hash_url).json()
            # if connection successful
            if hash_request['meta']['code'] == 200:
                # checking whether there is a caption or not
                if hash_request['data']['caption'] != None:

                    new_text = hash_request['data']['caption']['text']
                    print hash_request['data']['caption']['text']
                    # checking the caption is positive or negative
                    blob = TextBlob(new_text, analyzer=NaiveBayesAnalyzer())
                    # if positive then we append it friend list
                    if blob.sentiment.p_pos >= blob.sentiment.p_neg:
                        number = blob.sentiment.p_pos
                        friend.append(number)
                    # else append with a negative sign
                    elif blob.sentiment.p_pos < blob.sentiment.p_neg:
                        number2 = -blob.sentiment.p_neg
                        friend.append(number2)
                #if no caption on post
                else:
                    print 'No caption.'
                    number = 0
                    friend.append(number)
            elif hash_request['meta']['code'] != 200:
                print 'Invalid Url.'
    elif media['meta']['code'] != 200:
        print 'Invalid Url.'
    # defining axis for graph
    plt.axis([0, len(media['data']) + 1, -2, 2])
    # labeling the graph
    plt.xlabel('Happy')
    plt.ylabel('Post')
    post = 1
    # ploting the graph
    for i in range(0, len(friend)):
        if friend[i] != 0:
            plt.plot(post, friend[i], 'ro', lw=2)
        post = post + 1
    # making graph visible
    plt.show()
def get_sentiment_nbayes(trump_df_clean):
    sentiment = []
    '''extremely slow'''
    for i in trump_df_clean['PROCESSED_TEXT']:
        blob = TextBlob(i, analyzer = NaiveBayesAnalyzer())
        sentiment.append(blob.sentiment.classification)
    trump_df_clean['SENTIMENT_NB'] = sentiment
    return trump_df_clean
Пример #6
0
def analyze(data):
    text = data['text'].lower()

    sentiment = TextBlob(text, analyzer=NaiveBayesAnalyzer()).sentiment.classification
    if sentiment=="neg":
        return [data]
    else:
        return []
Пример #7
0
 def blob_sentiment_analyzer(self,text,imp_type):
     if(imp_type == 'NaiveBayesAnalyzer'):
         blob = TextBlob(text, analyzer=NaiveBayesAnalyzer())#NaiveBayesAnalyzer
     else:
         blob = TextBlob(text)#PatternAnalyzer
         #print("Tags:",blob.tags)
         #print("Words:",blob.words)
     return blob
def predictPolaritySubjectivity(text, custom_list):
    testimonial = TextBlob(text, analyzer=NaiveBayesAnalyzer())
    polarity = testimonial.polarity
    
    for k in text.split():
        if k in custom_list:
            polarity = - 1
    return([polarity, testimonial.subjectivity])
Пример #9
0
def textblob_naivebayes(df):
    labels = {"pos": 1, "neg": -1}
    analyzer=NaiveBayesAnalyzer()
    outputs = df['input'].apply(lambda x: TextBlob(x, analyzer=analyzer).sentiment.classification)
    correct = 0
    for label, output in zip(df["label"], outputs):
        if label == labels[output]: correct += 1
    return correct / len(df)
Пример #10
0
 def get_sentiment(self, tweet):
     analysis = TextBlob(self.clean_tweet(tweet), analyzer=NaiveBayesAnalyzer())
     if analysis.polarity > 0:
         return "positive"
     elif analysis.polarity == 0:
         return "neutral"
     else:
         return "negative"
Пример #11
0
def charts():
    #sentiment polarity calculation
    global s
    global t
    global v
    global s_blog
    global t_blog
    global v_blog
    s = 0
    t = 0
    v = 0
    s_blog = 0
    t_blog = 0
    v_blog = 0
    tb = Blobber(analyzer=NaiveBayesAnalyzer())
    tweets1 = db.aql.execute('FOR s IN ' + c + ' RETURN s')

    for line in tweets1:
        try:
            #Read in one line of the file, convert it into a json object
            x = tb(line['tweet'])
            if x.sentiment.p_pos > 0.55:
                s = s + x.sentiment.p_pos
            elif x.sentiment.p_neg > 0.55:
                t = t + x.sentiment.p_neg
            else:
                v = v + ((x.sentiment.p_pos + x.sentiment.p_neg) / 2.0)

            if line['followers'] > 20000:
                if x.sentiment.p_pos > 0.55:
                    s_blog = s_blog + x.sentiment.p_pos
                elif x.sentiment.p_neg > 0.55:
                    t_blog = t_blog + x.sentiment.p_neg
                else:
                    v_blog = v_blog + (
                        (x.sentiment.p_pos + x.sentiment.p_neg) / 2.0)

        except Exception as e:
            print(e)

    p = round(s / (s + t + v) * 100)
    neg = round(t / (s + t + v) * 100)
    neu = round(v / (s + t + v) * 100)
    p_blog = round(s_blog / (s_blog + t_blog + v_blog) * 100)
    neg_blog = round(t_blog / (s_blog + t_blog + v_blog) * 100)
    neu_blog = round(v_blog / (s_blog + t_blog + v_blog) * 100)

    if c != '':
        return render_template("charts.html",
                               c=c,
                               p=p,
                               neg=neg,
                               neu=neu,
                               p_blog=p_blog,
                               neg_blog=neg_blog,
                               neu_blog=neu_blog)
    else:
        return redirect(url_for("index"))
Пример #12
0
def textblob_pattern_sentiment(tweet):
    """
    Utility function to classify sentiment of passed tweet.

    Using textblob's sentiment method using the NaiveBayesAnalyzer.
    """
    # create TextBlob object of passed tweet text
    analysis = TextBlob(clean_tweet(tweet), analyzer=NaiveBayesAnalyzer())
    return analysis.sentiment.p_neg
Пример #13
0
def text(message):
    """Sent by a client when the user entered a new message.
    The message is sent to all people in the room."""
    room = session.get('room')
    emit('message', {'msg': session.get('name') + ':' + message['msg']},
         room=room)
    print(message, session.get('name'))
    opinion = TextBlob(str(message), analyzer=NaiveBayesAnalyzer())
    print(opinion.sentiment)
Пример #14
0
def textBllob(text):
	stop_words = set(stopwords.words('english'))

	stopwordsRemoved = [token for token in nltk.word_tokenize(text) if not token in stop_words]
	filteredReview = ' '.join(word for word in stopwordsRemoved)

	blob = TextBlob(text, analyzer=NaiveBayesAnalyzer())
	classification, pos, neg = blob.sentiment
	return (filteredReview,classification)
Пример #15
0
 def Sentimental_NaiveScore(line):
     blob = TextBlob(line, analyzer=NaiveBayesAnalyzer())
     sentiment_score = blob.sentiment.classification
     if sentiment_score >= 0.5:
         return 'positive'
     elif (sentiment_score > -0.5) and (sentiment_score < 0.5):
         return 'neutral'
     elif sentiment_score <= -0.5:
         return 'negative'
Пример #16
0
def tradFeelingNaiv(text, analyzer=NaiveBayesAnalyzer()):
    b = TextBlob(text)
    translatedText = text
    try:
        translatedText = b.translate(to="en")
    except exceptions.NotTranslated:
        pass
    b = TextBlob(str(translatedText), analyzer=analyzer)
    return {"polarity": b.sentiment.p_pos - b.sentiment.p_neg, "language": "en"}
Пример #17
0
def get_sentiment():
    raw_text = str(raw_entry.get())
    new_text = TextBlob(raw_text, analyzer=NaiveBayesAnalyzer())
    final_text = new_text.sentiment
    p = final_text.p_pos * 100
    n = final_text.p_neg * 100
    result = '\nPolarity:{}, Positive%:{}, Negative%:{}'.format(
        final_text.classification, p, n)
    tab1_display.insert(tk.END, result)
Пример #18
0
def delete_negative_comment(
        insta_username):  #     Defining the function ......
    media_id = get_post_id(
        insta_username
    )  #   Getting media id by calling the get post id function....
    request_url = (BASE_URL + 'media/%s/comments/?access_token=%s') % (
        media_id, APP_ACCESS_TOKEN)
    print(colored('GET request url : %s\n', 'blue') % (request_url))
    comment_info = requests.get(request_url).json()

    if comment_info['meta']['code'] == 200:
        if len(comment_info['data']):
            #Here's a naive implementation of how to delete the negative comments :)
            for x in range(0, len(comment_info['data'])):
                comment_id = comment_info['data'][x]['id']
                comment_text = comment_info['data'][x]['text']
                blob = TextBlob(
                    comment_text, analyzer=NaiveBayesAnalyzer()
                )  #    Analysing the sentiments and gives the exact figure upto positivity or negativity..
                print(
                    colored(blob.sentiment, 'magenta')
                )  #    Prints the sentiments and gives the exact figure upto positivity or negativity..
                if (blob.sentiment.p_neg > blob.sentiment.p_pos
                    ):  # Checking condition for negative condition..
                    print(
                        colored('Negative comment : %s', 'green') %
                        (comment_text))
                    delete_url = (BASE_URL +
                                  'media/%s/comments/%s/?access_token=%s') % (
                                      media_id, comment_id, APP_ACCESS_TOKEN)
                    print(
                        colored('DELETE request url : %s\n', 'blue') %
                        (delete_url))
                    delete_info = requests.delete(delete_url).json(
                    )  #  Fetching Json data after deleting the comment....

                    if delete_info['meta'][
                            'code'] == 200:  # Checking success code .....
                        print(
                            colored(
                                'The Negative Comment From the Post has successfully deleted!\n',
                                'green'))
                    else:
                        print(
                            colored(
                                'Check Network issues , Unable to delete the comment!!\n',
                                'red'))
                else:
                    print(
                        colored('The Comment is Positive comment : %s\n',
                                'green') % (comment_text))
        else:
            print(
                colored('There are no existing comments on the post!\n',
                        'red'))
    else:
        print(colored('Status code other than 200 received!\n', 'red'))
Пример #19
0
def get_sentiment_textblob(text):
    """
    # @param text: blob of text
    # @return list of (sentiment, score) -> ('pos', '0.33')
    """
    blob = TextBlob(text, analyzer=NaiveBayesAnalyzer())
    sentiment = blob.sentiment.classification
    score = '{0:.4f}'.format(blob.sentiment.p_pos - blob.sentiment.p_neg)
    return [sentiment, score]
Пример #20
0
def combine_sentiments(text, verbose=False):
    '''
    Combine Naive Bayes Classifier with Pattern Analyzer.
    '''
    blob_nb = TextBlob(text.raw, analyzer=NaiveBayesAnalyzer())
    blob_pa = TextBlob(text.raw, analyzer=PatternAnalyzer())
    average_sentiment = mean([prob_to_polarity(blob_nb.sentiment.p_pos),
                            blob_pa.sentiment.polarity])
    return average_sentiment
Пример #21
0
 def analyse_sentiment_textblob_nb(self, tweet):
     """Determine sentiment using TextBlob NaiveBayesAnalyzer
     :param tweet: tweet text as string
     :return: tweet sentiment value (determined from NaiveBayes classification)"""
     analysis = TextBlob(tweet, analyzer=NaiveBayesAnalyzer())
     if analysis.sentiment.classification == 'pos':
         return 1
     else:
         return -1
Пример #22
0
    def __init__(self):
        APP_KEY = 'gQhZuweqmNC9gMAolBfHEny4f'
        APP_SECRET = 'PBgFD6dX7vqfSfJByRg7uWd9bB4333t3e0VPnHkJOisWb8hkXi'

        twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2)
        ACCESS_TOKEN = twitter.obtain_access_token()

        self.twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN)
        self.tb = Blobber(analyzer=NaiveBayesAnalyzer())
Пример #23
0
def clean_description_sentiment(df, description_column):
    cleaned_description = clean_descriptions(df, description_column)
    tb = Blobber(analyzer=NaiveBayesAnalyzer())
    blob = [tb(text) for text in cleaned_description]
    sentiment_values = [text.sentiment for text in blob]
    sentiment_df = pd.DataFrame(zip(*sentiment_values)).T
    sentiment_df.columns = ['clf', 'pos', 'neg']
    sentiment_df = sentiment_df[['pos', 'neg']].astype(float)
    return df.join(sentiment_df)
Пример #24
0
def analyze_feeling(text):
    sentiment = ""
    phrase_pt = ''
    phrase_en = ''
    phrase = ''
    try:
        phrase_pt = TextBlob(text, analyzer=NaiveBayesAnalyzer())
        phrase_en = phrase_pt.translate(from_lang='pt', to='en')
        phrase = TextBlob(str(phrase_en), analyzer=NaiveBayesAnalyzer())
        print(phrase_pt)
        print(phrase.sentiment[0])
        print()
        sentiment = "neu" if phrase.sentiment[1] == phrase.sentiment[
            2] else phrase.sentiment[0]
    except Exception as error:
        print('analyze_feeling - ', phrase_pt, ' - ', phrase, ' - ', error)
        sentiment = "Not identified"
    return sentiment
Пример #25
0
def report(request):
    if not request.user.is_authenticated:
        return redirect('login')
    if request.method == 'POST':
        name = request.POST.get('PId')
        new_tweets = api.user_timeline(
            screen_name=request.POST['PId'], count=5)
        passedMessage = []
        bullyCount = 0
        nonBullyCount = 0
        users = []
        for tweet in new_tweets:
            analysis = TextBlob(tweet.text, analyzer=NaiveBayesAnalyzer())
            polarity = 'Positive'
            if (analysis.sentiment.p_pos < 0.55):
                polarity = 'Bully'
            elif((analysis.sentiment.p_pos > 0.56) and (analysis.sentiment.p_pos < 0.65)):
                polarity = 'none'
            else:
                polarity = 'Non Bully'
            # print(tweet.text)
            if(tweet.text[0] == 'R' and tweet.text[1] == 'T'):
                name = tweet.text.split('RT @')[1].split(":")[0]
            else:
                name = tweet.user.name
            message = tweet.text
            # print("Sentiment Analysis and Topic of Interest")
            # print("Tweet : ", tweet.text)
            # print("Sentiment:", polarity)
            # print("Confidence :  Positive score: ", analysis.sentiment.p_pos *
            #       100, "  Negative score: ", analysis.sentiment.p_neg*100)
            # print("Areas of interest: ", analysis.noun_phrases)
            passedMessage.append(
                {"name": name, "body": message, "type": polarity})
            if(polarity == 'Bully'):
                bullyCount += 1
            elif(polarity == 'Non Bully'):
                nonBullyCount += 1

            if(name not in users):
                users.append(name)

        if(bullyCount >= nonBullyCount):
            troll = 'Yes'
        else:
            troll = 'No'
        print(users)
        count = 0
        for i in request.POST['PId']:
            count = count + ord(i)

        data = {"submit": "block", "message": passedMessage,
                'troll': troll, 'user': '******'.join(users), "count": count % 97, "name": name}
    else:
        data = {"submit": "none"}
    return render(request, 'trollapp/reports.html', data)
Пример #26
0
def delete_negative_comment(insta_username):

    media_id = get_post_id(insta_username)

    request_url = (BASE_URL + 'media/%s/comments/?access_token=%s') % (
        media_id, APP_ACCESS_TOKEN)

    print 'GET request url : %s' % (request_url)

    comment_info = requests.get(request_url).json()

    if comment_info['meta']['code'] == 200:

        if len(comment_info['data']):

            #How to delete the negative comments

            for x in range(0, len(comment_info['data'])):

                comment_id = comment_info['data'][x]['id']

                comment_text = comment_info['data'][x]['text']

                blob = TextBlob(comment_text, analyzer=NaiveBayesAnalyzer())

                if (blob.sentiment.p_neg > blob.sentiment.p_pos):

                    print 'Negative comment : %s' % (comment_text)

                    delete_url = (BASE_URL +
                                  'media/%s/comments/%s/?access_token=%s') % (
                                      media_id, comment_id, APP_ACCESS_TOKEN)

                    print 'DELETE request url : %s' % (delete_url)

                    delete_info = requests.delete(delete_url).json()

                    if delete_info['meta']['code'] == 200:

                        print 'Comment successfully deleted!\n'

                    else:

                        print 'Unable to delete comment!'

                else:

                    print 'Positive comment : %s\n' % (comment_text)

        else:

            print 'There are no existing comments on the post!'

    else:

        print 'Status code other than 200 received!'
Пример #27
0
def translate(string, answer):
    bbb = len(string.split())

    blob = TextBlob(string, analyzer=NaiveBayesAnalyzer())

    translate_response = answer[0]
    print(blob, translate_response)

    if (translate_response.lower() == 'english'):
        resp = str(blob.translate(to='en')) + '\n'
        resp = resp + "Thank you!"
        return resp
    elif (translate_response.lower() == 'arabic'):
        resp = str(blob.translate(to='ar')) + '\n'
        resp = resp + "Thank you!"
        return resp
    elif (translate_response.lower() == 'chinese'):
        resp = str(blob.translate(to='zh')) + '\n'
        resp = resp + "Thank you!"
        return resp
    elif (translate_response.lower() == 'spanish'):
        resp = str(blob.translate(to='es')) + '\n'
        resp = resp + "Thank you!"
        return resp
    elif (translate_response.lower() == 'thai'):
        resp = str(blob.translate(to='th')) + '\n'
        resp = resp + "Thank you!"
        return resp
    elif (translate_response.lower() == 'russian'):
        resp = str(blob.translate(to='ru')) + '\n'
        resp = resp + "Thank you!"
        return resp
    elif (translate_response.lower() == 'portuguese'):
        resp = str(blob.translate(to='pt')) + '\n'
        resp = resp + "Thank you!"
        return resp
    elif (translate_response.lower() == 'japenese'):
        resp = str(blob.translate(to='ja')) + '\n'
        resp = resp + "Thank you!"
        return resp
    elif (translate_response.lower() == 'greek'):
        resp = str(blob.translate(to='el')) + '\n'
        resp = resp + "Thank you!"
        return resp
    elif (translate_response.lower() == 'german'):
        resp = str(blob.translate(to='de')) + '\n'
        resp = resp + "Thank you!"
        return resp
    elif (translate_response.lower() == 'french'):
        resp = str(blob.translate(to='fr')) + '\n'
        resp = resp + "Thank you!"
        return resp
    elif (translate_response.lower() == 'dutch'):
        resp = str(blob.translate(to='nl')) + '\n'
        resp = resp + "Thank you!"
        return resp
Пример #28
0
    def evaluate(self, file_to_evaluate: str, classifier):
        data = None
        try:
            with open(
                    "./backend/data/self_annotated/" + file_to_evaluate +
                    ".csv", "r") as csvfile:
                data = list(csv.reader(csvfile))
        except Exception as e:
            print(e)

        custom_values, ground_values, movie_values = [], [], []
        for i in trange(len(data)):
            # sentence
            datum = data[i]

            # values
            # - custom
            ground_values.append(int(datum[1]))

            # - annotate
            custom_values.append(int(classifier.classify(str(datum[0]))))

            blob = TextBlob(datum[0], analyzer=NaiveBayesAnalyzer())
            blob_values = []
            blob_values.append(blob.sentiment.p_pos)
            blob_values.append(blob.sentiment.p_neg)

            if blob_values[0] >= 0.5 and blob_values[1] < 0.5:
                movie_values.append(1)
            elif blob_values[1] >= 0.5 and blob_values[0] < 0.5:
                movie_values.append(3)
            else:
                movie_values.append(2)

        accuracy_custom, precision_custom, recall_custom = self.multi_precision_recall(
            ground_values, custom_values)
        accuracy_movie, precision_movie, recall_movie = self.multi_precision_recall(
            ground_values, movie_values)

        with open(
                "./backend/data/self_annotated/evaluation_" +
                file_to_evaluate + ".csv", "w") as file:
            file.write("acc_custom, acc_movie\n")
            file.write(f"{accuracy_custom}, {accuracy_movie}\n")
            file.write(
                "pre_custom_1, pre_custom_2, pre_custom_3, pre_movie_1, pre_movie_2, pre_movie_3\n"
            )
            file.write(
                f"{precision_custom[0]}, {precision_custom[1]}, {precision_custom[2]}, {precision_movie[0]}, {precision_movie[1]}, {precision_movie[2]}\n"
            )
            file.write(
                "rec_custom_1, rec_custom_2, rec_custom_3, rec_movie_1, rec_movie_2, rec_movie_3\n"
            )
            file.write(
                f"{recall_custom[0]}, {recall_custom[1]}, {recall_custom[2]}, {recall_movie[0]}, {recall_movie[1]}, {recall_movie[2]}"
            )
Пример #29
0
    def __init__(self, tweet_list):
        pattern = re.compile(r'https:\/\/[\w\d\.\-\/]+')
        self.tweet_list = []

        for status in tweet_list:
            # remove URLs from tweet
            cleaned_status = re.sub(pattern, '', status["full_text"])
            self.tweet_list.append(cleaned_status)

        self.blobber = Blobber(analyzer=NaiveBayesAnalyzer())
Пример #30
0
def sentimentAnalysis(comment):
    """
    Capture the sentiment of a comment
    """
    commentSentiment = TextBlob(cleanStopWords(str(comment)),
                                analyzer=NaiveBayesAnalyzer())
    # print('Inside function commentSentiment:',commentSentiment, '\nsentiment', commentSentiment.sentiment, '\npolarity', commentSentiment.polarity,
    #        '\nsubjectivity',
    #        commentSentiment.subjectivity, '\n=============')
    return commentSentiment
Пример #31
0
def get_song_feelings(song_id, song_lyric):
	# for each row / song, grab id and lyrics
	if not isinstance(song_lyric, float):
		song_sentiment = TextBlob(song_lyric, analyzer=NaiveBayesAnalyzer()).sentiment
		song_feeling = song_sentiment[0]
		
		add_song_to_sentiment_list(song_id, song_feeling)
		return song_feeling
	add_song_to_sentiment_list(song_id, "pos")
	return "pos"
Пример #32
0
class TestNaiveBayesAnalyzer(unittest.TestCase):

    def setUp(self):
        self.analyzer = NaiveBayesAnalyzer()

    def test_kind(self):
        assert_equal(self.analyzer.kind, DISCRETE)

    @attr('slow')
    def test_analyze(self):
        p1 = 'I feel great this morning.'
        n1 = 'This is a terrible car.'
        p1_result = self.analyzer.analyze(p1)
        assert_equal(p1_result[0], 'pos')
        assert_equal(self.analyzer.analyze(n1)[0], 'neg')
        # The 2nd item should be the probability that it is positive
        assert_true(isinstance(p1_result[1], float))
        # 3rd item is probability that it is negative
        assert_true(isinstance(p1_result[2], float))
        assert_about_equal(p1_result[1] + p1_result[2], 1)
        self.angle = np.deg2rad(np.r_[angles, angles[0]])
        self.ranges = ranges
        self.ax = axes[0]
    def plot(self, data, *args, **kw):
        sdata = _scale_data(data, self.ranges)
        self.ax.plot(self.angle, np.r_[sdata, sdata[0]], *args, **kw)
    def fill(self, data, *args, **kw):
        sdata = _scale_data(data, self.ranges)
        self.ax.fill(self.angle, np.r_[sdata, sdata[0]], *args, **kw)


if __name__ == "__main__":

    start = timeit.default_timer()
    # init the analyzers
    analyzerBayes = NaiveBayesAnalyzer()
    analyzerPattern = PatternAnalyzer()

    #training first
    resultBayes = analyzerBayes.analyze("train this")
    resultPattern = analyzerPattern.analyze("train this")

    sc = SparkContext(appName="MovieSentiment")

    # map reduce
    lines = sc.textFile("movieData.txt")

    posNneg = lines.map(sentimentAnalysis) \
                   .reduceByKey(lambda a, b: (a[0] + b[0], a[1] + b[1]))

    output = posNneg.collect()
Пример #34
0
 def setUp(self):
     self.analyzer = NaiveBayesAnalyzer()