jisx0402 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2722816e893e3e002ae219054da5ff44934f5e47
4
- data.tar.gz: 0ee5cac5347314583bb13527e4313c92a4c2b94a
3
+ metadata.gz: 5de0232be3ec65da31e16dc07ab76e475082c427
4
+ data.tar.gz: 84182ccad0d6aa9720c74eef71680ddab5037707
5
5
  SHA512:
6
- metadata.gz: e4df6c458b1e2a3af699a6f1a1230c95d93d57ec153b704b4ffd8d48db10a7a1987b11779135961b1dd2cf1661cf1280b4f2f09da4f666a8dda440794a811cbe
7
- data.tar.gz: 87a6813bb7798f9f753404cedc8bde35e68ff55b01c51d55ba7a45c2801122b3bbc753261ff01c29460c62b04c70e53f1d7a388673cf28fac7547cff86046128
6
+ metadata.gz: a3041634f80f824876e0034222f92771280324c2f0d51d21aac03923b4cfcc26a01e93439f6fc39df4fb884182dffe12e74ed9994013a385f930e4131a1617cf
7
+ data.tar.gz: 4d166002be767dd41e92a70a9bfc0937194d0fd8438f8f2105ec12379996225cc39dfaecbb579b03e73122bdefab7fbaf7387fe1974ff87065aa7584123cd45c
data/jisx0402.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "bundler", "~> 1.10"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "pry"
25
26
  end
@@ -1,11 +1,11 @@
1
1
  class Jisx0402::DistrictArray < Array
2
2
  def self.wrap(ary)
3
3
  this = new
4
- ary.map{|e| this << ary }
4
+ ary.flatten.map{|e| this << e }
5
5
  return this
6
6
  end
7
7
 
8
8
  def zipcodes
9
- self.flatten.map(&:zipcodes).flatten.uniq
9
+ self.map(&:zipcodes).flatten.uniq.compact
10
10
  end
11
11
  end
@@ -0,0 +1,52 @@
1
+ class Jisx0402::Tree
2
+ class Root
3
+ def initialize
4
+ @root_node = Node.new
5
+ end
6
+
7
+ def []=(key, val)
8
+ chars = key.chars
9
+ @root_node.insert(chars.shift, chars, val)
10
+ end
11
+
12
+ def [](key)
13
+ chars = key.chars
14
+ @root_node.search(chars.shift, chars)
15
+ end
16
+
17
+ def values
18
+ @root_node.values
19
+ end
20
+ end
21
+
22
+ class Node
23
+ attr_accessor :value
24
+
25
+ def initialize
26
+ @data = {}
27
+ @value = nil
28
+ end
29
+
30
+ def insert(key, remain, val)
31
+ @data[key] ||= Node.new
32
+ if remain.empty?
33
+ @data[key].value = val
34
+ else
35
+ @data[key].insert(remain.shift, remain, val)
36
+ end
37
+ end
38
+
39
+ def search(key, remain)
40
+ return nil unless @data[key]
41
+ if remain.empty?
42
+ @data[key].values
43
+ else
44
+ @data[key].search(remain.shift, remain)
45
+ end
46
+ end
47
+
48
+ def values
49
+ [value, @data.values.map(&:values)].flatten.compact
50
+ end
51
+ end
52
+ end
@@ -1,3 +1,3 @@
1
1
  module Jisx0402
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/jisx0402.rb CHANGED
@@ -1,46 +1,76 @@
1
1
  require "jisx0402/version"
2
2
  require 'jisx0402/district_array'
3
+ require 'jisx0402/tree'
3
4
  require 'msgpack'
5
+ require 'pry'
4
6
 
5
7
  module Jisx0402
6
8
  class << self
9
+ TREE_INDEX_KEYS = %i(code full)
7
10
  def search
8
11
  data
9
12
  end
10
13
 
11
- def search(word, by: nil)
12
- way = %w(code prefecture prefecture_yomi district district_yomi full full_yomi)
14
+ def forward_match_by_full(chunk)
15
+ forward_match_by(:full, chunk)
16
+ end
17
+
18
+ def forward_match_by_code(chunk)
19
+ forward_match_by(:code, chunk)
20
+ end
21
+
22
+ def forward_match_by(by, chunk)
23
+ ary = data_trees_index[by.to_sym][chunk.to_s] || []
24
+ return Jisx0402::DistrictArray.wrap(ary)
25
+ end
26
+
27
+ def match_by_zipcode(zipcode)
28
+ zipcode_to_jisx0402_table[zipcode]
29
+ end
13
30
 
31
+ def search(word, by: nil)
14
32
  if by
15
- result = data.select do |d|
16
- d.at(way.index(by.to_s)) =~ /#{word}/
17
- end
18
- if result.size == 1
19
- return Code.new(result.first)
20
- else
21
- Jisx0402::DistrictArray.wrap(
22
- result.map{|r| Code.new(r) }
23
- )
24
- end
33
+ result = data.select { |d| d.match?(word, by) }
25
34
  else
26
- result = way.map do |w|
27
- search(word, by: w)
28
- end
29
- result = result.flatten.uniq{|e| e.code }
30
- if result.size == 1
31
- return result.first
32
- else
33
- return Jisx0402::DistrictArray.wrap(result)
34
- end
35
+ result = Code::ATTRS_INDEX.map { |w| search(word, by: w) }.uniq.flatten
36
+ end
37
+
38
+ if result.size == 1
39
+ return result.first
40
+ else
41
+ Jisx0402::DistrictArray.wrap(result)
35
42
  end
36
43
  end
37
44
 
38
45
  def data
39
- @@data ||= open_msgpack_data('jisx0402.msgpack')
46
+ @@data ||= open_msgpack_data('jisx0402.msgpack').map do |d|
47
+ Code.new(d)
48
+ end
49
+ end
50
+
51
+ def data_trees_index
52
+ @@data_trees_index ||= begin
53
+ TREE_INDEX_KEYS.map.with_object({}) do |idx_key, h|
54
+ h[idx_key] = Jisx0402::Tree::Root.new
55
+ data.each { |d| h[idx_key][d.send(idx_key.to_sym)] = d }
56
+ end
57
+ end
58
+ end
59
+
60
+ def zipcode_to_jisx0402_table
61
+ @@zipcode_to_jisx0402_table ||= begin
62
+ jisx0402_to_zipcode_table.map.with_object({}) do |(jisx0402, zipcodes), hash|
63
+ zipcodes.map do |zipcode|
64
+ hash[zipcode] = forward_match_by_code(jisx0402).first
65
+ end
66
+
67
+ hash
68
+ end
69
+ end
40
70
  end
41
71
 
42
- def zipcodes_table
43
- @@zipcodes_table ||= open_msgpack_data('jisx0402_to_zipcode.msgpack')
72
+ def jisx0402_to_zipcode_table
73
+ @@jisx0402_to_zipcode_table ||= open_msgpack_data('jisx0402_to_zipcode.msgpack')
44
74
  end
45
75
 
46
76
  def open_msgpack_data(fname)
@@ -51,10 +81,24 @@ module Jisx0402
51
81
  end
52
82
 
53
83
  class Code
84
+ ATTRS_INDEX = %w(
85
+ code
86
+ prefecture
87
+ prefecture_yomi
88
+ district
89
+ district_yomi
90
+ full
91
+ full_yomi
92
+ ).freeze
93
+
54
94
  def initialize(row)
55
95
  @row = row
56
96
  end
57
97
 
98
+ def match?(word, by)
99
+ @row.at(ATTRS_INDEX.index(by.to_s)) =~ /#{word}/
100
+ end
101
+
58
102
  def code
59
103
  @row[0]
60
104
  end
@@ -80,7 +124,7 @@ module Jisx0402
80
124
  end
81
125
 
82
126
  def zipcodes
83
- Jisx0402.zipcodes_table[code_without_checkdigit]
127
+ Jisx0402.jisx0402_to_zipcode_table[code_without_checkdigit] || []
84
128
  end
85
129
  end
86
130
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jisx0402
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - cnosuke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-19 00:00:00.000000000 Z
11
+ date: 2016-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description:
70
84
  email:
71
85
  - shinnosuke@gmail.com
@@ -88,6 +102,7 @@ files:
88
102
  - lib/data/jisx0402_to_zipcode.msgpack
89
103
  - lib/jisx0402.rb
90
104
  - lib/jisx0402/district_array.rb
105
+ - lib/jisx0402/tree.rb
91
106
  - lib/jisx0402/version.rb
92
107
  homepage: https://github.com/cnosuke/jisx0402
93
108
  licenses: