Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/jldrill/model/moji/Kanji.rb | 169 | 138 | 86.98%
|
84.06%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 require 'jldrill/model/moji/Radical' |
2 require 'jldrill/model/DataFile' |
3 |
4 module JLDrill |
5 |
6 # Represents a Kanji (chinese character) |
7 class Kanji |
8 |
9 BUSHU_RE = /^B(\d+)/ |
10 GRADE_RE = /^G(\d+)/ |
11 STROKES_RE = /^S(\d+)/ |
12 |
13 attr_reader :character, :readings, :meanings, :bushu, :grade, :strokes |
14 |
15 def initialize(character, readings, meanings, bushu, grade, strokes) |
16 @character = character |
17 @readings = readings |
18 @meanings = meanings |
19 @bushu = bushu |
20 @grade = grade |
21 @strokes = strokes |
22 end |
23 |
24 def Kanji.getParts(string, separator) |
25 retVal = nil |
26 if !string.nil? |
27 retVal = string.split(separator, -1) |
28 end |
29 retVal |
30 end |
31 |
32 def Kanji.validSections?(sections) |
33 !sections.nil? && sections.size == 6 && !sections[0].nil? |
34 end |
35 |
36 def Kanji.parse(string) |
37 entry = nil |
38 sections = Kanji.getParts(string.chomp, "|") |
39 return if !Kanji.validSections?(sections) |
40 character = sections[0] |
41 readings = Kanji.getParts(sections[2], " ") |
42 meanings = Kanji.getParts(sections[5], ",") |
43 commands = Kanji.getParts(sections[1], " ") |
44 bushu = nil |
45 grade = nil |
46 strokes = nil |
47 commands.each do |command| |
48 case command |
49 when BUSHU_RE |
50 bushu = $1.to_i(10) |
51 when GRADE_RE |
52 grade = $1.to_i(10) |
53 when STROKES_RE |
54 strokes = $1.to_i(10) |
55 end |
56 end |
57 Kanji.new(character, readings, meanings, bushu, grade, strokes) |
58 end |
59 |
60 # Outputs values for optional items |
61 def optional_to_s(item) |
62 if item.nil? |
63 "*" |
64 else |
65 item.to_s |
66 end |
67 end |
68 |
69 def optional_join(list, separator) |
70 if list.nil? |
71 "" |
72 else |
73 list.join(separator) |
74 end |
75 end |
76 |
77 # This will create a string with the main bushu first, identified |
78 # with a *, followed by the rest of the radicals |
79 def radicals_to_s(radicals) |
80 retVal = "" |
81 rads = radicals.radicals(@character) |
82 if !@bushu.nil? |
83 bushu = radicals[@bushu - 1] |
84 retVal += "* " + bushu.to_s + "\n " |
85 rads.delete(bushu) |
86 end |
87 retVal += rads.join("\n ") |
88 end |
89 |
90 # Outputs kanji data with the added radical information |
91 # radicals is a radical list |
92 def withRadical_to_s(radicals) |
93 retVal = @character |
94 retVal += " [" + optional_join(@readings, " ") + "]\n" |
95 retVal += optional_join(@meanings, ", ") + "\n\n" |
96 retVal += "Grade " + optional_to_s(@grade) + ", " |
97 retVal += "Strokes " + optional_to_s(@strokes) + "\n" |
98 retVal += "\nRadicals:\n" |
99 retVal += radicals_to_s(radicals) |
100 end |
101 |
102 def to_s |
103 retVal = @character |
104 retVal += " [" + optional_join(@readings, " ") + "]\n" |
105 retVal += optional_join(@meanings, ", ") + "\n\n" |
106 retVal += "Grade " + optional_to_s(@grade) + ", " |
107 retVal += "Strokes " + optional_to_s(@strokes) + "\n" |
108 retVal += "Bushu " + optional_to_s(@bushu) + "\n" |
109 retVal |
110 end |
111 end |
112 |
113 # An array of Kanji. Useful for loading the kanji data. |
114 class KanjiList < Array |
115 |
116 def KanjiList.fromString(string) |
117 list = KanjiList.new |
118 string.each_line do |line| |
119 list.parse(line) |
120 end |
121 list |
122 end |
123 |
124 def KanjiList.fromFile(filename) |
125 list = KanjiList.new |
126 IO.foreach(filename) do |line| |
127 list.parse(line) |
128 end |
129 list |
130 end |
131 |
132 def parse(string) |
133 entry = Kanji.parse(string) |
134 if(!entry.nil?) |
135 self.push(entry) |
136 end |
137 end |
138 |
139 def findChar(char) |
140 self.find do |entry| |
141 entry.character == char |
142 end |
143 end |
144 |
145 def to_s |
146 self.join("\n") |
147 end |
148 end |
149 |
150 class KanjiFile < DataFile |
151 attr_reader :kanjiList |
152 attr_writer :kanjiList |
153 |
154 def initialize |
155 super |
156 @kanjiList = KanjiList.new |
157 @stepSize = 100 |
158 end |
159 |
160 def dataSize |
161 @kanjiList.size |
162 end |
163 |
164 def parser |
165 @kanjiList |
166 end |
167 end |
168 |
169 end |
Generated on Mon May 23 16:17:46 +0900 2011 with rcov 0.9.8