Class: NobleNames::MatchIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/noble_names/match_index.rb

Overview

A MatchIndex holds the data necessary for finding prefixes and particles in Strings and checks them for it. MatchIndexs use Hashes for finding particles to guarantee constant performance in big particle lists. A MatchIndex has a lot of mutable state to cache as much matching information as possible.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ MatchIndex

Takes either a String or any Object and tries to convert it to a hash.

Examples:

A correct data list

MatchIndex.new({
  'german' => ['von', 'zu'],
  'english' => ['of']
})

Parameters:

  • list (String, Object)

    if this is a string, it will be treated as a file-name in the data/ folder from which a Hash will be extracted, containing language-keys and data.



25
26
27
28
29
30
31
32
33
34
# File 'lib/noble_names/match_index.rb', line 25

def initialize(list)
  case list
  when String
    @data = YAML.load_file(File.expand_path(list, Data::DATA_PATH))
    @data = @data.values.first
  else
    @data = Hash[list]
  end
  @lanugages = NobleNames.configuration.languages
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



11
12
13
# File 'lib/noble_names/match_index.rb', line 11

def data
  @data
end

Instance Method Details

#in_particle_list?(word) ⇒ Boolean

Checks weither a word is in the nobility particle list.

Parameters:

  • word (String)

    the word that is checked.

Returns:

  • (Boolean)

    true if word is in the particle_list, false otherwise.



52
53
54
55
# File 'lib/noble_names/match_index.rb', line 52

def in_particle_list?(word)
  reindex if @languages != NobleNames.configuration.languages
  particles.key? word
end

#particlesHash

Returns and caches the particles of a MatchIndex.

Examples:

A particle hash

MatchIndex.new('nobility_particles.yml')
  .particles.has_key?('von')                 #=> true
MatchIndex.new('nobility_particles.yml')
  .particles['von']                          #=> 'von'

Returns:

  • (Hash)

    particles A Hash containing particles to match against.



44
45
46
# File 'lib/noble_names/match_index.rb', line 44

def particles
  @particles ||= Hash[selected_data.collect { |v| [v.downcase, v] }]
end

#prefix?(word) ⇒ String

Checks weither a word has a prefix as defined in the MatchIndexs data and returns it.

Examples:

prefix?('mcdormer')           #=> 'mc'

Parameters:

  • word (String)

    the word that needs to be checked.

Returns:

  • (String)

    pre the Prefix of the word. nil if it has none.



76
77
78
79
80
81
82
# File 'lib/noble_names/match_index.rb', line 76

def prefix?(word)
  reindex if @languages != NobleNames.configuration.languages
  prefixes.each do |pre|
    return pre if (word =~ Regexp.new(pre)) == 0
  end
  nil
end

#reindexObject

Resets the state of the MatchIndex



87
88
89
90
91
92
# File 'lib/noble_names/match_index.rb', line 87

def reindex
  @languages = NobleNames.configuration.languages
  @selected_data = nil
  @prefixes = nil
  @particles = nil
end

#selected_dataArray Also known as: prefixes

Caches the particles or prefixes by the languages selected in the config.

Returns:

  • (Array)

    selected_data The data filtered by used languages.



61
62
63
64
65
66
67
# File 'lib/noble_names/match_index.rb', line 61

def selected_data
  @selected_data ||=
    @data
    .select { |l| @languages.include? l.to_sym }
    .values
    .flatten
end