-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfidf.py
172 lines (118 loc) · 4.53 KB
/
tfidf.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
import nltk
import json
import string
from tweepy import API
import sys
import operator
import nltk.corpus
from keys import *
from time import sleep
from tweepy import OAuthHandler
from collections import Counter
from nltk.corpus import stopwords
from guess_language import guess_language
import database as db
word_RTcount_list = []
word_Tcount_list = []
word_tfidf_list = []
word_RTcount_dict = {}
word_Tcount_dict = {}
word_tfidf_dict = {}
tweet_tfidf_dict = {}
tweet_wordcount_dict = {}
sys.stdout = open('tfidf_word_output_count2.txt', 'w')
def fetch_word():
Tweet = db.Tweet
session = db.session
tweets = session.query(Tweet).limit(1000)
all_words = ''
for tweet in tweets:
if tweet.text:
#print tweet.text
# Initializing final dictionary {tweet:tfidf total} & {tweet:# of words}
tweet_tfidf_dict[tweet.text] = None
tweet_wordcount_dict[tweet.text] = None
if guess_language(tweet.text) == 'en':
tweet_text = str(tweet.text.encode('utf-8'))
tweet_lower = tweet_text.lower()
tweet_final = tweet_lower.translate(None, string.punctuation)
# Concatenate all words (including hashtags) in tweet
all_words = all_words + ' ' + tweet_final
return all_words
def tokenize(all_words):
tokens = nltk.word_tokenize(all_words)
return tokens
# Eradicate stop words calculate TFIDF values below:
words = fetch_word()
tokens = tokenize(words)
all_words = [w for w in tokens if not w in stopwords.words('english')]
# Query for RT values
Tweet = db.Tweet
session = db.session
tweets = session.query(Tweet).limit(1000)
for tweet in tweets:
if guess_language(tweet.text) == 'en':
tweet_text = str(tweet.text.encode('utf-8'))
# Find unique words (including hashtags) per tweet
tweet_lower = tweet_text.lower()
tweet_final = tweet_lower.translate(None, string.punctuation)
unique_word_list = nltk.word_tokenize(tweet_final)
# Calculate total RTs and Tweets to find TFIDF
for word_in_tweet in unique_word_list:
if word_in_tweet in all_words:
if word_in_tweet in word_RTcount_dict:
if word_RTcount_dict[word_in_tweet] != None:
word_RTcount_dict[word_in_tweet] = word_RTcount_dict[word_in_tweet] + tweet.retweet_count
if word_RTcount_dict[word_in_tweet] > 0:
print 'word_RTcount_dict[word_in_tweet]' + str(word_RTcount_dict[word_in_tweet])
print 'word: ' + word_in_tweet
print 'RTcount: ' + str(word_RTcount_dict[word_in_tweet])
else:
word_RTcount_dict[word_in_tweet] = tweet.retweet_count
#print 'word_RTcount_dict[word_in_tweet]' + str(word_RTcount_dict[word_in_tweet])
if word_in_tweet in word_Tcount_dict:
if word_Tcount_dict[word_in_tweet] != None:
word_Tcount_dict[word_in_tweet] = word_Tcount_dict[word_in_tweet] + 1
#print 'word_Tcount_dict[word_in_tweet]' + ' ' + word_in_tweet + ' ' + str(word_Tcount_dict[word_in_tweet])
else:
word_Tcount_dict[word_in_tweet] = 1
#print 'word_Tcount_dict[word_in_tweet]' + ' ' + word_in_tweet + ' ' + str(word_Tcount_dict[word_in_tweet])
# word_tfidf_dict = {}
# for word in word_Tcount_dict:
# num_RT = None
# num_Tweet = None
# key = ''
# for entry in word_RTcount_dict:
# #print "Key: " + str(entry)
# #print "Retweets: " + str(word_RTcount_dict[entry])
# num_RT = word_RTcount_dict[entry]
# for entry in word_Tcount_dict:
# #print "Key: " + str(entry)
# #print "Tweet Count: " + str(word_Tcount_dict[entry])
# num_Tweet = word_Tcount_dict[entry]
# tfidf_value = num_RT/num_Tweet
# word_tfidf_dict[word] = tfidf_value
# word_tfidf_sorted = sorted(word_tfidf_dict.iteritems(), key=operator.itemgetter(1))
# print 'Word TFIDF-retweet (num_RT/num_Tweet) popularity in descending order:'
# for x in word_tfidf_sorted:
# print x
# Find tweet tfidf TOTAL scores
# Find tweet # of words
# for word in word_tfidf_dict.keys():
# for tweet in tweet_tfidf_dict.keys():
# if word in tweet:
# if tweet_tfidf_dict[tweet] != None:
# tweet_tfidf_dict[tweet] = tweet_tfidf_dict[tweet] + word_tfidf_dict[word]
# tweet_wordcount_dict[tweet] = tweet_wordcount_dict[tweet] + 1
# else:
# tweet_tfidf_dict[tweet] = word_tfidf_dict[word]
# tweet_wordcount_dict[tweet] = 1
# # Calculates avg and sorts
# for tweet in tweet_tfidf_dict.keys():
# tweet_tfidf_dict[tweet] = tweet_tfidf_dict[tweet]/tweet_wordcount_dict[tweet]
# tweet_tfidf_sorted = {}
# tweet_tfidf_sorted = sorted(tweet_tfidf_sorted.iteritems(), key=operator.itemgetter(1))
# print 'Tweet and corresponding TFIDF scores in descending order:'
# for x in tweet_tfidf_sorted:
# print x