Class: KLookup::Database::SQLite
Overview
A singleton class to abstract RadK and KanjiDic.
Instance Method Summary
collapse
open_resource, resource_path
Constructor Details
#initialize ⇒ SQLite
Returns a new instance of SQLite.
19
20
21
22
23
|
# File 'lib/klookup/database_sqlite.rb', line 19
def initialize
require 'sqlite'
path = KLookup::Database.resource_path('data.db')
@db = ::SQLite::Database.new(path)
end
|
Instance Method Details
#finalize ⇒ Object
26
27
28
|
# File 'lib/klookup/database_sqlite.rb', line 26
def finalize
@db.close
end
|
#find_kanji(args = {}) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/klookup/database_sqlite.rb', line 45
def find_kanji(args={})
raise ArgumentError, 'args must be Hash or nil' unless args.kind_of?(Hash) or args.nil?
stroke_count = args[:stroke]
raise ArgumentError, ':stroke must be Integer or nil' unless stroke_count.kind_of?(Integer) or stroke_count.nil?
radical = args[:radical]
radical = [radical] if radical.kind_of?(String)
raise ArgumentError, ':radical must be Array, String, or nil' unless radical.kind_of?(Array) or radical.nil?
kanji = []
if stroke_count
kanji = @db.execute("select kanji from kanji where stroke_count=?",
stroke_count).flatten
else
kanji = @db.execute("select kanji from kanji").flatten
end
if radical
radical.each {|r|
raise ArgumentError, ':radical must contain radicals' unless is_radical?(r)
new_r = @db.execute("select kanji from mapping where radical=?",
r).flatten
kanji &= new_r
}
end
kanji
end
|
#find_radical(args = {}) ⇒ Object
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/klookup/database_sqlite.rb', line 34
def find_radical(args={})
raise ArgumentError unless args.kind_of?(Hash)
stroke_count = args[:stroke]
raise ArgumentError unless stroke_count.kind_of?(Integer) or stroke_count.nil?
if stroke_count
@db.execute("select radical from radical where stroke_count=?", stroke_count).flatten
else
@db.execute("select radical from radical").flatten
end
end
|
#get_meaning(character) ⇒ Object
95
96
97
98
|
# File 'lib/klookup/database_sqlite.rb', line 95
def get_meaning(character)
raise ArgumentError unless is_kanji?(character)
@db.execute("select meaning from meaning where kanji=?", character).flatten
end
|
#get_radical(character) ⇒ Object
82
83
84
85
86
|
# File 'lib/klookup/database_sqlite.rb', line 82
def get_radical(character)
raise ArgumentError unless is_kanji?(character)
@db.execute("select radical from mapping where kanji=?", character).
collect {|r| r.first }
end
|
#get_reading(character) ⇒ Object
88
89
90
91
92
93
|
# File 'lib/klookup/database_sqlite.rb', line 88
def get_reading(character)
raise ArgumentError unless is_kanji?(character)
reading = @db.execute("select reading from reading where kanji=? and is_name_reading='false'", character).flatten
name_reading = @db.execute("select reading from reading where kanji=? and is_name_reading='true'", character).flatten
Struct.new(:reading, :name_reading).new(reading, name_reading)
end
|
#get_stroke_count(character) ⇒ Object
72
73
74
75
76
77
78
79
80
|
# File 'lib/klookup/database_sqlite.rb', line 72
def get_stroke_count(character)
if is_kanji?(character)
@db.get_first_row("select stroke_count from kanji where kanji=?", character).first.to_i
elsif is_radical?(character)
@db.get_first_row("select stroke_count from radical where radical=?", character).first.to_i
else
raise ArgumentError
end
end
|
#is_kanji?(character) ⇒ Boolean
100
101
102
103
|
# File 'lib/klookup/database_sqlite.rb', line 100
def is_kanji?(character)
@db.get_first_row("select count(*) from kanji where kanji=?",
character).first.to_i >= 1
end
|
#is_radical?(character) ⇒ Boolean
105
106
107
108
|
# File 'lib/klookup/database_sqlite.rb', line 105
def is_radical?(character)
@db.get_first_row("select count(*) from radical where radical=?",
character).first.to_i >= 1
end
|
#radical_stroke_count ⇒ Object
30
31
32
|
# File 'lib/klookup/database_sqlite.rb', line 30
def radical_stroke_count
@db.execute("select distinct stroke_count from radical order by stroke_count").flatten.collect {|s| s.to_i}
end
|