konjac 0.1.9.2 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/konjac.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
+ s.add_runtime_dependency "amatch"
22
23
  s.add_runtime_dependency "i18n"
23
24
  s.add_runtime_dependency "nokogiri"
24
25
  s.add_runtime_dependency "sdoc"
data/lib/konjac/cli.rb CHANGED
@@ -12,7 +12,8 @@ module Konjac
12
12
 
13
13
  class << self
14
14
  # A list of valid subcommands
15
- SUB_COMMANDS = ["add", "edit", "export", "import", "language", "translate"]
15
+ SUB_COMMANDS = ["add", "edit", "export", "import", "language", "suggest",
16
+ "translate"]
16
17
 
17
18
  # The banner to displaying when requesting help through the command line
18
19
  BANNER = <<-eos
@@ -21,7 +22,7 @@ module Konjac
21
22
  #{Color.bold "%s"}
22
23
 
23
24
  #{Color.bold I18n.t :usage}
24
- konjac %s [#{Color.underscore I18n.t :options}] <#{I18n.t :filenames}>+%s
25
+ konjac %s [#{Color.underscore I18n.t :options}] %s
25
26
  #{I18n.t(:where_options) % Color.underscore(I18n.t(:options))}
26
27
  eos
27
28
 
@@ -34,6 +35,7 @@ eos
34
35
  banner BANNER % [
35
36
  I18n.t(:banner),
36
37
  "[#{Color.underscore I18n.t :subcommand}]",
38
+ I18n.t(:filenames_or_word_arg),
37
39
  "\n\n" + (I18n.t(:where_subcommand) % Color.underscore(I18n.t(:subcommand))) + ("\n%s\n" %
38
40
  Konjac::CLI.describe_subcommands)
39
41
  ]
@@ -47,11 +49,11 @@ eos
47
49
  # Get subcommand
48
50
  cmd = ARGV.shift
49
51
  ARGV << "-h" if ARGV.empty?
50
- sc_banner = BANNER % [I18n.t(cmd, :scope => :subcommands), cmd, "\n"]
52
+ sc_banner = BANNER % [I18n.t(cmd, :scope => :subcommands), cmd, "%s", "\n"]
51
53
  cmd_opts = case cmd
52
54
  when "add"
53
55
  opts = Trollop::options do
54
- banner sc_banner
56
+ banner sc_banner % I18n.t(:filenames_arg)
55
57
  opt :original, I18n.t(:original, :scope => :opts), :type => :string
56
58
  opt :translation, I18n.t(:translation, :scope => :opts), :type => :string, :short => :r
57
59
  opt :from, I18n.t(:from, :scope => :opts), :type => :string
@@ -63,14 +65,14 @@ eos
63
65
  Dictionary.add_word opts
64
66
  when "edit"
65
67
  Trollop::options do
66
- banner sc_banner
68
+ banner sc_banner % I18n.t(:filenames_arg)
67
69
  opt :editor, I18n.t(:editor, :scope => :opts), :type => :string
68
70
  opt :help, I18n.t(:help, :scope => :opts)
69
71
  end
70
72
  Word.edit_docx_tags ARGV, opts
71
73
  when "export"
72
74
  opts = Trollop::options do
73
- banner sc_banner
75
+ banner sc_banner % I18n.t(:filenames_arg)
74
76
  opt :from, I18n.t(:from, :scope => :opts), :type => :string
75
77
  opt :to, I18n.t(:to, :scope => :opts), :type => :string
76
78
  opt :using, I18n.t(:using, :scope => :opts), :type => :string,
@@ -80,20 +82,35 @@ eos
80
82
  Word.export_docx_tags ARGV, opts
81
83
  when "import"
82
84
  Trollop::options do
83
- banner sc_banner
85
+ banner sc_banner % I18n.t(:filenames_arg)
84
86
  opt :help, I18n.t(:help, :scope => :opts)
85
87
  end
86
88
  Word.import_docx_tags ARGV
87
89
  when "language"
88
90
  Trollop::options do
89
- banner sc_banner
91
+ banner sc_banner % I18n.t(:word_arg)
90
92
  opt :help, I18n.t(:help, :scope => :opts)
91
93
  end
92
94
  Config.language = ARGV[0]
93
95
  Config.save
96
+ when "suggest"
97
+ opts = Trollop::options do
98
+ banner sc_banner % I18n.t(:word_arg)
99
+ opt :from, I18n.t(:from, :scope => :opts), :type => :string
100
+ opt :to, I18n.t(:to, :scope => :opts), :type => :string
101
+ opt :using, I18n.t(:using, :scope => :opts), :type => :string,
102
+ :default => "dict", :multi => true
103
+ opt :use_cache, I18n.t(:use_cache, :scope => :opts),
104
+ :default => false, :short => :c
105
+ opt :help, I18n.t(:help, :scope => :opts)
106
+ end
107
+ results = suggest(ARGV[0].dup, opts)
108
+ results.each_with_index do |result, index|
109
+ puts "%i: %s (%i%%)" % [index + 1, result[2], result[0] * 100]
110
+ end
94
111
  when "translate"
95
112
  opts = Trollop::options do
96
- banner sc_banner
113
+ banner sc_banner % I18n.t(:filenames_or_word_arg)
97
114
  opt :from, I18n.t(:from, :scope => :opts), :type => :string
98
115
  opt :to, I18n.t(:to, :scope => :opts), :type => :string
99
116
  opt :using, I18n.t(:using, :scope => :opts), :type => :string,
@@ -126,6 +143,12 @@ eos
126
143
 
127
144
  private
128
145
 
146
+ def suggest(word, opts = {})
147
+ to_lang = Language.find(opts[:to]).to_s
148
+ from_lang = Language.find(opts[:from]).to_s
149
+ Suggestor.new(from_lang, to_lang, opts).suggest word
150
+ end
151
+
129
152
  # Parse commands to determine what should be translated and how
130
153
  def translate(files, opts = {}) # :doc:
131
154
  to_lang = Language.find(opts[:to]).to_s
@@ -84,6 +84,7 @@ module Konjac
84
84
  # create a regular expression in the dictionary file.
85
85
  # Matching is case-insensitive unless the expression contains a
86
86
  # capital letter.
87
+ from_fuzzy = from_term
87
88
  unless from_term.is_a?(Regexp)
88
89
  from_term = Regexp.new(from_template % from_term,
89
90
  ("i" unless from_term =~ /[A-Z]/))
@@ -91,7 +92,7 @@ module Konjac
91
92
 
92
93
  to_term = to_template % to_term unless to_term =~ BLANK || no_space
93
94
 
94
- return [ from_term, to_term ]
95
+ return [ from_term, to_term, from_fuzzy ]
95
96
  end
96
97
  end
97
98
 
@@ -0,0 +1,45 @@
1
+ module Konjac
2
+ # A class for suggesting translations
3
+ class Suggestor
4
+ # A list of all pairs available
5
+ attr_accessor :pairs
6
+
7
+ # The default suggested count
8
+ SUGGEST_COUNT = 10
9
+
10
+ # The default suggest cutoff
11
+ SUGGEST_CUTOFF = 0.5
12
+
13
+ # Creates a new Suggestor object
14
+ def initialize(from_lang, to_lang, opts = {})
15
+ @pairs = Dictionary.load(from_lang, to_lang, opts)
16
+ end
17
+
18
+ # Provides suggested translations for a word, providing an array containing
19
+ # the pair distance, fuzzy match used and the suggested replacement
20
+ def suggest(word, opts = {})
21
+ # Set defaults for optional arguments
22
+ opts = {
23
+ :count => SUGGEST_COUNT,
24
+ :cutoff => SUGGEST_CUTOFF
25
+ }.merge opts
26
+
27
+ results = []
28
+ matcher = Amatch::PairDistance.new(word)
29
+ @pairs.each do |search, replace, fuzzy|
30
+ if fuzzy.is_a?(String)
31
+ value = matcher.match(fuzzy)
32
+ else
33
+ value = (search =~ word ? 1 : 0)
34
+ end
35
+
36
+ if (results.empty? || value > results[0][0]) && value >= opts[:cutoff]
37
+ results << [value, fuzzy, replace]
38
+ results.sort!
39
+ results.shift if results.length > opts[:count]
40
+ end
41
+ end
42
+ results.reverse
43
+ end
44
+ end
45
+ end
@@ -1,4 +1,4 @@
1
1
  module Konjac
2
2
  # The current version number of Konjac
3
- VERSION = "0.1.9.2"
3
+ VERSION = "0.2"
4
4
  end
data/lib/konjac.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "konjac/version"
2
2
  require "konjac/exception"
3
+ autoload :Amatch, "amatch"
3
4
  autoload :FileUtils, "fileutils"
4
5
  autoload :I18n, "i18n"
5
6
  autoload :Nokogiri, "nokogiri"
@@ -12,6 +13,7 @@ module Konjac
12
13
  autoload :Config, "konjac/config"
13
14
  autoload :Dictionary, "konjac/dictionary"
14
15
  autoload :Language, "konjac/language"
16
+ autoload :Suggestor, "konjac/suggestor"
15
17
  autoload :Tag, "konjac/tag"
16
18
  autoload :TagManager, "konjac/tag_manager"
17
19
  autoload :Translator, "konjac/translator"
data/locales/en.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  en:
2
2
  banner: Konjac is a Ruby command-line utility for translating files using a YAML wordlist
3
- filenames: filenames
3
+ filenames_arg: <filenames>+
4
+ filenames_or_word_arg: <filenames>+ OR <text>
4
5
  me: Bryan McKelvey
5
6
  options: options
6
7
  subcommand: subcommand
@@ -8,6 +9,7 @@ en:
8
9
  usage: "Usage:"
9
10
  where_options: "where [%s] are:"
10
11
  where_subcommand: "where [%s] is any one of:"
12
+ word_arg: <text>
11
13
  opts:
12
14
  dry_run: Don't actually do anything
13
15
  editor: A command to run to pass the file to an editor (e.g. vi %s)
@@ -27,4 +29,5 @@ en:
27
29
  export: Export text tags from a .docx file
28
30
  import: Import text tags into a .docx file
29
31
  language: Sets the default language
32
+ suggest: Suggest similar words using fuzzy matching
30
33
  translate: Translate a file, word or phrase
data/locales/ja.yml CHANGED
@@ -1,24 +1,26 @@
1
1
  ja:
2
2
  banner: KonjacはYAML辞書でファイルを翻訳するRubyのコマンドライン・ユーティリティです。
3
- filenames: ファイル名
3
+ filenames_arg: <ファイル名>+
4
+ filenames_or_word_arg: <ファイル名>+ または <テキスト>
4
5
  me: ブライアン・マッケルビー
5
6
  options: オプション
6
7
  subcommand: サブコマンド
7
8
  unknown_subcommand: 不明なサブコマンド:%s
8
9
  usage: 使い方:
9
- where_options: "使用できる[%s]"
10
- where_subcommand: "使用できる[%s]"
10
+ where_options: "使用可能な[%s]は、以下のとおりです:"
11
+ where_subcommand: "使用可能な[%s]は、以下のとおりです:"
12
+ word_arg: <テキスト>
11
13
  opts:
12
- dry_run: 何もしない
14
+ dry_run: 実際には何も出力しない
13
15
  editor: 結果ファイルをエディターで開くコマンド(例:vi %s)
14
- from: 原文の言語
16
+ from: ソース言語
15
17
  help: このメッセージを表示する
16
- original: 翻訳されなかった元の言語の言葉
18
+ original: ソーステキスト
17
19
  quiet: エラーメッセージを抑止する
18
- to: 訳文の言語
19
- translation: 翻訳された言葉
20
+ to: ターゲット言語
21
+ translation: ターゲットテキスト
20
22
  using: 使用したい辞書の名前
21
- use_cache: キャッシュした辞書を使用する
23
+ use_cache: キャッシュした翻訳メモリを適用する
22
24
  version: バーションを表示し終了する
23
25
  word: 文字列を翻訳する
24
26
  subcommands:
@@ -27,4 +29,5 @@ ja:
27
29
  export: .docxファイルからタグを抽出する
28
30
  import: .docxファイルにタグをインポートする
29
31
  language: 言語を設定する
32
+ suggest: Suggest similar words
30
33
  translate: ファイル・文字列を翻訳する
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.9.2
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,9 +11,20 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: amatch
16
+ requirement: &70272603688640 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70272603688640
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: i18n
16
- requirement: &70168651073560 !ruby/object:Gem::Requirement
27
+ requirement: &70272603688020 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ! '>='
@@ -21,10 +32,10 @@ dependencies:
21
32
  version: '0'
22
33
  type: :runtime
23
34
  prerelease: false
24
- version_requirements: *70168651073560
35
+ version_requirements: *70272603688020
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: nokogiri
27
- requirement: &70168651073120 !ruby/object:Gem::Requirement
38
+ requirement: &70272603708460 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: '0'
33
44
  type: :runtime
34
45
  prerelease: false
35
- version_requirements: *70168651073120
46
+ version_requirements: *70272603708460
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: sdoc
38
- requirement: &70168651072660 !ruby/object:Gem::Requirement
49
+ requirement: &70272603708020 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: '0'
44
55
  type: :runtime
45
56
  prerelease: false
46
- version_requirements: *70168651072660
57
+ version_requirements: *70272603708020
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: term-ansicolor
49
- requirement: &70168651072140 !ruby/object:Gem::Requirement
60
+ requirement: &70272603707580 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: '0'
55
66
  type: :runtime
56
67
  prerelease: false
57
- version_requirements: *70168651072140
68
+ version_requirements: *70272603707580
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: trollop
60
- requirement: &70168651071580 !ruby/object:Gem::Requirement
71
+ requirement: &70272603707160 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: '0'
66
77
  type: :runtime
67
78
  prerelease: false
68
- version_requirements: *70168651071580
79
+ version_requirements: *70272603707160
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: autotest
71
- requirement: &70168651070920 !ruby/object:Gem::Requirement
82
+ requirement: &70272603706740 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ! '>='
@@ -76,10 +87,10 @@ dependencies:
76
87
  version: '0'
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *70168651070920
90
+ version_requirements: *70272603706740
80
91
  - !ruby/object:Gem::Dependency
81
92
  name: autotest-fsevent
82
- requirement: &70168651070260 !ruby/object:Gem::Requirement
93
+ requirement: &70272603706300 !ruby/object:Gem::Requirement
83
94
  none: false
84
95
  requirements:
85
96
  - - ! '>='
@@ -87,10 +98,10 @@ dependencies:
87
98
  version: '0'
88
99
  type: :development
89
100
  prerelease: false
90
- version_requirements: *70168651070260
101
+ version_requirements: *70272603706300
91
102
  - !ruby/object:Gem::Dependency
92
103
  name: autotest-growl
93
- requirement: &70168651096260 !ruby/object:Gem::Requirement
104
+ requirement: &70272603705820 !ruby/object:Gem::Requirement
94
105
  none: false
95
106
  requirements:
96
107
  - - ! '>='
@@ -98,10 +109,10 @@ dependencies:
98
109
  version: '0'
99
110
  type: :development
100
111
  prerelease: false
101
- version_requirements: *70168651096260
112
+ version_requirements: *70272603705820
102
113
  - !ruby/object:Gem::Dependency
103
114
  name: bundler
104
- requirement: &70168651095640 !ruby/object:Gem::Requirement
115
+ requirement: &70272603705300 !ruby/object:Gem::Requirement
105
116
  none: false
106
117
  requirements:
107
118
  - - ! '>='
@@ -109,10 +120,10 @@ dependencies:
109
120
  version: '0'
110
121
  type: :development
111
122
  prerelease: false
112
- version_requirements: *70168651095640
123
+ version_requirements: *70272603705300
113
124
  - !ruby/object:Gem::Dependency
114
125
  name: rspec
115
- requirement: &70168651095000 !ruby/object:Gem::Requirement
126
+ requirement: &70272603704760 !ruby/object:Gem::Requirement
116
127
  none: false
117
128
  requirements:
118
129
  - - ! '>='
@@ -120,7 +131,7 @@ dependencies:
120
131
  version: '0'
121
132
  type: :development
122
133
  prerelease: false
123
- version_requirements: *70168651095000
134
+ version_requirements: *70272603704760
124
135
  description: A Ruby command-line utility for translating files using a YAML wordlist
125
136
  email:
126
137
  - bryan.mckelvey@gmail.com
@@ -145,6 +156,7 @@ files:
145
156
  - lib/konjac/dictionary.rb
146
157
  - lib/konjac/exception.rb
147
158
  - lib/konjac/language.rb
159
+ - lib/konjac/suggestor.rb
148
160
  - lib/konjac/tag.rb
149
161
  - lib/konjac/tag_manager.rb
150
162
  - lib/konjac/translator.rb