hiwai 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hiwai.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 hibariya
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ # Hiwai
2
+
3
+ レシーバがひわいかどうかを確認します。
4
+ 公の場で表示されると困るオブジェクトをフィルタするのが目的です。
5
+
6
+ そのような文字列をなるべく目にしないで管理できるようにすることも目標にしています。
7
+
8
+ ## Installation
9
+
10
+ ** under construction **
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'hiwai'
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install hiwai
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ require 'hiwai'
24
+
25
+ # if you have your own dictionary
26
+ Hiwai.dictionary = 'path/to/dictionary'
27
+
28
+ # String
29
+ 'hibariya'.hiwai? # => false
30
+ 'とっても<<よくない文字列>>です'.hiwai? # => true
31
+ 'とっても<<よくない文字列>>です'.hiwai! # Hiwai::Censored: Receiver includes hiwai object
32
+
33
+ # Array
34
+ ['とっても<<よくない文字列>>です'].hiwai? # => true
35
+ ```
36
+
37
+ 今は単純にString#matchでマッチさせています(もっと精度の高い方法にしたい)。
38
+ ワードの一覧はvendor/hiwai.txtの下に改行区切で置いています(まだ何もない)。
39
+ 管理するときになるべく見ないためにBase64エンコーディングしています。
40
+
41
+ マッチ候補の文字列はHiwaiモジュールが持っていますが、inspectしたときにうっかり表示されないように拡張しています。
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ require 'bundler'
2
+
3
+ Bundler::GemHelper.install_tasks
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
8
+
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+
4
+ require 'hiwai/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'hiwai'
8
+ s.version = Hiwai::VERSION
9
+ s.authors = ['hibariya']
10
+ s.email = ['celluloid.key@gmail.com']
11
+ s.homepage = 'https://github.com/hibariya/hiwai'
12
+ s.summary = %q{detect hiwai string}
13
+ s.description = %q{detect hiwai string in japanese}
14
+
15
+ s.files = `git ls-files`.split($\)
16
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| file.basename(f) }
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+ s.require_paths = ['lib']
19
+
20
+ s.add_development_dependency 'rake', ['>= 0.9.2']
21
+ s.add_development_dependency 'rspec', ['>= 2.6.0']
22
+ end
@@ -0,0 +1,30 @@
1
+ require 'base64'
2
+ require 'hiwai/version'
3
+ require 'hiwai/object'
4
+ require 'hiwai/masked'
5
+
6
+ module Hiwai
7
+ class Censored < StandardError; end
8
+
9
+ GEM_ROOT = File.dirname(__FILE__) + '/..'
10
+
11
+ class << self
12
+ attr_accessor :dictionary
13
+
14
+ def censorable_words
15
+ @censorable_words ||=
16
+ File.read(dictionary).split("\n").map {|line|
17
+ # TODO: support other encoding
18
+ str = Base64.decode64(line).force_encoding('utf-8')
19
+
20
+ MaskedString.new(str)
21
+ }
22
+ end
23
+
24
+ def reset!
25
+ @censorable_words = nil
26
+ end
27
+ end
28
+ end
29
+
30
+ Hiwai.dictionary = Hiwai::GEM_ROOT + '/vendor/hiwai.txt'
@@ -0,0 +1,18 @@
1
+ module Hiwai
2
+ module Masked
3
+ def masked_string
4
+ '<censored>'
5
+ end
6
+
7
+ alias_method :to_s, :masked_string
8
+ alias_method :inspect, :masked_string
9
+ end
10
+
11
+ class MaskedString < String
12
+ include Masked
13
+ end
14
+
15
+ class MaskedRegexp < Regexp
16
+ include Masked
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+
3
+ class Object
4
+ def hiwai?
5
+ inspected = inspect
6
+
7
+ @hiwai_matches = Hiwai.censorable_words.select {|word|
8
+ inspected.match Regexp.new(word)
9
+ }
10
+
11
+ @hiwai_matches.any?
12
+ end
13
+
14
+ def hiwai!
15
+ raise Hiwai::Censored, 'Receiver includes hiwai object' if hiwai?
16
+ end
17
+
18
+ def censored
19
+ @hiwai_matches
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Hiwai
2
+ VERSION = '0.0.0'
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Hiwai do
6
+ describe '#hiwai?' do
7
+ it { "これは#{censored}です".should be_hiwai }
8
+ it { "小麦粉か何かだ".should_not be_hiwai }
9
+ end
10
+
11
+ describe '#hiwai!' do
12
+ it { expect { "これは#{censored}です".hiwai! }.should raise_error(Hiwai::Censored) }
13
+ it { expect { "小麦粉か何かだ".hiwai! }.should_not raise_error(Hiwai::Censored) }
14
+ end
15
+
16
+ describe 'censorable_words' do
17
+ subject { Hiwai.censorable_words }
18
+
19
+ its(:inspect) { should_not match /#{censored}/ }
20
+ its(:to_s) { should_not match /#{censored}/ }
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ # coding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../lib/hiwai'
4
+
5
+ Dir[File.dirname(__FILE__) + '/support/*'].each {|f| require f }
6
+
7
+ RSpec.configure do |config|
8
+ config.filter_run focus: true
9
+ config.run_all_when_everything_filtered = true
10
+
11
+ config.include ExampleGroupHelper
12
+ end
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+
3
+ module ExampleGroupHelper
4
+ def censored
5
+ Base64.decode64('PDzjgajjgabjgoLljZHnjKXjgarmloflrZfliJc+Pg==').force_encoding('utf-8')
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ PDzjgajjgabjgoLljZHnjKXjgarmloflrZfliJc+Pg==
2
+ PDzjgojjgY/jgarjgYTmloflrZfliJc+Pg==
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiwai
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - hibariya
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70155429296240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.2
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70155429296240
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70155429295720 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.6.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70155429295720
36
+ description: detect hiwai string in japanese
37
+ email:
38
+ - celluloid.key@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - hiwai.gemspec
49
+ - lib/hiwai.rb
50
+ - lib/hiwai/masked.rb
51
+ - lib/hiwai/object.rb
52
+ - lib/hiwai/version.rb
53
+ - spec/hiwai_spec.rb
54
+ - spec/spec_helper.rb
55
+ - spec/support/example_group_helper.rb
56
+ - vendor/hiwai.txt
57
+ homepage: https://github.com/hibariya/hiwai
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.11
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: detect hiwai string
81
+ test_files:
82
+ - spec/hiwai_spec.rb
83
+ - spec/spec_helper.rb
84
+ - spec/support/example_group_helper.rb