Class: Deluminator

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

Constant Summary collapse

MIN_DICTIONARY_LENGTH =
4
UPVOWELS =
%w(A E I O U Y)
LOWVOWELS =
%w(a e i o u y)
UPPERS =
('A'..'Z').to_a - UPVOWELS
LOWERS =
('a'..'z').to_a - LOWVOWELS
VOWEL_LEN =
UPVOWELS.length
NONVOWEL_LEN =
UPPERS.length

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Deluminator

Returns a new instance of Deluminator.



15
16
17
18
19
20
21
22
# File 'lib/deluminator.rb', line 15

def initialize(hash = {})
  raise "Deluminator.new expects a hash" unless hash.is_a?(Hash)
  @length_indexed_dict = hash[:length_indexed_dict] || {}
  @reserved   = hash[:reserved]   || []
  raise ":reserved value must be an array"   unless @reserved.is_a?(Array)
  raise ":length_indexed_dict value must be a hash" unless @length_indexed_dict.is_a?(Hash)
  raise ":length_indexed_dict keys must be integers" unless @length_indexed_dict.keys.all? { |k| k.is_a?(Integer) }
end

Instance Attribute Details

#reservedObject (readonly)

KEYWORDS = %w(def end collect each class module open close inject select detect reject upto downto

this self describe context it should
)


5
6
7
# File 'lib/deluminator.rb', line 5

def reserved
  @reserved
end

Instance Method Details

#add_to_dictionary(text) ⇒ Object



23
24
25
26
27
# File 'lib/deluminator.rb', line 23

def add_to_dictionary(text)
  text.split(/\s+|[^a-zA-z]+/).each do |word|
    add_one_word_to_dictionary(word)
  end
end

#deluminate(text) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/deluminator.rb', line 38

def deluminate(text)
  result = text
  # Replace the longer words first, to ensure we don't corrupt them if they contain shorter words
  # (we *assume* that our random replacements don't ever match a substring of a longer replacement string -
  #  which is probably a good bet since the odds are close to 1 in 26**4 or 1:456976)
  @length_indexed_dict.keys.sort.reverse.each do |word_len|
    @length_indexed_dict[word_len].each do |word, replacement|
      #regexp = Regexp.new("\\b#{word}\\b")
      regexp = Regexp.new(word)
      result.gsub!(regexp, replacement)
    end
  end
  result
end

#dictionaryObject

flattened hash of all our sub-hashes



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

def dictionary
  hash = {}
  @length_indexed_dict.each do |len, words_of_same_length|
    hash.merge! words_of_same_length
  end
  hash
end