Class: KLookup::Lookup::Kanji
- Inherits:
-
Object
- Object
- KLookup::Lookup::Kanji
- Defined in:
- lib/klookup/lookup_kanji.rb
Overview
An abstract representation of 漢字 (kanji/Chinese characters).
Constant Summary collapse
- @@data =
A class variable because we have class methods which use this.
KLookup::Lookup.default_handler
Class Method Summary collapse
-
.exist?(kanji) ⇒ Boolean
Returns true if kanji exists in database.
-
.handler ⇒ Object
Get handler used by this class.
-
.handler=(h) ⇒ Object
Set handler used by this class (see also Lookup.handler=).
-
.lookup(hash = nil) ⇒ Object
Returns an array of Kanji made up of :radicals, with a particular :meaning, with a particular :reading, or with a particular number of :strokes.
Instance Method Summary collapse
- #==(kanji) ⇒ Object
-
#initialize(kanji) ⇒ Kanji
constructor
A new instance of Kanji.
-
#meaning ⇒ Object
Returns an array of English meanings.
-
#radical ⇒ Object
(also: #radicals)
Returns an array of radicals that make up a kanji.
-
#reading ⇒ Object
Returns a Struct of arrays of Japanese readings.
- #stroke_count ⇒ Object
-
#to_s ⇒ Object
Returns a textual representation of a radical.
Constructor Details
#initialize(kanji) ⇒ Kanji
Returns a new instance of Kanji.
58 59 60 61 62 63 64 65 |
# File 'lib/klookup/lookup_kanji.rb', line 58 def initialize(kanji) unless @@data.instance.is_kanji?(kanji) raise ArgumentError end @kanji=kanji.to_s @reading=nil @meaning=nil end |
Class Method Details
.exist?(kanji) ⇒ Boolean
Returns true if kanji exists in database.
134 135 136 137 138 139 140 141 |
# File 'lib/klookup/lookup_kanji.rb', line 134 def self.exist?(kanji) begin new(kanji) return true rescue ArgumentError return false end end |
.handler ⇒ Object
Get handler used by this class.
68 69 70 |
# File 'lib/klookup/lookup_kanji.rb', line 68 def self.handler @@data end |
.handler=(h) ⇒ Object
Set handler used by this class (see also Lookup.handler=).
73 74 75 |
# File 'lib/klookup/lookup_kanji.rb', line 73 def self.handler=(h) @@data = h end |
.lookup(hash = nil) ⇒ Object
Returns an array of Kanji made up of :radicals, with a particular :meaning, with a particular :reading, or with a particular number of :strokes. A block can also be used which yields to Kanji objects. With no arguments and no block, all characters in the database are returned (you have been warned!).
Note that :meaning and :reading are slow.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/klookup/lookup_kanji.rb', line 84 def self.lookup(hash=nil) # Check values of the hash unless hash.respond_to?(:to_hash) or hash.nil? raise ArgumentError, 'Requires a hash or a block.' end hash = {} if hash.nil? meaning = hash[:meaning] meaning = [meaning] unless meaning.kind_of?(Array) or meaning.nil? reading = hash[:reading] reading = [reading] unless reading.kind_of?(Array) or reading.nil? radical = hash[:radical] unless radical.nil? radical = [radical] unless radical.kind_of?(Array) radical.map {|r| r.to_s } end stroke = hash[:stroke] stroke = stroke.to_i unless stroke.nil? kanji = @@data.instance.find_kanji(:stroke=>stroke, :radical=>radical).collect {|k| KLookup::Lookup::Kanji.new(k) } # Don't filter if reading.nil? and meaning.nil? and (not block_given?) return kanji end truths = 0 # Filter according to hash and block kanji = kanji.select {|k| unless reading.nil? next unless reading.any? {|r| k.reading.reading.any?{|s| norm_kana(r) === s } or k.reading.name_reading.any?{|s| norm_kana(r) === s } } end unless meaning.nil? next unless meaning.any? {|m| k.meaning.include?(m) } end next if block_given? and not yield k true } return kanji end |
Instance Method Details
#==(kanji) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/klookup/lookup_kanji.rb', line 143 def == (kanji) unless kanji.is_a? KLookup::Lookup::Kanji kanji = KLookup::Lookup::Kanji.new(kanji.to_s) end return true if kanji.to_s == to_s false end |
#meaning ⇒ Object
Returns an array of English meanings.
178 179 180 181 182 183 |
# File 'lib/klookup/lookup_kanji.rb', line 178 def meaning if @meaning.nil? @meaning = @@data.instance.get_meaning(to_s) end @meaning end |
#radical ⇒ Object Also known as: radicals
Returns an array of radicals that make up a kanji.
152 153 154 155 156 |
# File 'lib/klookup/lookup_kanji.rb', line 152 def radical return @@data.instance.get_radical(to_s).collect {|r| KLookup::Lookup::Radical.new(r) } end |
#reading ⇒ Object
Returns a Struct of arrays of Japanese readings.
KLookup::Lookup::Kanji.new(‘富’).reading KLookup::Database::FlatFile::KanjiDic.instance.get_reading(‘富’) #=> #<struct #<Class:0xb7d3b5dc> reading=[“フ”, “フウ”, “と.む”, “とみ”],
name_reading=["と", "とん", "ふっ"]>
170 171 172 173 174 175 |
# File 'lib/klookup/lookup_kanji.rb', line 170 def reading if @reading.nil? @reading = @@data.instance.get_reading(to_s) end @reading end |
#stroke_count ⇒ Object
185 186 187 |
# File 'lib/klookup/lookup_kanji.rb', line 185 def stroke_count @@data.instance.get_stroke_count(@kanji) end |
#to_s ⇒ Object
Returns a textual representation of a radical.
160 161 162 |
# File 'lib/klookup/lookup_kanji.rb', line 160 def to_s @kanji end |