lucarecord 0.2.22 → 0.2.23

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
  SHA256:
3
- metadata.gz: 217630d4010c2341954f05600982cf641470b244440849c7816873ed8d10e8d2
4
- data.tar.gz: f535546634069491d64b3e0359fc9a40f10059cd08d216f8a947c28d717d1c6c
3
+ metadata.gz: a408c56d2c0f47238bc8c17e4135867e1be31b9897f940370a9bc8e87f8d014e
4
+ data.tar.gz: 66fead2c3441f8b019f8d805fe564dbf9a9121abec424ea5d5482277d7bcc3ad
5
5
  SHA512:
6
- metadata.gz: 9e69a01066562a854ebe78128ead7c7d87be1227cfbce7453edf5510fa03f7b2acfbea5b334837b1c80e49e71a8a858d2d273277769e096baf29e4c90977816b
7
- data.tar.gz: c7caf90c504940b0533441f19cde460fbb680f69937412659ac04670f009b74ed84c5a2aa120f2554a7e4cfab8d4eba62309cd5c0ace1e057fc09400fdc05cee
6
+ metadata.gz: a6f3d2e4773bc6c29c657a7fbe5864893efa39ec739ae1df4eece68fa82907bf99feb72f5a43888b59e39bc9f9a93f023fa4c0243e2429d3c04ba2c2f09d2e20
7
+ data.tar.gz: 7fd98c339f4b81430dc961b009233e6de65228f6df83d361b81f4040857045740f0d2d18363e29bd2579be6b3d19a5a09cca37f9e39cfc1a8703212c32ba4e78
@@ -1,3 +1,7 @@
1
+ ## LucaRecord 0.2.23
2
+
3
+ * Enhance Dictionary, supporting extensible options.
4
+
1
5
  ## LucaRecord 0.2.22
2
6
 
3
7
  * add `LucaSupport::View.nushell()`, render nushell table directly.
@@ -6,7 +6,6 @@ require 'yaml'
6
6
  require 'pathname'
7
7
  require 'luca_support'
8
8
 
9
- #
10
9
  # Low level API
11
10
  #
12
11
  module LucaRecord
@@ -14,66 +13,40 @@ module LucaRecord
14
13
  include LucaSupport::Code
15
14
 
16
15
  def initialize(file = @filename)
17
- #@path = file
18
16
  @path = self.class.dict_path(file)
19
17
  set_driver
20
18
  end
21
19
 
22
- def search(word, default_word = nil)
23
- res = max_score_code(word.gsub(/[[:space:]]/, ''))
24
- if res[1] > 0.4
25
- res[0]
20
+ # Search word with n-gram.
21
+ # If dictionary has Hash or Array, it returns [label, options].
22
+ #
23
+ def search(word, default_word = nil, main_key: 'label', options: nil)
24
+ res, score = max_score_code(word.gsub(/[[:space:]]/, ''))
25
+ return default_word if score < 0.4
26
+
27
+ case res
28
+ when Hash
29
+ hash2multiassign(res, main_key, options: options)
30
+ when Array
31
+ res.map { |item| hash2multiassign(item, main_key, options: options) }
26
32
  else
27
- default_word
33
+ res
28
34
  end
29
35
  end
30
36
 
37
+ # Separate main item from other options.
38
+ # If options specified as Array of string, it works as safe list filter.
31
39
  #
32
- # Column number settings for CSV/TSV convert
33
- #
34
- # :label
35
- # for double entry data
36
- # :counter_label
37
- # must be specified with label
38
- # :debit_label
39
- # for double entry data
40
- # * debit_value
41
- # :credit_label
42
- # for double entry data
43
- # * credit_value
44
- # :note
45
- # can be the same column as another label
46
- #
47
- # :encoding
48
- # file encoding
49
- #
50
- def csv_config
51
- {}.tap do |config|
52
- if @config.dig('label')
53
- config[:label] = @config['label'].to_i
54
- if @config.dig('counter_label')
55
- config[:counter_label] = @config['counter_label']
56
- config[:type] = 'single'
57
- end
58
- elsif @config.dig('debit_label')
59
- config[:debit_label] = @config['debit_label'].to_i
60
- if @config.dig('credit_label')
61
- config[:credit_label] = @config['credit_label'].to_i
62
- config[:type] = 'double'
63
- end
40
+ def hash2multiassign(obj, main_key = 'label', options: nil)
41
+ options = {}.tap do |opt|
42
+ obj.map do |k, v|
43
+ next if k == main_key
44
+ next if !options.nil? && !options.include?(k)
45
+
46
+ opt[k.to_sym] = v
64
47
  end
65
- config[:type] ||= 'invalid'
66
- config[:debit_value] = @config['debit_value'].to_i if @config.dig('debit_value')
67
- config[:credit_value] = @config['credit_value'].to_i if @config.dig('credit_value')
68
- config[:note] = @config['note'] if @config.dig('note')
69
- config[:encoding] = @config['encoding'] if @config.dig('encoding')
70
-
71
- config[:year] = @config['year'] if @config.dig('year')
72
- config[:month] = @config['month'] if @config.dig('month')
73
- config[:day] = @config['day'] if @config.dig('day')
74
- config[:default_debit] = @config['default_debit'] if @config.dig('default_debit')
75
- config[:default_credit] = @config['default_credit'] if @config.dig('default_credit')
76
48
  end
49
+ [obj[main_key], options.compact]
77
50
  end
78
51
 
79
52
  #
@@ -122,6 +95,17 @@ module LucaRecord
122
95
  end
123
96
  end
124
97
 
98
+ def self.validate(filename, target_key = :label)
99
+ errors = load(filename).map { |k, v| v[target_key].nil? ? k : nil }.compact
100
+ if errors.empty?
101
+ puts 'No error detected.'
102
+ nil
103
+ else
104
+ "Key #{errors.join(', ')} has nil #{target_key}."
105
+ errors.count
106
+ end
107
+ end
108
+
125
109
  private
126
110
 
127
111
  def set_driver
@@ -140,7 +124,7 @@ module LucaRecord
140
124
 
141
125
  def max_score_code(str)
142
126
  res = @definitions.map do |k, v|
143
- [v, LucaSupport.match_score(str, k, 3)]
127
+ [v, match_score(str, k, 3)]
144
128
  end
145
129
  res.max { |x, y| x[1] <=> y[1] }
146
130
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LucaRecord
4
- VERSION = '0.2.22'
4
+ VERSION = '0.2.23'
5
5
  end
@@ -6,15 +6,4 @@ module LucaSupport
6
6
  autoload :Config, 'luca_support/config'
7
7
  autoload :Mail, 'luca_support/mail'
8
8
  autoload :View, 'luca_support/view'
9
-
10
- def self.match_score(a, b, n = 2)
11
- v_a = to_ngram(a, n)
12
- v_b = to_ngram(b, n)
13
-
14
- v_a.map { |item| v_b.include?(item) ? 1 : 0 }.sum / v_a.length.to_f
15
- end
16
-
17
- def self.to_ngram(str, n = 2)
18
- str.each_char.each_cons(n).map(&:join)
19
- end
20
9
  end
@@ -101,7 +101,7 @@ module LucaSupport
101
101
  (start_year..end_year).to_a.map do |y|
102
102
  g1 = y == start_year ? encode_month(start_month) : encode_month(1)
103
103
  g2 = y == end_year ? encode_month(end_month) : encode_month(12)
104
- "#{y}[#{g1}-#{g2}]"
104
+ g1 == g2 ? "#{y}#{g1}" : "#{y}[#{g1}-#{g2}]"
105
105
  end
106
106
  end
107
107
 
@@ -114,6 +114,17 @@ module LucaSupport
114
114
  Digest::SHA1.hexdigest(SecureRandom.uuid)
115
115
  end
116
116
 
117
+ def match_score(a, b, n = 2)
118
+ v_a = to_ngram(a, n)
119
+ v_b = to_ngram(b, n)
120
+
121
+ v_a.map { |item| v_b.include?(item) ? 1 : 0 }.sum / v_a.length.to_f
122
+ end
123
+
124
+ def to_ngram(str, n = 2)
125
+ str.each_char.each_cons(n).map(&:join)
126
+ end
127
+
117
128
  def decimalize(obj)
118
129
  case obj.class.name
119
130
  when 'Array'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lucarecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.22
4
+ version: 0.2.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuma Takahiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-22 00:00:00.000000000 Z
11
+ date: 2020-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail