polite_text 0.1.22 → 0.2.0
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 +44 -10
- data/lib/polite_text.rb +4 -4
- data/lib/polite_text/base.rb +9 -8
- data/lib/polite_text/text_cleaner.rb +3 -2
- data/lib/polite_text/text_scanner.rb +3 -2
- data/lib/polite_text/version.rb +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: b61709fd69469a4a019e0a041b18c95700d72132992293b2e2f49eebe9df6662
|
4
|
+
data.tar.gz: d243724fa7ef4eb931e4b6e295b6b1d8afec2a81c82d399e5759f764f808c1e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a4fbc965f3c668bedbe5e0bf1df64f72077199c743938ec009c82a71e6c889f11492c92d18c58776303e31d34acbacdd01ea6dc47666731a41ade9abb044255
|
7
|
+
data.tar.gz: c232a79a3dd3c5bd8ea7abfbfbe99f807de817143d54a57db31e1feb85a88ec5e6c75c3f040a3c05f14f89401b265b00f1e3c1b47a7daaad0dbf9e1a72c64613
|
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,7 @@ 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
27
|
|
28
28
|
### Swear words 🔞
|
29
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).
|
@@ -35,7 +35,6 @@ include PoliteText
|
|
35
35
|
str = "This gem is a fucking big shit but let's try it"
|
36
36
|
|
37
37
|
PoliteText.be_polite!(str)
|
38
|
-
|
39
38
|
=> "This gem is a *** big *** but let's try it"
|
40
39
|
```
|
41
40
|
|
@@ -46,7 +45,6 @@ include PoliteText
|
|
46
45
|
str = "This gem is a fucking big shit but let's try it"
|
47
46
|
|
48
47
|
PoliteText.is_polite?(str)
|
49
|
-
|
50
48
|
=> false
|
51
49
|
```
|
52
50
|
|
@@ -76,17 +74,53 @@ class Article < ApplicationRecord
|
|
76
74
|
end
|
77
75
|
```
|
78
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
|
+
```
|
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)
|
111
|
+
=> false
|
112
|
+
```
|
113
|
+
|
114
|
+
|
79
115
|
## Contributing
|
80
116
|
|
81
117
|
Bug reports and pull requests are welcome on GitHub at https://github.com/OpenGems/polite_text.
|
82
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.
|
83
119
|
|
84
120
|
### Todo 💪
|
85
|
-
- [ ]
|
86
|
-
- [ ]
|
87
|
-
- [ ]
|
88
|
-
- [ ] Add swear words from multiple languages.
|
89
|
-
- [ ] Improve swear words list and keeping it short.
|
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.
|
90
124
|
|
91
125
|
## License
|
92
126
|
|
data/lib/polite_text.rb
CHANGED
@@ -4,12 +4,12 @@ require 'polite_text/text_cleaner'
|
|
4
4
|
|
5
5
|
module PoliteText
|
6
6
|
class << self
|
7
|
-
def be_polite!(text)
|
8
|
-
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!
|
9
9
|
end
|
10
10
|
|
11
|
-
def is_polite?(text)
|
12
|
-
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?
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
data/lib/polite_text/base.rb
CHANGED
@@ -2,16 +2,13 @@ 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
|
@@ -20,5 +17,9 @@ module PoliteText
|
|
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,14 +2,15 @@ module PoliteText
|
|
2
2
|
class TextCleaner < Base
|
3
3
|
attr_reader :text
|
4
4
|
|
5
|
-
def initialize(text)
|
5
|
+
def initialize(text, custom_swear_words_path = nil)
|
6
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, '***') || text
|
13
|
+
text.gsub!(swear_words(@custom_swear_words_path), '***') || text
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
@@ -2,14 +2,15 @@ module PoliteText
|
|
2
2
|
class TextScanner < Base
|
3
3
|
attr_reader :text
|
4
4
|
|
5
|
-
def initialize(text)
|
5
|
+
def initialize(text, custom_swear_words_path = nil)
|
6
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
|
+
!@text.match?(swear_words(@custom_swear_words_path))
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
data/lib/polite_text/version.rb
CHANGED