Class: HarmonizerRedis::Linkage
- Inherits:
-
BaseObject
- Object
- BaseObject
- HarmonizerRedis::Linkage
- Defined in:
- lib/harmonizer_redis/linkage.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .find(linkage_id) ⇒ Object
- .get_category_id(linkage_id) ⇒ Object
- .get_content(linkage_id) ⇒ Object
- .get_content_normalized(linkage_id) ⇒ Object
- .get_phrase_id(linkage_id) ⇒ Object
- .is_linkage_saved?(linkage_id) ⇒ Boolean
Instance Method Summary collapse
-
#calculate_similarities ⇒ Object
Functionality.
- #category_id ⇒ Object
- #category_id=(value) ⇒ Object
-
#content ⇒ Object
Readers.
-
#content=(value) ⇒ Object
Writers.
- #content_normalized ⇒ Object
- #corrected ⇒ Object
- #generate_id ⇒ Object
- #get_similarities(num_phrases = 20) ⇒ Object
-
#initialize(params = {}) ⇒ Linkage
constructor
A new instance of Linkage.
- #is_calculated? ⇒ Boolean
-
#is_category_changed? ⇒ Boolean
Helpers.
- #is_saved? ⇒ Boolean
- #merge_with_phrase(phrase_id) ⇒ Object
- #phrase_id ⇒ Object
-
#recommend_labels ⇒ Object
Recommend possible labels for a linkage.
-
#save ⇒ Object
make sure that new phrase is saved.
- #set_corrected_label(label) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Linkage
Returns a new instance of Linkage.
9 10 11 12 |
# File 'lib/harmonizer_redis/linkage.rb', line 9 def initialize(params={}) @content = params[:content] @category_id = params[:category_id] end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/harmonizer_redis/linkage.rb', line 3 def id @id end |
Class Method Details
.find(linkage_id) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/harmonizer_redis/linkage.rb', line 158 def find(linkage_id) unless is_linkage_saved?(linkage_id) return nil end linkage = self.new linkage.instance_variable_set('@id', linkage_id) linkage end |
.get_category_id(linkage_id) ⇒ Object
171 172 173 |
# File 'lib/harmonizer_redis/linkage.rb', line 171 def get_category_id(linkage_id) Redis.current.get("#{self}:#{linkage_id}:category_id") end |
.get_content(linkage_id) ⇒ Object
179 180 181 |
# File 'lib/harmonizer_redis/linkage.rb', line 179 def get_content(linkage_id) Redis.current.get("#{self}:#{linkage_id}:content") end |
.get_content_normalized(linkage_id) ⇒ Object
183 184 185 |
# File 'lib/harmonizer_redis/linkage.rb', line 183 def get_content_normalized(linkage_id) Redis.current.get("#{self}:#{linkage_id}:content_normalized") end |
.get_phrase_id(linkage_id) ⇒ Object
175 176 177 |
# File 'lib/harmonizer_redis/linkage.rb', line 175 def get_phrase_id(linkage_id) Redis.current.get("#{self}:#{linkage_id}:phrase") end |
.is_linkage_saved?(linkage_id) ⇒ Boolean
167 168 169 |
# File 'lib/harmonizer_redis/linkage.rb', line 167 def is_linkage_saved?(linkage_id) Redis.current.sismember("#{self}:set", "#{linkage_id}") end |
Instance Method Details
#calculate_similarities ⇒ Object
Functionality
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/harmonizer_redis/linkage.rb', line 88 def calculate_similarities own_phrase_id = phrase_id own_cat_id = category_id phrase_list = Category.get_phrase_list(own_cat_id) matrix_list = Category.get_matrices(category_id, phrase_list) own_matrix = Phrase.get_matrix(own_phrase_id) Redis.current.pipelined do phrase_list.each_with_index do |other_id, index| score = Phrase.calc_soft_pair_similarity(own_matrix, matrix_list[index]) if score > 0.2 Redis.current.zadd("HarmonizerRedis::Category:#{own_cat_id}:#{own_phrase_id}:sims", score, other_id) end end end Category.set_phrase_calculated(own_cat_id, own_phrase_id, 1) Category.reset_changed(own_cat_id) end |
#category_id ⇒ Object
48 49 50 |
# File 'lib/harmonizer_redis/linkage.rb', line 48 def category_id @category_id ||= self.class.get_category_id(@id) end |
#category_id=(value) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/harmonizer_redis/linkage.rb', line 79 def category_id=(value) if self.is_saved? raise "Saved linkage category_id cannot be edited" else @category_id = value end end |
#content ⇒ Object
Readers
40 41 42 |
# File 'lib/harmonizer_redis/linkage.rb', line 40 def content @content ||= self.class.get_content(@id) end |
#content=(value) ⇒ Object
Writers
71 72 73 74 75 76 77 |
# File 'lib/harmonizer_redis/linkage.rb', line 71 def content=(value) if self.is_saved? raise "Saved linkage content cannot be edited" else @content = value end end |
#content_normalized ⇒ Object
44 45 46 |
# File 'lib/harmonizer_redis/linkage.rb', line 44 def content_normalized @content_normalized ||= self.class.get_content_normalized(@id) end |
#corrected ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/harmonizer_redis/linkage.rb', line 52 def corrected label = Category.get_group_label(category_id, phrase_id) if label.nil? if Category.get_group_count(category_id, phrase_id) return content else '(LABEL NOT SET)' end else label end end |
#generate_id ⇒ Object
5 6 7 |
# File 'lib/harmonizer_redis/linkage.rb', line 5 def generate_id SecureRandom.uuid end |
#get_similarities(num_phrases = 20) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/harmonizer_redis/linkage.rb', line 106 def get_similarities(num_phrases = 20) self_phrase_id = phrase_id unless is_calculated? calculate_similarities end phrase_id_list = Redis.current.zrevrange("HarmonizerRedis::Category:#{self.category_id}:#{self_phrase_id}:sims", 0, num_phrases, :with_scores => true) results = [] phrase_id_list.each do |phrase, score| unless Category.in_same_group?(category_id, self_phrase_id, phrase) results << [Phrase.get_content(phrase), Category.get_group_label(category_id, phrase), score, phrase] end end results end |
#is_calculated? ⇒ Boolean
149 150 151 152 153 154 155 |
# File 'lib/harmonizer_redis/linkage.rb', line 149 def is_calculated? if is_category_changed? || !is_saved? false else Category.is_phrase_calculated?(category_id, phrase_id) end end |
#is_category_changed? ⇒ Boolean
Helpers
138 139 140 141 142 143 |
# File 'lib/harmonizer_redis/linkage.rb', line 138 def is_category_changed? unless is_saved? raise "Linkage must be saved first" end Category.changed?(self.category_id) end |
#is_saved? ⇒ Boolean
145 146 147 |
# File 'lib/harmonizer_redis/linkage.rb', line 145 def is_saved? self.class.is_linkage_saved?(@id) end |
#merge_with_phrase(phrase_id) ⇒ Object
129 130 131 |
# File 'lib/harmonizer_redis/linkage.rb', line 129 def merge_with_phrase(phrase_id) Category.merge_phrase_groups(category_id, self.phrase_id, phrase_id) end |
#phrase_id ⇒ Object
65 66 67 |
# File 'lib/harmonizer_redis/linkage.rb', line 65 def phrase_id @phrase ||= self.class.get_phrase_id(@id) end |
#recommend_labels ⇒ Object
Recommend possible labels for a linkage
123 124 125 126 127 |
# File 'lib/harmonizer_redis/linkage.rb', line 123 def recommend_labels existing_labels = Category.get_all_group_labels(category_id, phrase_id) other_linkages = Category.get_group_popular_linkages(category_id, phrase_id) existing_labels + other_linkages end |
#save ⇒ Object
make sure that new phrase is saved
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/harmonizer_redis/linkage.rb', line 14 def save # make sure that new phrase is saved # if phrase already exists : set to that phrase # otherwise : create a new phrase and set linkage:phrase to that phrase # linkage is also added to the category with certain id (can be used to divide tasks) # Assert: all required fields have been set @id = generate_id unless @id && @content && @category_id raise "id, content, and category_id are not all set" end @content_normalized = HarmonizerRedis.normalize_string(@content) existing_phrase_id = HarmonizerRedis::Phrase.find_by_content(@content_normalized) if existing_phrase_id @phrase = existing_phrase_id else new_phrase = HarmonizerRedis::Phrase.new(@content_normalized) new_phrase.save @phrase = new_phrase.id end super() Category.add_linkage(self) end |
#set_corrected_label(label) ⇒ Object
133 134 135 |
# File 'lib/harmonizer_redis/linkage.rb', line 133 def set_corrected_label(label) Category.set_group_label(category_id, phrase_id, label) end |