-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.rb
184 lines (161 loc) · 5.66 KB
/
Main.rb
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# coding: utf-8
require 'redis'
require 'json'
require 'rubygems'
#User Defined
require 'WekaWrapper'
require 'TFIDFWrapper'
require 'RedisWrapper'
require 'FileWrapper'
require 'Proximity'
require 'PrintData'
require 'DataPoint'
require 'TwitterWrapper'
require 'Sentiment'
require 'Cluster'
require 'Point'
require 'Clusterer'
require 'Summary'
require 'NLP'
require 'rarff'
#Extending class array with a sum function.
module Enumerable
def sum
self.inject(0){|accum, i| accum + i }
end
def mean
self.sum/self.length.to_f
end
def sample_variance(mean)
return 1/0.0 if self.length <= 1
m = mean || self.mean
sum = self.inject(0){|accum, i| accum +(i-m)**2 }
sum/(self.length - 1).to_f
end
def standard_deviation(mean)
return Math.sqrt(self.sample_variance(mean))
end
end
#The main function
def main_function()
#Assigning command line variables
username = ARGV[1]
source = ARGV[0]
tweets = Array.new
if source == "twitter"
#Get user tweets from twitter
twitter = TwitterWrapper.new
tweets_json = twitter.user_tweets(username,100)
tweets = twitter.json_to_tweets(tweets_json)
elsif source == "redis"
redis = RedisWrapper.new
tweet_list = redis.redis_client.lrange username+"_tweets", 0, -1
tweet_list.each do |t|
tweets.push(JSON.parse(t)['text'])
end
chosen_lists = redis.redis_client.lrange "chosen_"+username+"_tweets", 0 , -1
user_tweets_file = File.new("Inputs/User_Tweets.txt","w+")
for list in chosen_lists
user_tweets_file << JSON.parse(list).join("\n")
user_tweets_file << "\n\n"
end
else
#Get tweets from a file in Inputs folder
twitter = FileWrapper.new()
twitter.filename = "Inputs/"+username+".txt"
tweets = twitter.get_tweets
end
#Filter retweets and reply. Just remove them from the list of tweets
tweets = tweets.delete_if {|tweet| tweet.is_reply? or tweet.is_retweet?}
puts "Tweets Returned Just fine" if !tweets.nil?
#Populating the word array of each tweet
tfidf = TFIDFWrapper.new(tweets)
proximity = Proximity.new
sentiment = Sentiment.new
tweet_index = 0
max = tweets.length #Highest proximity number possible.
for tweet in tweets
idf_array = tfidf.idf_sentence(tweet.processed_tweet)
proximity_array = proximity.proximity_sentence(tweet.processed_tweet,tweet_index,max)
sentiment_array = sentiment.sentiment_sentence(tweet.processed_tweet)
pos_array = NLP.pos_sentence(tweet)
i = 0 #Number of words
tweet.processed_tweet.split().uniq.each do |w|
temp = Word.new
temp.word = w
temp.idf = idf_array[i]
temp.proximity = proximity_array[i]
temp.sentiment = sentiment_array[i]
temp.pos = pos_array[i]
tweet.word_array << temp
i += 1
end
#Updating stuff in the loop
proximity.update_proximity(tweet.processed_tweet,tweet_index)
tweet_index += 1 #To maintain the tweet number for proximity.
end
#Filter all tweets if they have word_array as nil
all_points = Array.new
i = 0
tweets.each do |tweet|
if tweet.word_array.length >= 3
t = Point.new(tweet)
all_points << t
i += 1
end
end
input_tweets = all_points.clone
#Clustering using LDA here.
wk = WekaWrapper.new
wk.points_to_arff(all_points)
wk.run_em
wk.get_clusters(all_points)
final_clusters = Clusterer.map_to_clusters(all_points)
#Output part
output_filename = "Results/#{username}_em_results.txt"
filewrapper = FileWrapper.new
filewrapper.filename = "Inputs/User_Tweets.txt"
tweets_list = filewrapper.get_multi_user_tweets()
Clusterer.cluster_user_correlation("Results/#{username}_em_correlation.txt",final_clusters,tweets_list)
Clusterer.print_to(output_filename,input_tweets,final_clusters)
Clusterer.append_to(output_filename,"Max #words summary",Summary.simple_summary(final_clusters))
Clusterer.append_to(output_filename,"Cluster Center Summary",Summary.center_summary(final_clusters))
Clusterer.append_to(output_filename,"Highest Sentiment Summary",Summary.sentiment_summary(final_clusters))
Clusterer.append_to(output_filename,"Random Generated Summary",Summary.random_summary(input_tweets))
final_clusters = Array.new
i = 2
params_length = 3
while !all_points.empty?
clusters = Clusterer.kmeans(all_points,i,params_length)
flag = 0
all_points = Array.new
for cluster in clusters
if cluster.size <= 7 #This is the number limiting the final cluster size.
final_clusters << cluster
flag = 1
else
all_points += cluster.points
end
end
if flag == 0
i += 1
else
params_length += 1
end
puts "#{Time.now} #{final_clusters.length}"
puts "#{Point.get_counter}"
end
puts "Clustering done fine"
output_filename = "Results/#{username}_kmeans_results.txt"
Clusterer.cluster_user_correlation("Results/#{username}_kmeans_correlation.txt",final_clusters,tweets_list)
Clusterer.print_to(output_filename,input_tweets,final_clusters)
Clusterer.append_to(output_filename,"Max #words summary",Summary.simple_summary(final_clusters))
Clusterer.append_to(output_filename,"Cluster Center Summary",Summary.center_summary(final_clusters))
Clusterer.append_to(output_filename,"Highest Sentiment Summary",Summary.sentiment_summary(final_clusters))
Clusterer.append_to(output_filename,"Random Generated Summary",Summary.random_summary(input_tweets))
#puts "Cluster Centers"
#final_clusters.each {|cluster| puts "#{cluster.center} #{cluster.sd(params_length)}" if cluster.points.length > 1}
#Print tweets to a csv
PrintData.print_csv(input_tweets,"Results/#{username}_data.csv",tweets_list)
end
main_function()