rbstardict 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/stardict.rb +20 -27
- metadata +1 -1
data/lib/stardict.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#!/
|
1
|
+
#!/bin/ruby
|
2
2
|
# Stardict library for ruby.
|
3
3
|
#
|
4
4
|
# Manipulates stardict dictionaries.
|
@@ -27,15 +27,19 @@ require 'rubygems'
|
|
27
27
|
require 'zlib'
|
28
28
|
require 'stringio'
|
29
29
|
include Zlib
|
30
|
-
$VERSION=0.1
|
30
|
+
$VERSION='0.1.1'
|
31
31
|
|
32
32
|
class StarDict
|
33
|
-
#Initialize a new stardict object,
|
33
|
+
#Initialize a new stardict object, prefix
|
34
34
|
#is the name of stardict files without extensions.
|
35
35
|
#
|
36
36
|
#example:
|
37
|
+
#
|
37
38
|
# Dir.chdir(dict_dir)
|
38
|
-
# stardict=StarDict.new('lang_to_lang_dict') #without
|
39
|
+
# stardict=StarDict.new('lang_to_lang_dict') #without any extension.
|
40
|
+
# #you can also access dictionary options (.ifo file data) by:
|
41
|
+
#
|
42
|
+
# puts stardict.bookname #prints the book/dict name
|
39
43
|
def initialize(prefix)
|
40
44
|
@prefix=prefix
|
41
45
|
@info=info_data
|
@@ -65,23 +69,20 @@ class StarDict
|
|
65
69
|
def find_by_trans(trans)
|
66
70
|
@wordlist.invert[trans] if trans_exists?(trans)
|
67
71
|
end
|
68
|
-
#
|
69
|
-
def
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
@wordlist.values.map do |w|
|
77
|
-
w if w.include?(trans)
|
78
|
-
end.compact!
|
72
|
+
#Performs a search in the whole dictionary, returns a hash of results.
|
73
|
+
def search(keyword)
|
74
|
+
re=@wordlist.
|
75
|
+
select do |k,v|
|
76
|
+
k.include?(keyword)|
|
77
|
+
v.include?(keyword)
|
78
|
+
end
|
79
|
+
Hash[*re.flatten!]
|
79
80
|
end
|
80
81
|
#Wordlist count.
|
81
82
|
def wordlist_count
|
82
83
|
@wordlist.length
|
83
84
|
end
|
84
|
-
|
85
|
+
|
85
86
|
private
|
86
87
|
|
87
88
|
def info_data
|
@@ -117,24 +118,16 @@ class StarDict
|
|
117
118
|
re=entry.unpack("A#{t_pos}NN")
|
118
119
|
@dict.seek(re[1])
|
119
120
|
word,trans=re[0],@dict.read(re[2])
|
120
|
-
norm(word,trans)
|
121
121
|
#for duplicated key:
|
122
122
|
#append for exiting one
|
123
|
-
#
|
124
|
-
|
125
|
-
@wordlist[word]+="
|
123
|
+
#if it exisis
|
124
|
+
if @wordlist.key?(word)
|
125
|
+
@wordlist[word]+="\n#{trans}"
|
126
126
|
else
|
127
127
|
@wordlist[word]=trans
|
128
128
|
end
|
129
129
|
end;@wordlist
|
130
130
|
end
|
131
|
-
|
132
|
-
def norm(*args)
|
133
|
-
#avoid single quotes
|
134
|
-
#and replace \n with ::
|
135
|
-
args.each{|a|a.gsub!("'","''")
|
136
|
-
a.gsub!(/[\n\t]/,"::")}
|
137
|
-
end
|
138
131
|
|
139
132
|
def get_data(file,gz)
|
140
133
|
begin
|