Class: RegexList

Inherits:
Object show all
Defined in:
lib/regex_list.rb

Overview

Copyright

Copyright © 2005 Nicolas Pouillard. All rights reserved.

Author

Nicolas Pouillard <[email protected]>.

License

Gnu General Public License.

Revision

$Id: /w/fey/ruby_ex/trunk/lib/regex_list.rb 21865 2006-02-18T17:13:28.680350Z pouillar $

Defined Under Namespace

Modules: Assertions, PathListExtension

Constant Summary collapse

@@h =
HighLine.new

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RegexList

Returns a new instance of RegexList.



8
9
10
11
12
13
# 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

Instance Method Details

#[](score) ⇒ Object



61
62
63
# File 'lib/regex_list.rb', line 61

def [] ( score )
  @regexps[score]
end

#score(aString) ⇒ Object

Perform a cache over score_without_cache



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/regex_list.rb', line 35

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

#score_without_cache(aString) ⇒ Object

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


53
54
55
56
57
58
59
# 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