Class: StarDict

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

Instance Method Summary collapse

Constructor Details

#initialize(prefix) ⇒ StarDict

Initialize a new stardict object, prefix is the name of stardict files without extensions.

example:

Dir.chdir(dict_dir)
stardict=StarDict.new('lang_to_lang_dict') #without any extension.
#you can also access dictionary options (.ifo file data) by:

puts stardict.bookname #prints the book/dict name


43
44
45
46
47
48
49
50
# File 'lib/stardict.rb', line 43

def initialize(prefix)
  @prefix=prefix
  @info=info_data
  @idx=idx_data
  @dict=dict_data
  define_options
  parse
end

Instance Method Details

#find_by_trans(trans) ⇒ Object

Returns the word of ‘trans’.



69
70
71
# File 'lib/stardict.rb', line 69

def find_by_trans(trans)
  @wordlist.invert[trans] if trans_exists?(trans) 
end

#find_by_word(word) ⇒ Object

Returns the translaton of ‘word’.



65
66
67
# File 'lib/stardict.rb', line 65

def find_by_word(word)
  @wordlist[word] if word_exists?(word)
end

#search(keyword) ⇒ Object

Performs a search in the whole dictionary, returns a hash of results.



73
74
75
76
77
78
79
80
# File 'lib/stardict.rb', line 73

def search(keyword)
  re=@wordlist.
  select do |k,v|
    k.include?(keyword)|
    v.include?(keyword)
  end
  Hash[*re.flatten!]
end

#trans_exists?(trans) ⇒ Boolean

Returns true if exists, otherwise false.

Returns:

  • (Boolean)


61
62
63
# File 'lib/stardict.rb', line 61

def trans_exists?(trans)
  @wordlist.values.include?(trans)
end

#word_exists?(word) ⇒ Boolean

Returns true if exists, otherwise false.

Returns:

  • (Boolean)


57
58
59
# File 'lib/stardict.rb', line 57

def word_exists?(word)
  @wordlist.key?(word)
end

#wordlistObject

Returns a hash of wordlist word=>translation, also similar words had its translation merged.



53
54
55
# File 'lib/stardict.rb', line 53

def wordlist
  @wordlist
end

#wordlist_countObject

Wordlist count.



82
83
84
# File 'lib/stardict.rb', line 82

def wordlist_count
  @wordlist.length
end