klookup 0.2.4 → 0.3
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.
- data/bin/klookup.cgi +9 -7
- data/data/klookup/data.db +0 -0
- data/lib/klookup.rb +2 -8
- data/lib/klookup/database.rb +18 -7
- data/lib/klookup/database_flatfile.rb +26 -17
- data/lib/klookup/database_sqlite.rb +110 -0
- data/lib/klookup/lookup.rb +6 -1
- data/lib/klookup/lookup_kanji.rb +9 -30
- data/lib/klookup/lookup_radical.rb +4 -0
- data/lib/runicode.rb +39 -0
- data/lib/runicode/utf8.rb +132 -0
- data/test/database_test.rb +287 -61
- data/test/runicode_test.rb +89 -0
- data/test/suite.rb +71 -57
- metadata +18 -13
- data/lib/klookup/database_quickfile.rb +0 -38
data/bin/klookup.cgi
CHANGED
@@ -24,14 +24,16 @@ end
|
|
24
24
|
include KLookup::Lookup
|
25
25
|
|
26
26
|
require 'cgi'
|
27
|
+
|
28
|
+
# GetText is being problematic when it is found.
|
27
29
|
# If the platform doesn't have gettext we can survive
|
28
|
-
begin
|
29
|
-
require 'gettext'
|
30
|
-
include GetText
|
31
|
-
bindtextdomain 'klookup'
|
32
|
-
rescue LoadError
|
33
|
-
|
34
|
-
end
|
30
|
+
#begin
|
31
|
+
# require 'gettext'
|
32
|
+
# include GetText
|
33
|
+
# bindtextdomain 'klookup'
|
34
|
+
#rescue LoadError
|
35
|
+
def _(arg); arg; end
|
36
|
+
#end
|
35
37
|
|
36
38
|
class String
|
37
39
|
#Deletes the characters (NOT bytes) listed in str from the string
|
Binary file
|
data/lib/klookup.rb
CHANGED
@@ -11,19 +11,13 @@
|
|
11
11
|
=end
|
12
12
|
|
13
13
|
begin
|
14
|
-
require '
|
14
|
+
require 'rubygems'
|
15
15
|
rescue LoadError
|
16
|
-
# begin
|
17
|
-
# require 'rubygems'
|
18
|
-
# require 'active_support'
|
19
|
-
# rescue LoadError
|
20
|
-
require 'runicode'
|
21
|
-
# end
|
22
16
|
end
|
23
17
|
|
24
|
-
#This allows regular expressions to work on characters.
|
25
18
|
$KCODE='u'
|
26
19
|
require 'jcode'
|
20
|
+
require 'runicode'
|
27
21
|
|
28
22
|
# Contains Lookup and Database.
|
29
23
|
module KLookup
|
data/lib/klookup/database.rb
CHANGED
@@ -23,10 +23,7 @@ class KLookup::Database
|
|
23
23
|
|
24
24
|
public
|
25
25
|
|
26
|
-
|
27
|
-
#
|
28
|
-
# Priority: LOOKUP_PATH environment variable, Gem load path.
|
29
|
-
def self.open_resource(path, mod='klookup')
|
26
|
+
def self.resource_path(path, mod='klookup')
|
30
27
|
# Choose a directory
|
31
28
|
env=ENV['KLOOKUP_PATH']
|
32
29
|
if env and env != ''
|
@@ -36,12 +33,26 @@ class KLookup::Database
|
|
36
33
|
gem mod
|
37
34
|
dir=Gem.datadir(mod)
|
38
35
|
rescue NameError
|
39
|
-
|
36
|
+
# This is a fallback for older versions of RubyGems (0.8)
|
37
|
+
begin
|
38
|
+
require_gem mod
|
39
|
+
dir=Gem.datadir(mod)
|
40
|
+
rescue NameError
|
41
|
+
raise IOError, 'Could not find resource %s' % path
|
42
|
+
end
|
40
43
|
end
|
41
44
|
end
|
45
|
+
raise Errno::ENOENT if Dir["#{dir}/#{path}"].empty?
|
46
|
+
"#{dir}/#{path}"
|
47
|
+
end
|
42
48
|
|
49
|
+
# Opens a resource.
|
50
|
+
#
|
51
|
+
# Priority: LOOKUP_PATH environment variable, Gem load path.
|
52
|
+
def self.open_resource(path, mod='klookup')
|
43
53
|
# Open a file
|
44
|
-
|
54
|
+
path = resource_path(path, mod)
|
55
|
+
file = open(path)
|
45
56
|
if block_given?
|
46
57
|
yield file
|
47
58
|
else
|
@@ -60,6 +71,6 @@ class KLookup::Database
|
|
60
71
|
alias :is_radical? :undefined
|
61
72
|
|
62
73
|
require 'klookup/database_flatfile'
|
63
|
-
|
74
|
+
require 'klookup/database_sqlite'
|
64
75
|
# require 'klookup/database_unihan'
|
65
76
|
end
|
@@ -50,35 +50,44 @@ class KLookup::Database::FlatFile < KLookup::Database
|
|
50
50
|
kanji = RadK.instance.get_kanji(*args)
|
51
51
|
end
|
52
52
|
unless strokes.nil?
|
53
|
-
return kanji.select {|k|
|
53
|
+
return kanji.select {|k| get_stroke_count(k) == strokes }
|
54
54
|
end
|
55
55
|
return kanji
|
56
56
|
end
|
57
57
|
public
|
58
|
-
def find_kanji(args)
|
58
|
+
def find_kanji(args={})
|
59
|
+
if not args.kind_of?(Hash)
|
60
|
+
raise ArgumentError, 'only takes a hash'
|
61
|
+
elsif (not args[:stroke].nil?) and (not args[:stroke].kind_of?(Integer))
|
62
|
+
raise ArgumentError, ':stroke must be an Integer'
|
63
|
+
end
|
59
64
|
get_kanji(args[:stroke], args[:radical])
|
60
65
|
end
|
61
|
-
def
|
62
|
-
RadK.instance.get_strokes(*args)
|
63
|
-
end
|
64
|
-
def get_stroke_count
|
66
|
+
def get_stroke_count(*args)
|
65
67
|
begin
|
66
68
|
RadK.instance.get_strokes(*args)
|
67
|
-
rescue
|
68
|
-
|
69
|
+
rescue NoMethodError
|
70
|
+
begin
|
71
|
+
KanjiDic.instance.get_strokes(*args)
|
72
|
+
rescue NoMethodError
|
73
|
+
raise ArgumentError, 'character not found'
|
74
|
+
end
|
69
75
|
end
|
70
76
|
end
|
71
|
-
def
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
RadK.instance.get_radicals(*args)
|
77
|
+
def get_radical(kanji)
|
78
|
+
raise ArgumentError if kanji.chars.size > 1
|
79
|
+
raise ArgumentError unless is_kanji?(kanji)
|
80
|
+
RadK.instance.get_radicals(kanji)
|
76
81
|
end
|
77
|
-
def get_reading(
|
78
|
-
|
82
|
+
def get_reading(kanji)
|
83
|
+
raise ArgumentError if kanji.chars.size > 1
|
84
|
+
raise ArgumentError unless is_kanji?(kanji)
|
85
|
+
KanjiDic.instance.get_reading(kanji)
|
79
86
|
end
|
80
|
-
def get_meaning(
|
81
|
-
|
87
|
+
def get_meaning(kanji)
|
88
|
+
raise ArgumentError if kanji.chars.size > 1
|
89
|
+
raise ArgumentError unless is_kanji?(kanji)
|
90
|
+
KanjiDic.instance.get_meaning(kanji)
|
82
91
|
end
|
83
92
|
def is_kanji?(*args)
|
84
93
|
KanjiDic.instance.is_kanji?(*args)
|
@@ -0,0 +1,110 @@
|
|
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::SQLite < KLookup::Database
|
15
|
+
|
16
|
+
require 'singleton'
|
17
|
+
include Singleton
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
require 'sqlite'
|
21
|
+
path = KLookup::Database.resource_path('data.db')
|
22
|
+
@db = ::SQLite::Database.new(path)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Umm...
|
26
|
+
def finalize
|
27
|
+
@db.close
|
28
|
+
end
|
29
|
+
|
30
|
+
def radical_stroke_count
|
31
|
+
@db.execute("select distinct stroke_count from radical order by stroke_count").flatten.collect {|s| s.to_i}
|
32
|
+
end
|
33
|
+
|
34
|
+
def find_radical(args={})
|
35
|
+
raise ArgumentError unless args.kind_of?(Hash)
|
36
|
+
stroke_count = args[:stroke]
|
37
|
+
raise ArgumentError unless stroke_count.kind_of?(Integer) or stroke_count.nil?
|
38
|
+
if stroke_count
|
39
|
+
@db.execute("select radical from radical where stroke_count=?", stroke_count).flatten
|
40
|
+
else
|
41
|
+
@db.execute("select radical from radical").flatten
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def find_kanji(args={})
|
46
|
+
raise ArgumentError, 'args must be Hash or nil' unless args.kind_of?(Hash) or args.nil?
|
47
|
+
stroke_count = args[:stroke]
|
48
|
+
raise ArgumentError, ':stroke must be Integer or nil' unless stroke_count.kind_of?(Integer) or stroke_count.nil?
|
49
|
+
radical = args[:radical]
|
50
|
+
radical = [radical] if radical.kind_of?(String)
|
51
|
+
raise ArgumentError, ':radical must be Array, String, or nil' unless radical.kind_of?(Array) or radical.nil?
|
52
|
+
|
53
|
+
kanji = []
|
54
|
+
if stroke_count
|
55
|
+
kanji = @db.execute("select kanji from kanji where stroke_count=?",
|
56
|
+
stroke_count).flatten
|
57
|
+
else
|
58
|
+
kanji = @db.execute("select kanji from kanji").flatten
|
59
|
+
end
|
60
|
+
|
61
|
+
if radical
|
62
|
+
radical.each {|r|
|
63
|
+
raise ArgumentError, ':radical must contain radicals' unless is_radical?(r)
|
64
|
+
new_r = @db.execute("select kanji from mapping where radical=?",
|
65
|
+
r).flatten
|
66
|
+
kanji &= new_r
|
67
|
+
}
|
68
|
+
end
|
69
|
+
kanji
|
70
|
+
end
|
71
|
+
|
72
|
+
def get_stroke_count(character)
|
73
|
+
if is_kanji?(character)
|
74
|
+
@db.get_first_row("select stroke_count from kanji where kanji=?", character).first.to_i
|
75
|
+
elsif is_radical?(character)
|
76
|
+
@db.get_first_row("select stroke_count from radical where radical=?", character).first.to_i
|
77
|
+
else
|
78
|
+
raise ArgumentError
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_radical(character)
|
83
|
+
raise ArgumentError unless is_kanji?(character)
|
84
|
+
@db.execute("select radical from mapping where kanji=?", character).
|
85
|
+
collect {|r| r.first }
|
86
|
+
end
|
87
|
+
|
88
|
+
def get_reading(character)
|
89
|
+
raise ArgumentError unless is_kanji?(character)
|
90
|
+
reading = @db.execute("select reading from reading where kanji=? and is_name_reading='false'", character).flatten
|
91
|
+
name_reading = @db.execute("select reading from reading where kanji=? and is_name_reading='true'", character).flatten
|
92
|
+
Struct.new(:reading, :name_reading).new(reading, name_reading)
|
93
|
+
end
|
94
|
+
|
95
|
+
def get_meaning(character)
|
96
|
+
raise ArgumentError unless is_kanji?(character)
|
97
|
+
@db.execute("select meaning from meaning where kanji=?", character).flatten
|
98
|
+
end
|
99
|
+
|
100
|
+
def is_kanji?(character)
|
101
|
+
@db.get_first_row("select count(*) from kanji where kanji=?",
|
102
|
+
character).first.to_i >= 1
|
103
|
+
end
|
104
|
+
|
105
|
+
def is_radical?(character)
|
106
|
+
@db.get_first_row("select count(*) from radical where radical=?",
|
107
|
+
character).first.to_i >= 1
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
data/lib/klookup/lookup.rb
CHANGED
@@ -15,7 +15,12 @@ module KLookup::Lookup
|
|
15
15
|
|
16
16
|
# Returns the default handler for database lookups.
|
17
17
|
def self.default_handler
|
18
|
-
|
18
|
+
begin
|
19
|
+
KLookup::Database::SQLite.instance
|
20
|
+
KLookup::Database::SQLite
|
21
|
+
rescue LoadError
|
22
|
+
KLookup::Database::FlatFile
|
23
|
+
end
|
19
24
|
end
|
20
25
|
|
21
26
|
# Set the handler used by both Kanji and Radical.
|
data/lib/klookup/lookup_kanji.rb
CHANGED
@@ -19,34 +19,9 @@ class KLookup::Lookup::Kanji
|
|
19
19
|
# Returns a string containing the UTF-8 encoded character represented by the
|
20
20
|
# receiver’s value.
|
21
21
|
#
|
22
|
-
#
|
23
|
-
def self.
|
24
|
-
|
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
|
22
|
+
# Uses RUnicode's Integer#chr method
|
23
|
+
def self.cp_to_str(val)
|
24
|
+
return val.chr
|
50
25
|
end
|
51
26
|
|
52
27
|
# Returns a regular expression that matches strings in a kana-insensitive
|
@@ -69,9 +44,9 @@ class KLookup::Lookup::Kanji
|
|
69
44
|
re=''
|
70
45
|
str.each_char {|c|
|
71
46
|
if hiragana.include?(c.chars.first)
|
72
|
-
re << "[#{c}#{
|
47
|
+
re << "[#{c}#{cp_to_str(hkhash[c.chars.first])}]"
|
73
48
|
elsif katakana.include?(c.chars.first)
|
74
|
-
re << "[#{c}#{
|
49
|
+
re << "[#{c}#{cp_to_str(khhash[c.chars.first])}]"
|
75
50
|
else
|
76
51
|
re << c
|
77
52
|
end
|
@@ -206,4 +181,8 @@ class KLookup::Lookup::Kanji
|
|
206
181
|
end
|
207
182
|
@meaning
|
208
183
|
end
|
184
|
+
|
185
|
+
def stroke_count
|
186
|
+
@@data.instance.get_stroke_count(@kanji)
|
187
|
+
end
|
209
188
|
end
|
data/lib/runicode.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
=begin
|
2
|
+
|
3
|
+
lib/runicode.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
|
+
$KCODE='u'
|
14
|
+
require 'jcode'
|
15
|
+
|
16
|
+
module RUnicode # :nodoc:
|
17
|
+
end
|
18
|
+
|
19
|
+
# Ruby-based UTF-8 processing
|
20
|
+
require 'runicode/utf8'
|
21
|
+
|
22
|
+
class String # :nodoc:
|
23
|
+
|
24
|
+
#Yields to each codepoint of a string.
|
25
|
+
def each_char
|
26
|
+
raise LocalJumpError unless block_given?
|
27
|
+
each_codepoint {|cp| yield cp.chr}
|
28
|
+
end
|
29
|
+
|
30
|
+
#Collects the codepoints as an array.
|
31
|
+
def codepoints
|
32
|
+
codepoints = []
|
33
|
+
each_codepoint {|c| codepoints << c}
|
34
|
+
return codepoints
|
35
|
+
end
|
36
|
+
|
37
|
+
alias :chars :codepoints
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
=begin
|
2
|
+
|
3
|
+
lib/runicode/utf8.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
|
+
module RUnicode::UTF8 # :nodoc:
|
14
|
+
class ThisShouldNotHappen < Exception # :nodoc:
|
15
|
+
end
|
16
|
+
|
17
|
+
INVALID_BYTES = ((0xC0..0xC1).to_a + (0xF5..0xFE).to_a +
|
18
|
+
[0b11000000, 0b11100000, 0b11110000])
|
19
|
+
|
20
|
+
# Converts one set of bytes making up a character into an integer codepoint
|
21
|
+
def self.bytes_to_char(bytes)
|
22
|
+
case bytes.size
|
23
|
+
when 1 # 0xxxxxxx
|
24
|
+
return bytes.first & 0b01111111
|
25
|
+
when 2 # 110xxxxx 10xxxxxx
|
26
|
+
return ((bytes.first & 0b00011111) << 6) + rest(bytes[1..-1])
|
27
|
+
when 3 # 1110xxxx 10xxxxxx 10xxxxxx
|
28
|
+
return ((bytes.first & 0b00001111) << 12) + rest(bytes[1..-1])
|
29
|
+
when 4 # 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
30
|
+
return ((bytes.first & 0b00000111) << 18) + rest(bytes[1..-1])
|
31
|
+
else raise ThisShouldNotHappen
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Gives the total value from 10xxxxxx in each byte
|
36
|
+
def self.rest(bytes)
|
37
|
+
val = 0b0
|
38
|
+
bytes.each {|b|
|
39
|
+
val = (val << 6)
|
40
|
+
val += (b & 0b00111111)
|
41
|
+
}
|
42
|
+
val
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
class String # :nodoc:
|
48
|
+
#Passes each character in str to the given block as an Integer. A rewrite of
|
49
|
+
#codepoints.
|
50
|
+
def each_codepoint
|
51
|
+
raise LocalJumpError, 'no block given' unless block_given?
|
52
|
+
|
53
|
+
steal_next = 0
|
54
|
+
current_bytes = nil
|
55
|
+
|
56
|
+
to_s.each_byte {|b|
|
57
|
+
if steal_next==0 # No octets need to be stolen
|
58
|
+
unless current_bytes==nil
|
59
|
+
current_bytes.each {|byte|
|
60
|
+
if RUnicode::UTF8::INVALID_BYTES.include? byte
|
61
|
+
raise IOError, 'ill-formed UTF-8'
|
62
|
+
end
|
63
|
+
}
|
64
|
+
yield RUnicode::UTF8::bytes_to_char(current_bytes)
|
65
|
+
end
|
66
|
+
current_bytes = [b]
|
67
|
+
if b >> 7 == 0b0
|
68
|
+
steal_next = 0
|
69
|
+
elsif b >> 5 == 0b110
|
70
|
+
steal_next = 1
|
71
|
+
elsif b >> 4 == 0b1110
|
72
|
+
steal_next = 2
|
73
|
+
elsif b >> 3 == 0b11110
|
74
|
+
steal_next = 3
|
75
|
+
else
|
76
|
+
raise IOError, 'ill-formed UTF-8'
|
77
|
+
end
|
78
|
+
|
79
|
+
else # We have some octets to steal
|
80
|
+
raise IOError, 'ill-formed UTF-8' unless b >> 6 == 0b10
|
81
|
+
current_bytes << b
|
82
|
+
steal_next -= 1
|
83
|
+
end
|
84
|
+
}
|
85
|
+
|
86
|
+
# One last time:
|
87
|
+
unless current_bytes==nil
|
88
|
+
current_bytes.each {|byte|
|
89
|
+
if RUnicode::UTF8::INVALID_BYTES.include? byte
|
90
|
+
raise IOError, 'ill-formed UTF-8'
|
91
|
+
end
|
92
|
+
}
|
93
|
+
yield RUnicode::UTF8::bytes_to_char(current_bytes)
|
94
|
+
end
|
95
|
+
|
96
|
+
raise IOError, 'ill-formed UTF-8' if steal_next != 0
|
97
|
+
|
98
|
+
end # each_codepoint
|
99
|
+
end
|
100
|
+
|
101
|
+
class Integer # :nodoc:
|
102
|
+
#Returns a string containing the UTF-8 encoded character represented by the
|
103
|
+
#receiver’s value.
|
104
|
+
def chr
|
105
|
+
case to_i
|
106
|
+
when 0xD800..0xDFFF
|
107
|
+
raise RangeError, 'these values are surrogate pairs'
|
108
|
+
when 0x0000..0x007F
|
109
|
+
return "%c" % (to_i & 0b01111111) # 7
|
110
|
+
when 0x0080..0x07FF
|
111
|
+
byte = []
|
112
|
+
byte[0] = 0b11000000 + ((to_i >> 6) & 0b11111) # 5
|
113
|
+
byte[1] = 0b10000000 + (to_i & 0b111111) # 6
|
114
|
+
return "%c%c" % byte
|
115
|
+
when 0x0800..0xFFFF
|
116
|
+
byte = []
|
117
|
+
byte[0] = 0b11100000 + ((to_i >> 12) & 0b1111) # 4
|
118
|
+
byte[1] = 0b10000000 + ((to_i >> 6) & 0b111111) # 6
|
119
|
+
byte[2] = 0b10000000 + (to_i & 0b111111) # 6
|
120
|
+
return "%c%c%c" % byte
|
121
|
+
when 0x00010000..0x0010FFFF
|
122
|
+
byte = []
|
123
|
+
byte[0] = 0b11110000 + ((to_i >> 18) & 0b11111) # 5
|
124
|
+
byte[1] = 0b10000000 + ((to_i >> 12) & 0b111111) # 6
|
125
|
+
byte[2] = 0b10000000 + ((to_i >> 6) & 0b111111) # 6
|
126
|
+
byte[3] = 0b10000000 + (to_i & 0b111111) # 6
|
127
|
+
return "%c%c%c%c" % byte
|
128
|
+
else
|
129
|
+
raise RangeError, 'out of Unicode range'
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
data/test/database_test.rb
CHANGED
@@ -8,62 +8,58 @@ class Database_Test < Test::Unit::TestCase
|
|
8
8
|
@@db = @@dbc.instance
|
9
9
|
end
|
10
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
11
|
def test_open_resource
|
25
|
-
f =
|
26
|
-
|
12
|
+
f = KLookup::Database.open_resource('newradkfile')
|
13
|
+
assert_respond_to f, :each_line, 'valid path'
|
27
14
|
|
28
|
-
f =
|
29
|
-
|
15
|
+
f = KLookup::Database.open_resource('newradkfile', 'klookup')
|
16
|
+
assert_respond_to f, :each_line, 'valid path and valid gem'
|
30
17
|
|
31
18
|
flag=false
|
32
|
-
|
33
|
-
|
19
|
+
KLookup::Database.open_resource('newradkfile') {|f|
|
20
|
+
assert_respond_to f, :each_line
|
34
21
|
flag=true
|
35
22
|
}
|
36
23
|
assert flag, 'using a block'
|
37
24
|
|
38
|
-
|
39
|
-
|
25
|
+
assert_raise(Errno::ENOENT, 'invalid path') {
|
26
|
+
KLookup::Database.open_resource('all your base are belong to us')
|
40
27
|
}
|
41
|
-
|
42
|
-
|
28
|
+
assert_raise(Errno::ENOENT, 'invalid path') {
|
29
|
+
KLookup::Database.open_resource('all your base are belong to us')
|
30
|
+
}
|
31
|
+
assert_raise(Errno::ENOENT, 'invalid gem') {
|
32
|
+
KLookup::Database.open_resource('newradkfile', 'cookies are yummy')
|
43
33
|
}
|
44
34
|
end
|
45
35
|
|
46
36
|
def test_methods_exist
|
47
37
|
[:radical_stroke_count,
|
48
38
|
:find_radical,
|
49
|
-
:find_kanji,
|
50
|
-
:get_stroke_count,
|
51
|
-
:get_radical,
|
52
|
-
:get_reading,
|
53
|
-
:get_meaning,
|
54
|
-
:is_kanji?,
|
55
|
-
:is_radical?
|
39
|
+
:find_kanji,
|
40
|
+
:get_stroke_count,
|
41
|
+
:get_radical,
|
42
|
+
:get_reading,
|
43
|
+
:get_meaning,
|
44
|
+
:is_kanji?,
|
45
|
+
:is_radical?
|
56
46
|
].each {|m|
|
57
|
-
|
47
|
+
assert_respond_to @@db, m
|
58
48
|
}
|
59
|
-
|
49
|
+
# assert_respond_to @@dbc, :open_resource
|
60
50
|
end
|
61
51
|
|
62
52
|
def test_radical_stroke_count
|
63
|
-
|
53
|
+
assert_kind_of Enumerable, @@db.radical_stroke_count
|
64
54
|
assert (@@db.radical_stroke_count.size > 0), 'more than zero'
|
65
|
-
|
66
|
-
assert
|
55
|
+
(1..15).each {|n|
|
56
|
+
assert @@db.radical_stroke_count.include?(n)
|
57
|
+
}
|
58
|
+
@@db.radical_stroke_count.each {|s|
|
59
|
+
assert_kind_of Integer, s
|
60
|
+
}
|
61
|
+
assert_raise(ArgumentError) {
|
62
|
+
@@db.radical_stroke_count(7)
|
67
63
|
}
|
68
64
|
end
|
69
65
|
|
@@ -71,57 +67,287 @@ class Database_Test < Test::Unit::TestCase
|
|
71
67
|
test = @@db.find_radical(:stroke=>11)
|
72
68
|
assert test.include?('鳥')
|
73
69
|
assert (not test.include?('馬'))
|
70
|
+
|
74
71
|
test = @@db.find_radical(:stroke=>10)
|
75
72
|
assert test.include?('馬')
|
76
73
|
assert (not test.include?('鳥'))
|
74
|
+
|
77
75
|
test = @@db.find_radical
|
78
76
|
assert test.include?('鳥')
|
79
77
|
assert test.include?('馬')
|
78
|
+
|
80
79
|
newtest = @@db.find_radical(:stroke=>nil)
|
81
|
-
|
80
|
+
assert_equal test, newtest, 'Sets are identical'
|
82
81
|
test = @@db.find_radical(:stroke=>0)
|
83
|
-
|
82
|
+
assert_equal test.size, 0, 'Size is zero'
|
84
83
|
|
85
|
-
|
84
|
+
assert_raise(ArgumentError, 'not a hash (String)') {
|
86
85
|
@@db.find_radical('cats')
|
87
86
|
}
|
88
|
-
|
87
|
+
assert_raise(ArgumentError, 'not a hash (Integer)') {
|
89
88
|
@@db.find_radical(6)
|
90
89
|
}
|
91
|
-
|
90
|
+
assert_raise(ArgumentError, 'invalid :stroke') {
|
92
91
|
@@db.find_radical(:stroke=>'nobbly knees')
|
93
92
|
}
|
93
|
+
assert_raise(ArgumentError) {
|
94
|
+
@@db.find_radical('動', '詞')
|
95
|
+
}
|
96
|
+
assert_raise(ArgumentError) {
|
97
|
+
@@db.find_radical('動')
|
98
|
+
}
|
99
|
+
|
94
100
|
test = @@db.find_radical(:stroke=>10,:cats=>:dogs)
|
95
101
|
assert test.include?('馬')
|
96
102
|
assert (not test.include?('鳥'))
|
97
103
|
end
|
98
104
|
|
99
105
|
def test_find_kanji
|
106
|
+
# Simple cases
|
107
|
+
test = @@db.find_kanji(:stroke=>11)
|
108
|
+
assert test.include?('猫'), '11 includes 猫'
|
109
|
+
assert (not test.include?('犬')), '11 excludes 犬'
|
110
|
+
|
111
|
+
test = @@db.find_kanji(:stroke=>4)
|
112
|
+
assert test.include?('犬'), '10 includes 犬'
|
113
|
+
assert (not test.include?('猫')), '10 excludes 猫'
|
114
|
+
|
115
|
+
test = @@db.find_kanji(:radical=>['犭'])
|
116
|
+
assert test.include?('猫'), '犭 includes 猫'
|
117
|
+
assert (not test.include?('鳥')), '犭 excludes 鳥'
|
118
|
+
|
119
|
+
test = @@db.find_kanji(:radical=>['犭'],:stroke=>11)
|
120
|
+
assert test.include?('猫'), '犭 (11 strokes) includes 猫'
|
121
|
+
assert (not test.include?('鳥')), '犭 (11 strokes) excludes 鳥'
|
122
|
+
|
123
|
+
test = @@db.find_kanji(:radical=>['犭', '艹', '田'],:stroke=>11)
|
124
|
+
assert test.include?('猫'), '犭艹田 (11 strokes) includes 猫'
|
125
|
+
assert (not test.include?('鳥')), '犭艹田 (11 strokes) excludes 鳥'
|
126
|
+
|
127
|
+
test = @@db.find_kanji(:radical=>['犭', '艹', '田'])
|
128
|
+
assert test.include?('猫'), '犭艹田 (no strokes) includes 猫'
|
129
|
+
assert (not test.include?('鳥')), '犭艹田 (no strokes) excludes 鳥'
|
130
|
+
|
131
|
+
# Uncommon cases
|
132
|
+
test = @@db.find_kanji
|
133
|
+
assert test.include?('猫'), 'all includes 猫'
|
134
|
+
assert test.include?('犬'), 'all includes 犬'
|
135
|
+
|
136
|
+
test = @@db.find_kanji(:radical=>'犭')
|
137
|
+
assert test.include?('猫'), '犭 (not list) includes 猫'
|
138
|
+
assert (not test.include?('鳥')), '犭 (not list) excludes 鳥'
|
139
|
+
|
140
|
+
test = @@db.find_kanji(:radical=>'犭',:stroke=>11)
|
141
|
+
assert test.include?('猫'), '犭 (not list, 11 strokes) includes 猫'
|
142
|
+
assert (not test.include?('鳥')), '犭 (not list, 11 strokes) excludes 鳥'
|
143
|
+
|
144
|
+
test = @@db.find_kanji(:radical=>['犭', '艹', '田'], :stroke=>10)
|
145
|
+
assert_kind_of Enumerable, test, '犭艹田 (10 strokes!) is Enumerable'
|
146
|
+
assert_equal test.size, 0, '犭艹田 (10 strokes!) returns zero results'
|
147
|
+
|
148
|
+
# Extraneous input
|
149
|
+
test = @@db.find_kanji(:stroke=>4,:strokes=>'cat')
|
150
|
+
assert test.include?('犬'), '10 with extraneous input includes 犬'
|
151
|
+
assert (not test.include?('猫')), '10 with extraneous input excludes 猫'
|
152
|
+
|
153
|
+
# Problem cases
|
154
|
+
assert_raise(ArgumentError, ':stroke=>"cat"') {
|
155
|
+
@@db.find_kanji(:stroke=>'cat')
|
156
|
+
}
|
157
|
+
assert_raise(ArgumentError, ':radical=>"cat"') {
|
158
|
+
@@db.find_kanji(:radical=>'cat')
|
159
|
+
}
|
160
|
+
assert_raise(ArgumentError, ':radical=>["cat"]') {
|
161
|
+
@@db.find_kanji(:radical=>['cat'])
|
162
|
+
}
|
163
|
+
assert_raise(ArgumentError, ':radical=>["田犬"]') {
|
164
|
+
@@db.find_kanji(:radical=>['田犬'])
|
165
|
+
}
|
166
|
+
assert_raise(ArgumentError) {
|
167
|
+
@@db.find_kanji('犬', '田')
|
168
|
+
}
|
169
|
+
assert_raise(ArgumentError) {
|
170
|
+
@@db.find_kanji('犬')
|
171
|
+
}
|
100
172
|
end
|
101
173
|
|
102
|
-
|
174
|
+
def test_get_stroke_count
|
175
|
+
# Simple cases
|
176
|
+
|
177
|
+
assert_equal @@db.get_stroke_count('猫'), 11, '猫=11'
|
178
|
+
assert_equal @@db.get_stroke_count('鳥'), 11, '鳥=11'
|
179
|
+
assert_equal @@db.get_stroke_count('犬'), 4, '犬=4'
|
180
|
+
assert_equal @@db.get_stroke_count('田'), 5, '田=5'
|
181
|
+
assert_equal @@db.get_stroke_count('犭'), 3, '犭=3'
|
103
182
|
|
183
|
+
# Problem cases
|
184
|
+
assert_raise(ArgumentError, 'no arguments') {
|
185
|
+
@@db.get_stroke_count()
|
186
|
+
}
|
187
|
+
assert_raise(ArgumentError, ':radical=>"犬"') {
|
188
|
+
@@db.get_stroke_count(:radical=>'犬')
|
189
|
+
}
|
190
|
+
assert_raise(ArgumentError, 'two characters') {
|
191
|
+
@@db.get_stroke_count('犬猫')
|
192
|
+
}
|
193
|
+
assert_raise(ArgumentError) {
|
194
|
+
@@db.get_stroke_count
|
195
|
+
}
|
196
|
+
assert_raise(ArgumentError) {
|
197
|
+
@@db.get_stroke_count('動', '詞')
|
198
|
+
}
|
199
|
+
end
|
104
200
|
|
201
|
+
def test_get_radical
|
202
|
+
#TODO: May not play well with other data sources
|
203
|
+
cat = @@db.get_radical('猫')
|
204
|
+
assert_equal cat.size, 3
|
205
|
+
['犭', '艹', '田'].each {|r|
|
206
|
+
assert cat.include?(r), "contains #{r}"
|
207
|
+
}
|
105
208
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
db.
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
209
|
+
assert_raise(ArgumentError) {
|
210
|
+
@@db.get_radical('犭')
|
211
|
+
}
|
212
|
+
assert_raise(ArgumentError) {
|
213
|
+
@@db.get_radical('h')
|
214
|
+
}
|
215
|
+
assert_raise(ArgumentError) {
|
216
|
+
@@db.get_radical('猫犬')
|
217
|
+
}
|
218
|
+
assert_raise(ArgumentError) {
|
219
|
+
@@db.get_radical
|
220
|
+
}
|
221
|
+
assert_raise(ArgumentError) {
|
222
|
+
@@db.get_radical('動', '詞')
|
223
|
+
}
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_get_reading
|
227
|
+
#TODO: May not play well with other data sources
|
228
|
+
cat = @@db.get_reading('猫').reading
|
229
|
+
assert cat.size>=2
|
230
|
+
['ねこ', 'ビョウ'].each {|r|
|
231
|
+
assert cat.include?(r), "contains #{r}"
|
232
|
+
}
|
233
|
+
|
234
|
+
rich = @@db.get_reading('富')
|
235
|
+
assert rich.reading.size>=2
|
236
|
+
['とみ', 'フウ'].each {|r|
|
237
|
+
assert rich.reading.include?(r), "contains #{r}"
|
238
|
+
}
|
239
|
+
assert rich.name_reading.size>=2
|
240
|
+
['とん', 'ふっ'].each {|r|
|
241
|
+
assert rich.name_reading.include?(r), "contains #{r}"
|
242
|
+
}
|
243
|
+
|
244
|
+
assert_raise(ArgumentError) {
|
245
|
+
@@db.get_reading('犭')
|
246
|
+
}
|
247
|
+
assert_raise(ArgumentError) {
|
248
|
+
@@db.get_reading('h')
|
249
|
+
}
|
250
|
+
assert_raise(ArgumentError) {
|
251
|
+
@@db.get_reading('猫犬')
|
252
|
+
}
|
253
|
+
assert_raise(ArgumentError) {
|
254
|
+
@@db.get_reading
|
255
|
+
}
|
256
|
+
assert_raise(ArgumentError) {
|
257
|
+
@@db.get_reading('動', '詞')
|
258
|
+
}
|
259
|
+
end
|
260
|
+
|
261
|
+
def test_get_meaning
|
262
|
+
#TODO: May not play well with other data sources
|
263
|
+
pull = @@db.get_meaning('引')
|
264
|
+
assert pull.size>=3
|
265
|
+
['pull', 'tug', 'refer to'].each {|r|
|
266
|
+
assert pull.include?(r), "contains #{r}"
|
267
|
+
}
|
268
|
+
|
269
|
+
comp = @@db.get_meaning('以')
|
270
|
+
assert comp.size>=3
|
271
|
+
['by means of', 'because', 'in view of'].each {|r|
|
272
|
+
assert comp.include?(r), "contains #{r}"
|
273
|
+
}
|
274
|
+
|
275
|
+
assert_raise(ArgumentError) {
|
276
|
+
@@db.get_meaning('犭')
|
277
|
+
}
|
278
|
+
assert_raise(ArgumentError) {
|
279
|
+
@@db.get_meaning('h')
|
280
|
+
}
|
281
|
+
assert_raise(ArgumentError) {
|
282
|
+
@@db.get_meaning('猫犬')
|
283
|
+
}
|
284
|
+
assert_raise(ArgumentError) {
|
285
|
+
@@db.get_meaning
|
286
|
+
}
|
287
|
+
assert_raise(ArgumentError) {
|
288
|
+
@@db.get_meaning('動', '詞')
|
289
|
+
}
|
290
|
+
end
|
291
|
+
|
292
|
+
def test_is_kanji?
|
293
|
+
assert @@db.is_kanji?('猫')
|
294
|
+
assert @@db.is_kanji?('犬')
|
295
|
+
assert @@db.is_kanji?('富')
|
296
|
+
assert @@db.is_kanji?('夢')
|
297
|
+
assert @@db.is_kanji?('田')
|
298
|
+
assert (not @@db.is_kanji?('犭'))
|
299
|
+
assert (not @@db.is_kanji?('t'))
|
300
|
+
assert (not @@db.is_kanji?(''))
|
301
|
+
assert (not @@db.is_kanji?('犬猫'))
|
302
|
+
assert_raise(ArgumentError) {
|
303
|
+
@@db.is_kanji?
|
304
|
+
}
|
305
|
+
assert_raise(ArgumentError) {
|
306
|
+
@@db.is_kanji?('犬','猫')
|
307
|
+
}
|
308
|
+
end
|
309
|
+
|
310
|
+
def test_is_radical?
|
311
|
+
assert @@db.is_radical?('犭')
|
312
|
+
assert @@db.is_radical?('犬')
|
313
|
+
assert @@db.is_radical?('田')
|
314
|
+
assert (not @@db.is_radical?('富'))
|
315
|
+
assert (not @@db.is_radical?('t'))
|
316
|
+
assert (not @@db.is_radical?('夢'))
|
317
|
+
assert (not @@db.is_radical?(''))
|
318
|
+
assert (not @@db.is_radical?('犬田'))
|
319
|
+
assert_raise(ArgumentError) {
|
320
|
+
@@db.is_radical?
|
321
|
+
}
|
322
|
+
assert_raise(ArgumentError) {
|
323
|
+
@@db.is_radical?('犬','田')
|
324
|
+
}
|
325
|
+
end
|
326
|
+
|
327
|
+
end
|
328
|
+
|
329
|
+
|
330
|
+
if $0 == __FILE__
|
331
|
+
require 'test/unit/ui/console/testrunner'
|
332
|
+
results=[]
|
333
|
+
db=KLookup::Database
|
120
334
|
puts
|
121
|
-
|
335
|
+
puts "= Database_Test"
|
336
|
+
# Just use FlatFile for the moment
|
337
|
+
#[:FlatFile].each {|h|
|
338
|
+
db.constants.each {|h|
|
339
|
+
handler = "#{db.to_s}::#{h}"
|
340
|
+
next unless eval(handler).superclass == db
|
341
|
+
puts
|
342
|
+
puts "== Handler: #{handler}"
|
343
|
+
Database_Test.set_handler(eval(handler)) # Database class
|
344
|
+
results << [handler, Test::Unit::UI::Console::TestRunner.run(Database_Test)]
|
345
|
+
puts
|
346
|
+
}
|
122
347
|
|
123
|
-
results.each {|p|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
}
|
348
|
+
results.each {|p|
|
349
|
+
h,r=p
|
350
|
+
puts "#{r.passed? ? 'PASS' : 'FAIL'} : #{h}"
|
351
|
+
puts (" %s" % r.to_s) unless r.passed?
|
352
|
+
}
|
353
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
=begin
|
2
|
+
|
3
|
+
test/runicode_test.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
|
+
require 'runicode'
|
14
|
+
|
15
|
+
require 'test/unit'
|
16
|
+
|
17
|
+
# RUnicode tests
|
18
|
+
class RUnicode_Test < Test::Unit::TestCase
|
19
|
+
def test_all
|
20
|
+
str = "hello ÂÅ¿ 今日は ���"
|
21
|
+
nstr = ""
|
22
|
+
str.each_codepoint {|c| nstr << c.chr}
|
23
|
+
assert str == nstr, 'Testcase equals testcase.'
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_string_codepoints
|
27
|
+
str = "hello ÂÅ¿ 今日は ���"
|
28
|
+
strcp = []
|
29
|
+
str.each_codepoint {|c| strcp << c}
|
30
|
+
assert str.codepoints == strcp, 'codepoints and each_codepoints give the same values'
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_integer_chr
|
34
|
+
codepoint = 0x00110000 # This is one higher than the highest codepoint
|
35
|
+
begin
|
36
|
+
codepoint.chr
|
37
|
+
assert false, 'Failed to raise exception.'
|
38
|
+
rescue Exception
|
39
|
+
assert $!.is_a?(RangeError), 'Raised correct exception.'
|
40
|
+
end
|
41
|
+
assert (codepoint-1).chr.is_a?(String)
|
42
|
+
end
|
43
|
+
|
44
|
+
#The Unicode Standard, version 4.0--online edition.
|
45
|
+
#Section 3.9, D36.
|
46
|
+
def test_d36
|
47
|
+
# Example strings
|
48
|
+
cp = [0x004D, 0x0430, 0x4E8C, 0x10302]
|
49
|
+
str = [0x4D, 0xD0, 0xB0, 0xE4, 0xBA, 0x8C, 0xF0, 0x90, 0x8C, 0x82]
|
50
|
+
|
51
|
+
new_cp = ''
|
52
|
+
cp.each {|c| new_cp << c.chr}
|
53
|
+
new_str = "%c%c%c%c%c%c%c%c%c%c" % str
|
54
|
+
|
55
|
+
assert new_cp == new_str, 'Strings match.'
|
56
|
+
|
57
|
+
# Table 3-6
|
58
|
+
|
59
|
+
# Non-shortest form
|
60
|
+
#Covered by previous section
|
61
|
+
|
62
|
+
# Surrogate pairs
|
63
|
+
cprange = 0xD800..0xDFFF
|
64
|
+
cprange.each {|cp|
|
65
|
+
begin
|
66
|
+
cp.chr
|
67
|
+
assert false, ('Exception not thrown for U+%04X' % cp)
|
68
|
+
rescue RangeError
|
69
|
+
end
|
70
|
+
}
|
71
|
+
|
72
|
+
# Extra test cases mentioned around D36
|
73
|
+
|
74
|
+
begin
|
75
|
+
# 0b11000000 0b10101111
|
76
|
+
("%c%c" % [0xC0, 0xAF]).codepoints
|
77
|
+
assert false, 'Exception not thrown for invalid 2-byte construct'
|
78
|
+
rescue IOError
|
79
|
+
end
|
80
|
+
begin
|
81
|
+
# 0b11100000 0b10011111 0b10011111
|
82
|
+
("%c%c%c" % [0xE0, 0x9F, 0x80]).codepoints
|
83
|
+
assert false, 'Exception not thrown for invalid 3-byte construct'
|
84
|
+
rescue IOError
|
85
|
+
end
|
86
|
+
# This one should succeed
|
87
|
+
("%c%c%c%c" % [0xF4, 0x80, 0x83, 0x92]).codepoints
|
88
|
+
end
|
89
|
+
end
|
data/test/suite.rb
CHANGED
@@ -10,12 +10,8 @@
|
|
10
10
|
|
11
11
|
=end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
rescue LoadError
|
16
|
-
end
|
17
|
-
|
18
|
-
require 'database_test.rb'
|
13
|
+
require 'runicode_test'
|
14
|
+
require 'database_test'
|
19
15
|
|
20
16
|
require 'klookup'
|
21
17
|
require 'test/unit'
|
@@ -25,13 +21,13 @@ class Lookup_Test < Test::Unit::TestCase
|
|
25
21
|
|
26
22
|
# Check classes
|
27
23
|
def test_radical_type
|
28
|
-
|
29
|
-
|
24
|
+
assert_kind_of Array, KLookup::Lookup::Radical.strokes, 'KLookup::Lookup::Radical.strokes is an Array'
|
25
|
+
assert_kind_of KLookup::Lookup::Radical, KLookup::Lookup::Radical.new('田'), 'KLookup::Lookup::Radical initilaisation works'
|
30
26
|
end
|
31
27
|
|
32
28
|
# Check to_s behaviour
|
33
29
|
def test_radical_to_s
|
34
|
-
|
30
|
+
assert_equal KLookup::Lookup::Radical.new('田').to_s, '田', 'A field is a field.'
|
35
31
|
end
|
36
32
|
|
37
33
|
def test_radical_list
|
@@ -41,8 +37,8 @@ class Lookup_Test < Test::Unit::TestCase
|
|
41
37
|
|
42
38
|
# Initialisation should work
|
43
39
|
def test_kanji_to_s
|
44
|
-
|
45
|
-
|
40
|
+
assert_equal KLookup::Lookup::Kanji.new('猫').to_s, '猫', 'Cat.'
|
41
|
+
assert_equal KLookup::Lookup::Kanji.new('栗').to_s, '栗', 'Chestnut.'
|
46
42
|
end
|
47
43
|
|
48
44
|
# Lookups should work
|
@@ -62,27 +58,25 @@ class Lookup_Test < Test::Unit::TestCase
|
|
62
58
|
# Checks stroke count works
|
63
59
|
def test_kanji_lookup_stroke
|
64
60
|
# Unmatching stroke counts should result in empty lists
|
65
|
-
|
66
|
-
|
61
|
+
assert_equal 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.'
|
62
|
+
assert_equal 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
63
|
end
|
68
64
|
|
69
65
|
def test_kanji_radicals
|
70
66
|
cat=KLookup::Lookup::Kanji.new('猫').radicals
|
71
67
|
catr=[KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')]
|
72
|
-
|
73
|
-
assert false, 'KLookup::Lookup::Kanji#radicals works fine.'
|
74
|
-
end
|
68
|
+
assert (cat.all? {|r| catr.include? r } and cat.size==catr.size), 'KLookup::Lookup::Kanji#radicals works fine.'
|
75
69
|
end
|
76
70
|
|
71
|
+
# TODO: Not compatible with non-KANJIDIC data sources
|
77
72
|
def test_kanji_meaning
|
78
73
|
pull = KLookup::Lookup::Kanji.new('引')
|
79
|
-
|
74
|
+
assert_equal pull.meaning, ['pull','tug','jerk','admit','install','quote',
|
80
75
|
'refer to']
|
81
76
|
origin = KLookup::Lookup::Kanji.new('元')
|
82
|
-
|
77
|
+
assert_equal origin.meaning, ['beginning','former time','origin']
|
83
78
|
i = KLookup::Lookup::Kanji.new('以')
|
84
|
-
|
85
|
-
i.to_s
|
79
|
+
assert_equal i.meaning,['by means of','because','in view of','compared with']
|
86
80
|
end
|
87
81
|
|
88
82
|
def test_kanji_reading
|
@@ -92,30 +86,21 @@ class Lookup_Test < Test::Unit::TestCase
|
|
92
86
|
end
|
93
87
|
|
94
88
|
def test_multiple_kanji
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
rescue Exception
|
99
|
-
assert $!.is_a?(ArgumentError), 'Raised correct exception.'
|
100
|
-
end
|
89
|
+
assert_raise(ArgumentError) {
|
90
|
+
KLookup::Lookup::Kanji.new('馬鹿')
|
91
|
+
}
|
101
92
|
end
|
102
93
|
|
103
94
|
def test_un_kanji
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
rescue ArgumentError
|
108
|
-
# Win
|
109
|
-
end
|
95
|
+
assert_raise(ArgumentError) {
|
96
|
+
KLookup::Lookup::Kanji.new('k')
|
97
|
+
}
|
110
98
|
end
|
111
99
|
|
112
100
|
def test_un_radical
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
rescue ArgumentError
|
117
|
-
# Win
|
118
|
-
end
|
101
|
+
assert_raise(ArgumentError) {
|
102
|
+
KLookup::Lookup::Radical.new('r')
|
103
|
+
}
|
119
104
|
end
|
120
105
|
|
121
106
|
def test_exist
|
@@ -191,28 +176,57 @@ class Lookup_Test < Test::Unit::TestCase
|
|
191
176
|
assert (look.include?(cat) and not look.include?(dog)),
|
192
177
|
'true in block and reading'
|
193
178
|
end
|
179
|
+
|
180
|
+
def test_kanji_stroke_count
|
181
|
+
assert_equal KLookup::Lookup::Kanji.new('猫').stroke_count, 11
|
182
|
+
assert_equal KLookup::Lookup::Kanji.new('犬').stroke_count, 4
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_radical_stroke_count
|
186
|
+
assert_equal KLookup::Lookup::Radical.new('犬').stroke_count, 4
|
187
|
+
assert_equal KLookup::Lookup::Radical.new('犭').stroke_count, 3
|
188
|
+
end
|
194
189
|
end
|
195
190
|
|
191
|
+
if $0 == __FILE__
|
192
|
+
require 'test/unit/ui/console/testrunner'
|
193
|
+
results=[]
|
194
|
+
db_results=[]
|
195
|
+
db=KLookup::Database
|
196
|
+
puts
|
197
|
+
|
198
|
+
db.constants.each {|h|
|
199
|
+
handler = "#{db.to_s}::#{h}"
|
200
|
+
next unless eval(handler).superclass == db
|
201
|
+
puts
|
202
|
+
puts "= Handler: #{handler}"
|
203
|
+
Database_Test.set_handler(eval(handler)) # Database class
|
204
|
+
db_results << [handler,
|
205
|
+
Test::Unit::UI::Console::TestRunner.run(Database_Test)]
|
206
|
+
|
207
|
+
KLookup::Lookup.handler = eval(handler)
|
208
|
+
results << [handler,
|
209
|
+
Test::Unit::UI::Console::TestRunner.run(Lookup_Test)]
|
210
|
+
puts
|
211
|
+
}
|
196
212
|
|
197
|
-
require 'test/unit/ui/console/testrunner'
|
198
|
-
results=[]
|
199
|
-
db=KLookup::Database
|
200
|
-
puts
|
201
|
-
puts "= Lookup_Test"
|
202
|
-
# Just use FlatFile for the moment
|
203
|
-
#[:FlatFile].each {|h|
|
204
|
-
db.constants.each {|h|
|
205
|
-
handler = "#{db.to_s}::#{h}"
|
206
|
-
next unless eval(handler).superclass == db
|
207
213
|
puts
|
208
|
-
puts "
|
209
|
-
|
210
|
-
|
214
|
+
puts "= RUnicode_Test"
|
215
|
+
runicode_results = Test::Unit::UI::Console::TestRunner.run(RUnicode_Test)
|
216
|
+
puts "#{runicode_results.passed? ? 'PASS' : 'FAIL'}"
|
217
|
+
puts ("\t%s" % runicode_results.to_s) unless runicode_results.passed?
|
218
|
+
|
211
219
|
puts
|
212
|
-
|
220
|
+
puts "= Database_Test"
|
221
|
+
db_results.each {|h,r|
|
222
|
+
puts "#{r.passed? ? 'PASS' : 'FAIL'}\t: #{h}"
|
223
|
+
puts ("\t%s" % r.to_s) unless r.passed?
|
224
|
+
}
|
213
225
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
226
|
+
puts
|
227
|
+
puts "= Lookup_Test"
|
228
|
+
results.each {|h,r|
|
229
|
+
puts "#{r.passed? ? 'PASS' : 'FAIL'}\t: #{h}"
|
230
|
+
puts ("\t%s" % r.to_s) unless r.passed?
|
231
|
+
}
|
232
|
+
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: klookup
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-
|
6
|
+
version: "0.3"
|
7
|
+
date: 2007-03-01 00:00:00 +00:00
|
8
8
|
summary: A set of kanji lookup tools and a library.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -12,7 +12,7 @@ email: tom@holizz.com
|
|
12
12
|
homepage: http://klookup.rubyforge.org/
|
13
13
|
rubyforge_project: klookup
|
14
14
|
description: A character dictionary for Japanese kanji.
|
15
|
-
autorequire:
|
15
|
+
autorequire: klookup
|
16
16
|
default_executable: cklookup
|
17
17
|
bindir: bin
|
18
18
|
has_rdoc: true
|
@@ -29,22 +29,27 @@ post_install_message:
|
|
29
29
|
authors:
|
30
30
|
- Tom Adams
|
31
31
|
files:
|
32
|
+
- data/klookup/data.db
|
32
33
|
- data/klookup/kanjidic
|
33
34
|
- data/klookup/newradkfile
|
34
|
-
- lib/
|
35
|
+
- lib/runicode.rb
|
35
36
|
- lib/klookup
|
36
|
-
- lib/klookup/
|
37
|
-
- lib/klookup/
|
37
|
+
- lib/klookup/database_sqlite.rb
|
38
|
+
- lib/klookup/lookup_kanji.rb
|
38
39
|
- lib/klookup/database.rb
|
39
|
-
- lib/klookup/
|
40
|
-
- lib/klookup/database_flatfile_radk.rb
|
41
|
-
- lib/klookup/database_unihan.rb
|
40
|
+
- lib/klookup/lookup.rb
|
42
41
|
- lib/klookup/database_flatfile_kanjidic.rb
|
42
|
+
- lib/klookup/database_unihan.rb
|
43
|
+
- lib/klookup/database_flatfile_radk.rb
|
44
|
+
- lib/klookup/lookup_radical.rb
|
43
45
|
- lib/klookup/database_flatfile.rb
|
44
|
-
- lib/
|
46
|
+
- lib/runicode
|
47
|
+
- lib/runicode/utf8.rb
|
48
|
+
- lib/klookup.rb
|
45
49
|
test_files:
|
46
|
-
- test/suite.rb
|
47
50
|
- test/database_test.rb
|
51
|
+
- test/runicode_test.rb
|
52
|
+
- test/suite.rb
|
48
53
|
rdoc_options:
|
49
54
|
- - --main
|
50
55
|
- KLookup
|
@@ -61,11 +66,11 @@ requirements: []
|
|
61
66
|
|
62
67
|
dependencies:
|
63
68
|
- !ruby/object:Gem::Dependency
|
64
|
-
name:
|
69
|
+
name: sqlite-ruby
|
65
70
|
version_requirement:
|
66
71
|
version_requirements: !ruby/object:Gem::Version::Requirement
|
67
72
|
requirements:
|
68
73
|
- - ">="
|
69
74
|
- !ruby/object:Gem::Version
|
70
|
-
version:
|
75
|
+
version: 2.2.3
|
71
76
|
version:
|
@@ -1,38 +0,0 @@
|
|
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
|