-
Notifications
You must be signed in to change notification settings - Fork 0
/
RUBY.rb
executable file
·93 lines (79 loc) · 1.71 KB
/
RUBY.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/env ruby
MAX_SWAP_PER_WORD = 2;
SWAP_PROBABILITY = 0.2;
MAX_REMOVE_PER_WORD = 0.2;
MAX_REMOVE_VOCAL = 0.05;
MAX_REMOVE_CONSONANT = 0.10;
MAX_REMOVE_DOUBLE = 0.40;
H_PROBABILITY = 0.3;
def rnd(prob)
rand < prob
end
def letter?(char)
char =~ /[[:alpha:]]/
end
def space?(char)
char =~ /[[:space:]]/
end
def vocal?(char)
['A', 'E', 'I', 'O', 'U'].include?(char)
end
def swap_letters(line)
per_word = 0
for i in 0..(line.size-1)
c = line[i]
if space?(c)
per_word = 0
next
end
if letter?(c) && letter?(line[i+1]) && per_word < MAX_SWAP_PER_WORD && rnd(SWAP_PROBABILITY)
line[i], line[i+1] = line[i+1], line[i]
per_word += 1
end
end
line
end
def remove_letters(line)
per_word = 0
word_length = 0
for i in 0..(line.size-1)
c = line[i]
if space?(c) || i == 0
per_word = word_length = 0
for j in (i ? i+1 : 0)..(line.size-1)
break unless letter? line[j]
word_length += 1
end
end
skip = false
if vocal?(c) && rnd(MAX_REMOVE_VOCAL)
skip = true
elsif c == line[i+1] && rnd(MAX_REMOVE_DOUBLE)
skip = true
elsif letter?(c) && !vocal?(c) && rnd(MAX_REMOVE_CONSONANT)
skip = true
end
if skip && per_word < word_length*MAX_REMOVE_PER_WORD
word_length -= 1
line.slice! i
end
end
end
def add_h(line)
if rnd(H_PROBABILITY)
line.slice!(-1)
line << " H!?"
end
end
def process(line)
line.upcase!
swap_letters(line)
remove_letters(line)
add_h(line)
line
end
file = ARGV.size > 0 ? File.open(ARGV[0], "r") : $stdin
file.each_line do |line|
puts process(line)
end
file.close