Class: Resemblance::Shingling

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

Instance Method Summary collapse

Constructor Details

#initialize(content = '', options = {}) ⇒ Shingling

Returns a new instance of Shingling.



5
6
7
8
9
10
11
# File 'lib/shingling.rb', line 5

def initialize(content = '', options = {})
  @content = content
  @shingle_length = options[:shingle_length] || 5
  @stop_words = options[:stop_words] || []
  @downcase = options[:downcase] || false
  @replace_chars = options[:replace_chars] || {}
end

Instance Method Details

#each_shinglesObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/shingling.rb', line 13

def each_shingles
  word = ""
  char_flag = false
  shingling = []
  position_end_words = []
  position_start, position_end = 0, 0

  @content.each_char do |char|
    char = @replace_chars[char] if @replace_chars.key? char
    if char !~ /[[:word:]]+/
      char_flag = true
    else
      if char_flag
        if !stop_word?(word) || word !~ /\S/
          shingling << (@downcase ? Unicode::downcase(word) : word)
          word = ""
          position_end_words << position_end
          if shingling.size == @shingle_length
            yield(shingling, position_start, position_end - 1)
            position_start = position_end_words.shift
            shingling.shift
          end
        end
        char_flag = false
      end
      word << char
    end
    position_end += 1
  end        
end