Class: LanguageFilter::Filter

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

Constant Summary collapse

CREATIVE_BEG_REGEX =
'(?<=\\s|\\A|_|\\-|\\.)'
CREATIVE_END_REGEX =
'(?=\\b|\\s|\\z|_|\\-|\\.)'
DEFAULT_EXCEPTIONLIST =
[]
DEFAULT_MATCHLIST =
File.dirname(__FILE__) + "/../config/matchlists/profanity.txt"
DEFAULT_REPLACEMENT =
:stars
DEFAULT_CREATIVE_LETTERS =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Filter

Returns a new instance of Filter.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/language_filter.rb', line 20

def initialize(options={})
  @creative_letters = if options[:creative_letters] then
    options[:creative_letters]
  else DEFAULT_CREATIVE_LETTERS end

  @matchlist = if options[:matchlist] then
    validate_list_content(options[:matchlist])
    set_list_content(options[:matchlist])
  else set_list_content(DEFAULT_MATCHLIST) end
  @creative_matchlist = @matchlist.map {|list_item| use_creative_letters(list_item)}

  @exceptionlist = if options[:exceptionlist] then
    validate_list_content(options[:exceptionlist])
    set_list_content(options[:exceptionlist])
  elsif options[:matchlist].class == Symbol then
    set_list_content(options[:matchlist],folder: "exceptionlists")
  else set_list_content(DEFAULT_EXCEPTIONLIST) end

  @replacement = options[:replacement] || DEFAULT_REPLACEMENT
  validate_replacement
end

Instance Attribute Details

#creative_lettersObject

Returns the value of attribute creative_letters.



9
10
11
# File 'lib/language_filter.rb', line 9

def creative_letters
  @creative_letters
end

#creative_matchlistObject (readonly)

Returns the value of attribute creative_matchlist.



10
11
12
# File 'lib/language_filter.rb', line 10

def creative_matchlist
  @creative_matchlist
end

#exceptionlistObject

Returns the value of attribute exceptionlist.



9
10
11
# File 'lib/language_filter.rb', line 9

def exceptionlist
  @exceptionlist
end

#matchlistObject

Returns the value of attribute matchlist.



9
10
11
# File 'lib/language_filter.rb', line 9

def matchlist
  @matchlist
end

#replacementObject

Returns the value of attribute replacement.



9
10
11
# File 'lib/language_filter.rb', line 9

def replacement
  @replacement
end

Instance Method Details

#match?(text) ⇒ Boolean

LANGUAGE

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/language_filter.rb', line 72

def match?(text)
  return false unless text.to_s.size >= 3
  chosen_matchlist = case @creative_letters
  when true then @creative_matchlist
  else @matchlist
  end
  chosen_matchlist.each do |list_item|
    start_at = 0
    text.scan(%r"#{beg_regex}#{list_item}#{end_regex}"i) do |match|
      unless @exceptionlist.empty? then
        match_start = text[start_at..-1].index(%r"#{beg_regex}#{list_item}#{end_regex}"i) + start_at
        match_end = match_start + match.size-1
      end
      return true if @exceptionlist.empty? or not protected_by_exceptionlist?(match_start,match_end,text,start_at)
      start_at = match_end + 1 unless @exceptionlist.empty?
    end
  end
  false
end

#matched(text) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/language_filter.rb', line 92

def matched(text)
  words = []
  return words unless text.to_s.size >= 3
  chosen_matchlist = case @creative_letters
  when true then @creative_matchlist
  else @matchlist
  end
  chosen_matchlist.each do |list_item|
    start_at = 0
    text.scan(%r"#{beg_regex}#{list_item}#{end_regex}"i) do |match|
      unless @exceptionlist.empty? then
        match_start = text[start_at..-1].index(%r"#{beg_regex}#{list_item}#{end_regex}"i) + start_at
        match_end = match_start + match.size-1
      end
      words << match if @exceptionlist.empty? or not protected_by_exceptionlist?(match_start,match_end,text,start_at)
      start_at = match_end + 1 unless @exceptionlist.empty?
    end
  end
  words.uniq
end

#sanitize(text) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/language_filter.rb', line 113

def sanitize(text)
  return text unless text.to_s.size >= 3
  chosen_matchlist = case @creative_letters
  when true then @creative_matchlist
  else @matchlist
  end
  chosen_matchlist.each do |list_item|
    start_at = 0
    text.gsub!(%r"#{beg_regex}#{list_item}#{end_regex}"i) do |match|
      unless @exceptionlist.empty? then
        match_start = text[start_at..-1].index(%r"#{beg_regex}#{list_item}#{end_regex}"i) + start_at
        match_end = match_start + match.size-1
      end
      unless @exceptionlist.empty? or not protected_by_exceptionlist?(match_start,match_end,text,start_at) then
        start_at = match_end + 1 unless @exceptionlist.empty?
        match
      else
        start_at = match_end + 1 unless @exceptionlist.empty?
        replace(match)
      end
    end
  end
  text
end