Class: LanguageFilter::Filter
- Inherits:
-
Object
- Object
- LanguageFilter::Filter
- 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
-
#creative_letters ⇒ Object
Returns the value of attribute creative_letters.
-
#creative_matchlist ⇒ Object
readonly
Returns the value of attribute creative_matchlist.
-
#exceptionlist ⇒ Object
Returns the value of attribute exceptionlist.
-
#matchlist ⇒ Object
Returns the value of attribute matchlist.
-
#replacement ⇒ Object
Returns the value of attribute replacement.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Filter
constructor
A new instance of Filter.
-
#match?(text) ⇒ Boolean
LANGUAGE.
- #matched(text) ⇒ Object
- #sanitize(text) ⇒ Object
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(={}) @creative_letters = if [:creative_letters] then [:creative_letters] else DEFAULT_CREATIVE_LETTERS end @matchlist = if [:matchlist] then validate_list_content([:matchlist]) set_list_content([:matchlist]) else set_list_content(DEFAULT_MATCHLIST) end @creative_matchlist = @matchlist.map {|list_item| use_creative_letters(list_item)} @exceptionlist = if [:exceptionlist] then validate_list_content([:exceptionlist]) set_list_content([:exceptionlist]) elsif [:matchlist].class == Symbol then set_list_content([:matchlist],folder: "exceptionlists") else set_list_content(DEFAULT_EXCEPTIONLIST) end @replacement = [:replacement] || DEFAULT_REPLACEMENT validate_replacement end |
Instance Attribute Details
#creative_letters ⇒ Object
Returns the value of attribute creative_letters.
9 10 11 |
# File 'lib/language_filter.rb', line 9 def creative_letters @creative_letters end |
#creative_matchlist ⇒ Object (readonly)
Returns the value of attribute creative_matchlist.
10 11 12 |
# File 'lib/language_filter.rb', line 10 def creative_matchlist @creative_matchlist end |
#exceptionlist ⇒ Object
Returns the value of attribute exceptionlist.
9 10 11 |
# File 'lib/language_filter.rb', line 9 def exceptionlist @exceptionlist end |
#matchlist ⇒ Object
Returns the value of attribute matchlist.
9 10 11 |
# File 'lib/language_filter.rb', line 9 def matchlist @matchlist end |
#replacement ⇒ Object
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
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 |