Class: LooseTightDictionary
- Inherits:
-
Object
- Object
- LooseTightDictionary
- Includes:
- Amatch
- Defined in:
- lib/loose_tight_dictionary.rb
Defined Under Namespace
Classes: FalsePositive, I, Mismatch, MissedChecks, T
Instance Attribute Summary collapse
-
#blocking_only ⇒ Object
Returns the value of attribute blocking_only.
-
#case_sensitive ⇒ Object
readonly
Returns the value of attribute case_sensitive.
-
#left_reader ⇒ Object
Returns the value of attribute left_reader.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#negatives ⇒ Object
Returns the value of attribute negatives.
-
#positives ⇒ Object
Returns the value of attribute positives.
-
#right_reader ⇒ Object
Returns the value of attribute right_reader.
-
#right_records ⇒ Object
readonly
Returns the value of attribute right_records.
-
#tee ⇒ Object
Returns the value of attribute tee.
-
#tee_format ⇒ Object
Returns the value of attribute tee_format.
Instance Method Summary collapse
- #blocking(str) ⇒ Object
- #blocking_only? ⇒ Boolean
-
#cart_prod(*args) ⇒ Object
Thanks William James! www.ruby-forum.com/topic/95519#200484.
- #check(left_records) ⇒ Object
- #collision?(i_options_left, i_options_right) ⇒ Boolean
- #i_options(str) ⇒ Object
-
#initialize(right_records, options = {}) ⇒ LooseTightDictionary
constructor
A new instance of LooseTightDictionary.
- #inline_check(left_record, right_record) ⇒ Object
- #left_to_right(left_record) ⇒ Object
- #literal_regexp(str) ⇒ Object
- #optimize(t_options_left, t_options_right) ⇒ Object
- #read_left(left_record) ⇒ Object
- #read_right(right_record) ⇒ Object
- #t_options(str) ⇒ Object
Constructor Details
#initialize(right_records, options = {}) ⇒ LooseTightDictionary
Returns a new instance of LooseTightDictionary.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/loose_tight_dictionary.rb', line 57 def initialize(right_records, = {}) @right_records = right_records @_raw_tightenings = [:tightenings] || Array.new @_raw_identities = [:identities] || Array.new @_raw_blockings = [:blockings] || Array.new @left_reader = [:left_reader] @right_reader = [:right_reader] @positives = [:positives] @negatives = [:negatives] @logger = [:logger] @tee = [:tee] @tee_format = [:tee_format] || :fixed_width @case_sensitive = [:case_sensitive] || false @blocking_only = [:blocking_only] || false end |
Instance Attribute Details
#blocking_only ⇒ Object
Returns the value of attribute blocking_only.
55 56 57 |
# File 'lib/loose_tight_dictionary.rb', line 55 def blocking_only @blocking_only end |
#case_sensitive ⇒ Object (readonly)
Returns the value of attribute case_sensitive.
46 47 48 |
# File 'lib/loose_tight_dictionary.rb', line 46 def case_sensitive @case_sensitive end |
#left_reader ⇒ Object
Returns the value of attribute left_reader.
53 54 55 |
# File 'lib/loose_tight_dictionary.rb', line 53 def left_reader @left_reader end |
#logger ⇒ Object
Returns the value of attribute logger.
48 49 50 |
# File 'lib/loose_tight_dictionary.rb', line 48 def logger @logger end |
#negatives ⇒ Object
Returns the value of attribute negatives.
52 53 54 |
# File 'lib/loose_tight_dictionary.rb', line 52 def negatives @negatives end |
#positives ⇒ Object
Returns the value of attribute positives.
51 52 53 |
# File 'lib/loose_tight_dictionary.rb', line 51 def positives @positives end |
#right_reader ⇒ Object
Returns the value of attribute right_reader.
54 55 56 |
# File 'lib/loose_tight_dictionary.rb', line 54 def right_reader @right_reader end |
#right_records ⇒ Object (readonly)
Returns the value of attribute right_records.
45 46 47 |
# File 'lib/loose_tight_dictionary.rb', line 45 def right_records @right_records end |
#tee ⇒ Object
Returns the value of attribute tee.
49 50 51 |
# File 'lib/loose_tight_dictionary.rb', line 49 def tee @tee end |
#tee_format ⇒ Object
Returns the value of attribute tee_format.
50 51 52 |
# File 'lib/loose_tight_dictionary.rb', line 50 def tee_format @tee_format end |
Instance Method Details
#blocking(str) ⇒ Object
294 295 296 297 298 299 300 301 302 303 |
# File 'lib/loose_tight_dictionary.rb', line 294 def blocking(str) return @_blocking[str] if @_blocking.andand.has_key?(str) @_blocking ||= Hash.new blockings.each do |regexp| if regexp.match str return @_blocking[str] = regexp end end @_blocking[str] = nil end |
#blocking_only? ⇒ Boolean
87 88 89 |
# File 'lib/loose_tight_dictionary.rb', line 87 def blocking_only? !!blocking_only end |
#cart_prod(*args) ⇒ Object
Thanks William James! www.ruby-forum.com/topic/95519#200484
339 340 341 342 343 344 345 |
# File 'lib/loose_tight_dictionary.rb', line 339 def cart_prod(*args) args.inject([[]]){|old,lst| new = [] lst.each{|e| new += old.map{|c| c.dup << e }} new } end |
#check(left_records) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/loose_tight_dictionary.rb', line 120 def check(left_records) header = [ 'Left record (input)', 'Right record (output)', 'Prefix used (if any)', 'Score' ] case tee_format when :csv tee.andand.puts header.flatten.to_csv when :fixed_width tee.andand.puts header.map { |i| i.to_s.ljust(30) }.join end left_records.each do |left_record| begin right_record = left_to_right left_record ensure case tee_format when :csv tee.andand.puts $ltd_1.flatten.to_csv when :fixed_width tee.andand.puts $ltd_1.map { |i| i.to_s.ljust(30) }.join if $ltd_1 end end end end |
#collision?(i_options_left, i_options_right) ⇒ Boolean
274 275 276 277 278 279 280 |
# File 'lib/loose_tight_dictionary.rb', line 274 def collision?(, ) .any? do |r_left| .any? do |r_right| r_left.regexp == r_right.regexp and r_left.identity != r_right.identity end end end |
#i_options(str) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/loose_tight_dictionary.rb', line 282 def (str) return @_i_options[str] if @_i_options.andand.has_key?(str) @_i_options ||= Hash.new ary = Array.new identities.each do |regexp| if regexp.match str ary.push I.new(regexp, str, case_sensitive) end end @_i_options[str] = ary end |
#inline_check(left_record, right_record) ⇒ Object
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 |
# File 'lib/loose_tight_dictionary.rb', line 91 def inline_check(left_record, right_record) return unless positives.present? or negatives.present? left = read_left left_record right = read_right right_record if positive_record = positives.andand.detect { |record| record[0] == left } correct_right = positive_record[1] if correct_right.blank? and right.present? logger.andand.debug " Mismatch! (should match SOMETHING)" raise Mismatch elsif right != correct_right logger.andand.debug " Mismatch! (should be #{correct_right})" raise Mismatch end end if negative_record = negatives.andand.detect { |record| record[0] == left } incorrect_right = negative_record[1] if incorrect_right.blank? and right.present? logger.andand.debug " False positive! (should NOT match ANYTHING)" raise FalsePositive elsif right == incorrect_right logger.andand.debug " False positive! (should NOT be #{incorrect_right})" raise FalsePositive end end end |
#left_to_right(left_record) ⇒ Object
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/loose_tight_dictionary.rb', line 143 def left_to_right(left_record) left = read_left left_record blocking_left = blocking left return if blocking_only? and blocking_left.nil? = left = left history = Hash.new right_record = right_records.select do |right_record| right = read_right right_record blocking_right = blocking right (not blocking_left and not blocking_right) or (blocking_right and blocking_right.match(left)) or (blocking_left and blocking_left.match(right)) end.max do |a_record, b_record| a = read_right a_record b = read_right b_record = a = b collision_a = collision? , collision_b = collision? , if collision_a and collision_b # neither would ever work, so randomly rank one over the other rand(2) == 1 ? -1 : 1 elsif collision_a -1 elsif collision_b 1 else t_left_a, t_right_a = optimize , (a) t_left_b, t_right_b = optimize , (b) a_prefix, a_score = t_left_a.prefix_and_score t_right_a b_prefix, b_score = t_left_b.prefix_and_score t_right_b history[a_record] = [t_left_a.tightened_str, t_right_a.tightened_str, a_prefix ? a_prefix : 'NULL', a_score] history[b_record] = [t_left_b.tightened_str, t_right_b.tightened_str, b_prefix ? b_prefix : 'NULL', b_score] yep_dd = ($ltd_dd_right and $ltd_dd_left and [t_left_a, t_left_b].any? { |f| f.str =~ $ltd_dd_left } and [t_right_a, t_right_b].any? { |f| f.str =~ $ltd_dd_right } and (!$ltd_dd_left_not or [t_left_a, t_left_b].none? { |f| f.str =~ $ltd_dd_left_not })) if $ltd_dd_print and yep_dd logger.andand.debug t_left_a.inspect logger.andand.debug t_right_a.inspect logger.andand.debug t_left_b.inspect logger.andand.debug t_right_b.inspect logger.andand.debug end z = 1 debugger if yep_dd z = 1 if a_score != b_score a_score <=> b_score elsif a_prefix and b_prefix and a_prefix != b_prefix a_prefix <=> b_prefix else b.length <=> a.length end end end $ltd_1 = history[right_record] right = read_right right_record = right z = 1 debugger if $ltd_left.andand.match(left) or $ltd_right.andand.match(right) z = 1 if collision? , $ltd_0 = nil return else $ltd_0 = right_record end inline_check left_record, right_record right_record end |
#literal_regexp(str) ⇒ Object
305 306 307 308 309 310 311 312 313 |
# File 'lib/loose_tight_dictionary.rb', line 305 def literal_regexp(str) return @_literal_regexp[str] if @_literal_regexp.andand.has_key? str @_literal_regexp ||= Hash.new = str.split('/').last ignore_case = (!case_sensitive or .include?('i')) ? Regexp::IGNORECASE : nil multiline = .include?('m') ? Regexp::MULTILINE : nil extended = .include?('x') ? Regexp::EXTENDED : nil @_literal_regexp[str] = Regexp.new str.gsub(/\A\/|\/([ixm]*)\z/, ''), (ignore_case||multiline||extended) end |
#optimize(t_options_left, t_options_right) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/loose_tight_dictionary.rb', line 217 def optimize(, ) cart_prod(, ).max do |a, b| t_left_a, t_right_a = a t_left_b, t_right_b = b a_prefix, a_score = t_left_a.prefix_and_score t_right_a b_prefix, b_score = t_left_b.prefix_and_score t_right_b yep_ddd = ($ltd_ddd_right and $ltd_ddd_left and [t_left_a, t_left_b].any? { |f| f.str =~ $ltd_ddd_left } and [t_right_a, t_right_b].any? { |f| f.str =~ $ltd_ddd_right } and (!$ltd_ddd_left_not or [t_left_a, t_left_b].none? { |f| f.str =~ $ltd_ddd_left_not })) if $ltd_ddd_print and yep_ddd logger.andand.debug t_left_a.inspect logger.andand.debug t_right_a.inspect logger.andand.debug t_left_b.inspect logger.andand.debug t_right_b.inspect logger.andand.debug end z = 1 debugger if yep_ddd z = 1 if a_score != b_score a_score <=> b_score elsif a_prefix and b_prefix and a_prefix != b_prefix a_prefix <=> b_prefix else # randomly choose # maybe later i can figure out how big the inputs are and apply occam's razor rand(2) == 1 ? -1 : 1 end end end |
#read_left(left_record) ⇒ Object
315 316 317 318 319 320 321 322 323 324 |
# File 'lib/loose_tight_dictionary.rb', line 315 def read_left(left_record) return if left_record.nil? if left_reader left_reader.call(left_record) elsif left_record.is_a?(String) left_record else left_record[0] end end |
#read_right(right_record) ⇒ Object
326 327 328 329 330 331 332 333 334 335 |
# File 'lib/loose_tight_dictionary.rb', line 326 def read_right(right_record) return if right_record.nil? if right_reader right_reader.call(right_record) elsif right_record.is_a?(String) right_record else right_record[0] end end |
#t_options(str) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/loose_tight_dictionary.rb', line 251 def (str) return @_t_options[str] if @_t_options.andand.has_key?(str) @_t_options ||= Hash.new ary = Array.new ary.push T.new(str, str) tightenings.each do |regexp| if match_data = regexp.match(str) ary.push T.new(str, match_data.captures.compact.join) end end @_t_options[str] = ary end |