hiwai 0.0.0 → 0.0.1

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.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ tmp
@@ -0,0 +1,4 @@
1
+ # Version 0.0.0
2
+
3
+ Initial version released.
4
+
data/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # Hiwai
2
2
 
3
3
  レシーバがひわいかどうかを確認します。
4
- 公の場で表示されると困るオブジェクトをフィルタするのが目的です。
4
+ 公の場で表示されると困る文字列をフィルタするのが目的です。
5
5
 
6
6
  そのような文字列をなるべく目にしないで管理できるようにすることも目標にしています。
7
7
 
8
8
  ## Installation
9
9
 
10
- ** under construction **
10
+ **under construction**
11
11
 
12
12
  Add this line to your application's Gemfile:
13
13
 
@@ -34,12 +34,44 @@ Hiwai.dictionary = 'path/to/dictionary'
34
34
  ['とっても<<よくない文字列>>です'].hiwai? # => true
35
35
  ```
36
36
 
37
- 今は単純にString#matchでマッチさせています(もっと精度の高い方法にしたい)。
37
+ 今は単純にレシーバをto_sした結果をString#matchでマッチさせています(もっと精度の高い方法にしたい)。
38
38
  ワードの一覧はvendor/hiwai.txtの下に改行区切で置いています(まだ何もない)。
39
39
  管理するときになるべく見ないためにBase64エンコーディングしています。
40
40
 
41
41
  マッチ候補の文字列はHiwaiモジュールが持っていますが、inspectしたときにうっかり表示されないように拡張しています。
42
42
 
43
+ ## 単語リストの管理
44
+
45
+ ### 対話モードで単語を追加
46
+
47
+ デフォルトの単語リスト`vendor/hiwai.txt`に単語を追加できます。
48
+
49
+ ```
50
+ rake dic:interactive
51
+ ```
52
+
53
+ できることは追加だけです。`crtl+d`で追加した単語が保存されます。それまでの入力を破棄するには`ctrl+c`します。
54
+
55
+ 入力した文字は普通にエコーバックされます。
56
+
57
+ ### 素のままで編集する
58
+
59
+ **リダイレクトしなければあられもない文字列たちが標準出力に出力されます。**
60
+
61
+ 1. 単語リストをプレーンなテキストに展開します
62
+
63
+ ```
64
+ cat path/to/dictionary | rake dic:expand > temporary
65
+ ```
66
+
67
+ 2. がんばって編集したのち保存します
68
+
69
+ 3. 変換して上書きします
70
+
71
+ ```
72
+ cat temporary | rake dic:serialize > path/to/dictionary
73
+ ```
74
+
43
75
  ## Contributing
44
76
 
45
77
  1. Fork it
@@ -47,3 +79,4 @@ Hiwai.dictionary = 'path/to/dictionary'
47
79
  3. Commit your changes (`git commit -am 'Added some feature'`)
48
80
  4. Push to the branch (`git push origin my-new-feature`)
49
81
  5. Create new Pull Request
82
+
data/Rakefile CHANGED
@@ -1,8 +1,59 @@
1
1
  require 'bundler'
2
2
 
3
+ Bundler.require
4
+
3
5
  Bundler::GemHelper.install_tasks
4
6
  require 'rspec/core/rake_task'
5
7
  RSpec::Core::RakeTask.new(:spec)
6
8
 
7
- task default: :spec
9
+ namespace :dic do
10
+ desc 'Expand dictionary from STDIN'
11
+ task :expand do
12
+ print Hiwai::CensorableWords.load(STDIN.read).join("\n")
13
+ end
14
+
15
+ desc 'Serialize dictionary from STDIN'
16
+ task :serialize do
17
+ print Hiwai::CensorableWords.dump(STDIN.read.split("\n"))
18
+ end
19
+
20
+ desc 'Add new words in interactive mode,'
21
+ task :interactive do
22
+ puts "To commit, exit by ctrl+d.\nTo rollback, interrupt by ctrl+c."
23
+ prompt = -> { print 'new hiwai word > ' }.tap(&:[])
24
+ changes = []
25
+
26
+ trap :EXIT do
27
+ if changes.any?
28
+ File.open(Hiwai.dictionary, 'w') do |f|
29
+ f.write Hiwai::CensorableWords.dump(Hiwai.censorable_words + changes)
30
+ end
31
+
32
+ puts 'Committed successfully.'
33
+ end
34
+ end
8
35
 
36
+ trap :INT do
37
+ changes.clear
38
+ puts "\nRollbacked."
39
+ prompt[]
40
+ end
41
+
42
+ while input = STDIN.gets
43
+ word = input.strip.force_encoding(Encoding::UTF_8)
44
+
45
+ if Hiwai.censorable_words.index(word)
46
+ puts "#{word} is already exists."
47
+ else
48
+ unless word.empty?
49
+ changes << word
50
+ puts "#{word} was added."
51
+ end
52
+ end
53
+
54
+ prompt[]
55
+ end
56
+ end
57
+ end
58
+
59
+ task default: :spec
@@ -1,7 +1,7 @@
1
- require 'base64'
2
1
  require 'hiwai/version'
3
2
  require 'hiwai/object'
4
3
  require 'hiwai/masked'
4
+ require 'hiwai/censorable_words'
5
5
 
6
6
  module Hiwai
7
7
  class Censored < StandardError; end
@@ -12,18 +12,16 @@ module Hiwai
12
12
  attr_accessor :dictionary
13
13
 
14
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
- }
15
+ @censorable_words ||= CensorableWords.load(read_dictionary)
22
16
  end
23
17
 
24
18
  def reset!
25
19
  @censorable_words = nil
26
20
  end
21
+
22
+ def read_dictionary
23
+ File.read(dictionary)
24
+ end
27
25
  end
28
26
  end
29
27
 
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+
3
+ module Hiwai
4
+ module CensorableWords
5
+ extend self
6
+
7
+ def dump(words)
8
+ words.map {|word| encode(word) }.join
9
+ end
10
+
11
+ def load(string)
12
+ string.split("\n").map {|word| MaskedString.new(decode(word)) }
13
+ end
14
+
15
+ def decode(str)
16
+ str.unpack('m').join.force_encoding(Encoding::UTF_8)
17
+ end
18
+
19
+ def encode(str)
20
+ [str].pack('m')
21
+ end
22
+ end
23
+ end
@@ -1,11 +1,15 @@
1
1
  module Hiwai
2
2
  module Masked
3
- def masked_string
4
- '<censored>'
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ alias_method :to_s, :masked_string
6
+ alias_method :inspect, :masked_string
7
+ end
5
8
  end
6
9
 
7
- alias_method :to_s, :masked_string
8
- alias_method :inspect, :masked_string
10
+ def masked_string
11
+ '<masked>'
12
+ end
9
13
  end
10
14
 
11
15
  class MaskedString < String
@@ -2,10 +2,10 @@
2
2
 
3
3
  class Object
4
4
  def hiwai?
5
- inspected = inspect
5
+ string = to_s
6
6
 
7
7
  @hiwai_matches = Hiwai.censorable_words.select {|word|
8
- inspected.match Regexp.new(word)
8
+ string.match Hiwai::MaskedRegexp.new(word)
9
9
  }
10
10
 
11
11
  @hiwai_matches.any?
@@ -1,3 +1,3 @@
1
1
  module Hiwai
2
- VERSION = '0.0.0'
2
+ VERSION = '0.0.1'
3
3
  end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'fileutils'
5
+
6
+ describe Hiwai::CensorableWords do
7
+ def load_words(str)
8
+ Hiwai::CensorableWords.load str
9
+ end
10
+
11
+ def dump_words(arr)
12
+ Hiwai::CensorableWords.dump arr
13
+ end
14
+
15
+ let(:serialized) { dump_words(%w(ワカラナイ シラナイ コムギコカナニカダ)) }
16
+
17
+ describe '.load' do
18
+ subject { load_words(serialized) }
19
+
20
+ it { should include 'ワカラナイ' }
21
+ it { should include 'シラナイ' }
22
+ it { should include 'コムギコカナニカダ' }
23
+ end
24
+
25
+ describe '.dump' do
26
+ let(:censorable_words) { load_words serialized }
27
+
28
+ before do
29
+ censorable_words << 'ドイツ村'
30
+ end
31
+
32
+ subject { load_words(dump_words(censorable_words)) }
33
+
34
+ it { should have(4).words }
35
+ it { should include 'ドイツ村' }
36
+ end
37
+ end
@@ -3,17 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
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
6
+ describe '.censorable_words' do
17
7
  subject { Hiwai.censorable_words }
18
8
 
19
9
  its(:inspect) { should_not match /#{censored}/ }
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Object 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
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ExampleGroupHelper
4
4
  def censored
5
- Base64.decode64('PDzjgajjgabjgoLljZHnjKXjgarmloflrZfliJc+Pg==').force_encoding('utf-8')
5
+ Hiwai::CensorableWords.decode('PDzjgajjgabjgoLljZHnjKXjgarmloflrZfliJc+Pg==')
6
6
  end
7
7
  end
@@ -1,2 +1,100 @@
1
1
  PDzjgajjgabjgoLljZHnjKXjgarmloflrZfliJc+Pg==
2
2
  PDzjgojjgY/jgarjgYTmloflrZfliJc+Pg==
3
+ QVblpbPlhKo=
4
+ R+OCueODneODg+ODiA==
5
+ U03jgq/jg6njg5Y=
6
+ 5Lmx5Lqk
7
+ 5ZKM5aem
8
+ 5by15b2i
9
+ 5oSb5pKr
10
+ 5oSb5ray
11
+ 5rOh5aer
12
+ 542j5aem
13
+ 55Sf5aer
14
+ 55S35qC5
15
+ 55+t5bCP
16
+ 57Sg6IKh
17
+ 6Ieq5oWw
18
+ 6KaW5aem
19
+ 6LKe5pON
20
+ 6LK35pil
21
+ 6Zmw5q+b
22
+ 6Z2S5aem
23
+ 6aGU5bCE
24
+ 44OB44Oz44Od
25
+ 44Gh44KT44G9
26
+ 44Ki44Kv44Oh
27
+ 44Ki44OK44Or
28
+ 44Ki44OM44K5
29
+ 44K144Kq5bir
30
+ 44OB44Oz44Kz
31
+ 44OQ44Kk44OW
32
+ 44OV44Kn44Op
33
+ 44Oe44Oz44Kz
34
+ 5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 44Oe44Oz44Kw44Oq6L+U44GX
85
+ 44Ki44OK44Or44OQ44Kk44OW
86
+ 44Ki44OK44Or44OT44O844K6
87
+ 44Ki44OK44Or44OX44Op44Kw
88
+ 44Ki44OL44Oq44Oz44Kw44K5
89
+ 44K944O844OX44Op44Oz44OJ
90
+ 44OA44OD44OB44Ov44Kk44OV
91
+ 44Oe44OD44OI44OX44Os44Kk
92
+ 44Op44OW44K444Ol44O844K5
93
+ 44Ki44OK44Or44K744OD44Kv44K5
94
+ 44Kv44Oz44OL44Oq44Oz44Kw44K5
95
+ 44OU44Oz44Kv44Ot44O844K/44O8
96
+ 5aSn5Lq644Gu44GK44KC44Gh44KD
97
+ 5oCn5oSf44Oe44OD44K144O844K4
98
+ 44Ki44Oh44Oq44Kr44Oz44OB44Oz44Od
99
+ 44OV44Kj44K544OI44OV44Kh44OD44Kv
100
+ 44Oe44K544K/44O844OZ44O844K344On44Oz
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiwai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-26 00:00:00.000000000 Z
12
+ date: 2012-04-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70155429296240 !ruby/object:Gem::Requirement
16
+ requirement: &70125375902360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.9.2
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70155429296240
24
+ version_requirements: *70125375902360
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70155429295720 !ruby/object:Gem::Requirement
27
+ requirement: &70125375901560 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.6.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70155429295720
35
+ version_requirements: *70125375901560
36
36
  description: detect hiwai string in japanese
37
37
  email:
38
38
  - celluloid.key@gmail.com
@@ -41,16 +41,20 @@ extensions: []
41
41
  extra_rdoc_files: []
42
42
  files:
43
43
  - .gitignore
44
+ - ChangeLog.md
44
45
  - Gemfile
45
46
  - LICENSE
46
47
  - README.md
47
48
  - Rakefile
48
49
  - hiwai.gemspec
49
50
  - lib/hiwai.rb
51
+ - lib/hiwai/censorable_words.rb
50
52
  - lib/hiwai/masked.rb
51
53
  - lib/hiwai/object.rb
52
54
  - lib/hiwai/version.rb
55
+ - spec/censorable_words_spec.rb
53
56
  - spec/hiwai_spec.rb
57
+ - spec/object_spec.rb
54
58
  - spec/spec_helper.rb
55
59
  - spec/support/example_group_helper.rb
56
60
  - vendor/hiwai.txt
@@ -79,6 +83,8 @@ signing_key:
79
83
  specification_version: 3
80
84
  summary: detect hiwai string
81
85
  test_files:
86
+ - spec/censorable_words_spec.rb
82
87
  - spec/hiwai_spec.rb
88
+ - spec/object_spec.rb
83
89
  - spec/spec_helper.rb
84
90
  - spec/support/example_group_helper.rb