Class: JLDrill::Vocabulary
- Inherits:
-
Object
- Object
- JLDrill::Vocabulary
- Defined in:
- lib/jldrill/model/items/Vocabulary.rb
Constant Summary collapse
- DELIMITER_RE =
/[^\\]\//
- KANJI_RE =
/^Kanji: (.*)/
- HINT_RE =
/^Hint: (.*)/
- READING_RE =
/^Reading: (.*)/
- DEFINITIONS_RE =
/^Definitions: (.*)/
- MARKERS_RE =
/^Markers: (.*)/
- QUOTE_RE =
/["]/
- RETURN_RE =
/[\n]/
- TO_A_RE =
Regexp.new("",nil,'u')
- Headings =
[["kanji", "Kanji", 90], ["reading", "Reading", 130], ["definitions", "Meaning", 230]]
Class Method Summary collapse
-
.create(string) ⇒ Object
Create a new vocaublary item by parsing the string passed in.
Instance Method Summary collapse
-
#==(y) ⇒ Object
True if the two vocabulary are discussing the same word This does not compare the hint since it does not affect the meaning of the word.
- #arrayStartsWith?(array, string) ⇒ Boolean
-
#assign(vocab) ⇒ Object
Assign the contents of vocab to this object.
-
#clone ⇒ Object
Returns a deep copy of this item.
-
#contentString ⇒ Object
Output the contents in save file format.
-
#definitions ⇒ Object
Returns a string containing the definitions separated by commas.
-
#definitions=(string) ⇒ Object
Assigns the definitions from a string of comma separated definitions.
- #definitionsArray ⇒ Object
- #definitionsField ⇒ Object
- #definitionsRaw ⇒ Object
-
#eql?(y) ⇒ Boolean
True if the two vocabulary are discussing the same word This does not compare the hint since it does not affect the meaning of the word.
-
#hasDefinitions? ⇒ Boolean
Returns true if there are definitions set on the Vocabulary.
-
#hash ⇒ Object
Returns a hash for the Vocabulary.
- #hasHint? ⇒ Boolean
- #hasKanji? ⇒ Boolean
-
#hasMarkers? ⇒ Boolean
Returns true if there are definitions set on the Vocabulary.
- #hasReading? ⇒ Boolean
- #hint ⇒ Object
- #hint=(string) ⇒ Object
- #hintField ⇒ Object
- #hintRaw ⇒ Object
-
#initialize(kanji = nil, reading = nil, definitions = nil, markers = nil, hint = nil, position = nil) ⇒ Vocabulary
constructor
A new instance of Vocabulary.
- #kanji ⇒ Object
- #kanji=(string) ⇒ Object
- #kanjiField ⇒ Object
- #kanjiRaw ⇒ Object
-
#markers ⇒ Object
Returns a string containing the markers separated by commas.
-
#markers=(string) ⇒ Object
Assigns the definitions from a string of comma separated definitions.
- #markersArray ⇒ Object
- #markersField ⇒ Object
- #markersRaw ⇒ Object
-
#numCommonChars(string1, string2) ⇒ Object
Returns the number of characters at the beginning of string1 that are also at the beginning of string2.
-
#parse(string) ⇒ Object
Parses a vocabulary value in save format.
-
#rank(vocab) ⇒ Object
Returns a rank based on how “close” the vocab is to this one.
- #reading ⇒ Object
- #reading=(string) ⇒ Object
- #readingField ⇒ Object
- #readingRaw ⇒ Object
-
#split(string) ⇒ Object
Split string on / allowing it to be escaped with a .
-
#startsWith?(string) ⇒ Boolean
Returns true if any of the fields start with the string.
- #to_edict ⇒ Object
-
#to_s ⇒ Object
Output the vocabulary as a string in save file format.
-
#valid? ⇒ Boolean
Returns true if the vocabulary contains a reading and either at least one definition exists or kanji exists.
Constructor Details
#initialize(kanji = nil, reading = nil, definitions = nil, markers = nil, hint = nil, position = nil) ⇒ Vocabulary
Returns a new instance of Vocabulary.
25 26 27 28 29 30 31 32 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 25 def initialize(kanji=nil, reading=nil, definitions=nil, markers=nil, hint=nil, position=nil) @kanji = StringField.new("Kanji", kanji) @reading = StringField.new("Reading", reading) @hint = StringField.new("Hint", hint) @definitions = ListField.new("Definitions", definitions) @markers = ListField.new("Markers", markers) end |
Class Method Details
.create(string) ⇒ Object
Create a new vocaublary item by parsing the string passed in.
35 36 37 38 39 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 35 def Vocabulary.create(string) retVal = Vocabulary.new retVal.parse(string) retVal end |
Instance Method Details
#==(y) ⇒ Object
True if the two vocabulary are discussing the same word This does not compare the hint since it does not affect the meaning of the word.
64 65 66 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 64 def ==(y) return eql?(y) end |
#arrayStartsWith?(array, string) ⇒ Boolean
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 117 def arrayStartsWith?(array, string) retVal = false size = string.split(TO_A_RE).size if !array.nil? array.any? do |thing| retVal = numCommonChars(thing, string) == size end end return retVal end |
#assign(vocab) ⇒ Object
Assign the contents of vocab to this object.
138 139 140 141 142 143 144 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 138 def assign(vocab) @kanji.copy(vocab.kanjiField) @reading.copy(vocab.readingField) @definitions.copy(vocab.definitionsField) @markers.copy(vocab.markersField) @hint.copy(vocab.hintField) end |
#clone ⇒ Object
Returns a deep copy of this item. Note: Does not copy parameters that are not saveable. This is because of my cheezy implementation.
43 44 45 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 43 def clone Vocabulary.create(self.to_s) end |
#contentString ⇒ Object
Output the contents in save file format
324 325 326 327 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 324 def contentString return @kanji.to_s + @hint.to_s + @reading.to_s + @definitions.to_s + @markers.to_s end |
#definitions ⇒ Object
Returns a string containing the definitions separated by commas
208 209 210 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 208 def definitions @definitions.output end |
#definitions=(string) ⇒ Object
Assigns the definitions from a string of comma separated definitions
226 227 228 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 226 def definitions=(string) @definitions.assign(string) end |
#definitionsArray ⇒ Object
220 221 222 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 220 def definitionsArray @definitions.contents end |
#definitionsField ⇒ Object
212 213 214 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 212 def definitionsField @definitions end |
#definitionsRaw ⇒ Object
216 217 218 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 216 def definitionsRaw @definitions.raw end |
#eql?(y) ⇒ Boolean
True if the two vocabulary are discussing the same word This does not compare the hint since it does not affect the meaning of the word.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 50 def eql?(y) if !y.nil? @kanji.eql?(y.kanjiRaw) && @definitions.eql?(y.definitionsArray) && @markers.eql?(y.markersArray) && @reading.eql?(y.readingRaw) else false end end |
#hasDefinitions? ⇒ Boolean
Returns true if there are definitions set on the Vocabulary
231 232 233 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 231 def hasDefinitions? @definitions.assigned? end |
#hash ⇒ Object
Returns a hash for the Vocabulary. The hash is generated from the reading and kanji
70 71 72 73 74 75 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 70 def hash hashstring = "" hashstring += readingRaw hashstring += kanjiRaw return hashstring.hash end |
#hasHint? ⇒ Boolean
186 187 188 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 186 def hasHint? @hint.assigned? end |
#hasKanji? ⇒ Boolean
146 147 148 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 146 def hasKanji? @kanji.assigned? end |
#hasMarkers? ⇒ Boolean
Returns true if there are definitions set on the Vocabulary
260 261 262 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 260 def hasMarkers? @markers.assigned? end |
#hasReading? ⇒ Boolean
166 167 168 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 166 def hasReading? @reading.assigned? end |
#hint ⇒ Object
194 195 196 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 194 def hint @hint.output end |
#hint=(string) ⇒ Object
190 191 192 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 190 def hint=(string) @hint.assign(string) end |
#hintField ⇒ Object
198 199 200 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 198 def hintField @hint end |
#hintRaw ⇒ Object
202 203 204 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 202 def hintRaw @hint.raw end |
#kanji ⇒ Object
154 155 156 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 154 def kanji @kanji.output end |
#kanji=(string) ⇒ Object
150 151 152 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 150 def kanji=(string) @kanji.assign(string) end |
#kanjiField ⇒ Object
158 159 160 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 158 def kanjiField @kanji end |
#kanjiRaw ⇒ Object
162 163 164 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 162 def kanjiRaw @kanji.raw end |
#markers ⇒ Object
Returns a string containing the markers separated by commas
237 238 239 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 237 def markers @markers.output end |
#markers=(string) ⇒ Object
Assigns the definitions from a string of comma separated definitions
255 256 257 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 255 def markers=(string) @markers.assign(string) end |
#markersArray ⇒ Object
245 246 247 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 245 def markersArray @markers.contents end |
#markersField ⇒ Object
241 242 243 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 241 def markersField @markers end |
#markersRaw ⇒ Object
249 250 251 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 249 def markersRaw @markers.raw end |
#numCommonChars(string1, string2) ⇒ Object
Returns the number of characters at the beginning of string1 that are also at the beginning of string2.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 79 def numCommonChars(string1, string2) i = 0 if !string1.nil? && !string2.nil? a1 = string1.split(TO_A_RE) a2 = string2.split(TO_A_RE) while (i < a1.size) && (i < a2.size) && (a1[i] == a2[i]) do i += 1 end end return i end |
#parse(string) ⇒ Object
Parses a vocabulary value in save format.
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 304 def parse(string) split(string).each do |part| case part when KANJI_RE @kanji.assign($1) when HINT_RE @hint.assign($1) when READING_RE @reading.assign($1) when DEFINITIONS_RE @definitions.assign($1) when MARKERS_RE @markers.assign($1) else # Something we don't understand. Just ignore it end end end |
#rank(vocab) ⇒ Object
Returns a rank based on how “close” the vocab is to this one. Higher numbers are “closer”.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 94 def rank(vocab) theRank = 0 if !vocab.nil? theRank = numCommonChars(reading, vocab.reading) * 1000 theRank += numCommonChars(kanji, vocab.kanji) * 100 if !definitionsArray.nil? definitionsArray.each do |meaning| if vocab.definitions.include?(meaning) theRank += meaning.split(TO_A_RE).size end end end if !markersArray.nil? markersArray.each do |marker| if vocab.markers.include?(marker) theRank += marker.split(TO_A_RE).size end end end end return theRank end |
#reading ⇒ Object
174 175 176 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 174 def reading @reading.output end |
#reading=(string) ⇒ Object
170 171 172 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 170 def reading=(string) @reading.assign(string) end |
#readingField ⇒ Object
178 179 180 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 178 def readingField @reading end |
#readingRaw ⇒ Object
182 183 184 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 182 def readingRaw @reading.raw end |
#split(string) ⇒ Object
Split string on / allowing it to be escaped with a \
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 272 def split(string) retVal = [] int = "" esc = false string.split(TO_A_RE).each do |letter| if letter == "/" if !esc retVal.push(int) int = "" else int += letter esc = false end elsif letter == "\\" int += letter if !esc esc = true else esc = false end else esc = false int += letter end end if !int.empty? retVal.push(int) end return retVal end |
#startsWith?(string) ⇒ Boolean
Returns true if any of the fields start with the string
129 130 131 132 133 134 135 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 129 def startsWith?(string) size = string.split(TO_A_RE).size return (numCommonChars(reading, string) == size) || (numCommonChars(kanji, string) == size) || arrayStartsWith?(definitionsArray, string) || arrayStartsWith?(markersArray, string) end |
#to_edict ⇒ Object
334 335 336 337 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 334 def to_edict return self.kanji.to_s + " [" + self.reading.to_s + "] " + "(" + self.markers.to_s + ") " + self.definitions.to_s end |
#to_s ⇒ Object
Output the vocabulary as a string in save file format
330 331 332 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 330 def to_s contentString end |
#valid? ⇒ Boolean
Returns true if the vocabulary contains a reading and either at least one definition exists or kanji exists.
266 267 268 269 |
# File 'lib/jldrill/model/items/Vocabulary.rb', line 266 def valid? @reading.assigned? && (@definitions.assigned? || @kanji.assigned?) end |