ng_word 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0a2b392753e735250209d1c7f72beb7a76bb8ec0c66dfaa3225e7e1e2bae0bc
4
+ data.tar.gz: 0402e29327e647d1f676cec0aa41499304c99f9872e023933d712feca484837e
5
+ SHA512:
6
+ metadata.gz: e517d878be9d7ac30093263ea32207fa1715835be111908e1a071f4509b138bef95b4d3e239720e3bcd6b797f86f564578f08e9f279cab8a3cceb6b6a9fa1e29
7
+ data.tar.gz: 7ebb7744daf9e861f4cee1c05cd4d0fe34ee96cc9711ba5a4576441be7940472c48b8fb5ddb89d4506cb31ebe9de8bb5c587cfb3db2b245fab5ea605c27366a3
@@ -0,0 +1,30 @@
1
+ version: 2
2
+
3
+ jobs:
4
+ build: &build
5
+ docker:
6
+ - image: circleci/ruby:latest
7
+ steps:
8
+ - checkout
9
+ - run:
10
+ name: Install gems
11
+ command: bundle install
12
+ - run:
13
+ name: RSpec
14
+ command: rspec
15
+ ruby-2.4:
16
+ <<: *build
17
+ docker:
18
+ - image: circleci/ruby:2.4
19
+
20
+ ruby-2.5:
21
+ <<: *build
22
+ docker:
23
+ - image: circleci/ruby:2.5
24
+
25
+ workflows:
26
+ version: 2
27
+ build-multiple-rubies:
28
+ jobs:
29
+ - ruby-2.4
30
+ - ruby-2.5
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ vendor/
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.16.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ng_word.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Takahiro Ooishi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # NgWord
2
+
3
+ Verify NG words.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ng_word'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ng_word
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ rule_list = NgWord::RuleList.new([
25
+ NgWord::Rule.new('ng', exclude_words: ['ng word']),
26
+ NgWord::Rule.new('hoge')
27
+ ])
28
+
29
+ text = "This text is include NG word and hoge word."
30
+ result = rule_list.verify(text)
31
+ rule_list.match?(text)
32
+ masked_text = rule_list.masked_text(text)
33
+ ```
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Code Status
42
+
43
+ [![CircleCI](https://circleci.com/gh/taka0125/ng_word/tree/master.svg?style=svg)](https://circleci.com/gh/taka0125/ng_word/tree/master)
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/taka0125/ng_word.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/aaa ADDED
File without changes
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ng_word"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/ng_word.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+ require 'ng_word/version'
4
+ require 'ng_word/verification_result'
5
+ require 'ng_word/exclude_word_rule'
6
+ require 'ng_word/rule'
7
+ require 'ng_word/rule_list'
8
+
9
+ module NgWord
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,26 @@
1
+ module NgWord
2
+ class ExcludeWordRule
3
+ def initialize(downcased_ng_word, exclude_word)
4
+ @downcased_ng_word = downcased_ng_word
5
+ @exclude_word = exclude_word
6
+ end
7
+
8
+ def match?(downcased_text, matched_index)
9
+ downcased_text.slice(matched_index - index, length) == @downcased_exclude_word
10
+ end
11
+
12
+ private
13
+
14
+ def downcased_exclude_word
15
+ @downcased_exclude_word ||= @exclude_word.downcase
16
+ end
17
+
18
+ def index
19
+ @index ||= downcased_exclude_word.index(@downcased_ng_word)
20
+ end
21
+
22
+ def length
23
+ @length ||= downcased_exclude_word.length
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,66 @@
1
+ module NgWord
2
+ class Rule
3
+ attr_reader :ng_word, :exclude_word_rules
4
+
5
+ def initialize(ng_word, exclude_words: [])
6
+ @ng_word = ng_word
7
+ @exclude_words = exclude_words
8
+ end
9
+
10
+ def verify(downcased_text, offset: 0)
11
+ index = downcased_text.index(downcased_ng_word, offset)
12
+ return VerificationResult.new(true) if index.nil?
13
+
14
+ exclude_word_rules.each do |rule|
15
+ return verify(downcased_text, offset: index + 1) if rule.match?(downcased_text, index)
16
+ end
17
+
18
+ VerificationResult.new(false, ng_word: @ng_word)
19
+ end
20
+
21
+ def masked_text(text, replace_text: '***', for_monitoring: false)
22
+ downcased_text = text.downcase
23
+ index = downcased_text.index(downcased_ng_word)
24
+ return text if index.nil?
25
+
26
+ index_list = split(downcased_text)
27
+
28
+ words = []
29
+ current_index = 0
30
+ index_list.each do |index|
31
+ words << text.slice(current_index, index - current_index)
32
+ current_index = index + downcased_ng_word.length
33
+ end
34
+ words << text.slice(current_index, text.length - current_index)
35
+
36
+ replaced_text = for_monitoring ? "#{replace_text}(#{@ng_word})" : replace_text
37
+ words.join(replaced_text)
38
+ end
39
+
40
+ def match?(downcased_text)
41
+ result = verify(downcased_text)
42
+ !result.valid?
43
+ end
44
+
45
+ private
46
+
47
+ def downcased_ng_word
48
+ @downcased_ng_word ||= @ng_word.downcase
49
+ end
50
+
51
+ def exclude_word_rules
52
+ @exclude_word_rules ||= Array(@exclude_words).map { |w| ExcludeWordRule.new(downcased_ng_word, w) }
53
+ end
54
+
55
+ def split(downcased_text, offset: 0, index_list: [])
56
+ index = downcased_text.index(downcased_ng_word, offset)
57
+ return index_list if index.nil?
58
+
59
+ exclude_word_rules.each do |rule|
60
+ return split(downcased_text, offset: index + 1, index_list: index_list) if rule.match?(downcased_text, index)
61
+ end
62
+
63
+ split(downcased_text, offset: index + 1, index_list: index_list + [index])
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,51 @@
1
+ module NgWord
2
+ class RuleList
3
+ def initialize(rules)
4
+ @rules = {}
5
+ Array(rules).each do |rule|
6
+ @rules[rule.ng_word.downcase] = rule
7
+ end
8
+
9
+ @ng_words = @rules.keys
10
+ @regexp = Regexp.union(@rules.keys.map { |s| /#{s}/i })
11
+ end
12
+
13
+ def verify(text)
14
+ return VerificationResult.new(true) if text.blank?
15
+
16
+ candidate_ng_words = text.scan(@regexp)
17
+ return VerificationResult.new(true) if candidate_ng_words.blank?
18
+
19
+ downcased_text = text.downcase
20
+ candidate_ng_words.each do |ng_word|
21
+ rule = @rules[ng_word.downcase]
22
+ next if rule.blank?
23
+
24
+ return VerificationResult.new(false, ng_word: rule.ng_word) if rule.match?(downcased_text)
25
+ end
26
+
27
+ VerificationResult.new(true)
28
+ end
29
+
30
+ def masked_text(text, replace_text: '***', for_monitoring: false)
31
+ return text if text.blank?
32
+
33
+ candidate_ng_words = text.scan(@regexp)
34
+ return text if candidate_ng_words.blank?
35
+
36
+ candidate_ng_words.each do |ng_word|
37
+ rule = @rules[ng_word.downcase]
38
+ next if rule.blank?
39
+
40
+ text = rule.masked_text(text, replace_text: replace_text, for_monitoring: for_monitoring)
41
+ end
42
+
43
+ text
44
+ end
45
+
46
+ def match?(text)
47
+ result = verify(text)
48
+ !result.valid?
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,14 @@
1
+ module NgWord
2
+ class VerificationResult
3
+ attr_reader :ng_word
4
+
5
+ def initialize(is_valid, ng_word: nil)
6
+ @is_valid = is_valid
7
+ @ng_word = ng_word
8
+ end
9
+
10
+ def valid?
11
+ @is_valid
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module NgWord
2
+ VERSION = "0.1.0"
3
+ end
data/ng_word.gemspec ADDED
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ng_word/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ng_word"
8
+ spec.version = NgWord::VERSION
9
+ spec.authors = ["Takahiro Ooishi"]
10
+ spec.email = ["taka0125@gmail.com"]
11
+
12
+ spec.summary = %q{Verify NG words.}
13
+ spec.description = %q{Verify NG words.}
14
+ spec.homepage = "https://github.com/taka0125/ng_word"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "activesupport"
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ng_word
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takahiro Ooishi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Verify NG words.
70
+ email:
71
+ - taka0125@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".circleci/config.yml"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - aaa
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/ng_word.rb
88
+ - lib/ng_word/exclude_word_rule.rb
89
+ - lib/ng_word/rule.rb
90
+ - lib/ng_word/rule_list.rb
91
+ - lib/ng_word/verification_result.rb
92
+ - lib/ng_word/version.rb
93
+ - ng_word.gemspec
94
+ homepage: https://github.com/taka0125/ng_word
95
+ licenses: []
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.7.6
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Verify NG words.
117
+ test_files: []