Class: IMatch::Lexicon

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

Instance Method Summary collapse

Constructor Details

#initialize(file_or_set) ⇒ Lexicon

Returns a new instance of Lexicon.

Raises:

  • (InvalidLexiconError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lexicon.rb', line 5

def initialize(file_or_set)
  if file_or_set.kind_of?(Set)
    @file = "N/A"
    @data = file_or_set.clone.freeze
  elsif file_or_set.kind_of?(File)
    @file = File.expand_path(file_or_set.path)
    @data = IO.read(@file).split(/\r?\n/).to_set.freeze
  elsif file_or_set.kind_of?(String)
    raise(InvalidLexiconError, "Invalid/missing lexicon file: #{file_or_set}") unless File.exist?(file_or_set)
    @file = File.expand_path(file_or_set)
    @data = IO.read(@file).split(/\r?\n/).to_set.freeze
  else
    raise(InvalidLexiconError, "Invalid/missing lexicon argument: #{file_or_set}")
  end

  raise(InvalidLexiconError, "Empty lexicon file: #{file_or_set}") if @data.empty?
end

Instance Method Details

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/lexicon.rb', line 23

def include?(key)
  @data.include?(key)
end

#sizeObject



27
28
29
# File 'lib/lexicon.rb', line 27

def size
  @data.size
end

#subset(percentage) ⇒ Object

percentage should be between 0.0 and 1.0



36
37
38
39
40
41
42
43
44
45
# File 'lib/lexicon.rb', line 36

def subset(percentage)
  subset = Set.new
  @data.each do |term|
    if rand > percentage
      subset << term
    end
  end

  self.class.new(subset)
end

#to_sObject



31
32
33
# File 'lib/lexicon.rb', line 31

def to_s
  %Q{<IMatch::Lexicon size="#{size}" file="#{@file}" />}
end