repasta 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/repasta +71 -0
  2. metadata +52 -0
data/bin/repasta ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+
4
+ # Args.
5
+ dictionary = nil
6
+ level = 0
7
+ probability = 0.7
8
+ text = :stdin
9
+ # Parse args.
10
+ OptionParser.new do |opts|
11
+ opts.banner = "Usage: #{File.basename(__FILE__)} [options] [file]"
12
+ opts.separator ""
13
+ opts.separator "Rewrites the text from file. If file is omitted then standard"
14
+ opts.separator "input is read."
15
+ opts.separator ""
16
+ opts.separator "Options:"
17
+ opts.on "-d", "--dictionary DICT" do |x|
18
+ dictionary = x
19
+ end
20
+ opts.on "-l", "--level N", "N last letters of the word must coincide with",
21
+ "a word from the dictionary. Default is 0." do |n|
22
+ level = Integer(n)
23
+ end
24
+ opts.on "-p", "--probability PROB", "Probability of the replacement.",
25
+ "Default is 0.7." do |x|
26
+ probability = Float(x)
27
+ abort %(Probability must be between 0.0 and 1.0) unless probability.between? 0.0, 1.0
28
+ end
29
+ opts.on "-h", "--help", "Show this message and exit" do
30
+ puts opts
31
+ exit
32
+ end
33
+ end.parse!
34
+ text = ARGV.shift if not ARGV.empty?
35
+ abort %(Dictionary is not specified) unless dictionary
36
+
37
+ key = lambda do |word|
38
+ if level == 0 then nil
39
+ else word[-level..-1]
40
+ end
41
+ end
42
+
43
+ begin
44
+ # { key => [word1, word2, ...] }
45
+ dictionary = File.read(dictionary).
46
+ lines.
47
+ map(&:strip).
48
+ reject(&:empty?).
49
+ reduce(Hash.new { |h, key| h[key] = [] }) do |dictionary, word|
50
+ dictionary[key.(word)].push(word)
51
+ dictionary
52
+ end
53
+ # Read the text.
54
+ text =
55
+ if text == :stdin then STDIN.read
56
+ else File.read(text)
57
+ end
58
+ # Rewrite the text.
59
+ text = text.
60
+ gsub(/\S+/) do |word|
61
+ if rand <= probability then
62
+ dictionary[key.(word)].sample or word
63
+ else
64
+ word
65
+ end
66
+ end
67
+ #
68
+ print text
69
+ rescue Exception => e
70
+ abort e.message
71
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: repasta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lavir the Whiolet
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-01-31 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'A must-have rewriter''s productivity tool which fully automates the
15
+ rewriting process and provides fast and enterprise-quality solutions for your business
16
+ needs.
17
+
18
+ '
19
+ email: Lavir.th.Whiolet@gmail.com
20
+ executables:
21
+ - repasta
22
+ extensions: []
23
+ extra_rdoc_files: []
24
+ files:
25
+ - bin/repasta
26
+ homepage: https://rubygems.org/gems/repasta
27
+ licenses:
28
+ - EULA
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.23
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: The rewriter's productivity tool
51
+ test_files: []
52
+ has_rdoc: