kanjidic 0.4.0 → 0.4.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/kanjidic +9 -2
  3. data/lib/kanjidic.rb +37 -4
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a84afcc6326773e3dce339b1a5e7308d6ff38c4
4
- data.tar.gz: dfb53b89d2eefe510953ae9c3352239eb87b2e4a
3
+ metadata.gz: 8cb9fc88a4e195baca868cb39878d4c8691072ce
4
+ data.tar.gz: 5c070cc640d2f802662ad5dac854ded9f90717cd
5
5
  SHA512:
6
- metadata.gz: 3abbfb813e3532e9c37aba8349ccf6c18369a3ead426b13e4e3f51e0570233b5ed4bf45a8171886c823b398dea72e1eced94d6c5aa5bde0b8d738a6a2cbf8686
7
- data.tar.gz: ed7999d8710a4cebe97106c937812eaf9dca3f6b6ee925d73b9d4857ded86249f3c7141f3c971ebb4bdbe9827aeb302485f1ea0ff85dbff9e1d5c8423d1b7fb8
6
+ metadata.gz: 18763a461c35e4e02353ab06813eefe926767aeacf1bfbaafd7ecc40f4233fc6b6e784dd0b2eae67fa52bf55952c658acde4095e1f8dc19bb9f69c05a9a542bc
7
+ data.tar.gz: 8d229595ec660f8db02849bcaf379585fecc794aaf165d29ece4adfdf7a7ae7f19bcff1cb556fe2d1fb2c7201d1588c80b53fa0c78c4d4423e5eb31359110755
@@ -5,13 +5,20 @@ require "optparse"
5
5
  require "irb"
6
6
 
7
7
  file_name = ARGV[0]
8
+ unless file_name
9
+ $stderr.puts "Usage:\n\tkanjidic FILE [options]\n\tkanjidic FILE [options] -e {expression}"
10
+ exit
11
+ end
12
+ unless File.exist? file_name
13
+ $stderr.puts "No such file: #{file_name}"
14
+ exit
15
+ end
8
16
  defaults = { file: file_name, jis: 0, ext_jis: [1, 2] }
9
- raise "No such file #{file_name}" unless File.exist? file_name
10
17
  options = {}
11
18
 
12
19
  OptionParser.new do |opts|
13
20
 
14
- opts.banner = "Usage: \tkanjidic FILE [options]\n\t\t\tkanjidic FILE [options] -e {expression}"
21
+ opts.banner = "Usage:\n\tkanjidic FILE [options]\n\tkanjidic FILE [options] -e {expression}"
15
22
 
16
23
  opts.on("-e EXPRESSION", "--expression EXPRESSION", "Run a Ruby expression inside the context of the dictionary") do |e|
17
24
  options[:expression] = e
@@ -214,7 +214,7 @@ module Kanjidic
214
214
  # Takes a boolean parameter to indicate whether the regexp should be constructed from
215
215
  # scratches as opposed to retrieved from a cached value, false by default (returns the cache).
216
216
  #
217
- #The resulting regexp will return matches as follow:
217
+ # The resulting regexp will return matches as follow:
218
218
  #
219
219
  # 3 groups (code, sub code, value) if the element is code based,
220
220
  #
@@ -283,8 +283,6 @@ module Kanjidic
283
283
  end
284
284
 
285
285
  # Alias for Kanjidic::all_symbols
286
- #
287
- # See Kanjidic::all_symbols
288
286
  def self.symbols
289
287
  all_symbols
290
288
  end
@@ -301,7 +299,7 @@ module Kanjidic
301
299
  }.to_h)
302
300
  end
303
301
 
304
- # Return a hash of all symboles not associated with a letter code
302
+ # Return a hash of all symboles not associated with a letter code.
305
303
  #
306
304
  # The values are the description strings
307
305
  def self.uncoded_symbols
@@ -347,6 +345,22 @@ module Kanjidic
347
345
  end
348
346
  end
349
347
 
348
+
349
+ # Basic search tool matching any entry in the dictionary that matches the conditions
350
+ # given in parameter
351
+ #
352
+ # Example:
353
+ # search pronunciation: "あ", jlpt: '1'
354
+ def self.search h = {}
355
+ raise "Load the dictionary before searching" unless @@dic
356
+ @@dic.select do |kanji|
357
+ h.inject(true) do |acc, a|
358
+ key, value = a
359
+ acc && _include?(value, kanji[key])
360
+ end
361
+ end
362
+ end
363
+
350
364
  # Insert values in a hash depending on the previous content of the hash
351
365
  #
352
366
  # Essentially a deep_merge implementation..
@@ -391,5 +405,24 @@ module Kanjidic
391
405
  }
392
406
  r ? " (#{r[:character]})" : ""
393
407
  end
408
+
409
+ private_class_method def self._include? value, target
410
+ case target
411
+ when Hash
412
+ value.is_a?(Hash) ?
413
+ (value.to_a.inject(true) { |acc, a|
414
+ k, v = a
415
+ acc && (target[k] == v)
416
+ }) : false
417
+ when Array
418
+ value.is_a?(Array) ?
419
+ (value.inject(true) { |acc, e|
420
+ acc && target.include?(e)
421
+ }) : target.include?(value)
422
+
423
+ else
424
+ value == target
425
+ end
426
+ end
394
427
  end
395
428
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanjidic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Leclercq