Class: KLookup::Lookup::Kanji

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(kanji) ⇒ Kanji

Returns a new instance of Kanji.


26
27
28
29
30
31
32
33
# File 'lib/klookup/lookup_kanji.rb', line 26

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.

Returns:

  • (Boolean)

102
103
104
105
106
107
108
109
# File 'lib/klookup/lookup_kanji.rb', line 102

def self.exist?(kanji)
  begin
    new(kanji)
    return true
  rescue ArgumentError
    return false
  end
end

.handlerObject

Get handler used by this class.


36
37
38
# File 'lib/klookup/lookup_kanji.rb', line 36

def self.handler
  @@data
end

.handler=(h) ⇒ Object

Set handler used by this class (see also Lookup.handler=).


41
42
43
# File 'lib/klookup/lookup_kanji.rb', line 41

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.


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/klookup/lookup_kanji.rb', line 52

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


111
112
113
114
115
116
117
# File 'lib/klookup/lookup_kanji.rb', line 111

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

#meaningObject

Returns an array of English meanings.


146
147
148
149
150
151
# File 'lib/klookup/lookup_kanji.rb', line 146

def meaning
  if @meaning.nil?
    @meaning = @@data.instance.get_meaning(to_s)
  end
  @meaning
end

#radicalObject Also known as: radicals

Returns an array of radicals that make up a kanji.


120
121
122
123
124
# File 'lib/klookup/lookup_kanji.rb', line 120

def radical
  return @@data.instance.get_radical(to_s).collect {|r|
    KLookup::Lookup::Radical.new(r)
  }
end

#readingObject

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=["と", "とん", "ふっ"]>

138
139
140
141
142
143
# File 'lib/klookup/lookup_kanji.rb', line 138

def reading
  if @reading.nil?
    @reading = @@data.instance.get_reading(to_s)
  end
  @reading
end

#stroke_countObject


153
154
155
# File 'lib/klookup/lookup_kanji.rb', line 153

def stroke_count
  @@data.instance.get_stroke_count(@kanji)
end

#to_sObject

Returns a textual representation of a radical.


128
129
130
# File 'lib/klookup/lookup_kanji.rb', line 128

def to_s
  @kanji
end