Class: JLDrill::VocabularyUsage::Map

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

Overview

Map of VocabularyUsages to file positions that can be searched by kanji and reading quickly. This is used to store the location in the example dictionary where each vocabulary is used.

Defined Under Namespace

Classes: SearchResult

Instance Method Summary collapse

Constructor Details

#initializeMap

Returns a new instance of Map.



38
39
40
# File 'lib/jldrill/model/VocabularyUsage.rb', line 38

def initialize
    @usages = {}
end

Instance Method Details

#add(usageData, pos) ⇒ Object

Add a Vocabulary usage corresponding to Tanaka “B” line data and map it to the position, pos.



44
45
46
47
48
49
# File 'lib/jldrill/model/VocabularyUsage.rb', line 44

def add(usageData, pos)
    hash = VocabularyUsage.hashFrom_B_line(usageData)
    if !hash.empty?
        (@usages[hash] ||= []).push(pos)
    end
end

#add_B_line(b_line, pos) ⇒ Object

Take an entire Tanaka “B” line for an example sentences and add it to the map with position, pos.



53
54
55
56
57
58
# File 'lib/jldrill/model/VocabularyUsage.rb', line 53

def add_B_line(b_line, pos)
    w = b_line.split(' ')
    w.each do |usageData|
        add(usageData, pos)
    end
end

#search(kanji, reading) ⇒ Object

Search for VocabularyUsages which have the giving kanji and reading. If kanji is nil, reading will be used for the kanji (useful for vocabulary without kanji). This will first search for entries with both kanji and reading specified (to disambiguate entries with the same kanji and different readings). If this is empty, it will search for entries with just the kanji specified.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jldrill/model/VocabularyUsage.rb', line 66

def search(kanji, reading)
    hash = VocabularyUsage.hashFromStrings(kanji, reading)
    positions = @usages[hash]
    if positions.nil?
        # The corpus only uses readings to disambiguate
        # kanji.  Most usages don't have readings.  So
        # if we don't find anything, search again without
        # the reading.
        hash = JLDrill::VocabularyUsage.hashFromStrings(kanji, nil)
        positions = @usages[kanji]
    end

    return SearchResult.new(hash, positions)
end