konjac 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/konjac/cli.rb +12 -0
- data/lib/konjac/dictionary.rb +81 -15
- data/lib/konjac/version.rb +1 -1
- data/spec/dictionary_spec.rb +16 -0
- metadata +19 -19
data/lib/konjac/cli.rb
CHANGED
@@ -9,6 +9,7 @@ module Konjac
|
|
9
9
|
|
10
10
|
class << self
|
11
11
|
SUB_COMMANDS = {
|
12
|
+
"add" => "Add a definition to translation memory",
|
12
13
|
"edit" => "Edit the tags file for a .docx file",
|
13
14
|
"export" => "Export text tags from a .docx file",
|
14
15
|
"import" => "Import text tags into a .docx file",
|
@@ -41,6 +42,17 @@ eos
|
|
41
42
|
cmd = ARGV.shift
|
42
43
|
sc_banner = BANNER % [SUB_COMMANDS[cmd], cmd, ""]
|
43
44
|
cmd_opts = case cmd
|
45
|
+
when "add"
|
46
|
+
opts = Trollop::options do
|
47
|
+
banner sc_banner
|
48
|
+
opt :original, "The original word or phrase", :type => :string
|
49
|
+
opt :translation, "The translated word or phrase", :type => :string, :short => :r
|
50
|
+
opt :from, "The language from which to translate", :type => :string
|
51
|
+
opt :to, "The language into which to translate", :type => :string
|
52
|
+
opt :using, "The names of dictionaries to use", :type => :string,
|
53
|
+
:default => "dict", :multi => true
|
54
|
+
end
|
55
|
+
Dictionary.add_word opts
|
44
56
|
when "edit"
|
45
57
|
Trollop::options do
|
46
58
|
banner sc_banner
|
data/lib/konjac/dictionary.rb
CHANGED
@@ -28,24 +28,11 @@ module Konjac
|
|
28
28
|
cache_load from_lang, to_lang, opts[:using]
|
29
29
|
|
30
30
|
# Make sure dictionary exists and load
|
31
|
-
|
32
|
-
opts[:using].each do |dict|
|
33
|
-
if dict =~ /[\/.]/
|
34
|
-
sub_dictionaries = Dir.glob(File.expand_path(dict))
|
35
|
-
else
|
36
|
-
sub_dictionaries = Dir.glob(File.expand_path("~/.konjac/#{dict}.yml"))
|
37
|
-
end
|
38
|
-
|
39
|
-
sub_dictionaries.each do |sub_dict|
|
40
|
-
verify_dictionary_exists sub_dict
|
41
|
-
temp = ::YAML.load_file(sub_dict)
|
42
|
-
@dictionary += temp if temp.is_a?(Array)
|
43
|
-
end
|
44
|
-
end
|
31
|
+
dictionary = build_dictionary(opts[:using])
|
45
32
|
|
46
33
|
# Build a list of search and replace pairs
|
47
34
|
@pairs = []
|
48
|
-
|
35
|
+
dictionary.each do |term|
|
49
36
|
pair = extract_pair_from_term(term, from_lang, to_lang, from_template, to_template)
|
50
37
|
@pairs << pair unless pair.nil?
|
51
38
|
end
|
@@ -132,6 +119,55 @@ module Konjac
|
|
132
119
|
end
|
133
120
|
end
|
134
121
|
|
122
|
+
# Adds a word to the dictionary
|
123
|
+
def add_word(opts = {})
|
124
|
+
from_lang = Language.find(opts[:from]).to_s
|
125
|
+
to_lang = Language.find(opts[:to]).to_s
|
126
|
+
dictionaries = find_dictionaries(opts[:using])
|
127
|
+
counter = dictionaries.length
|
128
|
+
|
129
|
+
dictionaries.each do |path|
|
130
|
+
dict = ::YAML.load_file(path)
|
131
|
+
found = false
|
132
|
+
dict.each do |term|
|
133
|
+
if term.has_key?(to_lang)
|
134
|
+
if term[to_lang].is_a?(Hash)
|
135
|
+
to_term = term[to_lang][to_lang]
|
136
|
+
from_term = term[to_lang][from_lang]
|
137
|
+
else
|
138
|
+
to_term = term[to_lang]
|
139
|
+
end
|
140
|
+
|
141
|
+
from_term ||= term[from_lang]
|
142
|
+
puts "%s, %s" % [from_term, to_term]
|
143
|
+
puts "%s, %s" % [to_term == opts[:translation], (from_term.is_a?(Regexp) && from_term =~ opts[:original]) || from_term == opts[:original]]
|
144
|
+
|
145
|
+
if to_term == opts[:translation]
|
146
|
+
if from_term.is_a?(Regexp) && from_term =~ opts[:original]
|
147
|
+
found = true
|
148
|
+
break
|
149
|
+
elsif from_term == opts[:original]
|
150
|
+
found = true
|
151
|
+
break
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
if found
|
158
|
+
counter -= 1
|
159
|
+
else
|
160
|
+
dict << { to_lang => opts[:translation], from_lang => opts[:original] }
|
161
|
+
end
|
162
|
+
|
163
|
+
File.open(path, "w") do |file|
|
164
|
+
YAML.dump dict, file
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
counter
|
169
|
+
end
|
170
|
+
|
135
171
|
private
|
136
172
|
|
137
173
|
def parse_language(lang)
|
@@ -179,6 +215,36 @@ module Konjac
|
|
179
215
|
Marshal.dump(pairs, file)
|
180
216
|
end
|
181
217
|
end
|
218
|
+
|
219
|
+
# Builds a list of dictionaries from the supplied files, defaulting to
|
220
|
+
# ~/.konjac/*.yml
|
221
|
+
def build_dictionary(files)
|
222
|
+
dictionary = []
|
223
|
+
find_dictionaries(files).each do |dict|
|
224
|
+
verify_dictionary_exists dict
|
225
|
+
temp = ::YAML.load_file(dict)
|
226
|
+
dictionary += temp if temp.is_a?(Array)
|
227
|
+
end
|
228
|
+
dictionary
|
229
|
+
end
|
230
|
+
|
231
|
+
def find_dictionaries(files)
|
232
|
+
paths = []
|
233
|
+
files.each do |file|
|
234
|
+
if file =~ /[\/.]/
|
235
|
+
full_path = File.expand_path(file)
|
236
|
+
else
|
237
|
+
full_path = File.expand_path("~/.konjac/#{file}.yml")
|
238
|
+
end
|
239
|
+
|
240
|
+
if File.basename(file) =~ /\*/
|
241
|
+
FileUtils.touch full_path unless File.exists?(full_path)
|
242
|
+
end
|
243
|
+
|
244
|
+
paths += Dir.glob(full_path)
|
245
|
+
end
|
246
|
+
paths.uniq
|
247
|
+
end
|
182
248
|
end
|
183
249
|
end
|
184
250
|
end
|
data/lib/konjac/version.rb
CHANGED
data/spec/dictionary_spec.rb
CHANGED
@@ -85,4 +85,20 @@ describe Dictionary do
|
|
85
85
|
Dictionary.pairs[0].should == [/\bmouth\b/i, "boca"]
|
86
86
|
end
|
87
87
|
end
|
88
|
+
|
89
|
+
describe "when trying to add a word" do
|
90
|
+
before :each do
|
91
|
+
@opts = { :from => "en", :to => "ja", :using => [@dictionary.path] }
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should fail if the word already exists" do
|
95
|
+
@opts.merge!({ :original => "dogs", :translation => "犬" })
|
96
|
+
Dictionary.add_word(@opts).should == 0
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should succeed if the word does not already exist" do
|
100
|
+
@opts.merge!({ :original => "tree", :translation => "木" })
|
101
|
+
Dictionary.add_word(@opts).should == 1
|
102
|
+
end
|
103
|
+
end
|
88
104
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: konjac
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-01-15 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
16
|
-
requirement: &
|
16
|
+
requirement: &70300369394260 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70300369394260
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: term-ansicolor
|
27
|
-
requirement: &
|
27
|
+
requirement: &70300369393820 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70300369393820
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: trollop
|
38
|
-
requirement: &
|
38
|
+
requirement: &70300369393400 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70300369393400
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: autotest
|
49
|
-
requirement: &
|
49
|
+
requirement: &70300369392960 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70300369392960
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: autotest-fsevent
|
60
|
-
requirement: &
|
60
|
+
requirement: &70300369392520 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70300369392520
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: autotest-growl
|
71
|
-
requirement: &
|
71
|
+
requirement: &70300369392100 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70300369392100
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: bundler
|
82
|
-
requirement: &
|
82
|
+
requirement: &70300369391680 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70300369391680
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rspec
|
93
|
-
requirement: &
|
93
|
+
requirement: &70300369391260 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70300369391260
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: sdoc
|
104
|
-
requirement: &
|
104
|
+
requirement: &70300369390840 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,7 +109,7 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *70300369390840
|
113
113
|
description: A Ruby command-line utility for translating files using a YAML wordlist
|
114
114
|
email:
|
115
115
|
- bryan.mckelvey@gmail.com
|