-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.rb
68 lines (56 loc) · 1.68 KB
/
Point.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
class Point
attr_accessor :original_tweet, :word_array, :cluster
@@counter = 0
# Constructor that takes in a hash containing attributes and there properties as key and value respectively.
def self.get_counter
@@counter
end
def initialize(tweet=nil)
if !tweet.nil?
@original_tweet = tweet.original_tweet
@word_array = tweet.word_array.sort{|a,b| a.idf <=> b.idf}
@word_array.delete_if {|w| !w.pos.match(/NN.*|VB.*/) }
else
@original_tweet = nil
@word_array = nil
end
end
# Calculates the distance to Point p
def dist_to(operand,k=3)
matches = 0
@@counter += 1
temp_hash = Hash.new
@word_array[0..k-1].each {|word| temp_hash[word.word] = 1}
operand.word_array[0..k-1].each {|word| matches += 1 if !temp_hash[word.word].nil? }
#min_possible_count = [k, @word_array[0..k-1].length, operand.word_array[0..k-1].length].max
dist = k - matches
#puts "#{@word_array[0..k-1]}\n#{operand.word_array[0..k-1]}\n#{dist}"
return dist
end
# Overload operator ==
def ==(op2)
return true if @original_tweet == op2.original_tweet
return false
end
#Class function for average of a given number of tweets
def self.avg(input_array,k=3)
len = input_array.length
least_sum = nil
center_point = nil
(0..len - 1).each do |i|
sum = 0
(0..len - 1).each do |j|
sum += input_array[i].dist_to(input_array[j],k)
end
if least_sum.nil? or sum < least_sum
least_sum = sum
center_point = input_array[i]
end
end
return center_point
end
# Return a String representation of the object
def to_s
return "#{@original_tweet}"
end
end