Class: NameFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/name_finder.rb,
lib/name_finder/buffer.rb,
lib/name_finder/version.rb,
lib/name_finder/node_proxy.rb

Overview

Find names from a know list in a text, taking account of names that may overlap. For example, Waterloo and Waterloo East are separate stations; NameFinder, knowing both, will not give a false match for Waterloo in a text that mentions Waterloo East.

Defined Under Namespace

Classes: Buffer, NodeProxy

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree = {}) ⇒ NameFinder

Initialize a new NameFinder. tree, if supplied, should be the data generated by the export method.



16
17
18
19
# File 'lib/name_finder.rb', line 16

def initialize(tree={})
  @tree = tree
  @root = NodeProxy.new(tree, delimiter)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



21
22
23
# File 'lib/name_finder.rb', line 21

def root
  @root
end

Instance Method Details

#add(term) ⇒ Object

Add a term to NameFinder’s dictionary



25
26
27
# File 'lib/name_finder.rb', line 25

def add(term)
  root.add Buffer.new(normalize(term) + delimiter), term
end

#exportObject

Export the tree of the current dictionary for later re-importing.



50
51
52
# File 'lib/name_finder.rb', line 50

def export
  @tree
end

#find_all_in(haystack) ⇒ Object

Find all names from the dictionary in haystack.



40
41
42
43
44
45
46
# File 'lib/name_finder.rb', line 40

def find_all_in(haystack)
  Set.new.tap { |all|
    find(haystack) do |found|
      all << found
    end
  }.to_a
end

#find_in(haystack) ⇒ Object

Find the first name from the dictionary in haystack



31
32
33
34
35
36
# File 'lib/name_finder.rb', line 31

def find_in(haystack)
  find(haystack) do |found|
    return found
  end
  nil
end