pretty-profanity 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 698efd484610ee8a8dc5bb554b8b81dd41685625
4
+ data.tar.gz: a37d1ef50b9f4d109aa1696c52ea19030fb1ad7f
5
+ SHA512:
6
+ metadata.gz: 62128de4d3007a7832aa9f498ddb5b8d928a021c9747716c739f3b3ed90e53c332772127f0d5addaa4145ed4e1db76bbb884c7acd77b1d65168d7b48689e075b
7
+ data.tar.gz: f6dd3b558b1a9de74040257807a5ccc495f1fbc212255117ffba7787f7b84ed8f94515141ff8802f8cb4d98e86b51fca1923526673253758137fdae1fea7a138
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require rspec/pride
3
+ --format RSpec::Pride
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+ gem 'pry'
5
+ gem 'rspec', '~> 2.5'
6
+ gem 'rspec-pride'
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ coderay (1.1.0)
5
+ diff-lcs (1.2.5)
6
+ method_source (0.8.2)
7
+ pry (0.10.0)
8
+ coderay (~> 1.1.0)
9
+ method_source (~> 0.8.1)
10
+ slop (~> 3.4)
11
+ rake (10.1.0)
12
+ rspec (2.99.0)
13
+ rspec-core (~> 2.99.0)
14
+ rspec-expectations (~> 2.99.0)
15
+ rspec-mocks (~> 2.99.0)
16
+ rspec-core (2.99.0)
17
+ rspec-expectations (2.99.0)
18
+ diff-lcs (>= 1.1.3, < 2.0)
19
+ rspec-mocks (2.99.1)
20
+ rspec-pride (2.3.0)
21
+ rspec (~> 2.10)
22
+ slop (3.5.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ pry
29
+ rake
30
+ rspec (~> 2.5)
31
+ rspec-pride
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new('spec')
3
+
4
+ task default: :spec
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.authors = 'Blaine Schmeisser'
3
+ s.name = 'pretty-profanity'
4
+ s.email = 'blainesch@gmail.com'
5
+ s.homepage = 'https://github.com/blainesch/pretty-profanity.rb'
6
+ s.version = '0.0.2'
7
+ s.summary = 'Cleans up profanity in text.'
8
+ s.description = 'Cleans up profanity in text with blacklist, whitelists, and multiple types of sanitization rules. Easily add new sanitization rules.'
9
+ s.licenses = ['MIT']
10
+ s.require_paths = ['src']
11
+ s.files = `git ls-files`.split("\n")
12
+ s.has_rdoc = false
13
+ end
data/readme.md ADDED
@@ -0,0 +1,45 @@
1
+ ## Pretty Profanity
2
+ Cleans up profanity in text.
3
+
4
+ [![Build Status](https://travis-ci.org/blainesch/pretty-profanity.rb.svg?branch=master)](https://travis-ci.org/blainesch/pretty-profanity.rb)
5
+ [![Code Climate](https://codeclimate.com/github/blainesch/pretty-profanity.rb.png)](https://codeclimate.com/github/blainesch/pretty-profanity.rb)
6
+
7
+ ## Setup
8
+ ~~~ ruby
9
+ PrettyProfanity::Profanity.configure do |config|
10
+ config.blacklist = ['foo', 'bar']
11
+ config.whitelist = ['foo baz']
12
+ config.replacement = :vowels
13
+ end
14
+ ~~~
15
+
16
+ ### Blacklist
17
+ Whole words (or phrases) that you wish to remove. Because this requires whole words you'll need to add both `foo` and `foobaz`. The first will not match the second.
18
+
19
+ ### Whitelist
20
+ Phrases that contain a word from the whitelist that you do not wish to remove. For instance you might blacklist `foo` but `foo baz` is acceptable.
21
+
22
+ ### Replacements
23
+ Different replacements do different things. For instance the `:vowel` replacement just replaces vowels with stars, where the `:none` just deletes the word.
24
+ * `:none` - Deletes the entire word.
25
+ * `:star` - Stars the entire word.
26
+ * `:vowels` - Stars just the vowels.
27
+ * `:garbled` - Replaces each letter with garbled text `$@!#%`.
28
+ * `:L337` - Replaces every character with 1337 speak.
29
+ * `:piglatin` - Converts word to piglatin.
30
+
31
+ ## Creating Custom Replacements
32
+ To create your own sanitization rules, open the class up, add your method and add it to your configuration.
33
+ ~~~ ruby
34
+ module PrettyProfanity
35
+ class Sanitize
36
+ def reverse(word)
37
+ word.downcase.reverse
38
+ end
39
+ end
40
+ end
41
+ PrettyProfanity::Profanity.configure do |config|
42
+ # ...
43
+ config.replacement = :reverse
44
+ end
45
+ ~~~
@@ -0,0 +1,21 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'pry'
9
+ require File.dirname(__FILE__) + "/../src/pretty-profanity"
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+
3
+ describe PrettyProfanity::Profanity do
4
+
5
+ describe '.offensive' do
6
+
7
+ before do
8
+ PrettyProfanity::Profanity.configure do |config|
9
+ config.blacklist = ['php', 'asp', '.net']
10
+ config.whitelist = ['ugly php', 'ugly asp', 'ugly .net']
11
+ config.replacement = :none
12
+ end
13
+ end
14
+
15
+ it 'returns a list of offensive words' do
16
+ expect(PrettyProfanity::Profanity.offensive('I love php and asp! But not ugly .net.')).to eq(['php', 'asp'])
17
+ end
18
+
19
+ it 'doesnt break when I give it nil' do
20
+ expect(PrettyProfanity::Profanity.offensive(nil)).to eq([])
21
+ end
22
+
23
+ end
24
+
25
+ describe '.profane?' do
26
+
27
+ context 'a blacklist word' do
28
+
29
+ before do
30
+ PrettyProfanity::Profanity.configure do |config|
31
+ config.blacklist = ['php']
32
+ config.whitelist = []
33
+ config.replacement = :none
34
+ end
35
+ end
36
+
37
+ it 'returns true when that curse word is in my text' do
38
+ expect(PrettyProfanity::Profanity.profane?('I wrote an app in php!')).to be_truthy
39
+ end
40
+
41
+ it 'returns true when that curse word is in my text and capitalized' do
42
+ expect(PrettyProfanity::Profanity.profane?('I wrote an app in PHP!')).to be_truthy
43
+ end
44
+
45
+ it 'returns false when that curse word is in my text' do
46
+ expect(PrettyProfanity::Profanity.profane?('I wrote a ruby gem!')).to be_falsey
47
+ end
48
+
49
+ end
50
+
51
+ context 'a blacklist and a whitelist' do
52
+
53
+ before do
54
+ PrettyProfanity::Profanity.configure do |config|
55
+ config.blacklist = ['php']
56
+ config.whitelist = ['not using php']
57
+ config.replacement = :none
58
+ end
59
+ end
60
+
61
+ it 'does not filter when I am using the whitelist version' do
62
+ expect(PrettyProfanity::Profanity.profane?('I am not using php!')).to be_falsey
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ describe '.sanitize' do
70
+
71
+ context 'a blacklist word and replacement style' do
72
+
73
+ before do
74
+ PrettyProfanity::Profanity.configure do |config|
75
+ config.blacklist = ['php']
76
+ config.whitelist = []
77
+ config.replacement = :star
78
+ end
79
+ end
80
+
81
+ it 'replaces the blacklisted word with stars' do
82
+ expect(PrettyProfanity::Profanity.sanitize('I wrote an app in php!')).to eq('I wrote an app in ***!')
83
+ end
84
+
85
+ it 'replaces none of the good words' do
86
+ expect(PrettyProfanity::Profanity.sanitize('I wrote a ruby gem!')).to eq('I wrote a ruby gem!')
87
+ end
88
+
89
+ end
90
+
91
+ context 'a blacklist and a whitelist' do
92
+
93
+ before do
94
+ PrettyProfanity::Profanity.configure do |config|
95
+ config.blacklist = ['php']
96
+ config.whitelist = ['not using php']
97
+ config.replacement = :star
98
+ end
99
+ end
100
+
101
+ it 'does not filter when I am using the whitelist version' do
102
+ expect(PrettyProfanity::Profanity.sanitize('I am not using php!')).to eq('I am not using php!')
103
+ end
104
+
105
+ end
106
+
107
+ end
108
+
109
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe PrettyProfanity::Sanitize do
4
+
5
+ describe '#none' do
6
+ it 'returns an empty string' do
7
+ expect(subject.none('hello')).to eq('')
8
+ end
9
+ end
10
+
11
+ describe '#vowels' do
12
+ it 'replaces vowels with stars' do
13
+ expect(subject.vowels('hello')).to eq('h*ll*')
14
+ end
15
+ end
16
+
17
+ describe '#star' do
18
+ it 'replaces characters with stars' do
19
+ expect(subject.star('hello')).to eq('*****')
20
+ end
21
+ end
22
+
23
+ describe '#garbled' do
24
+
25
+ it 'returns a a string just as long' do
26
+ expect(subject.garbled('hello').length).to eq(5)
27
+ expect(subject.garbled('helloworld').length).to eq(10)
28
+ end
29
+
30
+ it 'matches just garbled characters' do
31
+ expect(subject.garbled('helloworldhowareyou')).to match(/[$@!#%]+/)
32
+ end
33
+
34
+ end
35
+
36
+ describe '#hollow' do
37
+
38
+ it 'stars out inner word' do
39
+ expect(subject.hollow('foobar')).to eq('f****r')
40
+ end
41
+
42
+ it 'leaves two letter words alone' do
43
+ expect(subject.hollow('hi')).to eq('hi')
44
+ end
45
+
46
+ end
47
+
48
+ describe '#L337' do
49
+
50
+ it 'gives back 1337 speak' do
51
+ expect(subject.L337('foobar')).to eq('Ph00B4R')
52
+ end
53
+
54
+ end
55
+
56
+ describe '#piglatin' do
57
+
58
+ it 'converts to pig latin examples taken from wikipedia' do
59
+ expect(subject.piglatin('happy')).to eq('appyhay')
60
+ expect(subject.piglatin('duck')).to eq('uckday')
61
+ expect(subject.piglatin('glove')).to eq('oveglay')
62
+ expect(subject.piglatin('egg')).to eq('eggway')
63
+ expect(subject.piglatin('inbox')).to eq('inboxway')
64
+ expect(subject.piglatin('eight')).to eq('eightway')
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe PrettyProfanity::Word do
4
+
5
+ before do
6
+ PrettyProfanity::Profanity.whitelist = ['hello world', 'goodbye hello']
7
+ end
8
+
9
+ subject { PrettyProfanity::Word.new('hello') }
10
+
11
+ describe '#to_regex' do
12
+
13
+ it 'matches the base hello' do
14
+ expect(subject.to_regex).to match('this is hello meow')
15
+ end
16
+
17
+ it 'matches the base HELLO in caps' do
18
+ expect(subject.to_regex).to match('this is HELLO meow')
19
+ end
20
+
21
+ it 'does not match beginning whitelist phrase' do
22
+ expect(subject.to_regex).to_not match('this is hello world meow')
23
+ end
24
+
25
+ it 'does not match ending whitelist phrase' do
26
+ expect(subject.to_regex).to_not match('this is goodbye hello meow')
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,3 @@
1
+ require_relative 'pretty-profanity/profanity'
2
+ require_relative 'pretty-profanity/sanitize'
3
+ require_relative 'pretty-profanity/word'
@@ -0,0 +1,40 @@
1
+ module PrettyProfanity
2
+
3
+ class Profanity
4
+
5
+ class << self
6
+
7
+ attr_accessor :whitelist, :blacklist, :replacement
8
+
9
+ def configure
10
+ yield(self)
11
+ end
12
+
13
+ def profane?(text)
14
+ text = text.to_s
15
+ !!blacklist.detect do |word|
16
+ true if text.match(Word.new(word).to_regex)
17
+ end
18
+ end
19
+
20
+ def sanitize(text)
21
+ text = text.to_s
22
+ cleanse = Sanitize.new
23
+ blacklist.each do |word|
24
+ text.gsub!(Word.new(word).to_regex, cleanse.send(replacement, word))
25
+ end
26
+ text
27
+ end
28
+
29
+ def offensive(text)
30
+ text = text.to_s
31
+ blacklist.select do |word|
32
+ true if text.match(Word.new(word).to_regex)
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,47 @@
1
+ module PrettyProfanity
2
+
3
+ class Sanitize
4
+
5
+ def none(word)
6
+ ''
7
+ end
8
+
9
+ def vowels(word)
10
+ word.gsub(/[aeiou]/, '*')
11
+ end
12
+
13
+ def star(word)
14
+ '*' * word.length
15
+ end
16
+
17
+ def garbled(word)
18
+ text = '$@!#%'
19
+ word.length.times do |i|
20
+ word[i] = text[rand(0..(text.length-1))]
21
+ end
22
+ word
23
+ end
24
+
25
+ def hollow(word)
26
+ word[1,word.length-2] = '*' * (word.length - 2)
27
+ word
28
+ end
29
+
30
+ def L337(word)
31
+ letters = %w(4 B \( D 3 Ph 9 |-| 1 j |< L |V| |V 0 P Q R 5 7 U V VV >< '/ Z)
32
+ word.split('').map { |letter, index|
33
+ letters[letter.ord - 97]
34
+ }.join
35
+ end
36
+
37
+ def piglatin(word)
38
+ if word.match(/^[aeiou]/)
39
+ word + 'way'
40
+ else
41
+ word.gsub(/^([^aeiou]+)(.+)$/, '\2\1') + 'ay'
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,32 @@
1
+ module PrettyProfanity
2
+
3
+ class Word
4
+
5
+ attr_accessor :word, :whitelist
6
+
7
+ def initialize(word)
8
+ @word = word
9
+ @whitelist = Profanity.whitelist
10
+ end
11
+
12
+ def to_regex
13
+ /#{before_regex}\b(#{word})\b#{after_regex}/i
14
+ end
15
+
16
+ def before_regex
17
+ words = whitelist(/^#{word}/)
18
+ "(?!#{words.join('|')})" unless words.empty?
19
+ end
20
+
21
+ def after_regex
22
+ words = whitelist(/#{word}$/)
23
+ "(?<!#{words.join('|')})" unless words.empty?
24
+ end
25
+
26
+ def whitelist(word)
27
+ @whitelist.grep(word)
28
+ end
29
+
30
+ end
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretty-profanity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Blaine Schmeisser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Cleans up profanity in text with blacklist, whitelists, and multiple
14
+ types of sanitization rules. Easily add new sanitization rules.
15
+ email: blainesch@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".travis.yml"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - Rakefile
25
+ - pretty-profanity.gemspec
26
+ - readme.md
27
+ - spec/spec_helper.rb
28
+ - spec/src/pretty-profanity/profanity_spec.rb
29
+ - spec/src/pretty-profanity/sanitize_spec.rb
30
+ - spec/src/pretty-profanity/word_spec.rb
31
+ - src/pretty-profanity.rb
32
+ - src/pretty-profanity/profanity.rb
33
+ - src/pretty-profanity/sanitize.rb
34
+ - src/pretty-profanity/word.rb
35
+ homepage: https://github.com/blainesch/pretty-profanity.rb
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - src
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.2.2
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Cleans up profanity in text.
59
+ test_files: []