klookup 0.2

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.
@@ -0,0 +1,29 @@
1
+ =begin
2
+
3
+ lib/klookup/lookup.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 module containing abstract classes representing Radical and Kanji.
14
+ module KLookup::Lookup
15
+
16
+ # Returns the default handler for database lookups.
17
+ def self.default_handler
18
+ KLookup::Database::FlatFile
19
+ end
20
+
21
+ # Set the handler used by both Kanji and Radical.
22
+ def self.handler=(h)
23
+ KLookup::Lookup::Kanji.handler=h
24
+ KLookup::Lookup::Radical.handler=h
25
+ end
26
+
27
+ require 'klookup/lookup_radical'
28
+ require 'klookup/lookup_kanji'
29
+ end
@@ -0,0 +1,107 @@
1
+ =begin
2
+
3
+ lib/klookup/lookup_kanji.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
+ # An abstract representation of 漢字 (kanji).
14
+ class KLookup::Lookup::Kanji
15
+ # A class variable because we have class methods which need this.
16
+ @@data = KLookup::Lookup.default_handler
17
+
18
+ def initialize(kanji)
19
+ unless @@data.instance.is_kanji?(kanji)
20
+ raise ArgumentError
21
+ end
22
+ @kanji=kanji.to_s
23
+ @reading=nil
24
+ @meaning=nil
25
+ end
26
+
27
+ # Get handler used by this class.
28
+ def self.handler
29
+ @@data
30
+ end
31
+
32
+ # Set handler used by this class (see also Lookup.handler=).
33
+ def self.handler=(h)
34
+ @@data = h
35
+ end
36
+
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
43
+ end
44
+
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|
53
+ KLookup::Lookup::Kanji.new(k)
54
+ }
55
+ end
56
+
57
+ # Returns true if kanji exists in database.
58
+ def self.exist?(kanji)
59
+ begin
60
+ new(kanji)
61
+ return true
62
+ rescue ArgumentError
63
+ return false
64
+ end
65
+ end
66
+
67
+ def == (kanji)
68
+ unless kanji.is_a? KLookup::Lookup::Kanji
69
+ kanji = KLookup::Lookup::Kanji.new(kanji.to_s)
70
+ end
71
+ return true if kanji.to_s == to_s
72
+ false
73
+ end
74
+
75
+ # Returns an array of radicals that make up a kanji.
76
+ def radicals
77
+ return @@data.instance.get_radicals(to_s).collect {|r|
78
+ KLookup::Lookup::Radical.new(r)
79
+ }
80
+ end
81
+
82
+ # Returns a textual representation of a radical.
83
+ def to_s
84
+ @kanji
85
+ end
86
+
87
+ #Returns a Struct of arrays of Japanese readings.
88
+ #
89
+ # KLookup::Lookup::Kanji.new('富').reading
90
+ # KLookup::Database::FlatFile::KanjiDic.instance.get_reading('富')
91
+ # #=> #<struct #<Class:0xb7d3b5dc> reading=["フ", "フウ", "と.む", "とみ"],
92
+ # name_reading=["と", "とん", "ふっ"]>
93
+ def reading
94
+ if @reading.nil?
95
+ @reading = @@data.instance.get_reading(to_s)
96
+ end
97
+ @reading
98
+ end
99
+
100
+ # Returns an array of English meanings.
101
+ def meaning
102
+ if @meaning.nil?
103
+ @meaning = @@data.instance.get_meaning(to_s)
104
+ end
105
+ @meaning
106
+ end
107
+ end
@@ -0,0 +1,74 @@
1
+ =begin
2
+
3
+ lib/klookup/lookup_radical.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
+ # An abstract representation of radicals.
14
+ class KLookup::Lookup::Radical
15
+ # A class variable because we have class methods which need this.
16
+ @@data = KLookup::Lookup.default_handler
17
+
18
+ def initialize(radical)
19
+ unless @@data.instance.is_radical?(radical)
20
+ raise ArgumentError
21
+ end
22
+ @radical = radical.to_s
23
+ end
24
+
25
+ # Get handler used by this class.
26
+ def self.handler
27
+ @@data
28
+ end
29
+
30
+ # Set handler used by this class (see also Lookup.handler=).
31
+ def self.handler=(h)
32
+ @@data = h
33
+ end
34
+
35
+ # Return an array of how many strokes radicals can be made up of.
36
+ def self.strokes
37
+ @@data.instance.stroke_count_list
38
+ end
39
+
40
+ # Returns an array of radicals with +strokes+ strokes. Without an argument,
41
+ # all radicals are returned.
42
+ def self.list(strokes=nil)
43
+ if strokes.nil?
44
+ @@data.instance.radicals_by_strokes.values.flatten
45
+ else
46
+ raise ArgumentError unless strokes.respond_to?(:to_i)
47
+
48
+ @@data.instance.radicals_by_strokes[strokes.to_i]
49
+ end
50
+ end
51
+
52
+ # Returns true if +radical+ exists in the database.
53
+ def self.exist?(radical)
54
+ begin
55
+ new(radical)
56
+ return true
57
+ rescue ArgumentError
58
+ return false
59
+ end
60
+ end
61
+
62
+ def == (radical)
63
+ unless radical.is_a? KLookup::Lookup::Radical
64
+ radical = KLookup::Lookup::Radical.new(radical.to_s)
65
+ end
66
+ return true if radical.to_s == to_s
67
+ false
68
+ end
69
+
70
+ # Returns a textual representation of a radical.
71
+ def to_s
72
+ @radical
73
+ end
74
+ end
data/test/suite.rb ADDED
@@ -0,0 +1,148 @@
1
+ =begin
2
+
3
+ test/suite.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
+ begin
14
+ require 'runicode_test'
15
+ rescue LoadError
16
+ end
17
+
18
+ require 'klookup'
19
+ require 'test/unit'
20
+
21
+ # KLookup::Lookup tests
22
+ class KLookup_Test < Test::Unit::TestCase
23
+
24
+ # Check classes
25
+ def test_radical_type
26
+ assert KLookup::Lookup::Radical.strokes.kind_of?(Array), 'KLookup::Lookup::Radical.strokes is an Array'
27
+ assert KLookup::Lookup::Radical.new('田').kind_of?(KLookup::Lookup::Radical), 'KLookup::Lookup::Radical initilaisation works'
28
+ end
29
+
30
+ # Check to_s behaviour
31
+ def test_radical_to_s
32
+ assert KLookup::Lookup::Radical.new('田').to_s=='田', 'A field is a field.'
33
+ end
34
+
35
+ def test_radical_list
36
+ assert KLookup::Lookup::Radical.list(1).any? {|k| k.to_s=='一'}, 'The first radical is fine.'
37
+ assert KLookup::Lookup::Radical.list(17).any? {|k| k.to_s=='龠'}, 'A flute is a flute.'
38
+ end
39
+
40
+ # Initialisation should work
41
+ def test_kanji_to_s
42
+ assert KLookup::Lookup::Kanji.new('猫').to_s=='猫', 'Cat.'
43
+ assert KLookup::Lookup::Kanji.new('栗').to_s=='栗', 'Chestnut.'
44
+ end
45
+
46
+ # Lookups should work
47
+ def test_kanji_lookup
48
+ # 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'
53
+
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.'
57
+
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.'
60
+ end
61
+
62
+ # Checks stroke count works
63
+ def test_kanji_lookup_stroke
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.'
67
+ end
68
+
69
+ def test_kanji_radicals
70
+ cat=KLookup::Lookup::Kanji.new('猫').radicals
71
+ catr=[KLookup::Lookup::Radical.new('艹'), KLookup::Lookup::Radical.new('犭'), KLookup::Lookup::Radical.new('田')]
72
+ unless cat.all? {|r| catr.include? r } and cat.size==catr.size
73
+ assert false, 'KLookup::Lookup::Kanji#radicals works fine.'
74
+ end
75
+ end
76
+
77
+ def test_kanji_meaning
78
+ pull = KLookup::Lookup::Kanji.new('引')
79
+ assert pull.meaning==['pull','tug','jerk','admit','install','quote',
80
+ 'refer to']
81
+ origin = KLookup::Lookup::Kanji.new('元')
82
+ assert origin.meaning==['beginning','former time','origin'], origin.to_s
83
+ i = KLookup::Lookup::Kanji.new('以')
84
+ assert i.meaning==['by means of','because','in view of','compared with'],
85
+ i.to_s
86
+ end
87
+
88
+ def test_kanji_reading
89
+ pull = KLookup::Lookup::Kanji.new('引')
90
+ assert pull.reading[:reading]==['イン','ひ.く','ひ.き','ひ.き-','-び.き','ひ.ける'], pull.to_s + ' reading'
91
+ assert pull.reading[:name_reading]==['いな','ひき','ひけ','びき'], pull.to_s + ' name_reading'
92
+ end
93
+
94
+ def test_multiple_kanji
95
+ begin
96
+ stupid = KLookup::Lookup::Kanji.new('馬鹿')
97
+ assert false, 'Failed to raise exception.'
98
+ rescue Exception
99
+ assert $!.is_a?(ArgumentError), 'Raised correct exception.'
100
+ end
101
+ end
102
+
103
+ def test_un_kanji
104
+ begin
105
+ not_a_kanji = KLookup::Lookup::Kanji.new('k')
106
+ assert false, 'Raised exception.'
107
+ rescue ArgumentError
108
+ # Win
109
+ end
110
+ end
111
+
112
+ def test_un_radical
113
+ begin
114
+ not_a_radical = KLookup::Lookup::Radical.new('r')
115
+ assert false, 'Raised exception.'
116
+ rescue ArgumentError
117
+ # Win
118
+ end
119
+ end
120
+
121
+ def test_exist
122
+ assert KLookup::Lookup::Kanji.exist?('猫')
123
+ assert (not KLookup::Lookup::Kanji.exist?('k'))
124
+
125
+ assert KLookup::Lookup::Radical.exist?('田')
126
+ assert (not KLookup::Lookup::Radical.exist?('た'))
127
+ end
128
+ end
129
+
130
+
131
+ require 'test/unit/ui/console/testrunner'
132
+ results=[]
133
+ db=KLookup::Database
134
+ db.constants.each {|h|
135
+ handler = "#{db.to_s}::#{h}"
136
+ next unless eval(handler).superclass == db
137
+ puts
138
+ puts "== KLookup::Lookup.handler = #{handler}"
139
+ KLookup::Lookup.handler = eval(handler)
140
+ results << [handler, Test::Unit::UI::Console::TestRunner.run(KLookup_Test)]
141
+ puts
142
+ }
143
+
144
+ results.each {|p|
145
+ h,r=p
146
+ puts "#{r.passed? ? 'PASS' : 'FAIL'} : #{h}"
147
+ puts (" %s" % r.to_s) unless r.passed?
148
+ }
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: klookup
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.2"
7
+ date: 2007-01-28 00:00:00 +00:00
8
+ summary: A set of kanji lookup tools and a library.
9
+ require_paths:
10
+ - lib
11
+ email: tom@holizz.com
12
+ homepage: http://klookup.rubyforge.org/
13
+ rubyforge_project: klookup
14
+ description: A character dictionary for Japanese kanji.
15
+ autorequire:
16
+ default_executable: cklookup
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Tom Adams
30
+ files:
31
+ - data/klookup/newradkfile
32
+ - data/klookup/kanjidic
33
+ - lib/klookup
34
+ - lib/klookup.rb
35
+ - lib/klookup/lookup_kanji.rb
36
+ - lib/klookup/database.rb
37
+ - lib/klookup/lookup.rb
38
+ - lib/klookup/database_flatfile_kanjidic.rb
39
+ - lib/klookup/database_unihan.rb
40
+ - lib/klookup/lookup_radical.rb
41
+ - lib/klookup/database_flatfile.rb
42
+ - lib/klookup/database_flatfile_radk.rb
43
+ test_files:
44
+ - test/suite.rb
45
+ rdoc_options:
46
+ - - --main
47
+ - KLookup
48
+ - --charset
49
+ - UTF-8
50
+ extra_rdoc_files: []
51
+
52
+ executables:
53
+ - cklookup
54
+ - gklookup
55
+ - klookup.cgi
56
+ extensions: []
57
+
58
+ requirements: []
59
+
60
+ dependencies:
61
+ - !ruby/object:Gem::Dependency
62
+ name: activesupport
63
+ version_requirement:
64
+ version_requirements: !ruby/object:Gem::Version::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.4.0
69
+ 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: