polite_text 0.1.2 → 0.1.3
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +72 -8
- data/lib/locales/en.yml +6 -0
- data/lib/polite_text.rb +4 -6
- data/lib/polite_text/base.rb +10 -9
- data/lib/polite_text/text_cleaner.rb +5 -10
- data/lib/polite_text/text_scanner.rb +5 -10
- data/lib/polite_text/version.rb +1 -1
- data/polite_text.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b3eeefd6c47fe3bdc029ddfd592486673db2b55bcfff7405fc17e3a45c4c960
|
4
|
+
data.tar.gz: 891412e5952963ea6109a9ac649b05b250bc0ae19035f5f80de10bb245f45a7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b87c8045b5f0cab33be0462390fac0d28c628ec3a28d9345748d620578930d83c21eeedb02fb7431a4049f835c18e9e8e397feccc247e0bcaff6606a0966ccb
|
7
|
+
data.tar.gz: 3ce5f283f3b2775564581aeb7bde43c0b4995de59c9824838537facfef5348eee23068367fea1f6f8ba8d2a124473e43b00a2477e6532d6eade19749d3f3abc7
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -5,9 +5,9 @@
|
|
5
5
|
|
6
6
|
PoliteText is making your users' input polite 👀
|
7
7
|
|
8
|
-
👉 Replace the swear words or execute callbacks if the input is not polite.
|
8
|
+
👉 Replace the swear words or execute callbacks if the input is not polite based on a default swear words list or your own.
|
9
9
|
|
10
|
-
## Installation
|
10
|
+
## Installation 👨💻 👩💻
|
11
11
|
|
12
12
|
Add this line to your application's Gemfile:
|
13
13
|
|
@@ -23,7 +23,10 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
$ gem install polite_text
|
25
25
|
|
26
|
-
## Usage
|
26
|
+
## Usage with the default swear words list 🌝
|
27
|
+
|
28
|
+
### Swear words 🔞
|
29
|
+
PoliteText has a default list of forbidden words, this list includes the most common swear words used on the web. You can acces the list of swear words [here](https://github.com/OpenGems/polite_text/blob/master/lib/locales/en.yml).
|
27
30
|
|
28
31
|
### Remove swear words 🤬
|
29
32
|
```
|
@@ -32,7 +35,6 @@ include PoliteText
|
|
32
35
|
str = "This gem is a fucking big shit but let's try it"
|
33
36
|
|
34
37
|
PoliteText.be_polite!(str)
|
35
|
-
|
36
38
|
=> "This gem is a *** big *** but let's try it"
|
37
39
|
```
|
38
40
|
|
@@ -43,20 +45,82 @@ include PoliteText
|
|
43
45
|
str = "This gem is a fucking big shit but let's try it"
|
44
46
|
|
45
47
|
PoliteText.is_polite?(str)
|
48
|
+
=> false
|
49
|
+
```
|
50
|
+
|
51
|
+
### Example for an Article model
|
52
|
+
|
53
|
+
```
|
54
|
+
# == Schema Information
|
55
|
+
#
|
56
|
+
# Table name: articles
|
57
|
+
#
|
58
|
+
# id :bigint not null, primary key
|
59
|
+
# text :string default(""), not null
|
60
|
+
# created_at :datetime not null
|
61
|
+
# updated_at :datetime not null
|
62
|
+
#
|
63
|
+
|
64
|
+
class Article < ApplicationRecord
|
65
|
+
include PoliteText
|
66
|
+
|
67
|
+
# Callbacks
|
68
|
+
before_save :make_text_polite
|
69
|
+
|
70
|
+
# Methods
|
71
|
+
def make_text_polite
|
72
|
+
PoliteText.be_polite!(text)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
## Usage with custom swear words list 🌞
|
78
|
+
|
79
|
+
### Custom swear words list 🔞
|
80
|
+
Create a custom list in a **YAML** file following this format :
|
81
|
+
```
|
82
|
+
# my_custom_swear_words.yml
|
83
|
+
|
84
|
+
swear_words:
|
85
|
+
- gem
|
86
|
+
- big
|
87
|
+
- but
|
88
|
+
```
|
46
89
|
|
90
|
+
Place it where you want in your app, we recommend here : `./lib/polite_text/my_custom_swear_words.yml`
|
91
|
+
|
92
|
+
👉 You just need to pass the path to this file as a second argument.
|
93
|
+
|
94
|
+
### Example for an Article model
|
95
|
+
|
96
|
+
### Remove or detect swear words 🤬 🙅♂️ 🙅♀️
|
97
|
+
```
|
98
|
+
# Inside a random_model.rb
|
99
|
+
|
100
|
+
include PoliteText
|
101
|
+
|
102
|
+
str = "This gem is a fucking big shit but let's try it"
|
103
|
+
|
104
|
+
current_dir = __dir__
|
105
|
+
custom_path = "#{current_dir}/../lib/polite_text/fr.yml"
|
106
|
+
|
107
|
+
PoliteText.be_polite!(str, custom_path)
|
108
|
+
=> "This *** is a fucking *** shit *** let's try it"
|
109
|
+
|
110
|
+
PoliteText.is_polite?(str, custom_path)
|
47
111
|
=> false
|
48
112
|
```
|
49
113
|
|
114
|
+
|
50
115
|
## Contributing
|
51
116
|
|
52
117
|
Bug reports and pull requests are welcome on GitHub at https://github.com/OpenGems/polite_text.
|
53
118
|
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
|
54
119
|
|
55
120
|
### Todo 💪
|
56
|
-
- [ ]
|
57
|
-
- [ ]
|
58
|
-
- [ ]
|
59
|
-
- [ ] Add swear words from multiple languages
|
121
|
+
- [ ] Manage locales with I18N for default swear words lists by country.
|
122
|
+
- [ ] Add default swear words lists from multiple languages.
|
123
|
+
- [ ] Improve the default swear words list and keeping it short.
|
60
124
|
|
61
125
|
## License
|
62
126
|
|
data/lib/locales/en.yml
CHANGED
@@ -4,18 +4,24 @@ swear_words:
|
|
4
4
|
- fuck
|
5
5
|
- fuckyou
|
6
6
|
- fucker
|
7
|
+
- fuckers
|
7
8
|
- fucking
|
8
9
|
- dick head
|
9
10
|
- dick
|
10
11
|
- dickhole
|
11
12
|
- cock
|
12
13
|
- asshole
|
14
|
+
- assholes
|
13
15
|
- son of a bitch
|
14
16
|
- bitch
|
17
|
+
- bitches
|
15
18
|
- biatch
|
16
19
|
- whore
|
20
|
+
- whores
|
17
21
|
- bastard
|
22
|
+
- bastards
|
18
23
|
- wanker
|
24
|
+
- wankers
|
19
25
|
- shit
|
20
26
|
- piss
|
21
27
|
- pussy
|
data/lib/polite_text.rb
CHANGED
@@ -3,15 +3,13 @@ require 'polite_text/text_scanner'
|
|
3
3
|
require 'polite_text/text_cleaner'
|
4
4
|
|
5
5
|
module PoliteText
|
6
|
-
class Error < StandardError; end
|
7
|
-
|
8
6
|
class << self
|
9
|
-
def be_polite!(text)
|
10
|
-
PoliteText::TextCleaner.new(text).clean!
|
7
|
+
def be_polite!(text, custom_swear_words_path = nil)
|
8
|
+
PoliteText::TextCleaner.new(text, custom_swear_words_path).clean!
|
11
9
|
end
|
12
10
|
|
13
|
-
def is_polite?(text)
|
14
|
-
PoliteText::TextScanner.new(text).match_swear_word?
|
11
|
+
def is_polite?(text, custom_swear_words_path = nil)
|
12
|
+
PoliteText::TextScanner.new(text, custom_swear_words_path).match_swear_word?
|
15
13
|
end
|
16
14
|
end
|
17
15
|
end
|
data/lib/polite_text/base.rb
CHANGED
@@ -2,23 +2,24 @@ require 'yaml'
|
|
2
2
|
|
3
3
|
module PoliteText
|
4
4
|
class Base
|
5
|
-
def swear_words
|
6
|
-
@swear_words ||= /\b(#{Regexp.union(swear_words_list).source})\b/
|
5
|
+
def swear_words(custom_swear_words_path)
|
6
|
+
@swear_words ||= /\b(#{Regexp.union(swear_words_list(custom_swear_words_path)).source})\b/
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def swear_words_list
|
14
|
-
YAML.load_file(swear_words_load_path)['swear_words']
|
9
|
+
def swear_words_list(custom_swear_words_path = nil)
|
10
|
+
path = custom_swear_words_path ? custom_swear_words_path.to_s : swear_words_load_path
|
11
|
+
YAML.load_file(path)['swear_words']
|
15
12
|
end
|
16
13
|
|
17
14
|
private
|
18
15
|
|
19
|
-
#
|
16
|
+
# TODO: Implement dynamic locale with I18N
|
20
17
|
def locale
|
21
18
|
@locale ||= 'en'
|
22
19
|
end
|
20
|
+
|
21
|
+
def swear_words_load_path
|
22
|
+
"#{__dir__}/../locales/#{locale}.yml"
|
23
|
+
end
|
23
24
|
end
|
24
25
|
end
|
@@ -2,20 +2,15 @@ module PoliteText
|
|
2
2
|
class TextCleaner < Base
|
3
3
|
attr_reader :text
|
4
4
|
|
5
|
-
def initialize(text)
|
6
|
-
|
5
|
+
def initialize(text, custom_swear_words_path = nil)
|
6
|
+
raise ArgumentError.new('The text can not be nil') if text.nil?
|
7
7
|
|
8
|
-
|
8
|
+
@text = text.to_s
|
9
|
+
@custom_swear_words_path = custom_swear_words_path
|
9
10
|
end
|
10
11
|
|
11
12
|
def clean!
|
12
|
-
text.gsub!(swear_words, '***')
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def validate_text!
|
18
|
-
raise ArgumentError.new('The text can not be empty') if @text.empty?
|
13
|
+
text.gsub!(swear_words(@custom_swear_words_path), '***') || text
|
19
14
|
end
|
20
15
|
end
|
21
16
|
end
|
@@ -2,20 +2,15 @@ module PoliteText
|
|
2
2
|
class TextScanner < Base
|
3
3
|
attr_reader :text
|
4
4
|
|
5
|
-
def initialize(text)
|
6
|
-
|
5
|
+
def initialize(text, custom_swear_words_path = nil)
|
6
|
+
raise ArgumentError.new('The text can not be nil') if text.nil?
|
7
7
|
|
8
|
-
|
8
|
+
@text = text.to_s
|
9
|
+
@custom_swear_words_path = custom_swear_words_path
|
9
10
|
end
|
10
11
|
|
11
12
|
def match_swear_word?
|
12
|
-
!@text.match?(swear_words)
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def validate_text!
|
18
|
-
raise ArgumentError.new('The text can not be empty') if @text.empty?
|
13
|
+
!@text.match?(swear_words(@custom_swear_words_path))
|
19
14
|
end
|
20
15
|
end
|
21
16
|
end
|
data/lib/polite_text/version.rb
CHANGED
data/polite_text.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
|
18
18
|
# Specify which files should be added to the gem when it is released.
|
19
19
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
-
spec.files = Dir.chdir(File.expand_path(
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
21
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
22
|
end
|
23
23
|
spec.bindir = 'exe'
|