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 +4 -4
- data/jisx0402.gemspec +1 -0
- data/lib/jisx0402/district_array.rb +2 -2
- data/lib/jisx0402/tree.rb +52 -0
- data/lib/jisx0402/version.rb +1 -1
- data/lib/jisx0402.rb +69 -25
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5de0232be3ec65da31e16dc07ab76e475082c427
|
4
|
+
data.tar.gz: 84182ccad0d6aa9720c74eef71680ddab5037707
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3041634f80f824876e0034222f92771280324c2f0d51d21aac03923b4cfcc26a01e93439f6fc39df4fb884182dffe12e74ed9994013a385f930e4131a1617cf
|
7
|
+
data.tar.gz: 4d166002be767dd41e92a70a9bfc0937194d0fd8438f8f2105ec12379996225cc39dfaecbb579b03e73122bdefab7fbaf7387fe1974ff87065aa7584123cd45c
|
data/jisx0402.gemspec
CHANGED
@@ -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 <<
|
4
|
+
ary.flatten.map{|e| this << e }
|
5
5
|
return this
|
6
6
|
end
|
7
7
|
|
8
8
|
def zipcodes
|
9
|
-
self.
|
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
|
data/lib/jisx0402/version.rb
CHANGED
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
|
12
|
-
|
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
|
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 =
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
43
|
-
@@
|
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.
|
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.
|
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-
|
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:
|