rbstardict 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/stardict.rb +151 -0
- metadata +55 -0
data/lib/stardict.rb
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Stardict library for ruby.
|
3
|
+
#
|
4
|
+
# Manipulates stardict dictionaries.
|
5
|
+
#
|
6
|
+
# NOTE: see pystardict.py and stardict doc in
|
7
|
+
# the source's tarball
|
8
|
+
#
|
9
|
+
# Author: Abdelrahman ghanem <abom.jdev@jmail.com>
|
10
|
+
#
|
11
|
+
# www.programming-fr34ks.net
|
12
|
+
#
|
13
|
+
# This program is free software; you can redistribute it and/or modify
|
14
|
+
# it under the terms of the GNU General Public License as published by
|
15
|
+
# the Free Software Foundation; either version 2 of the License, or
|
16
|
+
# (at your option) any later version.
|
17
|
+
#
|
18
|
+
# This program is distributed in the hope that it will be useful,
|
19
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
+
# GNU General Public License for more details.
|
22
|
+
|
23
|
+
# You should have received a copy of the GNU General Public License along
|
24
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
25
|
+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
26
|
+
require 'rubygems'
|
27
|
+
require 'zlib'
|
28
|
+
require 'stringio'
|
29
|
+
include Zlib
|
30
|
+
$VERSION=0.1
|
31
|
+
|
32
|
+
class StarDict
|
33
|
+
#Initialize a new stardict object, preifx
|
34
|
+
#is the name of stardict files without extensions.
|
35
|
+
#
|
36
|
+
#example:
|
37
|
+
# Dir.chdir(dict_dir)
|
38
|
+
# stardict=StarDict.new('lang_to_lang_dict') #without '.idx','.ifo' or any extension .
|
39
|
+
def initialize(prefix)
|
40
|
+
@prefix=prefix
|
41
|
+
@info=info_data
|
42
|
+
@idx=idx_data
|
43
|
+
@dict=dict_data
|
44
|
+
define_options
|
45
|
+
parse
|
46
|
+
end
|
47
|
+
#Returns a hash of wordlist {word=>translation},
|
48
|
+
#also similar words had its translation merged.
|
49
|
+
def wordlist
|
50
|
+
@wordlist
|
51
|
+
end
|
52
|
+
#Returns true if exists, otherwise false.
|
53
|
+
def word_exists?(word)
|
54
|
+
@wordlist.key?(word)
|
55
|
+
end
|
56
|
+
#Returns true if exists, otherwise false.
|
57
|
+
def trans_exists?(trans)
|
58
|
+
@wordlist.values.include?(trans)
|
59
|
+
end
|
60
|
+
#Returns the translaton of 'word'.
|
61
|
+
def find_by_word(word)
|
62
|
+
@wordlist[word] if word_exists?(word)
|
63
|
+
end
|
64
|
+
#Returns the word of 'trans'.
|
65
|
+
def find_by_trans(trans)
|
66
|
+
@wordlist.invert[trans] if trans_exists?(trans)
|
67
|
+
end
|
68
|
+
#Perform a fuzzy search in words, returns an array of results.
|
69
|
+
def fuzzy_find_by_word(word)
|
70
|
+
@wordlist.keys.map do |w|
|
71
|
+
w if w.include?(word)
|
72
|
+
end.compact!
|
73
|
+
end
|
74
|
+
#Perform a fuzzy search in translations, returns an array of results.
|
75
|
+
def fuzzy_find_by_trans(trans)
|
76
|
+
@wordlist.values.map do |w|
|
77
|
+
w if w.include?(trans)
|
78
|
+
end.compact!
|
79
|
+
end
|
80
|
+
#Wordlist count.
|
81
|
+
def wordlist_count
|
82
|
+
@wordlist.length
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def info_data
|
88
|
+
data=get_data("#{@prefix}.ifo",nil)
|
89
|
+
opts=data.scan(/(.*)=(.*)/)
|
90
|
+
Hash[*opts.flatten]
|
91
|
+
end
|
92
|
+
|
93
|
+
def idx_data
|
94
|
+
get_data("#{@prefix}.idx","#{@prefix}.idx.gz")
|
95
|
+
end
|
96
|
+
|
97
|
+
def dict_data
|
98
|
+
StringIO.new(get_data("#{@prefix}.dict","#{@prefix}.dict.dz"))
|
99
|
+
end
|
100
|
+
|
101
|
+
def define_options
|
102
|
+
@info.each{|k,v|eval("def #{k};'#{v}';end")}
|
103
|
+
end
|
104
|
+
|
105
|
+
def parse
|
106
|
+
entries=@idx.scan(/([\d\D]+?\x00[\d\D]{8})/)
|
107
|
+
|
108
|
+
if entries.length!=@info['wordcount'].to_i
|
109
|
+
raise 'word count is not correct'
|
110
|
+
end
|
111
|
+
|
112
|
+
@wordlist={}
|
113
|
+
|
114
|
+
entries.each do |entry|
|
115
|
+
entry=entry.to_s
|
116
|
+
t_pos=entry.index("\x00")+1
|
117
|
+
re=entry.unpack("A#{t_pos}NN")
|
118
|
+
@dict.seek(re[1])
|
119
|
+
word,trans=re[0],@dict.read(re[2])
|
120
|
+
norm(word,trans)
|
121
|
+
#for duplicated key:
|
122
|
+
#append for exiting one
|
123
|
+
#unless exists?(word)
|
124
|
+
unless @wordlist[word].nil?
|
125
|
+
@wordlist[word]+="::#{trans}"
|
126
|
+
else
|
127
|
+
@wordlist[word]=trans
|
128
|
+
end
|
129
|
+
end;@wordlist
|
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
|
+
|
139
|
+
def get_data(file,gz)
|
140
|
+
begin
|
141
|
+
File.open(file,'rb'){|f|f.read}
|
142
|
+
rescue
|
143
|
+
begin
|
144
|
+
GzipReader.open(gz){|f|f.read}
|
145
|
+
rescue
|
146
|
+
raise IOError, "can not open '#{file}' or gz '#{gz}'"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rbstardict
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Abdelrahmn Ghanem
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-21 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Provides a library that can manipulate with stardict dictionaries, it can perform searching operations and/or getting dictionary's data.
|
17
|
+
email: abom.jdev@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/stardict.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage: http://rubyforge.org/projects/rbstardict/
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project: rbstardict
|
50
|
+
rubygems_version: 1.3.5
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: StarDict library for ruby
|
54
|
+
test_files: []
|
55
|
+
|