smartdict-core 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +77 -0
- data/bin/smartdict +20 -0
- data/bin/smartdict-populator +50 -0
- data/config/default_config.yml +15 -0
- data/lib/smartdict/commands/abstract_command.rb +225 -0
- data/lib/smartdict/commands/has_format_list.rb +28 -0
- data/lib/smartdict/commands/help_command.rb +53 -0
- data/lib/smartdict/commands/list_command.rb +45 -0
- data/lib/smartdict/commands/translate_command.rb +35 -0
- data/lib/smartdict/commands.rb +10 -0
- data/lib/smartdict/core/command_manager.rb +27 -0
- data/lib/smartdict/core/driver_manager.rb +9 -0
- data/lib/smartdict/core/format_manager.rb +8 -0
- data/lib/smartdict/core/has_logger.rb +12 -0
- data/lib/smartdict/core/is_manager.rb +21 -0
- data/lib/smartdict/core/logger.rb +5 -0
- data/lib/smartdict/core/plugin_manager.rb +32 -0
- data/lib/smartdict/core.rb +12 -0
- data/lib/smartdict/drivers/abstract_driver.rb +61 -0
- data/lib/smartdict/drivers/google_translate_driver.rb +56 -0
- data/lib/smartdict/drivers.rb +6 -0
- data/lib/smartdict/errors.rb +11 -0
- data/lib/smartdict/formats/abstract_format.rb +21 -0
- data/lib/smartdict/formats/fb2_format.rb +40 -0
- data/lib/smartdict/formats/text_color_format.rb +52 -0
- data/lib/smartdict/formats/text_format.rb +9 -0
- data/lib/smartdict/formats.rb +8 -0
- data/lib/smartdict/list_builder.rb +51 -0
- data/lib/smartdict/models/driver.rb +14 -0
- data/lib/smartdict/models/language.rb +16 -0
- data/lib/smartdict/models/translated_word.rb +14 -0
- data/lib/smartdict/models/translation.rb +83 -0
- data/lib/smartdict/models/translation_query.rb +13 -0
- data/lib/smartdict/models/word.rb +13 -0
- data/lib/smartdict/models/word_class.rb +14 -0
- data/lib/smartdict/models.rb +11 -0
- data/lib/smartdict/plugin/initializer_context.rb +7 -0
- data/lib/smartdict/plugin.rb +9 -0
- data/lib/smartdict/runner.rb +5 -0
- data/lib/smartdict/storage/seeder.rb +32 -0
- data/lib/smartdict/storage/seeds/drivers.csv +2 -0
- data/lib/smartdict/storage/seeds/languages.csv +188 -0
- data/lib/smartdict/storage/seeds/word_classes.csv +10 -0
- data/lib/smartdict/storage.rb +44 -0
- data/lib/smartdict/translation.rb +19 -0
- data/lib/smartdict/translator.rb +38 -0
- data/lib/smartdict/version.rb +3 -0
- data/lib/smartdict-core.rb +1 -0
- data/lib/smartdict.rb +97 -0
- metadata +468 -0
@@ -0,0 +1,188 @@
|
|
1
|
+
id,code,name
|
2
|
+
1,ru,Russian
|
3
|
+
2,en,English
|
4
|
+
3,de,German
|
5
|
+
4,uk,Ukrainian
|
6
|
+
5,be,Belarusian
|
7
|
+
6,ab,Abkhazian
|
8
|
+
7,aa,Afar
|
9
|
+
8,af,Afrikaans
|
10
|
+
9,ak,Akan
|
11
|
+
10,sq,Albanian
|
12
|
+
11,am,Amharic
|
13
|
+
12,ar,Arabic
|
14
|
+
13,an,Aragonese
|
15
|
+
14,hy,Armenian
|
16
|
+
15,as,Assamese
|
17
|
+
16,av,Avaric
|
18
|
+
17,ae,Avestan
|
19
|
+
18,ay,Aymara
|
20
|
+
19,az,Azerbaijani
|
21
|
+
20,bm,Bambara
|
22
|
+
21,ba,Bashkir
|
23
|
+
22,eu,Basque
|
24
|
+
23,bn,Bengali
|
25
|
+
24,bh,Bihari languages
|
26
|
+
25,bi,Bislama
|
27
|
+
26,bs,Bosnian
|
28
|
+
27,br,Breton
|
29
|
+
28,bg,Bulgarian
|
30
|
+
29,my,Burmese
|
31
|
+
30,ca,Catalan
|
32
|
+
31,km,Central Khmer
|
33
|
+
32,ch,Chamorro
|
34
|
+
33,ce,Chechen
|
35
|
+
34,zh,Chinese
|
36
|
+
35,za,Chuang
|
37
|
+
36,cu,Church
|
38
|
+
37,cv,Chuvash
|
39
|
+
38,kw,Cornish
|
40
|
+
39,co,Corsican
|
41
|
+
40,cr,Cree
|
42
|
+
41,hr,Croatian
|
43
|
+
42,cs,Czech
|
44
|
+
43,da,Danish
|
45
|
+
44,nl,Dutch
|
46
|
+
45,dz,Dzongkha
|
47
|
+
46,eo,Esperanto
|
48
|
+
47,et,Estonian
|
49
|
+
48,ee,Ewe
|
50
|
+
49,fo,Faroese
|
51
|
+
50,fj,Fijian
|
52
|
+
51,fi,Finnish
|
53
|
+
52,fr,French
|
54
|
+
53,ff,Fulah
|
55
|
+
54,gd,Gaelic
|
56
|
+
55,gl,Galician
|
57
|
+
56,lg,Ganda
|
58
|
+
57,ka,Georgian
|
59
|
+
58,el,Greek Modern
|
60
|
+
59,kl,Greenlandic
|
61
|
+
60,gn,Guarani
|
62
|
+
61,gu,Gujarati
|
63
|
+
62,ht,Haitian
|
64
|
+
63,ha,Hausa
|
65
|
+
64,he,Hebrew
|
66
|
+
65,hz,Herero
|
67
|
+
66,hi,Hindi
|
68
|
+
67,ho,Hiri Motu
|
69
|
+
68,hu,Hungarian
|
70
|
+
69,is,Icelandic
|
71
|
+
70,io,Ido
|
72
|
+
71,ig,Igbo
|
73
|
+
72,id,Indonesian
|
74
|
+
73,ia,Interlingua
|
75
|
+
74,ie,Interlingue
|
76
|
+
75,iu,Inuktitut
|
77
|
+
76,ik,Inupiaq
|
78
|
+
77,ga,Irish
|
79
|
+
78,it,Italian
|
80
|
+
79,ja,Japanese
|
81
|
+
80,jv,Javanese
|
82
|
+
81,kn,Kannada
|
83
|
+
82,kr,Kanuri
|
84
|
+
83,ks,Kashmiri
|
85
|
+
84,kk,Kazakh
|
86
|
+
85,ki,Kikuyu
|
87
|
+
86,rw,Kinyarwanda
|
88
|
+
87,ky,Kirghiz
|
89
|
+
88,kv,Komi
|
90
|
+
89,kg,Kongo
|
91
|
+
90,ko,Korean
|
92
|
+
91,kj,Kuanyama
|
93
|
+
92,ku,Kurdish
|
94
|
+
93,lo,Lao
|
95
|
+
94,la,Latin
|
96
|
+
95,lv,Latvian
|
97
|
+
96,li,Limburgish
|
98
|
+
97,ln,Lingala
|
99
|
+
98,lt,Lithuanian
|
100
|
+
99,lu,Luba-Katanga
|
101
|
+
100,lb,Luxembourgish
|
102
|
+
101,mk,Macedonian
|
103
|
+
102,mg,Malagasy
|
104
|
+
103,ms,Malay
|
105
|
+
104,ml,Malayalam
|
106
|
+
105,dv,Maldivian
|
107
|
+
106,mt,Maltese
|
108
|
+
107,gv,Manx
|
109
|
+
108,mi,Maori
|
110
|
+
109,mr,Marathi
|
111
|
+
110,mh,Marshallese
|
112
|
+
111,mn,Mongolian
|
113
|
+
112,na,Nauru
|
114
|
+
113,nv,Navaho
|
115
|
+
114,nv,Navajo
|
116
|
+
115,nd,Ndebele North
|
117
|
+
116,nr,Ndebele South
|
118
|
+
117,ng,Ndonga
|
119
|
+
118,ne,Nepali
|
120
|
+
119,nd,North Ndebele
|
121
|
+
120,se,Northern Sami
|
122
|
+
121,no,Norwegian
|
123
|
+
122,nn,Norwegian Nynorsk
|
124
|
+
123,ii,Nuosu
|
125
|
+
124,ny,Nyanja
|
126
|
+
125,oc,Occitan (post 1500)
|
127
|
+
126,oj,Ojibwa
|
128
|
+
127,or,Oriya
|
129
|
+
128,om,Oromo
|
130
|
+
129,os,Ossetian
|
131
|
+
130,os,Ossetic
|
132
|
+
131,pi,Pali
|
133
|
+
132,pa,Panjabi
|
134
|
+
133,ps,Pashto
|
135
|
+
134,fa,Persian
|
136
|
+
135,pl,Polish
|
137
|
+
136,pt,Portuguese
|
138
|
+
137,pa,Punjabi
|
139
|
+
138,ps,Pushto
|
140
|
+
139,qu,Quechua
|
141
|
+
140,ro,Romanian
|
142
|
+
141,rm,Romansh
|
143
|
+
142,rn,Rundi
|
144
|
+
143,sm,Samoan
|
145
|
+
144,sg,Sango
|
146
|
+
145,sa,Sanskrit
|
147
|
+
146,sc,Sardinian
|
148
|
+
147,sr,Serbian
|
149
|
+
148,sn,Shona
|
150
|
+
149,sd,Sindhi
|
151
|
+
150,si,Sinhala
|
152
|
+
151,sk,Slovak
|
153
|
+
152,sl,Slovenian
|
154
|
+
153,so,Somali
|
155
|
+
154,st,Sotho
|
156
|
+
155,es,Spanish
|
157
|
+
156,su,Sundanese
|
158
|
+
157,sw,Swahili
|
159
|
+
158,ss,Swati
|
160
|
+
159,sv,Swedish
|
161
|
+
160,tl,Tagalog
|
162
|
+
161,ty,Tahitian
|
163
|
+
162,tg,Tajik
|
164
|
+
163,ta,Tamil
|
165
|
+
164,tt,Tatar
|
166
|
+
165,te,Telugu
|
167
|
+
166,th,Thai
|
168
|
+
167,bo,Tibetan
|
169
|
+
168,ti,Tigrinya
|
170
|
+
169,to,Tonga (Tonga Islands)
|
171
|
+
170,ts,Tsonga
|
172
|
+
171,tn,Tswana
|
173
|
+
172,tr,Turkish
|
174
|
+
173,tk,Turkmen
|
175
|
+
174,tw,Twi
|
176
|
+
175,ug,Uighur
|
177
|
+
176,ur,Urdu
|
178
|
+
177,uz,Uzbek
|
179
|
+
178,ve,Venda
|
180
|
+
179,vi,Vietnamese
|
181
|
+
180,wa,Walloon
|
182
|
+
181,cy,Welsh
|
183
|
+
182,fy,Western Frisian
|
184
|
+
183,wo,Wolof
|
185
|
+
184,xh,Xhosa
|
186
|
+
185,yi,Yiddish
|
187
|
+
186,yo,Yoruba
|
188
|
+
187,zu,Zul
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Smartdict::Storage
|
2
|
+
extend self
|
3
|
+
extend ActiveSupport::Autoload
|
4
|
+
|
5
|
+
autoload :Seeder
|
6
|
+
|
7
|
+
include Smartdict::Models
|
8
|
+
|
9
|
+
def prepare!
|
10
|
+
#DataMapper::Logger.new(STDOUT, :debug)
|
11
|
+
setup_sqlite
|
12
|
+
update_enums_cache
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def setup_sqlite
|
19
|
+
DataMapper.setup(:default, storage_line)
|
20
|
+
DataMapper.finalize
|
21
|
+
if migrate_and_seed?
|
22
|
+
DataMapper.auto_migrate!
|
23
|
+
Seeder.seed!
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def update_enums_cache
|
28
|
+
[Language, Driver, WordClass].each do |model_class|
|
29
|
+
model_class.update_enums_cache!
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def storage_line
|
34
|
+
(Smartdict.env == :test) ? "sqlite::memory:" : "sqlite://#{db_file}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def migrate_and_seed?
|
38
|
+
storage_line =~ /memory/ or !File.exists?(db_file)
|
39
|
+
end
|
40
|
+
|
41
|
+
def db_file
|
42
|
+
"#{Smartdict.user_dir}/database.sqlite"
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Smartdict::Translation
|
2
|
+
attr_accessor :word,
|
3
|
+
:transcription,
|
4
|
+
:from_lang,
|
5
|
+
:to_lang,
|
6
|
+
:driver,
|
7
|
+
:translated
|
8
|
+
|
9
|
+
def initialize(attrs)
|
10
|
+
@translated = {}
|
11
|
+
attrs.each do |attr, value|
|
12
|
+
self.send("#{attr}=", value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def [](word_class)
|
17
|
+
@translated[word_class.to_s] ||= []
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Smartdict
|
2
|
+
class Translator
|
3
|
+
|
4
|
+
class_attribute :driver_name
|
5
|
+
self.driver_name = :google_translate
|
6
|
+
|
7
|
+
class_attribute :from_lang_code
|
8
|
+
self.from_lang_code = :en
|
9
|
+
|
10
|
+
class_attribute :to_lang_code
|
11
|
+
self.to_lang_code = :ru
|
12
|
+
|
13
|
+
class_attribute :log_queries
|
14
|
+
self.log_queries = true
|
15
|
+
|
16
|
+
|
17
|
+
def self.translate(word)
|
18
|
+
unless translation_model = Models::Translation.find(word, from_lang_code, to_lang_code, driver_name)
|
19
|
+
translation = driver.translate(word, from_lang_code, to_lang_code)
|
20
|
+
translation_model = Models::Translation.create_from_struct(translation)
|
21
|
+
end
|
22
|
+
log_query(translation_model) if log_queries
|
23
|
+
translation_model.to_struct
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.log_query(translation)
|
27
|
+
Models::TranslationQuery.create(:translation => translation)
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def self.driver
|
34
|
+
Smartdict::Core::DriverManager.find(driver_name)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path("../smartdict", __FILE__)
|
data/lib/smartdict.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
require 'dm-core'
|
4
|
+
require 'dm-validations'
|
5
|
+
require 'dm-enum'
|
6
|
+
require 'dm-migrations'
|
7
|
+
require 'dm-sqlite-adapter'
|
8
|
+
|
9
|
+
require 'active_support/core_ext/class'
|
10
|
+
require 'active_support/dependencies/autoload'
|
11
|
+
require 'active_support/core_ext/object'
|
12
|
+
require 'active_support/inflector'
|
13
|
+
|
14
|
+
require 'configatron'
|
15
|
+
require 'net/http'
|
16
|
+
|
17
|
+
require 'smartdict/errors'
|
18
|
+
require 'smartdict/models'
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
module Smartdict
|
23
|
+
extend self
|
24
|
+
extend ActiveSupport::Autoload
|
25
|
+
|
26
|
+
autoload :VERSION, 'smartdict/version'
|
27
|
+
autoload :Core
|
28
|
+
autoload :Storage
|
29
|
+
autoload :Models
|
30
|
+
autoload :Plugin
|
31
|
+
autoload :Commands
|
32
|
+
autoload :Drivers
|
33
|
+
autoload :Translator
|
34
|
+
autoload :Formats
|
35
|
+
autoload :Translation
|
36
|
+
autoload :ListBuilder
|
37
|
+
|
38
|
+
include Smartdict::Core
|
39
|
+
|
40
|
+
ENVIRONMENTS = [:user, :test, :cucumber]
|
41
|
+
|
42
|
+
def load_plugins
|
43
|
+
Core::PluginManager.load_plugins
|
44
|
+
end
|
45
|
+
|
46
|
+
def run
|
47
|
+
Dir.mkdir user_dir unless File.exists?(user_dir)
|
48
|
+
init_config
|
49
|
+
Storage.prepare!
|
50
|
+
end
|
51
|
+
|
52
|
+
def init_config
|
53
|
+
default_config_file = File.join(root_dir, 'config', 'default_config.yml')
|
54
|
+
configatron.configure_from_hash YAML.load_file(default_config_file)
|
55
|
+
|
56
|
+
config_file = File.join(user_dir, 'configuration.yml')
|
57
|
+
if File.exists?(config_file)
|
58
|
+
configatron.configure_from_hash YAML.load_file(config_file)
|
59
|
+
else
|
60
|
+
FileUtils.cp default_config_file, config_file
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
def user_dir
|
66
|
+
dirname = {
|
67
|
+
:user => '.smartdict',
|
68
|
+
:test => '.smartdict_test',
|
69
|
+
:cucumber => '.smartdict_test' }[env]
|
70
|
+
File.join(home_dir, dirname)
|
71
|
+
end
|
72
|
+
|
73
|
+
def home_dir
|
74
|
+
ENV['HOME']
|
75
|
+
end
|
76
|
+
|
77
|
+
def env=(environment)
|
78
|
+
@env = environment.to_sym
|
79
|
+
raise "env must be one of #{ENVIRONMENTS.inspect}" unless ENVIRONMENTS.include?(@env)
|
80
|
+
end
|
81
|
+
|
82
|
+
def env
|
83
|
+
@env || raise("No env setted for Smartdict")
|
84
|
+
end
|
85
|
+
|
86
|
+
def log_path
|
87
|
+
File.join(user_dir, 'smartdict.log')
|
88
|
+
end
|
89
|
+
|
90
|
+
def root_dir
|
91
|
+
File.join(File.dirname(__FILE__), '..')
|
92
|
+
end
|
93
|
+
|
94
|
+
def plugins_dir
|
95
|
+
ENV['SMARTDICT_PLUGINS_DIR'] or File.join(root_dir, 'plugins')
|
96
|
+
end
|
97
|
+
end
|