Class: JLDrill::Kanji

Inherits:
Object
  • Object
show all
Defined in:
lib/jldrill/model/moji/Kanji.rb

Overview

Represents a Kanji (chinese character)

Constant Summary collapse

BUSHU_RE =
/^B(\d+)/
GRADE_RE =
/^G(\d+)/
STROKES_RE =
/^S(\d+)/
PINYIN_RE =
/^Y([^|\s]+)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(character, readings, meanings, bushu, grade, strokes, pinyin) ⇒ Kanji

Returns a new instance of Kanji.



18
19
20
21
22
23
24
25
26
27
# File 'lib/jldrill/model/moji/Kanji.rb', line 18

def initialize(character, readings, meanings, bushu, 
                     grade, strokes, pinyin)
	@character = character
	@readings = readings
	@meanings = meanings
	@bushu = bushu
	@grade = grade
	@strokes = strokes
          @pinyin = pinyin
end

Instance Attribute Details

#bushuObject (readonly)

Returns the value of attribute bushu.



15
16
17
# File 'lib/jldrill/model/moji/Kanji.rb', line 15

def bushu
  @bushu
end

#characterObject (readonly)

Returns the value of attribute character.



15
16
17
# File 'lib/jldrill/model/moji/Kanji.rb', line 15

def character
  @character
end

#gradeObject (readonly)

Returns the value of attribute grade.



15
16
17
# File 'lib/jldrill/model/moji/Kanji.rb', line 15

def grade
  @grade
end

#meaningsObject (readonly)

Returns the value of attribute meanings.



15
16
17
# File 'lib/jldrill/model/moji/Kanji.rb', line 15

def meanings
  @meanings
end

#pinyinObject (readonly)

Returns the value of attribute pinyin.



15
16
17
# File 'lib/jldrill/model/moji/Kanji.rb', line 15

def pinyin
  @pinyin
end

#readingsObject (readonly)

Returns the value of attribute readings.



15
16
17
# File 'lib/jldrill/model/moji/Kanji.rb', line 15

def readings
  @readings
end

#strokesObject (readonly)

Returns the value of attribute strokes.



15
16
17
# File 'lib/jldrill/model/moji/Kanji.rb', line 15

def strokes
  @strokes
end

Class Method Details

.getParts(string, separator) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/jldrill/model/moji/Kanji.rb', line 29

def Kanji.getParts(string, separator)
    retVal = nil
    if !string.nil?
        retVal = string.split(separator, -1)
    end
    retVal
end

.parse(string) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jldrill/model/moji/Kanji.rb', line 41

def Kanji.parse(string)
	entry = nil
	sections = Kanji.getParts(string.chomp, "|")
	return if !Kanji.validSections?(sections)
	character = sections[0]
	readings = Kanji.getParts(sections[2], " ")
	meanings = Kanji.getParts(sections[5], ",")
	commands = Kanji.getParts(sections[1], " ")
	bushu = nil
	grade = nil
	strokes = nil
          pinyin=[]
	commands.each do |command|
	    case command
	        when BUSHU_RE
      				bushu = $1.to_i(10)
	        when GRADE_RE
      				grade = $1.to_i(10)
	        when STROKES_RE
      				strokes = $1.to_i(10)
                  when PINYIN_RE
                      pinyin.push($1)
	    end
	end
	Kanji.new(character, readings, meanings, bushu, grade, strokes, pinyin)
end

.validSections?(sections) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/jldrill/model/moji/Kanji.rb', line 37

def Kanji.validSections?(sections)
    !sections.nil? && sections.size == 6 && !sections[0].nil?
end

Instance Method Details

#optional_join(list, separator) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/jldrill/model/moji/Kanji.rb', line 77

def optional_join(list, separator)
    if list.nil?
        ""
    else
        list.join(separator)
    end
end

#optional_to_s(item) ⇒ Object

Outputs values for optional items



69
70
71
72
73
74
75
# File 'lib/jldrill/model/moji/Kanji.rb', line 69

def optional_to_s(item)
    if item.nil?
        "*"
    else
        item.to_s
    end
end

#pinyin_radicals_to_s(kanjilist, radicals) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jldrill/model/moji/Kanji.rb', line 98

def pinyin_radicals_to_s(kanjilist, radicals)
    retVal = ""
  		rads = radicals.radicals(@character)
  		if !@bushu.nil?
        bushu = radicals[@bushu - 1]
      		retVal += "* " + bushu.to_s_with_pinyin(kanjilist) + "\n  "
      		rads.delete(bushu)
          end
          rads.each do |rad|
              retVal += rad.to_s_with_pinyin(kanjilist) + "\n  "
          end
          retVal
end

#radicals_to_s(radicals) ⇒ Object

This will create a string with the main bushu first, identified with a *, followed by the rest of the radicals



87
88
89
90
91
92
93
94
95
96
# File 'lib/jldrill/model/moji/Kanji.rb', line 87

def radicals_to_s(radicals)
    retVal = ""
  		rads = radicals.radicals(@character)
  		if !@bushu.nil?
        bushu = radicals[@bushu - 1]
      		retVal += "* " + bushu.to_s + "\n  "
      		rads.delete(bushu)
          end
  		retVal += rads.join("\n  ")
end

#to_sObject



134
135
136
137
138
139
140
141
142
# File 'lib/jldrill/model/moji/Kanji.rb', line 134

def to_s
    retVal = @character
    retVal += " [" + optional_join(@readings, " ") + "]\n"
    retVal += optional_join(@meanings, ", ") + "\n\n"
    retVal += "Grade " + optional_to_s(@grade) + ", "
    retVal += "Strokes " + optional_to_s(@strokes) + "\n"
    retVal += "Bushu " + optional_to_s(@bushu) + "\n"
    retVal
end

#to_s_with_pinyinObject



144
145
146
147
148
149
150
151
152
# File 'lib/jldrill/model/moji/Kanji.rb', line 144

def to_s_with_pinyin
    retVal = @character
    retVal += " [" + optional_join(@pinyin, " ") + "]\n"
    retVal += optional_join(@meanings, ", ") + "\n\n"
    retVal += "Grade " + optional_to_s(@grade) + ", "
    retVal += "Strokes " + optional_to_s(@strokes) + "\n"
    retVal += "Bushu " + optional_to_s(@bushu) + "\n"
    retVal
end

#withPinYinRadical_to_s(kanjilist, radicals) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/jldrill/model/moji/Kanji.rb', line 124

def withPinYinRadical_to_s(kanjilist, radicals)
		    retVal = @character
		    retVal += " [" + optional_join(@pinyin, " ") + "]\n"
		    retVal += optional_join(@meanings, ", ") + "\n\n"
		    retVal += "Grade " + optional_to_s(@grade) + ", "
		    retVal += "Strokes " + optional_to_s(@strokes) + "\n"
    		retVal += "\nRadicals:\n"
    		retVal += pinyin_radicals_to_s(kanjilist, radicals)
end

#withRadical_to_s(radicals) ⇒ Object

Outputs kanji data with the added radical information radicals is a radical list



114
115
116
117
118
119
120
121
122
# File 'lib/jldrill/model/moji/Kanji.rb', line 114

def withRadical_to_s(radicals)
    retVal = @character
    retVal += " [" + optional_join(@readings, " ") + "]\n"
    retVal += optional_join(@meanings, ", ") + "\n\n"
    retVal += "Grade " + optional_to_s(@grade) + ", "
    retVal += "Strokes " + optional_to_s(@strokes) + "\n"
  		retVal += "\nRadicals:\n"
  		retVal += radicals_to_s(radicals)
end