class PerseusMatch::Cluster

Public Class Methods

new(phrases = [], pm_options = {}, list_options = {}) click to toggle source
Calls superclass method
   # File lib/perseus_match/cluster.rb
35 def initialize(phrases = [], pm_options = {}, list_options = {})
36   super() { |h, k| h[k] = [] }
37 
38   List.pair(phrases, pm_options, list_options) { |pm| add(pm) }
39 end

Public Instance Methods

<<(pm)
Alias for: add
add(pm) click to toggle source
   # File lib/perseus_match/cluster.rb
41 def add(pm)
42   self[pm.phrase] << pm
43 end
Also aliased as: <<
rank(options = {}) click to toggle source
   # File lib/perseus_match/cluster.rb
89 def rank(options = {})
90   coeff = options[:coeff]
91   sort(options) { |match| [match.target, match.similarity(coeff)] }
92 end
sort(options = {}) { |*a| ... } click to toggle source
   # File lib/perseus_match/cluster.rb
84 def sort(options = {})
85   args = [:similarity, options.delete(:coeff), options]
86   block_given? ? sort_by(*args) { |*a| yield(*a) } : sort_by(*args)
87 end
sort_by(attribute, *args) { |match| ... } click to toggle source
   # File lib/perseus_match/cluster.rb
47 def sort_by(attribute, *args)
48   options = args.last.is_a?(Hash) ? args.pop : {}
49 
50   _ = map { |phrase, matches|
51     res = {}
52 
53     matches = matches.sort_by { |match|
54       res[match] = match.send(attribute, *args)
55     }
56 
57     # premise: if any is, then all are (i.e., only first needs checking)
58     numeric = res.any? { |_, r| break r.is_a?(Numeric) }
59 
60     # sort numeric results in reverse order
61     matches.reverse! if numeric
62 
63     if threshold = options[:threshold]
64       condition = numeric ?
65         lambda { |match| res[match] < threshold } :
66         lambda { |match| res[match] > threshold }
67 
68       matches.reject!(&condition)
69     end
70 
71     if limit = options[:limit]
72       matches.slice!(limit..-1) if matches.size > limit
73     end
74 
75     # transform entries if so requested
76     matches.map! { |match| yield(match) } if block_given?
77 
78     [phrase, matches]
79   }.sort
80 
81   _  # rcov hack :-(
82 end