Class: PragmaticTokenizer::FullStopSeparator

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

Overview

This class separates true full stops while ignoring periods that are part of an abbreviation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens:, abbreviations:, downcase:) ⇒ FullStopSeparator

Returns a new instance of FullStopSeparator.



8
9
10
11
12
# File 'lib/pragmatic_tokenizer/full_stop_separator.rb', line 8

def initialize(tokens:, abbreviations:, downcase:)
  @tokens = tokens
  @abbreviations = abbreviations
  @downcase = downcase
end

Instance Attribute Details

#abbreviationsObject (readonly)

Returns the value of attribute abbreviations.



7
8
9
# File 'lib/pragmatic_tokenizer/full_stop_separator.rb', line 7

def abbreviations
  @abbreviations
end

#downcaseObject (readonly)

Returns the value of attribute downcase.



7
8
9
# File 'lib/pragmatic_tokenizer/full_stop_separator.rb', line 7

def downcase
  @downcase
end

#tokensObject (readonly)

Returns the value of attribute tokens.



7
8
9
# File 'lib/pragmatic_tokenizer/full_stop_separator.rb', line 7

def tokens
  @tokens
end

Instance Method Details

#separateObject



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
43
44
45
46
47
# File 'lib/pragmatic_tokenizer/full_stop_separator.rb', line 14

def separate
  abbr = {}
  abbreviations.each do |i|
    abbr[i] = true
  end
  cleaned_tokens = []
  tokens.each_with_index do |_t, i|
    if tokens[i + 1] && tokens[i] =~ /\A(.+)\.\z/
      w = Regexp.last_match(1)
      if downcase
        abbreviation = abbr[w]
      else
        abbreviation = abbr[Unicode.downcase(w)]
      end
      unless abbreviation || w =~ /\A[a-z]\z/i ||
             w =~ /[a-z](?:\.[a-z])+\z/i
        cleaned_tokens << w
        cleaned_tokens << '.'
        next
      end
    end
    cleaned_tokens << tokens[i]
  end
  if downcase
    abbreviation = abbreviations.include?(cleaned_tokens[-1].chomp(".")) unless cleaned_tokens[-1].nil?
  else
    abbreviation = abbreviations.include?(Unicode.downcase(cleaned_tokens[-1]).chomp(".")) unless cleaned_tokens[-1].nil?
  end
  if cleaned_tokens[-1] && cleaned_tokens[-1] =~ /\A(.*\w)\.\z/ && !abbreviation
    cleaned_tokens[-1] = Regexp.last_match(1)
    cleaned_tokens.push '.'
  end
  cleaned_tokens
end