Class: IMatch::Lexicon
- Inherits:
-
Object
- Object
- IMatch::Lexicon
- Defined in:
- lib/lexicon.rb
Instance Method Summary collapse
- #include?(key) ⇒ Boolean
-
#initialize(file_or_set) ⇒ Lexicon
constructor
A new instance of Lexicon.
- #size ⇒ Object
-
#subset(percentage) ⇒ Object
percentage should be between 0.0 and 1.0.
- #to_s ⇒ Object
Constructor Details
#initialize(file_or_set) ⇒ Lexicon
Returns a new instance of Lexicon.
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.(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.(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
23 24 25 |
# File 'lib/lexicon.rb', line 23 def include?(key) @data.include?(key) end |
#size ⇒ Object
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_s ⇒ Object
31 32 33 |
# File 'lib/lexicon.rb', line 31 def to_s %Q{<IMatch::Lexicon size="#{size}" file="#{@file}" />} end |