Class: KLookup::Database::FlatFile::KanjiDic

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/klookup/database_flatfile_kanjidic.rb

Overview

Access to the KANJIDIC (additional information about kanji).

Instance Method Summary collapse

Instance Method Details

#get_all_kanjiObject

Returns an array of all kanji


64
65
66
# File 'lib/klookup/database_flatfile_kanjidic.rb', line 64

def get_all_kanji
  @records.keys
end

#get_meaning(kanji) ⇒ Object

Returns an array of English meanings of kanji.


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
# File 'lib/klookup/database_flatfile_kanjidic.rb', line 94

def get_meaning(kanji)
  items = @records[kanji][:items]
  # Select the items with the meaning
  flag=false
  temp_meaning = items.select {|i|
    if i =~ /^\{|\}$/ or flag
      flag=true
    end
  }
  temp_meaning.map! {|i| i.sub(/^\{(.*)\}$/, '\1') }
  temp_meaning.delete_if {|i| i=~/^\s*$/}

  # Stick the meanings together
  finish=true
  meaning=[]
  temp_meaning.each {|m|
    if m =~ /^\{(.*)$/ # {one
      meaning << $1
      finish = false
    elsif finish # there are no strings being constructed
      meaning << m
    elsif m =~ /^(.*)\}$/ # three}
      meaning.last<< ' ' + $1
      finish = true
    else # two (when there are strings being finished)
      meaning.last<< ' ' + m.dup
    end
  }
  meaning
end

#get_reading(kanji) ⇒ Object

Returns a Struct of arrays of Japanese readings.

KLookup::Database::FlatFile::KanjiDic.instance.get_reading('富') #=> #<struct #<Class:0xb7d3b5dc> reading=[“フ”, “フウ”, “と.む”, “とみ”],

name_reading=["と", "とん", "ふっ"]>

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/klookup/database_flatfile_kanjidic.rb', line 73

def get_reading(kanji)
  items = @records[kanji][:items]
  # Set readings
  name_flag = false
  reading = []
  name_reading = []
  items.each {|i|
    name_flag = true if i=='T1'
    if KLookup.include_kana?(i)
      if name_flag
        name_reading << i
      else
        reading << i
      end
    end
  }

  return Struct.new(:reading,:name_reading).new(reading, name_reading)
end

#get_strokes(kanji) ⇒ Object

Returns the number of strokes needed to write a given kanji.


58
59
60
61
# File 'lib/klookup/database_flatfile_kanjidic.rb', line 58

def get_strokes(kanji)
  @records[kanji][:items].select {|i|
    i =~ /^S\d+$/}.first.sub(/^S(\d+)$/, '\1').to_i
end

#is_kanji?(kanji) ⇒ Boolean

Returns true if a kanji exists in the database.

Returns:

  • (Boolean)

50
51
52
53
54
55
# File 'lib/klookup/database_flatfile_kanjidic.rb', line 50

def is_kanji?(kanji)
  return false unless kanji.respond_to?(:to_s)
  return false if kanji.to_s.chars.length != 1
  return false if @records[kanji.to_s].nil?
  return true
end