| Class | RegexList |
| In: |
lib/regex_list.rb
|
| Parent: | Object |
| Copyright: | Copyright (c) 2005 Nicolas Pouillard. All rights reserved. |
| Author: | Nicolas Pouillard <ertai@lrde.epita.fr>. |
| License: | Gnu General Public License. |
| Revision: | $Id: /w/fey/ruby_ex/trunk/lib/regex_list.rb 21865 2006-02-18T17:13:28.680350Z pouillar $ |
# File lib/regex_list.rb, line 8 def initialize ( *args ) @regexps = args.flatten @regexps.map! { |re| (re.is_a? Regexp)? re : Regexp.new(re) } @regexps.reverse! @scores = {} end
# File lib/regex_list.rb, line 19 def score ( aString ) if @scores.has_key? aString result = @scores[aString] action = '<%= color "Hit ", :green %>' else result = score_without_cache(aString) action = '<%= color "Miss", :red %>' @scores[aString] = result end @@h.say "#{action}: #{aString} -> #{result.inspect}" result end
Perform a cache over score_without_cache
# File lib/regex_list.rb, line 35 def score ( aString ) return @scores[aString] if @scores.has_key? aString return @scores[aString] = score_without_cache(aString) end
Score represent a weighted sum of the match result with each regexp.
0: Not matched at all 1: Just the last regexp matches (the Nth regexp) 2: Just the (N - 1)th matches 3: The Nth and the (N - 1)th 2^(N - 1): Just the first one matches
Formula:
U(i <- 0 .. N - 1) is a vector with 1 when it matches and 0 otherwise Score = U(N - 1) * 2^(N - 1) + U(N - 2) * 2^(N - 2) + ... + U(0) * 2^0
# File lib/regex_list.rb, line 53 def score_without_cache ( aString ) result = 0 @regexps.each_with_index do |re, i| result += 2 ** i if aString =~ re end return result end