klookup 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/cklookup CHANGED
@@ -64,7 +64,7 @@ DOC
64
64
  puts radicals
65
65
  puts radicals.size
66
66
  elsif str.match /^\s*kanji\s+with\s+(.+)$/
67
- kanji = Kanji.lookup(nil, $1.split(//))
67
+ kanji = Kanji.lookup(:radical=>$1.split(//))
68
68
  puts kanji
69
69
  puts kanji.size
70
70
  elsif str.match /^\s*meaning\s+of\s+(.)\s*$/
data/bin/klookup.cgi CHANGED
@@ -102,6 +102,24 @@ def put_list(items)
102
102
  puts "<p>Total: " + items.size.to_s
103
103
  end
104
104
 
105
+ def show_radicals(params)
106
+ puts "<p><a href=\"?"+query_string(params.merge({'show_radicals'=>['false']}))+'">'+_('Hide radicals')+'</a>'
107
+ puts '<table>'
108
+ tparams = params.dup
109
+ tparams.delete 'kanji'
110
+ Radical.strokes.each { |strokes|
111
+ puts '<tr>'
112
+ puts '<td>' + strokes.to_s + ':'
113
+ Radical.list(strokes).each { |rad|
114
+ puts '<td>'
115
+ put_link rad.to_s, '?'+query_string(tparams.merge({'radicals'=>[params['radicals'].join+rad.to_s]}))
116
+ puts '</td>'
117
+ }
118
+ puts '</tr>'
119
+ }
120
+ puts '</table>'
121
+ end
122
+
105
123
  def run
106
124
  cgi = CGI.new
107
125
  params = cgi.params
@@ -110,7 +128,14 @@ def run
110
128
  puts '<style type="text/css">'
111
129
  puts 'a {text-decoration: none}'
112
130
  puts '</style>' # This could be external
113
- puts '<title>' + _('klookup %s') + '</title>'
131
+ puts '<title>' + _('klookup') + '</title>'
132
+
133
+ if not params['show_radicals'].empty? and params['show_radicals'].first=='true'
134
+ show_radicals(params)
135
+ else
136
+ puts "<p><a href=\"?"+query_string(params.merge({'show_radicals'=>['true']}))+'">'+_('Show radicals')+'</a>'
137
+ end
138
+ puts '<hr>'
114
139
 
115
140
  if not params['radicals'].empty?
116
141
  radicals = []
@@ -130,7 +155,7 @@ def run
130
155
  problem = true unless Radical.exist?(r) }
131
156
 
132
157
  unless problem
133
- kanji = Kanji.lookup(nil, radicals)
158
+ kanji = Kanji.lookup(:radical=>radicals)
134
159
  kanji.collect! { |k|
135
160
  get_kanji(k, params)
136
161
  }
@@ -168,27 +193,6 @@ def run
168
193
  puts '<p>' + _('Hello there. This is the greatest kanji lookup tool in existence!')
169
194
  end
170
195
 
171
- puts '<hr>'
172
- if not params['show_radicals'].empty? and params['show_radicals'].first=='true'
173
- puts "<p><a href=\"?"+query_string(params.merge({'show_radicals'=>['false']}))+'">'+_('Hide radicals')+'</a>'
174
- puts '<table>'
175
- tparams = params.dup
176
- tparams.delete 'kanji'
177
- Radical.strokes.each { |strokes|
178
- puts '<tr>'
179
- puts '<td>' + strokes.to_s + ':'
180
- Radical.list(strokes).each { |rad|
181
- puts '<td>'
182
- put_link rad.to_s, '?'+query_string(tparams.merge({'radicals'=>[params['radicals'].join+rad.to_s]}))
183
- puts '</td>'
184
- }
185
- puts '</tr>'
186
- }
187
- puts '</table>'
188
- else
189
- puts "<p><a href=\"?"+query_string(params.merge({'show_radicals'=>['true']}))+'">'+_('Show radicals')+'</a>'
190
- end
191
-
192
196
  puts <<DOC
193
197
  <hr>
194
198
  <p><a href="?">Start over</a>
data/lib/klookup.rb CHANGED
@@ -13,7 +13,12 @@
13
13
  begin
14
14
  require 'active_support'
15
15
  rescue LoadError
16
+ # begin
17
+ # require 'rubygems'
18
+ # require 'active_support'
19
+ # rescue LoadError
16
20
  require 'runicode'
21
+ # end
17
22
  end
18
23
 
19
24
  #This allows regular expressions to work on characters.
@@ -26,15 +26,15 @@ class KLookup::Database
26
26
  # Opens a resource.
27
27
  #
28
28
  # Priority: LOOKUP_PATH environment variable, Gem load path.
29
- def self.open_resource(path)
29
+ def self.open_resource(path, mod='klookup')
30
30
  # Choose a directory
31
31
  env=ENV['KLOOKUP_PATH']
32
32
  if env and env != ''
33
- dir=env+'/klookup'
33
+ dir=env+"/#{mod}"
34
34
  else
35
35
  begin
36
- gem 'klookup'
37
- dir=Gem.datadir 'klookup'
36
+ gem mod
37
+ dir=Gem.datadir(mod)
38
38
  rescue NameError
39
39
  raise IOError, 'Could not find resource %s' % path
40
40
  end
@@ -49,17 +49,17 @@ class KLookup::Database
49
49
  end
50
50
  end
51
51
 
52
- alias :stroke_count_list :undefined
53
- alias :radicals_by_strokes :undefined
54
- alias :get_kanji :undefined
55
- alias :get_radical_strokes :undefined
56
- alias :get_kanji_strokes :undefined
57
- alias :get_radicals :undefined
52
+ alias :radical_stroke_count :undefined
53
+ alias :find_radical :undefined
54
+ alias :find_kanji :undefined
55
+ alias :get_stroke_count :undefined
56
+ alias :get_radical :undefined
58
57
  alias :get_reading :undefined
59
58
  alias :get_meaning :undefined
60
59
  alias :is_kanji? :undefined
61
60
  alias :is_radical? :undefined
62
61
 
63
62
  require 'klookup/database_flatfile'
64
- require 'klookup/database_unihan'
63
+ # require 'klookup/database_quickfile'
64
+ # require 'klookup/database_unihan'
65
65
  end
@@ -18,23 +18,60 @@ class KLookup::Database::FlatFile < KLookup::Database
18
18
  require 'singleton'
19
19
  include Singleton
20
20
 
21
- def stroke_count_list(*args)
21
+ def radical_stroke_count(*args)
22
22
  RadK.instance.stroke_count_list(*args)
23
23
  end
24
+
25
+ private
24
26
  def radicals_by_strokes
25
27
  RadK.instance.radicals_by_strokes
26
28
  end
27
- def get_kanji(strokes, *args)
28
- RadK.instance.get_kanji(*args).delete_if {|k|
29
- not strokes.nil? and not get_kanji_strokes(k) == strokes }
29
+ public
30
+
31
+ def find_radical(args={})
32
+ raise ArgumentError, 'a hash or nothing' unless args.kind_of?(Hash)
33
+ if not args[:stroke]
34
+ RadK.instance.radicals_by_strokes.values.flatten
35
+ elsif not args[:stroke].kind_of?(Integer)
36
+ raise ArgumentError, ':stroke must be an Integer'
37
+ elsif radical_stroke_count.include?(args[:stroke])
38
+ RadK.instance.radicals_by_strokes[args[:stroke]]
39
+ else
40
+ []
41
+ end
42
+ end
43
+ private
44
+ # If *args is nil, returns all kanji.
45
+ def get_kanji(strokes=nil, *args)
46
+ radicals = *args
47
+ if radicals.nil? or radicals.length == 0 or radicals.all? {|r| r.nil?}
48
+ kanji = KanjiDic.instance.get_all_kanji
49
+ else
50
+ kanji = RadK.instance.get_kanji(*args)
51
+ end
52
+ unless strokes.nil?
53
+ return kanji.select {|k| get_kanji_strokes(k) == strokes }
54
+ end
55
+ return kanji
56
+ end
57
+ public
58
+ def find_kanji(args)
59
+ get_kanji(args[:stroke], args[:radical])
30
60
  end
31
61
  def get_radical_strokes(*args)
32
62
  RadK.instance.get_strokes(*args)
33
63
  end
64
+ def get_stroke_count
65
+ begin
66
+ RadK.instance.get_strokes(*args)
67
+ rescue
68
+ KanjiDic.instance.get_strokes(*args)
69
+ end
70
+ end
34
71
  def get_kanji_strokes(*args)
35
72
  KanjiDic.instance.get_strokes(*args)
36
73
  end
37
- def get_radicals(*args)
74
+ def get_radical(*args)
38
75
  RadK.instance.get_radicals(*args)
39
76
  end
40
77
  def get_reading(*args)
@@ -69,6 +69,11 @@ class KLookup::Database::FlatFile::KanjiDic
69
69
  i =~ /^S\d+$/}.first.sub(/^S(\d+)$/, '\1').to_i
70
70
  end
71
71
 
72
+ # Returns an array of all kanji
73
+ def get_all_kanji
74
+ @records.keys
75
+ end
76
+
72
77
  #Returns a Struct of arrays of Japanese readings.
73
78
  #
74
79
  # KLookup::Database::FlatFile::KanjiDic.instance.get_reading('富')
@@ -106,10 +106,13 @@ class KLookup::Database::FlatFile::RadK
106
106
  # Returns a list of kanji corresponding to given radicals.
107
107
  def get_kanji(*radicals)
108
108
  kanji = nil
109
+ if radicals.nil? or radicals.length == 0 or radicals.all? {|r| r.nil?}
110
+ raise ArgumentError, "This method doesn't do that sort of thing."
111
+ end
109
112
  radicals.flatten.each { |rad|
110
- raise ArgumentError unless @records[rad.to_s]
113
+ raise ArgumentError, 'Radical does not exist' unless @records[rad.to_s]
111
114
  current_kanji = @records[rad.to_s][:kanji]
112
- if kanji==nil
115
+ if kanji.nil?
113
116
  kanji = current_kanji
114
117
  else
115
118
  kanji &= current_kanji
@@ -0,0 +1,38 @@
1
+ =begin
2
+
3
+ lib/klookup/database_flatfile.rb
4
+
5
+ Copyright © Tom Adams 2006
6
+
7
+ This programme is free software.
8
+ You can distribute/modify this program under
9
+ the terms of the Ruby License.
10
+
11
+ =end
12
+
13
+ # A singleton class to abstract RadK and KanjiDic.
14
+ class KLookup::Database::QuickFile < KLookup::Database
15
+
16
+ require 'singleton'
17
+ include Singleton
18
+
19
+ def radical_stroke_count
20
+ end
21
+ def find_radical(opts)
22
+ end
23
+ def find_kanji(opts)
24
+ end
25
+ def get_stroke_count(character)
26
+ end
27
+ def get_radical(character)
28
+ end
29
+ def get_reading(character)
30
+ end
31
+ def get_meaning(character)
32
+ end
33
+ def is_kanji?(character)
34
+ end
35
+ def is_radical?(character)
36
+ end
37
+
38
+ end
@@ -35,7 +35,7 @@ class KLookup::Database::Unihan < KLookup::Database
35
35
  private
36
36
 
37
37
  def initialize
38
- @unihan = KLookup::Database::Unihan.open_resource('Unihan.txt')
38
+ @unihan = KLookup::Database.open_resource('Unihan.txt','unihan')
39
39
  end
40
40
 
41
41
  # Returns the numeric codepoint of the first codepoint in +str+.
@@ -51,33 +51,6 @@ class KLookup::Database::Unihan < KLookup::Database
51
51
 
52
52
  public
53
53
 
54
- # Opens a resource.
55
- #
56
- # Priority: LOOKUP_PATH environment variable, Gem load path.
57
- def self.open_resource(path)
58
- # Choose a directory
59
- env=ENV['KLOOKUP_PATH']
60
- if env and env != ''
61
- dir=env+'/unihan'
62
- else
63
- dir=gem_path
64
- begin
65
- gem 'unihan'
66
- dir=Gem.datadir 'unihan'
67
- rescue NameError
68
- raise IOError, 'Could not find resource %s' % path
69
- end
70
- end
71
-
72
- # Open a file
73
- file = open("#{dir}/#{path}")
74
- if block_given?
75
- yield file
76
- else
77
- return file
78
- end
79
- end
80
-
81
54
  def stroke_count_list(*args)
82
55
  end
83
56
  def radicals_by_strokes
@@ -10,11 +10,76 @@
10
10
 
11
11
  =end
12
12
 
13
- # An abstract representation of 漢字 (kanji).
13
+ # An abstract representation of 漢字 (kanji/Chinese characters).
14
14
  class KLookup::Lookup::Kanji
15
- # A class variable because we have class methods which need this.
15
+ # A class variable because we have class methods which use this.
16
16
  @@data = KLookup::Lookup.default_handler
17
17
 
18
+ private
19
+ # Returns a string containing the UTF-8 encoded character represented by the
20
+ # receiver’s value.
21
+ #
22
+ # This should ideally be elsewhere - i.e. jcode or active_support
23
+ def self.int_to_utf8(val)
24
+ case val
25
+ when 0xD800..0xDFFF
26
+ raise RangeError, 'these values are surrogate pairs'
27
+ when 0x0000..0x007F
28
+ return "%c" % (val & 0b01111111) # 7
29
+ when 0x0080..0x07FF
30
+ byte = []
31
+ byte[0] = 0b11000000 + ((val >> 6) & 0b11111) # 5
32
+ byte[1] = 0b10000000 + (val & 0b111111) # 6
33
+ return "%c%c" % byte
34
+ when 0x0800..0xFFFF
35
+ byte = []
36
+ byte[0] = 0b11100000 + ((val >> 12) & 0b1111) # 4
37
+ byte[1] = 0b10000000 + ((val >> 6) & 0b111111) # 6
38
+ byte[2] = 0b10000000 + (val & 0b111111) # 6
39
+ return "%c%c%c" % byte
40
+ when 0x00010000..0x0010FFFF
41
+ byte = []
42
+ byte[0] = 0b11110000 + ((val >> 18) & 0b11111) # 5
43
+ byte[1] = 0b10000000 + ((val >> 12) & 0b111111) # 6
44
+ byte[2] = 0b10000000 + ((val >> 6) & 0b111111) # 6
45
+ byte[3] = 0b10000000 + (val & 0b111111) # 6
46
+ return "%c%c%c%c" % byte
47
+ else
48
+ raise RangeError, 'out of Unicode range'
49
+ end
50
+ end
51
+
52
+ # Returns a regular expression that matches strings in a kana-insensitive
53
+ # manner.
54
+ def self.norm_kana(str)
55
+ # Relevant codepoints:
56
+ # ひらがな == カタカナ
57
+ # 3041 - 3096 == 30A1 - 30F6 - ァ-ヶ
58
+ # 309D - 309E == 30FD - 30FE - ヽ-ヾ
59
+ hiragana = (0x3041..0x3096).to_a + (0x309D..0x309E).to_a
60
+ katakana = (0x30A1..0x30F6).to_a + (0x30FD..0x30FE).to_a
61
+ hkhash = {}
62
+ khhash = {}
63
+ i=0
64
+ hiragana.each {|c|
65
+ hkhash[c] = katakana[i]
66
+ khhash[katakana[i]] = c
67
+ i+=1
68
+ }
69
+ re=''
70
+ str.each_char {|c|
71
+ if hiragana.include?(c.chars.first)
72
+ re << "[#{c}#{int_to_utf8(hkhash[c.chars.first])}]"
73
+ elsif katakana.include?(c.chars.first)
74
+ re << "[#{c}#{int_to_utf8(khhash[c.chars.first])}]"
75
+ else
76
+ re << c
77
+ end
78
+ }
79
+ Regexp.new("^#{re}$")
80
+ end
81
+ public
82
+
18
83
  def initialize(kanji)
19
84
  unless @@data.instance.is_kanji?(kanji)
20
85
  raise ArgumentError
@@ -34,24 +99,60 @@ class KLookup::Lookup::Kanji
34
99
  @@data = h
35
100
  end
36
101
 
37
- # Returns an array of Kanji that are made up of these +radicals+ which have
38
- # +strokes+ strokes.
39
- def self.lookup(strokes=nil, *radicals)
40
- unless strokes.nil?
41
- raise ArgumentError unless strokes.respond_to?(:to_i)
42
- strokes=strokes.to_i
102
+ # Returns an array of Kanji made up of :radicals, with a particular :meaning,
103
+ # with a particular :reading, or with a particular number of :strokes.
104
+ # A block can also be used which yields to Kanji objects. With no arguments
105
+ # and no block, all characters in the database are returned (you have been
106
+ # warned!).
107
+ #
108
+ # Note that :meaning and :reading are slow.
109
+ def self.lookup(hash=nil)
110
+ # Check values of the hash
111
+ unless hash.respond_to?(:to_hash) or hash.nil?
112
+ raise ArgumentError, 'Requires a hash or a block.'
43
113
  end
114
+ hash = {} if hash.nil?
44
115
 
45
- raise ArgumentError unless radicals.respond_to?(:to_a)
46
- radicals = radicals.to_a.flatten
47
- raise ArgumentError unless radicals.size > 0
48
- radicals.collect! { |r|
49
- raise ArgumentError unless r.respond_to?(:to_s)
50
- r.to_s
51
- }
52
- return @@data.instance.get_kanji(strokes, radicals).collect {|k|
116
+ meaning = hash[:meaning]
117
+ meaning = [meaning] unless meaning.kind_of?(Array) or meaning.nil?
118
+
119
+ reading = hash[:reading]
120
+ reading = [reading] unless reading.kind_of?(Array) or reading.nil?
121
+
122
+ radical = hash[:radical]
123
+ unless radical.nil?
124
+ radical = [radical] unless radical.kind_of?(Array)
125
+ radical.map {|r| r.to_s }
126
+ end
127
+
128
+ stroke = hash[:stroke]
129
+ stroke = stroke.to_i unless stroke.nil?
130
+
131
+ kanji = @@data.instance.find_kanji(:stroke=>stroke, :radical=>radical).collect {|k|
53
132
  KLookup::Lookup::Kanji.new(k)
54
133
  }
134
+
135
+ # Don't filter
136
+ if reading.nil? and meaning.nil? and (not block_given?)
137
+ return kanji
138
+ end
139
+
140
+ truths = 0
141
+ # Filter according to hash and block
142
+ kanji = kanji.select {|k|
143
+ unless reading.nil?
144
+ next unless reading.any? {|r|
145
+ k.reading.reading.any?{|s| norm_kana(r) === s } or
146
+ k.reading.name_reading.any?{|s| norm_kana(r) === s } }
147
+ end
148
+ unless meaning.nil?
149
+ next unless meaning.any? {|m| k.meaning.include?(m) }
150
+ end
151
+ next if block_given? and not yield k
152
+ true
153
+ }
154
+
155
+ return kanji
55
156
  end
56
157
 
57
158
  # Returns true if kanji exists in database.
@@ -73,11 +174,12 @@ class KLookup::Lookup::Kanji
73
174
  end
74
175
 
75
176
  # Returns an array of radicals that make up a kanji.
76
- def radicals
77
- return @@data.instance.get_radicals(to_s).collect {|r|
177
+ def radical
178
+ return @@data.instance.get_radical(to_s).collect {|r|
78
179
  KLookup::Lookup::Radical.new(r)
79
180
  }
80
181
  end
182
+ alias :radicals :radical
81
183
 
82
184
  # Returns a textual representation of a radical.
83
185
  def to_s
@@ -34,18 +34,18 @@ class KLookup::Lookup::Radical
34
34
 
35
35
  # Return an array of how many strokes radicals can be made up of.
36
36
  def self.strokes
37
- @@data.instance.stroke_count_list
37
+ @@data.instance.radical_stroke_count
38
38
  end
39
39
 
40
40
  # Returns an array of radicals with +strokes+ strokes. Without an argument,
41
41
  # all radicals are returned.
42
42
  def self.list(strokes=nil)
43
43
  if strokes.nil?
44
- @@data.instance.radicals_by_strokes.values.flatten
44
+ @@data.instance.find_radical
45
45
  else
46
46
  raise ArgumentError unless strokes.respond_to?(:to_i)
47
47
 
48
- @@data.instance.radicals_by_strokes[strokes.to_i]
48
+ @@data.instance.find_radical(:stroke=>strokes.to_i)
49
49
  end
50
50
  end
51
51
 
@@ -0,0 +1,127 @@
1
+ require 'klookup'
2
+ require 'test/unit'
3
+
4
+ class Database_Test < Test::Unit::TestCase
5
+
6
+ def self.set_handler(cl)
7
+ @@dbc = cl
8
+ @@db = @@dbc.instance
9
+ end
10
+
11
+ # Asserts false with message +msg+ if +exception+ is not raised within
12
+ # the given block.
13
+ def must_except(exception=Exception, msg=nil)
14
+ raise LocalJumpError unless block_given?
15
+ begin
16
+ yield
17
+ rescue exception
18
+ else
19
+ assert false, msg
20
+ end
21
+ end
22
+
23
+
24
+ def test_open_resource
25
+ f = @@dbc.open_resource('newradkfile')
26
+ assert f.respond_to?(:each_line), 'valid path'
27
+
28
+ f = @@dbc.open_resource('newradkfile', 'klookup')
29
+ assert f.respond_to?(:each_line), 'valid path and valid gem'
30
+
31
+ flag=false
32
+ @@dbc.open_resource('newradkfile') {|f|
33
+ assert f.respond_to?(:each_line)
34
+ flag=true
35
+ }
36
+ assert flag, 'using a block'
37
+
38
+ must_except(Errno::ENOENT, 'invalid path') {
39
+ @@dbc.open_resource('all your base are belong to us')
40
+ }
41
+ must_except(Errno::ENOENT, 'invalid gem') {
42
+ @@dbc.open_resource('newradkfile', 'cookies are yummy')
43
+ }
44
+ end
45
+
46
+ def test_methods_exist
47
+ [:radical_stroke_count,
48
+ :find_radical,
49
+ :find_kanji, #TODO
50
+ :get_stroke_count, #TODO
51
+ :get_radical, #TODO
52
+ :get_reading, #TODO
53
+ :get_meaning, #TODO
54
+ :is_kanji?, #TODO
55
+ :is_radical? #TODO
56
+ ].each {|m|
57
+ assert @@db.respond_to?(m), "#{m.to_s} exists"
58
+ }
59
+ assert @@dbc.respond_to?(:open_resource)
60
+ end
61
+
62
+ def test_radical_stroke_count
63
+ assert @@db.radical_stroke_count.kind_of?(Enumerable), 'an Enumerable'
64
+ assert (@@db.radical_stroke_count.size > 0), 'more than zero'
65
+ assert @@db.radical_stroke_count.each {|s|
66
+ assert s.kind_of?(Integer), 'an Integer'
67
+ }
68
+ end
69
+
70
+ def test_find_radical
71
+ test = @@db.find_radical(:stroke=>11)
72
+ assert test.include?('鳥')
73
+ assert (not test.include?('馬'))
74
+ test = @@db.find_radical(:stroke=>10)
75
+ assert test.include?('馬')
76
+ assert (not test.include?('鳥'))
77
+ test = @@db.find_radical
78
+ assert test.include?('鳥')
79
+ assert test.include?('馬')
80
+ newtest = @@db.find_radical(:stroke=>nil)
81
+ assert test==newtest, 'Sets are identical'
82
+ test = @@db.find_radical(:stroke=>0)
83
+ assert test.size == 0, 'Size is zero'
84
+
85
+ must_except(ArgumentError, 'not a hash (String)') {
86
+ @@db.find_radical('cats')
87
+ }
88
+ must_except(ArgumentError, 'not a hash (Integer)') {
89
+ @@db.find_radical(6)
90
+ }
91
+ must_except(ArgumentError, 'invalid :stroke') {
92
+ @@db.find_radical(:stroke=>'nobbly knees')
93
+ }
94
+ test = @@db.find_radical(:stroke=>10,:cats=>:dogs)
95
+ assert test.include?('馬')
96
+ assert (not test.include?('鳥'))
97
+ end
98
+
99
+ def test_find_kanji
100
+ end
101
+
102
+ end
103
+
104
+
105
+
106
+ require 'test/unit/ui/console/testrunner'
107
+ results=[]
108
+ db=KLookup::Database
109
+ puts
110
+ puts "= Database_Test"
111
+ # Just use FlatFile for the moment
112
+ #[:FlatFile].each {|h|
113
+ db.constants.each {|h|
114
+ handler = "#{db.to_s}::#{h}"
115
+ next unless eval(handler).superclass == db
116
+ puts
117
+ puts "== Handler: #{handler}"
118
+ Database_Test.set_handler(eval(handler)) # Database class
119
+ results << [handler, Test::Unit::UI::Console::TestRunner.run(Database_Test)]
120
+ puts
121
+ }
122
+
123
+ results.each {|p|
124
+ h,r=p
125
+ puts "#{r.passed? ? 'PASS' : 'FAIL'} : #{h}"
126
+ puts (" %s" % r.to_s) unless r.passed?
127
+ }
data/test/suite.rb CHANGED
@@ -15,11 +15,13 @@ begin
15
15
  rescue LoadError
16
16
  end
17
17
 
18
+ require 'database_test.rb'
19
+
18
20
  require 'klookup'
19
21
  require 'test/unit'
20
22
 
21
- # KLookup::Lookup tests
22
- class KLookup_Test < Test::Unit::TestCase
23
+ # Lookup::Lookup tests
24
+ class Lookup_Test < Test::Unit::TestCase
23
25
 
24
26
  # Check classes
25
27
  def test_radical_type
@@ -46,24 +48,22 @@ class KLookup_Test < Test::Unit::TestCase
46
48
  # Lookups should work
47
49
  def test_kanji_lookup
48
50
  # Cat
49
- assert KLookup::Lookup::Kanji.lookup(nil, KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')).include?(KLookup::Lookup::Kanji.new('猫')), 'Without stroke count. Grass, left-dog, field => cat'
50
- assert KLookup::Lookup::Kanji.lookup(11, KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')).include?(KLookup::Lookup::Kanji.new('猫')), 'With stroke count. Grass, left-dog, field => cat'
51
- assert KLookup::Lookup::Kanji.lookup(nil, [KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')]).include?(KLookup::Lookup::Kanji.new('猫')), 'Without stroke count. Arrayed grass, left-dog, field => cat'
52
- assert KLookup::Lookup::Kanji.lookup(11, [KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')]).include?(KLookup::Lookup::Kanji.new('猫')), 'With stroke count. Arrayed grass, left-dog, field => cat'
51
+ assert KLookup::Lookup::Kanji.lookup(:radical=>[KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')]).include?(KLookup::Lookup::Kanji.new('猫')), 'Without stroke count. Arrayed grass, left-dog, field => cat'
52
+ assert KLookup::Lookup::Kanji.lookup(:stroke=>11, :radical=>[KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')]).include?(KLookup::Lookup::Kanji.new('猫')), 'With stroke count. Arrayed grass, left-dog, field => cat'
53
53
 
54
54
  # Chestnut
55
- assert KLookup::Lookup::Kanji.lookup(nil, KLookup::Lookup::Radical.new('西'), KLookup::Lookup::Radical.new('木')).include?(KLookup::Lookup::Kanji.new('栗')), 'Without stroke count. West, tree => chestnut.'
56
- assert KLookup::Lookup::Kanji.lookup(10, KLookup::Lookup::Radical.new('西'), KLookup::Lookup::Radical.new('木')).include?(KLookup::Lookup::Kanji.new('栗')), 'With stroke count. West, tree => chestnut.'
55
+ assert KLookup::Lookup::Kanji.lookup(:stroke=>nil, :radical=>[KLookup::Lookup::Radical.new('西'), KLookup::Lookup::Radical.new('木')]).include?(KLookup::Lookup::Kanji.new('栗')), 'Without stroke count. West, tree => chestnut.'
56
+ assert KLookup::Lookup::Kanji.lookup(:stroke=>10, :radical=>[KLookup::Lookup::Radical.new('西'), KLookup::Lookup::Radical.new('木')]).include?(KLookup::Lookup::Kanji.new('栗')), 'With stroke count. West, tree => chestnut.'
57
57
 
58
58
  # Test that radicals are using to_s
59
- assert KLookup::Lookup::Kanji.lookup(10, '西'.to_sym, KLookup::Lookup::Radical.new('木'.to_sym)).include?(KLookup::Lookup::Kanji.new('栗'.to_sym)), 'With stroke count (and symbols). West, tree => chestnut.'
59
+ assert KLookup::Lookup::Kanji.lookup(:radical=>10, :radical=>['西'.to_sym, KLookup::Lookup::Radical.new('木'.to_sym)]).include?(KLookup::Lookup::Kanji.new('栗'.to_sym)), 'With stroke count (and symbols). West, tree => chestnut.'
60
60
  end
61
61
 
62
62
  # Checks stroke count works
63
63
  def test_kanji_lookup_stroke
64
64
  # Unmatching stroke counts should result in empty lists
65
- assert KLookup::Lookup::Kanji.lookup(10, [KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')])==[], '10 strokes for grass, left-dog, and field is impossible.'
66
- assert KLookup::Lookup::Kanji.lookup(12, [KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')])==[], '12 strokes for grass, left-dog, and field results in no characters.'
65
+ assert KLookup::Lookup::Kanji.lookup(:stroke=>10, :radical=>[KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')])==[], '10 strokes for grass, left-dog, and field is impossible.'
66
+ assert KLookup::Lookup::Kanji.lookup(:stroke=>12, :radical=>[KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')])==[], '12 strokes for grass, left-dog, and field results in no characters.'
67
67
  end
68
68
 
69
69
  def test_kanji_radicals
@@ -125,19 +125,89 @@ class KLookup_Test < Test::Unit::TestCase
125
125
  assert KLookup::Lookup::Radical.exist?('田')
126
126
  assert (not KLookup::Lookup::Radical.exist?('た'))
127
127
  end
128
+
129
+ def test_just_meaning_lookup
130
+ cat_m = KLookup::Lookup::Kanji.lookup(:meaning=>'cat')
131
+ cat = KLookup::Lookup::Kanji.new('猫')
132
+ dog = KLookup::Lookup::Kanji.new('犬')
133
+ assert (cat_m.include?(cat) and not cat_m.include?(dog)), ':meaning'
134
+ end
135
+
136
+ def test_meaning_lookup
137
+ cat_m = KLookup::Lookup::Kanji.lookup(:meaning=>'cat')
138
+ cat_ms = KLookup::Lookup::Kanji.lookup(:meaning=>'cat', :stroke=>11)
139
+ cat_mss = KLookup::Lookup::Kanji.lookup(:meaning=>'cat', :stroke=>12)
140
+ cat = KLookup::Lookup::Kanji.new('猫')
141
+ dog = KLookup::Lookup::Kanji.new('犬')
142
+ assert (cat_m.include?(cat) and not cat_m.include?(dog)), ':meaning'
143
+ assert (cat_ms.include?(cat) and not cat_ms.include?(dog)), ':meaning and valid :stroke'
144
+ assert (not cat_mss.include?(cat) and not cat_mss.include?(dog)), ':meaning and invalid :stroke'
145
+ end
146
+
147
+ def test_reading_lookup
148
+ cat_r = KLookup::Lookup::Kanji.lookup(:reading=>'ねこ')
149
+ cat_rs = KLookup::Lookup::Kanji.lookup(:reading=>'ねこ', :stroke=>11)
150
+ cat_rss = KLookup::Lookup::Kanji.lookup(:reading=>'ねこ', :stroke=>12)
151
+ cat = KLookup::Lookup::Kanji.new('猫')
152
+ dog = KLookup::Lookup::Kanji.new('犬')
153
+ assert (cat_r.include?(cat) and not cat_r.include?(dog)), ':reading'
154
+ assert (cat_rs.include?(cat) and not cat_rs.include?(dog)), ':reading and valid :stroke'
155
+ assert (not cat_rss.include?(cat) and not cat_rss.include?(dog)), ':reading and invalid :stroke'
156
+ end
157
+
158
+ def test_just_reading_lookup
159
+ cat = KLookup::Lookup::Kanji.new('猫')
160
+ dog = KLookup::Lookup::Kanji.new('犬')
161
+ cat_r = KLookup::Lookup::Kanji.lookup(:reading=>'ねこ')
162
+ assert (cat_r.include?(cat) and not cat_r.include?(dog)), ':reading in same kana'
163
+ cat_r = KLookup::Lookup::Kanji.lookup(:reading=>'ネコ')
164
+ assert (cat_r.include?(cat) and not cat_r.include?(dog)), ':reading in different kana'
165
+ end
166
+
167
+ def test_all_lookup
168
+ #TODO: this may not be a good test
169
+ assert KLookup::Lookup::Kanji.lookup.length > 1000
170
+ end
171
+
172
+ def test_just_block_lookup
173
+ cat = KLookup::Lookup::Kanji.new('猫')
174
+ dog = KLookup::Lookup::Kanji.new('犬')
175
+
176
+ look = KLookup::Lookup::Kanji.lookup {|k| k.meaning.include?('cat')}
177
+ assert (look.include?(cat) and not look.include?(dog)), 'meaning in block'
178
+ end
179
+
180
+ def test_block_lookup
181
+ cat = KLookup::Lookup::Kanji.new('猫')
182
+ dog = KLookup::Lookup::Kanji.new('犬')
183
+
184
+ look = KLookup::Lookup::Kanji.lookup(:reading=>'ねこ') {|k|
185
+ k.meaning.include?('cat')}
186
+ assert (look.include?(cat) and not look.include?(dog)),
187
+ 'meaning in block and reading'
188
+
189
+ look = KLookup::Lookup::Kanji.lookup(:reading=>'ねこ') {|k|
190
+ true}
191
+ assert (look.include?(cat) and not look.include?(dog)),
192
+ 'true in block and reading'
193
+ end
128
194
  end
129
195
 
130
196
 
131
197
  require 'test/unit/ui/console/testrunner'
132
198
  results=[]
133
199
  db=KLookup::Database
200
+ puts
201
+ puts "= Lookup_Test"
202
+ # Just use FlatFile for the moment
203
+ #[:FlatFile].each {|h|
134
204
  db.constants.each {|h|
135
205
  handler = "#{db.to_s}::#{h}"
136
206
  next unless eval(handler).superclass == db
137
207
  puts
138
208
  puts "== KLookup::Lookup.handler = #{handler}"
139
209
  KLookup::Lookup.handler = eval(handler)
140
- results << [handler, Test::Unit::UI::Console::TestRunner.run(KLookup_Test)]
210
+ results << [handler, Test::Unit::UI::Console::TestRunner.run(Lookup_Test)]
141
211
  puts
142
212
  }
143
213
 
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.1
2
+ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: klookup
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.2
7
- date: 2007-01-29 00:00:00 +00:00
6
+ version: 0.2.4
7
+ date: 2007-02-18 00:00:00 +00:00
8
8
  summary: A set of kanji lookup tools and a library.
9
9
  require_paths:
10
10
  - lib
@@ -36,6 +36,7 @@ files:
36
36
  - lib/klookup/lookup.rb
37
37
  - lib/klookup/lookup_radical.rb
38
38
  - lib/klookup/database.rb
39
+ - lib/klookup/database_quickfile.rb
39
40
  - lib/klookup/database_flatfile_radk.rb
40
41
  - lib/klookup/database_unihan.rb
41
42
  - lib/klookup/database_flatfile_kanjidic.rb
@@ -43,6 +44,7 @@ files:
43
44
  - lib/klookup/lookup_kanji.rb
44
45
  test_files:
45
46
  - test/suite.rb
47
+ - test/database_test.rb
46
48
  rdoc_options:
47
49
  - - --main
48
50
  - KLookup
@@ -67,12 +69,3 @@ dependencies:
67
69
  - !ruby/object:Gem::Version
68
70
  version: 1.4.0
69
71
  version:
70
- - !ruby/object:Gem::Dependency
71
- name: unihan
72
- version_requirement:
73
- version_requirements: !ruby/object:Gem::Version::Requirement
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- version: 5.0.0.1.1
78
- version: