Class: JDict::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/dictionary.rb

Direct Known Subclasses

JMDict

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index_path = JDict.configuration.index_path, dictionary_path = nil, lazy_index_loading = JDict.configuration.lazy_index_loading) ⇒ Dictionary

Returns a new instance of Dictionary.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dictionary.rb', line 8

def initialize(index_path = JDict.configuration.index_path, dictionary_path = nil, lazy_index_loading = JDict.configuration.lazy_index_loading)
  path_specified = dictionary_path.nil? ? false : true
  if path_specified and not File.exists? dictionary_path
    raise "Dictionary not found at path #{dictionary_path}"
  end

  #store some args for future reference
  @dictionary_path    = dictionary_path
  @lazy_index_loading = lazy_index_loading

  @entries       = []
  @entries_cache = []

  #instantiate and load the full-text search index
  @index = DictIndex.new(index_path, dictionary_path, lazy_index_loading)
end

Instance Attribute Details

#entries_cacheObject (readonly)

Returns the value of attribute entries_cache.



6
7
8
# File 'lib/dictionary.rb', line 6

def entries_cache
  @entries_cache
end

#lazy_index_loadingObject (readonly)

Returns the value of attribute lazy_index_loading.



6
7
8
# File 'lib/dictionary.rb', line 6

def lazy_index_loading
  @lazy_index_loading
end

Instance Method Details

#get_pos(pos) ⇒ String

Retrieves the definition of a part-of-speech from its abbreviation

Parameters:

  • pos (String)

    the abbreviation for the part-of-speech

Returns:

  • (String)

    the full description of the part-of-speech



48
49
50
# File 'lib/dictionary.rb', line 48

def get_pos(pos)
  @index.get_pos(pos)
end

#loaded?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/dictionary.rb', line 29

def loaded?
  @index.built?
end

#search(query) ⇒ Array(Entry)

Search this dictionary’s index for the given string.

Parameters:

  • query (String)

    the search query

Returns:

  • (Array(Entry))

    the results of the search



36
37
38
39
40
41
42
43
# File 'lib/dictionary.rb', line 36

def search(query)
  results = []
  return results if query.empty?

  load_index if lazy_index_loading and not loaded?

  results = @index.search(query)
end

#sizeObject



25
26
27
# File 'lib/dictionary.rb', line 25

def size
  @entries.size
end