-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammartest.rb
85 lines (72 loc) · 2.02 KB
/
grammartest.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
#encoding=utf-8
#
# THIS IS ENTIRELY EXPERIMENTAL AND MAY USE LARGE AMOUNTS OF CPU/RAM
# FOR PROTRACTED PERIODS!
require 'pp'
def word_iterator(data)
Enumerator.new do |result|
first, *parts = data
num = parts.size
start = parts.map(&:peek)
# The first iterator I limited to cycle once, so we'll just base our iteration off it.
first.each do |letter|
# This will produce all a__ combinations, iterating from right to left.
begin
result << letter + parts.map(&:peek).join
parts[-1].next
# This will roll the iteration from right to left odometer-style.
# When the right iterator (i) has rolled to its beginning it increments
# the one before it (i-1).
(num - 1).downto(1) do |i|
parts[i-1].next if parts[i].peek == start[i]
end
end while parts.map(&:peek) != start
end
end
end
class Array
def permutation_count
self.map{ |item| item.size }.inject(:*)
end
def permutation_size
self.map do |pa|
pa.max.length
end.inject(:+)
end
end
class Fixnum
def to_formatted_s
to_s.reverse.scan(/\d{1,3}/).join(',').reverse
end
end
class Numeric
def to_bytesize
unit = 0
temp = to_f
while temp > 1024
temp /= 1024
unit += 1
end
sprintf('%.2f %s', temp, %w{B kB MB GB TB PB EB ZB YB}[unit]) # 100% future-proof AFAIK
end
end
$rules = {
'V' => %w{a e i o u},
'H' => %w{ä ë ï ö ü},
'D' => %w{d t th},
'N' => %w{m l n r s},
'L' => %w{w p q b d}
}
$rules['Q'] = $rules['V'] | $rules['H']
$rules['C'] = $rules['D'] | $rules['N'] | $rules['L'] | %w{ k c x j v }
$rules['K'] = $rules['N'] | $rules['L']
$rules['P'] = ($rules['Q'].product($rules['C']) | $rules['C'].product($rules['Q'])).map { |i| i.join }
pattern = []
pattern << $rules['H'].cycle(1) # produces an enumerator that cycles once
pattern << $rules['C'].cycle # produces an enumerator that cycles infinitely
pattern << $rules['V'].cycle
generator = word_iterator(pattern)
loop do
print generator.next.ljust(8)
end
puts