gibber 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 96f2393f48044e7f68250fb1803ad8d8a32dcd50
4
+ data.tar.gz: 7bb7d2ee2ac684646c6bda120c233a08bcf0899c
5
+ SHA512:
6
+ metadata.gz: 145f49f221924d5d9259e612e251f021a7e5d2eb3fc55dcb6e55ebcb5f59e86e81ce2b0b5b8fe7aab691574fdbc6fb9ad91b2dc754a6b17dfbc68da1ea2b274a
7
+ data.tar.gz: 6cf10ce0831bf50cb2f08f47650f0d923f30845839f37062f86f36a4f32887a410162f05d8f0f2f7e8bca231711577c7c4050efd054d2b933b400ad37867d58a
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in latin_replace.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Timon Vonk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Gibber
2
+
3
+ Gibber replaces text with nonsensical latin with a maximum size difference of +/- 30%.
4
+
5
+ Useful for testing the effects of localization on UI.
6
+
7
+ [![Build Status](https://travis-ci.org/timonv/gibber.svg?branch=master)](https://travis-ci.org/timonv/gibber)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'gibber'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install gibber
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ gibber = Gibber.new
29
+ gibber.replace("I have a passion for crooked bananas")
30
+ # => A arcu a fringilla nisi tempus venenatis (maybe)
31
+ ```
32
+
33
+ Gibber preserves any non wordy characters to give your text that texty feel.
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it ( https://github.com/[my-github-username]/latin_replace/fork )
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create a new Pull Request
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/test*.rb']
7
+ end
8
+
9
+ task :default => :test
10
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gibber/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gibber"
8
+ spec.version = Gibber::VERSION
9
+ spec.authors = ["Timon Vonk"]
10
+ spec.email = ["timonv@gmail.com"]
11
+ spec.summary = %q{Nonsensical text translation}
12
+ spec.description = %q{}
13
+ spec.homepage = "http://www.github.com/timonv/gibber"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "minitest"
25
+ end
@@ -0,0 +1,93 @@
1
+ require_relative "gibber/version"
2
+
3
+ class Gibber
4
+ MARGIN = 0.3
5
+
6
+ def replace(text)
7
+ text
8
+ .gsub(/\W/, '|\0|')
9
+ .split('|')
10
+ .map(&method(:replace_word))
11
+ .join('')
12
+ end
13
+
14
+ private
15
+
16
+ # recursive if missing translation
17
+ def replace_word(word)
18
+ return word if blank?(word) || numeric?(word) || !wordy?(word)
19
+
20
+ possible_keys = keys_within_margin_for_word(word)
21
+ if translation = translation_at_keys(*possible_keys)
22
+ conditional_capitalize(word, translation)
23
+ else
24
+ replace(word[0..-2])
25
+ end
26
+ end
27
+
28
+ def blank?(word)
29
+ !word || word == ""
30
+ end
31
+
32
+ def numeric?(word)
33
+ word =~ /^\d+$/
34
+ end
35
+
36
+ def wordy?(word)
37
+ word =~ /^\w+$/
38
+ end
39
+
40
+ def keys_within_margin_for_word(word)
41
+ word_hash.keys.select { |k| word.length >= k[0] && word.length <= k[1]}
42
+ end
43
+
44
+ def translation_at_keys(*keys)
45
+ if translation = word_hash
46
+ .values_at(*keys)
47
+ .flatten
48
+ .reject { |t| t == @previous } # Never have two identical translations after each other
49
+ .sample
50
+
51
+ @previous = translation
52
+ end
53
+ end
54
+
55
+ def conditional_capitalize(original_word, translated_word)
56
+ if original_word[0] == original_word[0].upcase
57
+ translated_word.capitalize
58
+ else
59
+ translated_word
60
+ end
61
+ end
62
+
63
+ def word_hash
64
+ @_word_hash ||= begin
65
+ text.split(' ').select(&method(:wordy?)).inject({}) do |hash, word|
66
+ key = generate_key(word)
67
+ hash[key] ||= []
68
+ hash[key] = (hash[key] + [word.downcase]).uniq; hash
69
+ end
70
+ end
71
+ end
72
+
73
+ def generate_key(word)
74
+ margin = (MARGIN * word.length).to_i
75
+ lbound = [word.length - margin, 1].max
76
+ rbound = word.length + margin
77
+ [lbound, rbound]
78
+ end
79
+
80
+ def text
81
+ <<-LATIN
82
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce id risus lorem. Fusce in nulla auctor felis viverra suscipit. Sed auctor lobortis massa, euismod varius nulla interdum a. Morbi rhoncus ultrices urna, vitae vulputate dui tempor gravida. Quisque quis diam lorem. Nam non urna posuere, faucibus leo vitae, ullamcorper sem. Donec nec felis ultrices, ultrices augue et, fringilla ante. Etiam lacinia dolor non odio malesuada elementum. Vestibulum vitae risus sollicitudin, lobortis massa eget, auctor ante. Curabitur nec urna quis sapien iaculis hendrerit. Nam scelerisque leo dolor, non pulvinar ipsum tempus nec. Proin volutpat magna eu feugiat egestas. Pellentesque sed massa arcu. Nam vel magna posuere, viverra nunc eu, mollis turpis. Integer ultrices lacus ut velit ullamcorper, ac molestie risus rhoncus. Morbi at ante luctus, dignissim ligula in, ullamcorper elit.
83
+
84
+ Aenean vel efficitur lacus. Suspendisse quis nunc libero. Nullam a elit venenatis, porttitor nibh vel, dignissim turpis. Cras luctus arcu vitae velit semper, et convallis eros ultricies. Nunc at risus nec sem scelerisque tincidunt. Suspendisse eu accumsan orci. In hac habitasse platea dictumst. Etiam faucibus odio dui, quis volutpat eros pulvinar at. Donec tristique quam sem, sit amet feugiat felis tincidunt sit amet. In tincidunt lobortis est et euismod. Quisque tempus ex id enim tincidunt scelerisque. Fusce odio tortor, consequat pulvinar molestie interdum, mollis aliquam erat.
85
+
86
+ Nam nec augue ornare, laoreet ante et, finibus dolor. Aenean pulvinar felis vel mauris efficitur faucibus. Duis venenatis id nisl eget molestie. In venenatis, sem quis dignissim fermentum, justo risus tempus quam, at venenatis diam ex a massa. Etiam urna diam, placerat ac commodo eget, egestas a urna. Proin vitae malesuada libero, eu porta risus. Nullam congue, magna non ullamcorper lobortis, lorem urna gravida sem, non pellentesque tortor elit nec nisi. Nunc lobortis dui non eros rhoncus volutpat. Nunc imperdiet urna magna, sed blandit purus efficitur aliquam. Morbi mattis, sapien non faucibus aliquam, tortor dui rhoncus diam, vitae aliquam ipsum diam nec ex. Vivamus hendrerit tortor fermentum, pulvinar felis et, porta massa. Nam et ultricies ligula. Nulla vitae augue in odio pulvinar facilisis. Pellentesque purus felis, posuere ullamcorper nibh sit amet, pellentesque pulvinar libero.
87
+
88
+ Quisque a mi tellus. Nunc pretium massa in tempor maximus. Vivamus fermentum sapien nec eros ornare molestie. Vestibulum vitae est condimentum, varius nulla et, ornare justo. Vivamus gravida, neque ut faucibus placerat, metus metus lacinia lorem, ac feugiat arcu leo in arcu. Vestibulum eu augue non tellus hendrerit pharetra id a purus. Sed et velit quis leo efficitur rutrum ac eu leo. Maecenas rhoncus est est, vel eleifend arcu pharetra sed. Curabitur ligula enim, dapibus non vulputate id, efficitur ut nisl.
89
+
90
+ Nulla facilisi. Integer vulputate ultricies mi id interdum. Sed pharetra risus vitae urna mattis, nec maximus nisl bibendum. Sed in elit sit amet ipsum condimentum commodo. Etiam consequat purus magna, vel malesuada est interdum iaculis. Proin commodo, magna ac vehicula pharetra, tellus urna fermentum dui, in rutrum turpis lacus in sem. Donec in elementum nisi. Etiam fringilla diam vitae nulla varius volutpat. Vivamus at lacus porttitor, luctus dui at, eleifend ligula. Sed ac elit in enim varius vestibulum quis rhoncus arcu. Nullam tristique id nisi non feugiat.
91
+ LATIN
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ class Gibber
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,100 @@
1
+ require_relative '../lib/gibber'
2
+ require "minitest/autorun"
3
+ require "pry"
4
+
5
+ class TestGibber < MiniTest::Test
6
+ def test_single_char
7
+ word = 'x'
8
+ result = Gibber.new.replace(word)
9
+
10
+ assert(result != "", "Single char should always have a match!")
11
+ end
12
+
13
+ def test_single_word
14
+ word = 'word'
15
+ result = Gibber.new.replace(word)
16
+ margin = (Gibber::MARGIN * word.length).to_i
17
+ lbound = word.length - margin
18
+ rbound = word.length + margin
19
+
20
+ assert(result.length >= lbound && result.length <= rbound, "Word length not within margin")
21
+ assert(result != word, "Word should not equal result")
22
+ end
23
+
24
+ def test_numerics
25
+ word = '1'
26
+ result = Gibber.new.replace(word)
27
+
28
+ assert(result == word, "#{result} does not equal #{word}")
29
+
30
+ word = '12 342345 677 63454'
31
+ result = Gibber.new.replace(word)
32
+
33
+ assert(result == word, "#{result} does not equal #{word}")
34
+ end
35
+
36
+ def test_sentence
37
+ sentence = "I'm passionate about bananas."
38
+ result = Gibber.new.replace(sentence)
39
+ margin = (Gibber::MARGIN * sentence.length).to_i
40
+ lbound = sentence.length - margin
41
+ rbound = sentence.length + margin
42
+
43
+ assert(result.length >= lbound && result.length <= rbound, "Sentence length not within margin")
44
+ assert(result != sentence, "Sentence should not equal result")
45
+ end
46
+
47
+ def test_capitalization
48
+ sentence = "I'm passionate about bananas."
49
+ result = Gibber.new.replace(sentence)
50
+
51
+ assert(result[0] == result[0].upcase, "Expected first letter to be uppercase")
52
+
53
+ sentence = "i'm passionate about bananas."
54
+ result = Gibber.new.replace(sentence)
55
+
56
+ assert(result[0] == result[0].downcase, "Expected first letter to be lowercase")
57
+ end
58
+
59
+ def test_always_match_upto_100
60
+ (1..100).each do |i|
61
+ word = 'z' * i
62
+ result = Gibber.new.replace(word)
63
+
64
+ assert(result != word, 'Expected a match but got nil')
65
+ end
66
+ end
67
+
68
+ def test_preserve_trailing
69
+ %w{! ? .}.each do |trailing|
70
+ sentence = "I'm passionate about bananas#{trailing}"
71
+ result = Gibber.new.replace(sentence)
72
+
73
+ assert(result[-1] == "#{trailing}", "Expected last char to be #{trailing} but got #{result}")
74
+ end
75
+ end
76
+
77
+ def test_preserve_non_wordy_characters
78
+ sentence = 'I really like playing "God", but I am not a fan'
79
+ result = Gibber.new.replace(sentence)
80
+
81
+ assert(result =~ /^.+"\w+",.+$/, "Expected result to match original non-wordy chars but got #{result}")
82
+ end
83
+
84
+ # Quick test for user verifying bigger texts
85
+ def test_big_text
86
+ text = <<-NIETZSCHE
87
+ The Madman. Have you ever heard of the madman who on a bright morning lighted a lantern and ran to the market-place calling out unceasingly: "I seek God! I seek God!" As there were many people standing about who did not believe in God, he caused a great deal of amusement. Why? is he lost? said one. Has he strayed away like a child? said another. Or does he keep himself hidden? Is he afraid of us? Has he taken a sea voyage? Has he emigrated? - the people cried out laughingly, all in a hubbub.
88
+
89
+ The insane man jumped into their midst and transfixed them with his glances. "Where is God gone?" he called out. "I mean to tell you! We have killed him, you and I! We are all his murderers! But how have we done it? How were we able to drink up the sea? Who gave us the sponge to wipe away the whole horizon? What did we do when we loosened this earth from its sun? Whither does it now move? Whither do we move? Away from all suns? Do we not dash on unceasingly? Backwards, sideways, forwards, in all directions? Is there still an above and below? Do we not stray, as through infinite nothingness? Does not empty space breathe upon us? Has it not become colder? Does not night come on continually, darker and darker? Shall we not have to light lanterns in the morning? Do we not hear the noise of the grave-diggers who are burying God? Do we not smell the divine putrefaction? - for even Gods putrify! God is dead! God remains dead! And we have killed him!
90
+
91
+ How shall we console ourselves, the most murderous of all murderers? The holiest and the mightiest that the world has hitherto possessed, has bled to death under our knife - who will wipe the blood from us? With what water could we cleanse ourselves? What lustrums, what sacred games shall we have to devise? Is not the magnitude of this deed too great for us? Shall we not ourselves have to become Gods, merely to seem worthy of it? There never was a greater event - and on account of it, all who are born after us belong to a higher history than any history hitherto!" Here the madman was silent and looked again at his hearers; they also were silent and looked at him in surprise.
92
+
93
+ At last he threw his lantern on the ground, so that it broke in pieces and was extinguished. "I come too early," he then said. "I am not yet at the right time. This prodigious event is still on its way, and is traveling - it has not yet reached men's ears. Lightning and thunder need time, the light of the stars needs time, deeds need time, even after they are done, to be seen and heard. This deed is as yet further from them than the furthest star - and yet they have done it themselves!" It is further stated that the madman made his way into different churches on the same day, and there intoned his Requiem aeternam deo. When led out and called to account, he always gave the reply: "What are these churches now, if they are not the tombs and monuments of God?"
94
+ NIETZSCHE
95
+
96
+ gibber = Gibber.new
97
+ result = text.gsub(/[.?!]/, '\0|').split('|').map { |sentence| gibber.replace(sentence) }.join("")
98
+ puts result
99
+ end
100
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gibber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Timon Vonk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ''
70
+ email:
71
+ - timonv@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - gibber.gemspec
82
+ - lib/gibber.rb
83
+ - lib/gibber/version.rb
84
+ - test/tests.rb
85
+ homepage: http://www.github.com/timonv/gibber
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Nonsensical text translation
109
+ test_files:
110
+ - test/tests.rb