Parent

Namespace

Matcher

Matcher

 Matcher derives from Ruby Quiz #103, the DictionaryMatcher quiz.

Attributes

word_count[R]

Public Class Methods

new() click to toggle source

Create a DictionaryMatcher with no words in it

    # File lib/more/facets/matcher.rb, line 20
20:   def initialize
21:     @trie = {}
22:     @word_count = 0
23:   end

Public Instance Methods

<<(word) click to toggle source
Alias for: add
===(text) click to toggle source

Case equality. Similar to =~.

Alias for: =~
=~(text) click to toggle source

Determines whether one of the words in the DictionaryMatcher is a substring of string. Returns the index of the match if found, nil if not found.

    # File lib/more/facets/matcher.rb, line 83
83:   def =~ text
84:     internal_match(text){|md| return md.index}
85:     nil
86:   end
Also aliased as: ===
add(word) click to toggle source

Add a word to the DictionaryMatcher

    # File lib/more/facets/matcher.rb, line 26
26:   def add(word)
27:     @word_count += 1
28:     container = @trie
29:     containers=[]
30: 
31:     i=0
32:     word.each_byte do |b|
33:       container[b] = {} unless container.has_key? b
34:       container[:depth]=i
35:       containers << container
36:       container = container[b]
37:       i+=1
38:     end
39:     containers << container
40: 
41:     container[0] = true # Mark end of word
42:     container[:depth]=i
43: 
44:     ff=compute_failure_function word
45:     ff.zip(containers).each do |pointto,container|
46:       container[:failure]=containers[pointto] if pointto
47:     end
48: 
49:     self
50: 
51:   end
Also aliased as: <<
include?(word) click to toggle source

Determine whether string was previously added to the Trie.

    # File lib/more/facets/matcher.rb, line 70
70:   def include?(word)
71:     container = @trie
72:     word.each_byte do |b|
73:       break unless container.has_key? b
74:       container = container[b]
75:     end
76:     container[0]
77:   end
inspect() click to toggle source
    # File lib/more/facets/matcher.rb, line 15
15:   def inspect
16:     to_s
17:   end
match(text) click to toggle source

Determine whether one of the words in the DictionaryMatcher is a substring of string. Returns a DictionaryMatcher::MatchData object if found, nil if not #.

    # File lib/more/facets/matcher.rb, line 92
92:   def match text
93:     internal_match(text){|md| return md}
94:     nil
95:   end
scan(text, &block) click to toggle source

Scans string for all occurrances of strings in the DictionaryMatcher. Overlapping matches are skipped (only the first one is yielded), and when some strings in the DictionaryMatcher are substrings of others, only the shortest match at a given position is found.

     # File lib/more/facets/matcher.rb, line 130
130:   def scan(text, &block)
131:     matches=[]
132:     block= lambda{ |md| matches << md } unless block
133:     internal_match(text,&block)
134:     matches
135:   end

Private Instance Methods

compute_failure_function(p) click to toggle source
    # File lib/more/facets/matcher.rb, line 55
55:   def compute_failure_function p
56:     m=p.size
57:     pi=[nil,0]
58:     k=0
59:     2.upto m do |q|
60:       k=pi[k] while k>0 and p[k] != p[q-1]
61:       k=k+1 if p[k]==p[q-1]
62:       pi[q]=k
63:     end
64:     pi
65:   end
internal_match(string) click to toggle source
     # File lib/more/facets/matcher.rb, line 97
 97:   def internal_match string
 98:       node=@trie
 99:       pos=0
100:       string.each_byte do |b|
101:          advance=false
102:          until advance
103:             nextnode=node[b]
104:             if not nextnode
105:                if node[:failure]
106:                   node=node[:failure]
107:                else
108:                   advance=true
109:                end
110:             elsif nextnode[0]
111:                yield MatchData.new(pos, string[pos+1-nextnode[:depth],nextnode[:depth]])
112:                advance=true
113:                node=@trie
114:             else
115:                advance=true
116:                node=nextnode
117:             end
118:             pos+=1
119:          end
120:       end
121:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.