Class: JLDrill::Vocabulary

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Returns:

  • (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

#cloneObject

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

#contentStringObject

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

#definitionsObject

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

#definitionsArrayObject



220
221
222
# File 'lib/jldrill/model/items/Vocabulary.rb', line 220

def definitionsArray
    @definitions.contents
end

#definitionsFieldObject



212
213
214
# File 'lib/jldrill/model/items/Vocabulary.rb', line 212

def definitionsField
    @definitions
end

#definitionsRawObject



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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


231
232
233
# File 'lib/jldrill/model/items/Vocabulary.rb', line 231

def hasDefinitions?
    @definitions.assigned?
end

#hashObject

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

Returns:

  • (Boolean)


186
187
188
# File 'lib/jldrill/model/items/Vocabulary.rb', line 186

def hasHint?
    @hint.assigned?
end

#hasKanji?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


260
261
262
# File 'lib/jldrill/model/items/Vocabulary.rb', line 260

def hasMarkers?
    @markers.assigned?
end

#hasReading?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/jldrill/model/items/Vocabulary.rb', line 166

def hasReading?
    @reading.assigned?
end

#hintObject



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

#hintFieldObject



198
199
200
# File 'lib/jldrill/model/items/Vocabulary.rb', line 198

def hintField
    @hint
end

#hintRawObject



202
203
204
# File 'lib/jldrill/model/items/Vocabulary.rb', line 202

def hintRaw
    @hint.raw
end

#kanjiObject



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

#kanjiFieldObject



158
159
160
# File 'lib/jldrill/model/items/Vocabulary.rb', line 158

def kanjiField
    @kanji
end

#kanjiRawObject



162
163
164
# File 'lib/jldrill/model/items/Vocabulary.rb', line 162

def kanjiRaw
    @kanji.raw
end

#markersObject

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

#markersArrayObject



245
246
247
# File 'lib/jldrill/model/items/Vocabulary.rb', line 245

def markersArray
    @markers.contents
end

#markersFieldObject



241
242
243
# File 'lib/jldrill/model/items/Vocabulary.rb', line 241

def markersField
    @markers
end

#markersRawObject



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

#readingObject



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

#readingFieldObject



178
179
180
# File 'lib/jldrill/model/items/Vocabulary.rb', line 178

def readingField
    @reading
end

#readingRawObject



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

Returns:

  • (Boolean)


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_edictObject



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_sObject

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.

Returns:

  • (Boolean)


266
267
268
269
# File 'lib/jldrill/model/items/Vocabulary.rb', line 266

def valid?
    @reading.assigned? &&
        (@definitions.assigned? || @kanji.assigned?)
end