wookie-translator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rbenv-gemsets +1 -0
- data/.rbenv-version +1 -0
- data/.rspec +2 -0
- data/.yardopts +1 -0
- data/Gemfile +4 -0
- data/README.md +70 -0
- data/Rakefile +7 -0
- data/bin/wookie +11 -0
- data/lib/wookie.rb +10 -0
- data/lib/wookie/cli.rb +89 -0
- data/lib/wookie/dialect.rb +45 -0
- data/lib/wookie/dialects/simple.rb +60 -0
- data/lib/wookie/translator.rb +30 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/wookie/identity_dialect_spec.rb +23 -0
- data/spec/wookie/simple_dialect_spec.rb +22 -0
- data/spec/wookie/translator_spec.rb +31 -0
- data/wookie-translator.gemspec +19 -0
- metadata +88 -0
data/.gitignore
ADDED
data/.rbenv-gemsets
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
wookie-translator
|
data/.rbenv-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3-p125
|
data/.rspec
ADDED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
lib/**/*.rb --title WookieTranslator --private --protected
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# WookieTranslator
|
2
|
+
|
3
|
+
*2012/04/01*
|
4
|
+
|
5
|
+
This gem is based on http://selarips.free.fr/wookieetranslator/ and built after
|
6
|
+
a tweet from [@MikeSwole](https://twitter.com/#!/MikeSwole/status/185882254432677888).
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'wookie-translator'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install wookie-translator
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
The gem comes with a command line interface:
|
25
|
+
|
26
|
+
$ wookie -e "enter your text here!"
|
27
|
+
$ wookie -w "wowhaoworc rooohurc aowokao acworcwo!"
|
28
|
+
|
29
|
+
You may also use the gem's API:
|
30
|
+
|
31
|
+
require 'wookie-translator'
|
32
|
+
t = Wookie::Translator.new
|
33
|
+
wookie = t.to_wookie "Hello World."
|
34
|
+
english = t.from_wookie wookie #=> "hello world."
|
35
|
+
|
36
|
+
If you want to use another wookie dialect, you want to inherit from
|
37
|
+
`Wookie::Dialect::Base` and implement the methods `#to_wookie` and `#from_wookie`:
|
38
|
+
|
39
|
+
require 'wookie-translator'
|
40
|
+
class ReverseWookie < Wookie::Dialect::Base
|
41
|
+
def to_wookie(english)
|
42
|
+
english.reverse
|
43
|
+
end
|
44
|
+
|
45
|
+
def from_wookie(wookie)
|
46
|
+
wookie.reverse
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
t = Wookie::Translator.new(dialect: ReverseWookie)
|
51
|
+
wookie = t.to_wookie "Hello World." #=> ".dlroW olleH"
|
52
|
+
english = t.from_wookie wookie #=> "Hello World."
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
The wookie translator lacks of translations for the three main dialects:
|
57
|
+
Shyriiwook, Xaczik and Thykarann. If you are a native speaker (or at least
|
58
|
+
familiar with one of those dialects), you're encouraged to follow these steps:
|
59
|
+
|
60
|
+
1. Fork it
|
61
|
+
2. Create your dialect branch (`git checkout -b dialect-X`)
|
62
|
+
3. Build a new dialect class
|
63
|
+
4. Commit your changes (`git commit -am 'Added missing dialect'`)
|
64
|
+
5. Push to the branch (`git push origin dialect-X`)
|
65
|
+
6. Create new Pull Request
|
66
|
+
|
67
|
+
## License
|
68
|
+
|
69
|
+
This code is public domain. Remember that »Wookie« and »Star Wars« are courtesy
|
70
|
+
of [LucasArts](http://www.lucasarts.com/).
|
data/Rakefile
ADDED
data/bin/wookie
ADDED
data/lib/wookie.rb
ADDED
data/lib/wookie/cli.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
# Command line interface for this gem.
|
4
|
+
#
|
5
|
+
# == Usage
|
6
|
+
#
|
7
|
+
# wookie [options] string
|
8
|
+
# wookie [options] -
|
9
|
+
#
|
10
|
+
# == Common Options
|
11
|
+
#
|
12
|
+
# +-w+ or +--from-wookie+::
|
13
|
+
# Translate from wookiespeak to english.
|
14
|
+
#
|
15
|
+
# +-e+ or +--to-wookie+::
|
16
|
+
# Translate from english to wookiespeak.
|
17
|
+
#
|
18
|
+
# +-dDIALECT+ or +--dialect DIALECT+::
|
19
|
+
# Use a specific dialect (default and currently only available option: simple).
|
20
|
+
#
|
21
|
+
# +-v+ or +--version+::
|
22
|
+
# Prints the version and exits.
|
23
|
+
#
|
24
|
+
# == Read from STDIN
|
25
|
+
#
|
26
|
+
# When the option +-+ is given, any non-option argument is ignored and reads from STDIN.
|
27
|
+
#
|
28
|
+
# == Examples
|
29
|
+
#
|
30
|
+
# $ wookie -e 'Hello World!'
|
31
|
+
# acwoananoo ohoorcanwa!
|
32
|
+
# $ wookie -w 'acwoananoo ohoorcanwa!' --dialect simple
|
33
|
+
# hello world!
|
34
|
+
# $ echo 'rchurhro ahc raohwocooscwo :-)' | wookie -w -
|
35
|
+
# ruby is awesome :-)
|
36
|
+
class Wookie::CLI
|
37
|
+
|
38
|
+
# Initializes the command line interface with the command line parameters (or
|
39
|
+
# any given argument array). See above.
|
40
|
+
#
|
41
|
+
# @param [Array] argv The command line arguments (`ARGV` is substituted, if `nil`).
|
42
|
+
def initialize(argv)
|
43
|
+
@argv = argv || ARGV
|
44
|
+
@options = {
|
45
|
+
use_stdin: false,
|
46
|
+
dialect: :simple
|
47
|
+
}
|
48
|
+
OptionParser.new do |opts|
|
49
|
+
opts.banner = "Usage: wookie [options] string\n wookie [options] -"
|
50
|
+
opts.separator ""
|
51
|
+
opts.separator "Common Options"
|
52
|
+
opts.on('-w', '--from-wookie', 'Translate from wookiespeak to english') do
|
53
|
+
@options[:translation] = :from_wookie
|
54
|
+
end
|
55
|
+
opts.on('-e', '--to-wookie', 'Translate from english to wookiespeak') do
|
56
|
+
@options[:translation] = :to_wookie
|
57
|
+
end
|
58
|
+
opts.on('-d', '--dialect DIALECT', [:simple], 'Use a specific dialect (defaults to simple)', 'Currently available: simple') do |dialect|
|
59
|
+
@options[:dialect] = dialect
|
60
|
+
end
|
61
|
+
opts.on('-v', '--version', 'Prints the version and exits') do
|
62
|
+
puts "Wookie Translator v#{Wookie::VERSION}.\nThis software is public domain."
|
63
|
+
exit 0
|
64
|
+
end
|
65
|
+
opts.separator ""
|
66
|
+
opts.separator "Read from STDIN"
|
67
|
+
opts.separator ' - When given, ignore any non-option arguments'
|
68
|
+
opts.separator ' and read from STDIN.'
|
69
|
+
end.parse!(@argv)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Actually executes the arguments and prints a translation.
|
73
|
+
# @return [void]
|
74
|
+
def run
|
75
|
+
dialect = case @options[:dialect]
|
76
|
+
when :simple
|
77
|
+
@options[:dialect] = Wookie::Dialect::Simple
|
78
|
+
else
|
79
|
+
raise ArgumentError, "Unknown dialact."
|
80
|
+
end
|
81
|
+
string = if @argv.include?('-')
|
82
|
+
ARGF.read
|
83
|
+
else
|
84
|
+
@argv.join(' ')
|
85
|
+
end
|
86
|
+
t = Wookie::Translator.new(dialect: dialect)
|
87
|
+
puts t.send(@options[:translation], string)
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module Wookie::Dialect
|
4
|
+
|
5
|
+
# Basic class to be inherited from, when building new dialects.
|
6
|
+
# Though currently not implemented, a future version of the {Wookie::Translator}
|
7
|
+
# may check, if any given dialect is actually a subclass of {Wookie::Dialect::Base}.
|
8
|
+
class Base
|
9
|
+
|
10
|
+
# Keeps track of available subclasses.
|
11
|
+
# @param [Class] sub The subclass.
|
12
|
+
def self.inherited(sub)
|
13
|
+
@@subclasses ||= []
|
14
|
+
@@subclasses << sub
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns the known subclasses
|
18
|
+
# @return [Array] A list of known subclasses.
|
19
|
+
def self.subclasses
|
20
|
+
@@subclasses
|
21
|
+
end
|
22
|
+
|
23
|
+
# Translates an english string to wookiespeack.
|
24
|
+
#
|
25
|
+
# @note Must be implemented in the sub class.
|
26
|
+
# @param [String] The string to be translated.
|
27
|
+
# @return [String] The translation.
|
28
|
+
# @raise NotImplementedError
|
29
|
+
def to_wookie(str)
|
30
|
+
raise NotImplementedError
|
31
|
+
end
|
32
|
+
|
33
|
+
# Translates a wookiespeak string to english.
|
34
|
+
#
|
35
|
+
# @note Must be implemented in the sub class.
|
36
|
+
# @param [String] The string to be translated.
|
37
|
+
# @return [String] The translation.
|
38
|
+
# @raise NotImplementedError
|
39
|
+
def from_wookie(str)
|
40
|
+
raise NotImplementedError
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
autoload :Simple, 'wookie/dialects/simple'
|
45
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module Wookie::Dialect
|
4
|
+
|
5
|
+
# A simple wookie dialect found on the net. Works case insensitive (i.e. any
|
6
|
+
# string to be translated will be converted to downcase).
|
7
|
+
#
|
8
|
+
# @see http://selarips.free.fr/wookieetranslator/
|
9
|
+
class Simple < Base
|
10
|
+
# The basic lookup table.
|
11
|
+
LOOKUP = {
|
12
|
+
'a' => 'ra', 'b' => 'rh', 'c' => 'oa', 'd' => 'wa', 'e' => 'wo', 'f' => 'ww',
|
13
|
+
'g' => 'rr', 'h' => 'ac', 'i' => 'ah', 'j' => 'sh', 'k' => 'or', 'l' => 'an',
|
14
|
+
'm' => 'sc', 'n' => 'wh', 'o' => 'oo', 'p' => 'ak', 'q' => 'rq', 'r' => 'rc',
|
15
|
+
's' => 'c', 't' => 'ao', 'u' => 'hu', 'v' => 'ho', 'w' => 'oh', 'x' => 'k',
|
16
|
+
'y' => 'ro', 'z' => 'uf'
|
17
|
+
}.freeze
|
18
|
+
|
19
|
+
# A short code is defined by a length of 1 of a lookup value.
|
20
|
+
SHORT_CODES = LOOKUP.map{|_,v| v if v.length == 1 }.compact.join
|
21
|
+
|
22
|
+
# Translates an english string to wookiespeack.
|
23
|
+
#
|
24
|
+
# @param [String] The string to be translated.
|
25
|
+
# @return [String] The translation.
|
26
|
+
def to_wookie(str)
|
27
|
+
wookie = ''
|
28
|
+
str.downcase.each_char do |c|
|
29
|
+
wookie << (LOOKUP[c].nil? ? c : LOOKUP[c])
|
30
|
+
end
|
31
|
+
wookie
|
32
|
+
end
|
33
|
+
|
34
|
+
# Translates a wookiespeak string to english.
|
35
|
+
#
|
36
|
+
# @param [String] The string to be translated.
|
37
|
+
# @return [String] The translation.
|
38
|
+
def from_wookie(str)
|
39
|
+
chars = str.downcase.split('')
|
40
|
+
lookup = Hash[ LOOKUP.to_a.map(&:reverse) ]
|
41
|
+
english = ''
|
42
|
+
offset = 0
|
43
|
+
while offset < chars.length
|
44
|
+
cur = chars[offset]
|
45
|
+
nxt = chars[offset,2].join
|
46
|
+
if lookup.keys.include?(cur)
|
47
|
+
english << lookup[cur]
|
48
|
+
elsif lookup.keys.include?(nxt)
|
49
|
+
english << lookup[nxt]
|
50
|
+
offset += 1
|
51
|
+
else
|
52
|
+
english << cur
|
53
|
+
end
|
54
|
+
offset += 1
|
55
|
+
end
|
56
|
+
english
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
# Translates a given string either from wookie to english or vice versa.
|
4
|
+
class Wookie::Translator
|
5
|
+
|
6
|
+
# Contructor, defines the dialect ({Wookie::Dialect::Simple} by default).
|
7
|
+
# @param [Hash] options The instance options.
|
8
|
+
# @option options [#to_wookie,#from_wookie] :dialect A wookie dialect.
|
9
|
+
def initialize(options={ dialect: Wookie::Dialect::Simple })
|
10
|
+
@dialect = options[:dialect].new
|
11
|
+
end
|
12
|
+
|
13
|
+
# Translates an english string to wookiespeak using the dialect given to the
|
14
|
+
# initializer.
|
15
|
+
#
|
16
|
+
# @param [#to_s] str The string to be translated.
|
17
|
+
# @return [String] The translated string.
|
18
|
+
def to_wookie(str)
|
19
|
+
@dialect.to_wookie(str.to_s)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Translates a string from wookiespeak to english using the dialect given to
|
23
|
+
# the initializer.
|
24
|
+
#
|
25
|
+
# @param [#to_s] str The string to be translated.
|
26
|
+
# @return [String] The english translation.
|
27
|
+
def from_wookie(str)
|
28
|
+
@dialect.from_wookie(str.to_s)
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
class IdentityDialect < Wookie::Dialect::Base
|
5
|
+
def to_wookie(str); str; end
|
6
|
+
def from_wookie(str); str; end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Wookie::Translator do
|
10
|
+
before do
|
11
|
+
@t = Wookie::Translator.new(dialect: IdentityDialect)
|
12
|
+
@english = 'enter your text here!'
|
13
|
+
@wookie = 'wowhaoworc rooohurc aowokao acworcwo!'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "translates english to english" do
|
17
|
+
@t.to_wookie(@english).should eq(@english)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "translates wookie to wookie" do
|
21
|
+
@t.from_wookie(@wookie).should eq(@wookie)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Wookie::Dialect::Simple do
|
5
|
+
before do
|
6
|
+
@dialect = Wookie::Dialect::Simple.new
|
7
|
+
@english = 'enter your text here!'
|
8
|
+
@wookie = 'wowhaoworc rooohurc aowokao acworcwo!'
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should have a distinct set of short codes" do
|
12
|
+
Wookie::Dialect::Simple::SHORT_CODES.should eq("ck")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "translates to wookie" do
|
16
|
+
@dialect.to_wookie(@english).should eq(@wookie)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "translates back to english" do
|
20
|
+
@dialect.from_wookie(@wookie).should eq(@english)
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Wookie::Translator do
|
5
|
+
before do
|
6
|
+
@english = 'enter your text here!'
|
7
|
+
@wookie = 'wowhaoworc rooohurc aowokao acworcwo!'
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should load the default dialect, if not otherwise specified" do
|
11
|
+
t = Wookie::Translator.new
|
12
|
+
t.to_wookie(@english).should eq(@wookie)
|
13
|
+
t.from_wookie(@wookie).should eq(@english)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should translate with the simple default dialect (hash form)" do
|
17
|
+
t = Wookie::Translator.new dialect: Wookie::Dialect::Simple
|
18
|
+
t.to_wookie(@english).should eq(@wookie)
|
19
|
+
t.from_wookie(@wookie).should eq(@english)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should raise a NotImplementedError if using an incomplete dialect" do
|
23
|
+
t = Wookie::Translator.new dialect: Wookie::Dialect::Base
|
24
|
+
expect {
|
25
|
+
t.to_wookie(@english).should eq(@wookie)
|
26
|
+
}.to raise_error(NotImplementedError)
|
27
|
+
expect {
|
28
|
+
t.from_wookie(@wookie).should eq(@english)
|
29
|
+
}.to raise_error(NotImplementedError)
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/wookie', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dominik Menke\n"]
|
6
|
+
gem.email = ['wookie-translator@dmke.org']
|
7
|
+
gem.description = 'Translates an english string to wookie speak and vice versa.'
|
8
|
+
gem.summary = gem.description
|
9
|
+
gem.homepage = 'https://github.com/dmke/wookie-translator'
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = 'wookie-translator'
|
15
|
+
gem.require_paths = ['lib']
|
16
|
+
gem.version = Wookie::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rspec', '~> 2.0'
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wookie-translator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- ! 'Dominik Menke
|
9
|
+
|
10
|
+
'
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2012-03-31 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
requirement: &18289820 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '2.0'
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *18289820
|
27
|
+
description: Translates an english string to wookie speak and vice versa.
|
28
|
+
email:
|
29
|
+
- wookie-translator@dmke.org
|
30
|
+
executables:
|
31
|
+
- wookie
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- .gitignore
|
36
|
+
- .rbenv-gemsets
|
37
|
+
- .rbenv-version
|
38
|
+
- .rspec
|
39
|
+
- .yardopts
|
40
|
+
- Gemfile
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- bin/wookie
|
44
|
+
- lib/wookie.rb
|
45
|
+
- lib/wookie/cli.rb
|
46
|
+
- lib/wookie/dialect.rb
|
47
|
+
- lib/wookie/dialects/simple.rb
|
48
|
+
- lib/wookie/translator.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
- spec/wookie/identity_dialect_spec.rb
|
51
|
+
- spec/wookie/simple_dialect_spec.rb
|
52
|
+
- spec/wookie/translator_spec.rb
|
53
|
+
- wookie-translator.gemspec
|
54
|
+
homepage: https://github.com/dmke/wookie-translator
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
hash: 785250080120456516
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
hash: 785250080120456516
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.8.11
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Translates an english string to wookie speak and vice versa.
|
84
|
+
test_files:
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- spec/wookie/identity_dialect_spec.rb
|
87
|
+
- spec/wookie/simple_dialect_spec.rb
|
88
|
+
- spec/wookie/translator_spec.rb
|