Class: Sexp

Inherits:
Object
  • Object
show all
Defined in:
lib/flay.rb

Instance Method Summary collapse

Instance Method Details

#all_subhashesObject



331
332
333
334
335
336
337
# File 'lib/flay.rb', line 331

def all_subhashes
  hashes = []
  self.deep_each do |node|
    hashes << node.fuzzy_hash
  end
  hashes
end

#compare_to(they) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/flay.rb', line 305

def compare_to they
  l = s = r = 0

  l_sexp, l_lits = self.partition { |o| Sexp === o }
  r_sexp, r_lits = they.partition { |o| Sexp === o }

  l += (l_lits - r_lits).size
  s += (l_lits & r_lits).size
  r += (r_lits - l_lits).size

  # TODO: I think this is wrong, since it isn't positional. What to do?
  l_sexp.zip(r_sexp).each do |l_sub, r_sub|
    next unless l_sub && r_sub # HACK
    l2, s2, r2 = l_sub.compare_to r_sub
    l += l2
    s += s2
    r += r2
  end

  return l, s, r
end

#deep_each(&block) ⇒ Object



339
340
341
342
343
344
# File 'lib/flay.rb', line 339

def deep_each(&block)
  self.each_sexp do |sexp|
    block[sexp]
    sexp.deep_each(&block)
  end
end

#each_sexpObject



346
347
348
349
350
351
352
# File 'lib/flay.rb', line 346

def each_sexp
  self.each do |sexp|
    next unless Sexp === sexp

    yield sexp
  end
end

#fuzzy_hashObject



327
328
329
# File 'lib/flay.rb', line 327

def fuzzy_hash
  @fuzzy_hash ||= self.structure.hash
end

#massObject



291
292
293
# File 'lib/flay.rb', line 291

def mass
  @mass ||= self.structure.flatten.size
end

#similarity(o) ⇒ Object



300
301
302
303
# File 'lib/flay.rb', line 300

def similarity o
  l, s, r = self.compare_to o
  (2.0 * s) / (2.0 * s + l + r)
end

#structureObject



296
297
298
# File 'lib/flay.rb', line 296

def structure
  @structure ||= self.uncached_structure
end

#uncached_structureObject



295
# File 'lib/flay.rb', line 295

alias :uncached_structure :structure