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.



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

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.



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

def data
  @data
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.



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

def particles
  @particles ||= Hash[selected_data.collect { |v| [v.downcase, v] }]
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.



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

def selected_data
  @selected_data ||=
    data
    .select { |l| languages.include? l.to_sym }
    .values
    .flatten
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.



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

def in_particle_list?(word)
  reindex if languages != NobleNames.configuration.languages
  particles.key? word
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.



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

def prefix?(word)
  reindex if languages != NobleNames.configuration.languages
  prefixes.each do |pre|
    return pre unless (word =~ Regexp.new('^' + pre.to_s)).nil?
  end
  nil
end

#reindexObject

Resets the state of the MatchIndex



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

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