habakiri 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 56cc0c84f2e66aaa1180f159e5bcd52d7b24c13e7e3f077ad44ad0158fa0887f
4
+ data.tar.gz: f272408238825b7b44415de618e080b140abce02c044a26d16bf86319cb14b4f
5
+ SHA512:
6
+ metadata.gz: 291d44495b80ffd282023aeff8102fbc5e1543978d78de94aaa715743ffa2db1aa0b5fe7e979504fb7a31c5ec143e78955931a3285349c998ae2e4f000ed9f19
7
+ data.tar.gz: 226b5d38f5ebbecddfedc8620e15e9c6ae6ec12bffb5f09b5361f3be9b75014c0c0a1a2aef525b4ee1f1c5bcea4a539feec0b1aa0b5aee182f0793d1aeb5037d
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in habakiri.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rake', '~> 12.0'
8
+ gem 'rubocop'
9
+ gem 'minitest'
10
+ end
11
+
12
+ gem 'diff-lcs'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 diaphragm
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ # Habakiri
2
+
3
+ Habariki is a template match engine for strings that can extract part of text by template keywords.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'habakiri'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install habakiri
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'habakiri'
25
+
26
+ template = <<TEMPLATE
27
+ YOUR NAME:{{name}}
28
+ EMAIL:{{email}}
29
+ TELEPHONE NUMBER:{{tel}}
30
+ Website URL:{{url}}
31
+ ZIP CODE:{{zipcode}}
32
+ ADDRESS:{{address}}
33
+ NOTE:{{note}}
34
+ TEMPLATE
35
+
36
+ text = <<TEXT
37
+ YOUR NAME:John
38
+ EMAIL:email@email.com
39
+ TELEPHONE NUMBER:+199999999
40
+ Website URL:https://example.com/website/url
41
+ ZIP CODE:000-0000
42
+ ADDRESS:San Francisco, CA 94107 United States
43
+ NOTE:
44
+ Hello, world!
45
+ Hello, ruby!
46
+ Hello, habakiri!
47
+ TEXT
48
+
49
+ result = Habakiri.exec(template, text)
50
+ pp result
51
+ # =>
52
+ # {"name"=>"John",
53
+ # "email"=>"email@email.com",
54
+ # "tel"=>"+199999999",
55
+ # "url"=>"https://example.com/website/url",
56
+ # "zipcode"=>"000-0000",
57
+ # "address"=>"San Francisco, CA 94107 United States",
58
+ # "note"=>"Hello, world!\n" + "Hello, ruby!\n" + "Hello, habakiri!"}
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/diaphragm/habakiri.
64
+
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task default: :test
5
+
6
+ Rake::TestTask.new do |test|
7
+ test.test_files = Dir['test/**/test_*.rb']
8
+ test.verbose = true
9
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'lib/habakiri/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'habakiri'
5
+ spec.version = Habakiri::VERSION
6
+ spec.authors = ['diaphragm']
7
+ spec.email = ['7163746+diaphragm@users.noreply.github.com']
8
+
9
+ spec.summary = 'Strings template match engine'
10
+ spec.description = 'Habariki is a template match engine for strings that can extract part of text by template keywords.'
11
+ spec.homepage = 'https://github.com/diaphragm/habakiri'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0") - Dir['.*']
19
+ end
20
+ spec.require_paths = ['lib']
21
+ end
@@ -0,0 +1,44 @@
1
+ require 'habakiri/version'
2
+ require 'diff/lcs'
3
+
4
+ class Habakiri
5
+ def initialize(prefix = '{{', suffix = '}}')
6
+ @prefix = prefix
7
+ @suffix = suffix
8
+ end
9
+
10
+ def exec(template, text)
11
+ diffs = Diff::LCS.sdiff(template.to_s, text.to_s)
12
+ matches = template.to_enum(:scan, /#{@prefix}.+?#{@suffix}/).map{ Regexp.last_match }
13
+
14
+ matches.each.with_object({}){|match, object|
15
+ start = match.begin(0)
16
+ last = match.end(0)
17
+ range = start...last
18
+ list = diffs.select{|d| range.include?(d.old_position) }
19
+
20
+ # 改行後の追加要素も対応する文字列とみなす
21
+ i = diffs.index{|d| d.old_position == last }
22
+ if i
23
+ loop do
24
+ diff = diffs[i]
25
+ if diff && (diff.action == '+' || (diff.action == '=' && /\R/.match?(diff.old_element)))
26
+ list.push(diff)
27
+ i += 1
28
+ else
29
+ break
30
+ end
31
+ end
32
+ end
33
+
34
+ key = match[0].delete_prefix(@prefix).delete_suffix(@suffix).chomp
35
+ object[key] = list.map(&:new_element).join.strip
36
+ }
37
+ end
38
+
39
+ class << self
40
+ def exec(template, text)
41
+ new.exec(template, text)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ class Habakiri
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,10 @@
1
+ 名前:{{name}}
2
+ メールアドレス:{{email}}
3
+ 電話番号:{{tel}}
4
+ URL:{{url}}
5
+ 郵便番号:{{zipcode}}
6
+ 住所:{{address}}
7
+ 駅名:{{station}}
8
+ メモ1:{{memo1}}
9
+ メモ2:{{memo2}}
10
+ 備考:{{note}}
@@ -0,0 +1,34 @@
1
+ お客様より、下記内容のお問合せがありました。
2
+ ご確認の上、ご連絡をお願いいたします。
3
+
4
+ ■ お問合せのあったデータの情報 ■
5
+  下記URLをクリックすると問合せデータの詳細をご確認いただけます。
6
+  http://example.com/detail/
7
+   No  :{{no}}
8
+   データ名:{{data_name}}
9
+   住所 :
10
+   料金 :{{cost}}
11
+
12
+ ■ お問合せ内容 ■
13
+ ・最新の状況を知りたい
14
+
15
+ ○その他、ご要望・ご質問など
16
+ {{question}}
17
+
18
+ ■ お客様情報 ■
19
+ ○お名前/ふりがな
20
+ {{name}}/{{kana}}
21
+
22
+ ○メールアドレス
23
+ {{email}}
24
+
25
+ ○電話番号
26
+ {{tel}} 連絡希望時間:特に希望なし
27
+
28
+ ○性別
29
+ {{gender}}
30
+
31
+
32
+ ※お問合せ情報には、ご本人の個人情報が含まれている場合がございます。
33
+ お取り扱いには十分にご留意ください。
34
+ なお本個人情報は、資料送付や問合せに対する連絡以外での使用はできません。
@@ -0,0 +1,7 @@
1
+ YOUR NAME:{{name}}
2
+ EMAIL:{{email}}
3
+ TELEPHONE NUMBER:{{tel}}
4
+ Website URL:{{url}}
5
+ ZIP CODE:{{zipcode}}
6
+ ADDRESS:{{address}}
7
+ NOTE:{{note}}
@@ -0,0 +1,10 @@
1
+ 名前:テストテスト
2
+ メールアドレス:test.test@example.com
3
+ 電話番号:01201234567
4
+ URL:https://example.com/path/to/homepage/1232
5
+ 郵便番号:100-0001
6
+ 住所:東京都千代田区1-1-1
7
+ 駅名:東京駅
8
+ メモ1:メモメモ
9
+ メモ2:メモメモ2
10
+ 備考:備考備考
@@ -0,0 +1,35 @@
1
+ お客様より、下記内容のお問合せがありました。
2
+ ご確認の上、ご連絡をお願いいたします。
3
+
4
+ ■ お問合せのあったデータの情報 ■
5
+  下記URLをクリックすると問合せデータの詳細をご確認いただけます。
6
+  http://example.com/detail/111/11111/111111/?&lang=ja
7
+   No  : 12345678
8
+   データ名:テストデータ
9
+   住所 :北海道札幌市中央区12丁目3456
10
+   料金 :12万3456円
11
+
12
+ ■ お問合せ内容 ■
13
+ ・最新の状況を知りたい
14
+
15
+ ○その他、ご要望・ご質問など
16
+ いろはにほへとちりぬるをわかよたれそつねならむ
17
+ ういのおくやまけふこえてあさきゆめみしえひもせす
18
+
19
+ ■ お客様情報 ■
20
+ ○お名前/ふりがな
21
+ テスト太郎 / てすとたろう
22
+
23
+ ○メールアドレス
24
+ test.test@example.com
25
+
26
+ ○電話番号
27
+ 090-0900-0900 連絡希望時間:12時から18時
28
+
29
+ ○性別
30
+ 女性
31
+
32
+
33
+ ※お問合せ情報には、ご本人の個人情報が含まれている場合がございます。
34
+ お取り扱いには十分にご留意ください。
35
+ なお本個人情報は、資料送付や問合せに対する連絡以外での使用はできません。
@@ -0,0 +1,10 @@
1
+ YOUR NAME:John
2
+ EMAIL:email@email.com
3
+ TELEPHONE NUMBER:+199999999
4
+ Website URL:https://example.com/website/url
5
+ ZIP CODE:000-0000
6
+ ADDRESS:San Francisco, CA 94107 United States
7
+ NOTE:
8
+ Hello, world!
9
+ Hello, ruby!
10
+ Hello, habakiri!
@@ -0,0 +1,53 @@
1
+ require 'minitest/autorun'
2
+ require 'habakiri'
3
+
4
+ class HabakiriTest < Minitest::Unit::TestCase
5
+ def test_case_1
6
+ template = File.read('test/test_data/template_1.txt')
7
+ text = File.read('test/test_data/text_1.txt')
8
+ result = Habakiri.exec(template, text)
9
+
10
+ assert_instance_of(Hash, result)
11
+ assert_equal('テストテスト', result['name'])
12
+ assert_equal('test.test@example.com', result['email'])
13
+ assert_equal('01201234567', result['tel'])
14
+ assert_equal('100-0001', result['zipcode'])
15
+ assert_equal('東京都千代田区1-1-1', result['address'])
16
+ assert_equal('東京駅', result['station'])
17
+ assert_equal('メモメモ', result['memo1'])
18
+ assert_equal('メモメモ2', result['memo2'])
19
+ assert_equal('備考備考', result['note'])
20
+ end
21
+
22
+ def test_case_2
23
+ template = File.read('test/test_data/template_2.txt')
24
+ text = File.read('test/test_data/text_2.txt')
25
+ result = Habakiri.exec(template, text)
26
+
27
+ assert_instance_of(Hash, result)
28
+ assert_equal('12345678', result['no'])
29
+ assert_equal('テストデータ', result['data_name'])
30
+ assert_equal('12万3456円', result['cost'])
31
+ assert_equal("いろはにほへとちりぬるをわかよたれそつねならむ\nういのおくやまけふこえてあさきゆめみしえひもせす", result['question'])
32
+ assert_equal('テスト太郎', result['name'])
33
+ assert_equal('てすとたろう', result['kana'])
34
+ assert_equal('test.test@example.com', result['email'])
35
+ assert_equal('090-0900-0900', result['tel'])
36
+ assert_equal('女性', result['gender'])
37
+ end
38
+
39
+ def test_case_3
40
+ template = File.read('test/test_data/template_3.txt')
41
+ text = File.read('test/test_data/text_3.txt')
42
+ result = Habakiri.exec(template, text)
43
+
44
+ assert_instance_of(Hash, result)
45
+ assert_equal('John', result['name'])
46
+ assert_equal('email@email.com', result['email'])
47
+ assert_equal('+199999999', result['tel'])
48
+ assert_equal('https://example.com/website/url', result['url'])
49
+ assert_equal('000-0000', result['zipcode'])
50
+ assert_equal('San Francisco, CA 94107 United States', result['address'])
51
+ assert_equal("Hello, world!\nHello, ruby!\nHello, habakiri!", result['note'])
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: habakiri
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - diaphragm
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Habariki is a template match engine for strings that can extract part
14
+ of text by template keywords.
15
+ email:
16
+ - 7163746+diaphragm@users.noreply.github.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - habakiri.gemspec
26
+ - lib/habakiri.rb
27
+ - lib/habakiri/version.rb
28
+ - test/test_data/template_1.txt
29
+ - test/test_data/template_2.txt
30
+ - test/test_data/template_3.txt
31
+ - test/test_data/text_1.txt
32
+ - test/test_data/text_2.txt
33
+ - test/test_data/text_3.txt
34
+ - test/test_habakiri.rb
35
+ homepage: https://github.com/diaphragm/habakiri
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.3.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.1.4
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Strings template match engine
58
+ test_files: []