Class: Language::Matcher

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

Overview

Matcher

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

Defined Under Namespace

Classes: MatchData

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMatcher

Create a DictionaryMatcher with no words in it



25
26
27
28
# File 'lib/language/matcher.rb', line 25

def initialize
  @trie = {}
  @word_count = 0
end

Instance Attribute Details

#word_countObject (readonly)

Returns the value of attribute word_count.



11
12
13
# File 'lib/language/matcher.rb', line 11

def word_count
  @word_count
end

Instance Method Details

#=~(text) ⇒ Object Also known as: ===

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.



88
89
90
91
# File 'lib/language/matcher.rb', line 88

def =~ text
  internal_match(text){|md| return md.index}
  nil
end

#add(word) ⇒ Object Also known as: <<

Add a word to the DictionaryMatcher



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/language/matcher.rb', line 31

def add(word)
  @word_count += 1
  container = @trie
  containers=[]

  i=0
  word.each_byte do |b|
    container[b] = {} unless container.has_key? b
    container[:depth]=i
    containers << container
    container = container[b]
    i+=1
  end
  containers << container

  container[0] = true # Mark end of word
  container[:depth]=i

  ff=compute_failure_function word
  ff.zip(containers).each do |pointto,container|
    container[:failure]=containers[pointto] if pointto
  end

  self

end

#include?(word) ⇒ Boolean

Determine whether string was previously added to the Trie.

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
# File 'lib/language/matcher.rb', line 75

def include?(word)
  container = @trie
  word.each_byte do |b|
    break unless container.has_key? b
    container = container[b]
  end
  container[0]
end

#inspectObject



20
21
22
# File 'lib/language/matcher.rb', line 20

def inspect
  to_s
end

#match(text) ⇒ Object

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



97
98
99
100
# File 'lib/language/matcher.rb', line 97

def match text
  internal_match(text){|md| return md}
  nil
end

#scan(text, &block) ⇒ Object

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.



135
136
137
138
139
140
# File 'lib/language/matcher.rb', line 135

def scan(text, &block)
  matches=[]
  block= lambda{ |md| matches << md } unless block
  internal_match(text,&block)
  matches
end