wordlist 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ruby.yml +27 -0
  3. data/.gitignore +6 -3
  4. data/ChangeLog.md +45 -1
  5. data/Gemfile +13 -0
  6. data/LICENSE.txt +1 -3
  7. data/README.md +266 -61
  8. data/Rakefile +7 -32
  9. data/benchmarks.rb +115 -0
  10. data/bin/wordlist +4 -7
  11. data/data/stop_words/ar.txt +104 -0
  12. data/data/stop_words/bg.txt +259 -0
  13. data/data/stop_words/bn.txt +363 -0
  14. data/data/stop_words/ca.txt +126 -0
  15. data/data/stop_words/cs.txt +138 -0
  16. data/data/stop_words/da.txt +101 -0
  17. data/data/stop_words/de.txt +129 -0
  18. data/data/stop_words/el.txt +79 -0
  19. data/data/stop_words/en.txt +175 -0
  20. data/data/stop_words/es.txt +178 -0
  21. data/data/stop_words/eu.txt +98 -0
  22. data/data/stop_words/fa.txt +332 -0
  23. data/data/stop_words/fi.txt +747 -0
  24. data/data/stop_words/fr.txt +116 -0
  25. data/data/stop_words/ga.txt +109 -0
  26. data/data/stop_words/gl.txt +160 -0
  27. data/data/stop_words/he.txt +499 -0
  28. data/data/stop_words/hi.txt +97 -0
  29. data/data/stop_words/hr.txt +179 -0
  30. data/data/stop_words/hu.txt +35 -0
  31. data/data/stop_words/hy.txt +45 -0
  32. data/data/stop_words/id.txt +357 -0
  33. data/data/stop_words/it.txt +134 -0
  34. data/data/stop_words/ja.txt +44 -0
  35. data/data/stop_words/ko.txt +677 -0
  36. data/data/stop_words/ku.txt +63 -0
  37. data/data/stop_words/lt.txt +507 -0
  38. data/data/stop_words/lv.txt +163 -0
  39. data/data/stop_words/mr.txt +99 -0
  40. data/data/stop_words/nl.txt +48 -0
  41. data/data/stop_words/no.txt +172 -0
  42. data/data/stop_words/pl.txt +138 -0
  43. data/data/stop_words/pt.txt +147 -0
  44. data/data/stop_words/ro.txt +281 -0
  45. data/data/stop_words/ru.txt +421 -0
  46. data/data/stop_words/sk.txt +173 -0
  47. data/data/stop_words/sv.txt +386 -0
  48. data/data/stop_words/th.txt +115 -0
  49. data/data/stop_words/tr.txt +114 -0
  50. data/data/stop_words/uk.txt +28 -0
  51. data/data/stop_words/ur.txt +513 -0
  52. data/data/stop_words/zh.txt +125 -0
  53. data/gemspec.yml +4 -10
  54. data/lib/wordlist/abstract_wordlist.rb +24 -0
  55. data/lib/wordlist/builder.rb +170 -138
  56. data/lib/wordlist/cli.rb +458 -0
  57. data/lib/wordlist/compression/reader.rb +72 -0
  58. data/lib/wordlist/compression/writer.rb +80 -0
  59. data/lib/wordlist/exceptions.rb +31 -0
  60. data/lib/wordlist/file.rb +176 -0
  61. data/lib/wordlist/format.rb +38 -0
  62. data/lib/wordlist/lexer/lang.rb +32 -0
  63. data/lib/wordlist/lexer/stop_words.rb +68 -0
  64. data/lib/wordlist/lexer.rb +218 -0
  65. data/lib/wordlist/list_methods.rb +462 -0
  66. data/lib/wordlist/modifiers/capitalize.rb +45 -0
  67. data/lib/wordlist/modifiers/downcase.rb +45 -0
  68. data/lib/wordlist/modifiers/gsub.rb +51 -0
  69. data/lib/wordlist/modifiers/modifier.rb +44 -0
  70. data/lib/wordlist/modifiers/mutate.rb +133 -0
  71. data/lib/wordlist/modifiers/mutate_case.rb +25 -0
  72. data/lib/wordlist/modifiers/sub.rb +97 -0
  73. data/lib/wordlist/modifiers/tr.rb +71 -0
  74. data/lib/wordlist/modifiers/upcase.rb +45 -0
  75. data/lib/wordlist/modifiers.rb +8 -0
  76. data/lib/wordlist/operators/binary_operator.rb +38 -0
  77. data/lib/wordlist/operators/concat.rb +47 -0
  78. data/lib/wordlist/operators/intersect.rb +55 -0
  79. data/lib/wordlist/operators/operator.rb +29 -0
  80. data/lib/wordlist/operators/power.rb +72 -0
  81. data/lib/wordlist/operators/product.rb +50 -0
  82. data/lib/wordlist/operators/subtract.rb +54 -0
  83. data/lib/wordlist/operators/unary_operator.rb +29 -0
  84. data/lib/wordlist/operators/union.rb +61 -0
  85. data/lib/wordlist/operators/unique.rb +52 -0
  86. data/lib/wordlist/operators.rb +7 -0
  87. data/lib/wordlist/unique_filter.rb +40 -61
  88. data/lib/wordlist/version.rb +1 -1
  89. data/lib/wordlist/words.rb +71 -0
  90. data/lib/wordlist.rb +103 -2
  91. data/spec/abstract_list_spec.rb +18 -0
  92. data/spec/builder_spec.rb +220 -76
  93. data/spec/cli_spec.rb +801 -0
  94. data/spec/compression/reader_spec.rb +137 -0
  95. data/spec/compression/writer_spec.rb +194 -0
  96. data/spec/file_spec.rb +258 -0
  97. data/spec/fixtures/wordlist.txt +15 -0
  98. data/spec/fixtures/wordlist.txt.bz2 +0 -0
  99. data/spec/fixtures/wordlist.txt.gz +0 -0
  100. data/spec/fixtures/wordlist.txt.xz +0 -0
  101. data/spec/fixtures/wordlist_with_ambiguous_format +3 -0
  102. data/spec/fixtures/wordlist_with_comments.txt +19 -0
  103. data/spec/fixtures/wordlist_with_empty_lines.txt +19 -0
  104. data/spec/format_spec.rb +50 -0
  105. data/spec/helpers/text.rb +3 -3
  106. data/spec/helpers/wordlist.rb +2 -2
  107. data/spec/lexer/lang_spec.rb +70 -0
  108. data/spec/lexer/stop_words_spec.rb +77 -0
  109. data/spec/lexer_spec.rb +652 -0
  110. data/spec/list_methods_spec.rb +181 -0
  111. data/spec/modifiers/capitalize_spec.rb +27 -0
  112. data/spec/modifiers/downcase_spec.rb +27 -0
  113. data/spec/modifiers/gsub_spec.rb +59 -0
  114. data/spec/modifiers/modifier_spec.rb +20 -0
  115. data/spec/modifiers/mutate_case_spec.rb +46 -0
  116. data/spec/modifiers/mutate_spec.rb +39 -0
  117. data/spec/modifiers/sub_spec.rb +98 -0
  118. data/spec/modifiers/tr_spec.rb +46 -0
  119. data/spec/modifiers/upcase_spec.rb +27 -0
  120. data/spec/operators/binary_operator_spec.rb +19 -0
  121. data/spec/operators/concat_spec.rb +26 -0
  122. data/spec/operators/intersect_spec.rb +37 -0
  123. data/spec/operators/operator_spec.rb +16 -0
  124. data/spec/operators/power_spec.rb +57 -0
  125. data/spec/operators/product_spec.rb +39 -0
  126. data/spec/operators/subtract_spec.rb +37 -0
  127. data/spec/operators/union_spec.rb +37 -0
  128. data/spec/operators/unique_spec.rb +25 -0
  129. data/spec/spec_helper.rb +2 -1
  130. data/spec/unique_filter_spec.rb +108 -18
  131. data/spec/wordlist_spec.rb +55 -3
  132. data/spec/words_spec.rb +41 -0
  133. metadata +183 -120
  134. data/lib/wordlist/builders/website.rb +0 -216
  135. data/lib/wordlist/builders.rb +0 -1
  136. data/lib/wordlist/flat_file.rb +0 -47
  137. data/lib/wordlist/list.rb +0 -162
  138. data/lib/wordlist/mutator.rb +0 -113
  139. data/lib/wordlist/parsers.rb +0 -74
  140. data/lib/wordlist/runners/list.rb +0 -116
  141. data/lib/wordlist/runners/runner.rb +0 -67
  142. data/lib/wordlist/runners.rb +0 -2
  143. data/scripts/benchmark +0 -59
  144. data/scripts/text/comedy_of_errors.txt +0 -4011
  145. data/spec/flat_file_spec.rb +0 -25
  146. data/spec/list_spec.rb +0 -58
  147. data/spec/mutator_spec.rb +0 -43
  148. data/spec/parsers_spec.rb +0 -118
@@ -0,0 +1,133 @@
1
+ require 'wordlist/modifiers/sub'
2
+
3
+ module Wordlist
4
+ module Modifiers
5
+ #
6
+ # Lazily enumerates through every combination of a string substitution
7
+ # on every word in the wordlist.
8
+ #
9
+ # @since 1.0.0
10
+ #
11
+ class Mutate < Sub
12
+
13
+ #
14
+ # Enumerates over every mutation of every word in the wordlist.
15
+ #
16
+ # @yield [word]
17
+ # The given block will be passed each mutation of each word.
18
+ #
19
+ # @yieldparam [String] word
20
+ # A mutated word.
21
+ #
22
+ # @return [Enumerator]
23
+ # If no block is given, an Enumerator object will be returned.
24
+ #
25
+ # @example
26
+ # wordlist = Wordlist::List["foo", "bar", "baz"]
27
+ # wordlist.mutate(/[oa]/, {'o' => '0', 'a' => '@'}).each do |word|
28
+ # puts word
29
+ # end
30
+ # # foo
31
+ # # f0o
32
+ # # fo0
33
+ # # f00
34
+ # # bar
35
+ # # b@r
36
+ # # baz
37
+ # # b@z
38
+ #
39
+ # @api public
40
+ #
41
+ def each
42
+ return enum_for(__method__) unless block_given?
43
+
44
+ @wordlist.each do |word|
45
+ yield word
46
+
47
+ matches = all_matches(word)
48
+
49
+ each_combination(matches) do |selected_matches|
50
+ new_word = word.dup
51
+ offset = 0
52
+
53
+ selected_matches.each do |match|
54
+ index, end_index = match.offset(0)
55
+ length = end_index - index
56
+
57
+ matched_string = match[0]
58
+ replace_string = substitute(matched_string)
59
+
60
+ new_word[index+offset,length] = replace_string
61
+
62
+ offset += (replace_string.length - length)
63
+ end
64
+
65
+ yield new_word
66
+ end
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ #
73
+ # Finds all matches of the {#pattern} within a string.
74
+ #
75
+ # @param [String] string
76
+ # The given string.
77
+ #
78
+ # @return [Array<MatchData>]
79
+ # The array of all found non-overlapping matches.
80
+ #
81
+ def all_matches(string)
82
+ offset = 0
83
+ matches = []
84
+
85
+ while (match = string.match(@pattern,offset))
86
+ index, end_index = match.offset(0)
87
+
88
+ matches << match
89
+
90
+ offset = end_index
91
+ end
92
+
93
+ return matches
94
+ end
95
+
96
+ #
97
+ # Enumerates through every combination of every match.
98
+ #
99
+ # @param [Array<MatchData>] matches
100
+ # The array of matches.
101
+ #
102
+ # @yield [matches]
103
+ # The given block will be passed each combination of the matches.
104
+ #
105
+ # @yieldparam [Array<MatchData>] matches
106
+ # A combination of the matches.
107
+ #
108
+ def each_combination(matches,&block)
109
+ (1..matches.length).each do |count|
110
+ matches.combination(count,&block)
111
+ end
112
+ end
113
+
114
+ #
115
+ # Returns the replacement string for a matched substring.
116
+ #
117
+ # @param [String] matched
118
+ # The matched substring.
119
+ #
120
+ # @return [String]
121
+ # The replacement string.
122
+ #
123
+ def substitute(matched)
124
+ case @replace
125
+ when Hash then @replace[matched].to_s
126
+ when nil then @block.call(matched)
127
+ else @replace
128
+ end
129
+ end
130
+
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,25 @@
1
+ require 'wordlist/modifiers/mutate'
2
+
3
+ module Wordlist
4
+ module Modifiers
5
+ #
6
+ # Lazily enumerates through every possible upper/lower-case variation of
7
+ # each word in the wordlist.
8
+ #
9
+ # @since 1.0.0
10
+ #
11
+ class MutateCase < Mutate
12
+
13
+ #
14
+ # Initializes the case mutator.
15
+ #
16
+ # @param [Enumerable] wordlist
17
+ # The wordlist to modify.
18
+ #
19
+ def initialize(wordlist)
20
+ super(wordlist,/[[:alpha:]]/) { |letter| letter.swapcase }
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,97 @@
1
+ require 'wordlist/modifiers/modifier'
2
+
3
+ module Wordlist
4
+ module Modifiers
5
+ #
6
+ # Lazily calls `String#sub` on every word in the wordlist.
7
+ #
8
+ # @since 1.0.0
9
+ #
10
+ class Sub < Modifier
11
+
12
+ # The pattern to substitute.
13
+ #
14
+ # @return [Regexp, String]
15
+ attr_reader :pattern
16
+
17
+ # The replacement String or map of Strings.
18
+ #
19
+ # @return [String, Hash{String => String, nil}]
20
+ attr_reader :replace
21
+
22
+ # The optional block to call when replacing matched substrings.
23
+ #
24
+ # @return [Proc, nil]
25
+ attr_reader :block
26
+
27
+ #
28
+ # Initializes the `String#sub` modifier.
29
+ #
30
+ # @param [Regexp, String] pattern
31
+ # The pattern to replace.
32
+ #
33
+ # @param [String, Hash, nil] replace
34
+ # The characters or character range to use as the replacement.
35
+ #
36
+ # @yield [match]
37
+ # The given block will be call to replace the matched substring,
38
+ # if `replace` is nil.
39
+ #
40
+ # @yieldparam [String] match
41
+ # A matched substring.
42
+ #
43
+ # @raise [TypeError]
44
+ # The `replace` value was not a String, Hash, or `nil`.
45
+ #
46
+ def initialize(wordlist,pattern,replace=nil,&block)
47
+ super(wordlist)
48
+
49
+ @pattern = pattern
50
+ @replace = case replace
51
+ when String, Hash, nil then replace
52
+ else
53
+ raise(TypeError,"no implicit conversion of #{replace.class} to String")
54
+ end
55
+ @block = block
56
+ end
57
+
58
+ #
59
+ # Enumerates over every `sub`ed word in the wordlist.
60
+ #
61
+ # @yield [word]
62
+ # The given block will be passed each `sub`ed word.
63
+ #
64
+ # @yieldparam [String] word
65
+ # A `sub`ed word.
66
+ #
67
+ # @return [Enumerator]
68
+ # If no block is given, an Enumerator object will be returned.
69
+ #
70
+ # @example
71
+ # wordlist = Wordlist::List["foo", "bar", "baz"]
72
+ # wordlist.sub(/o/, '0').each do |word|
73
+ # puts word
74
+ # end
75
+ # # f0o
76
+ # # bar
77
+ # # baz
78
+ #
79
+ # @api public
80
+ #
81
+ def each
82
+ return enum_for(__method__) unless block_given?
83
+
84
+ if @replace
85
+ @wordlist.each do |word|
86
+ yield word.sub(@pattern,@replace)
87
+ end
88
+ else
89
+ @wordlist.each do |word|
90
+ yield word.sub(@pattern,&block)
91
+ end
92
+ end
93
+ end
94
+
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,71 @@
1
+ require 'wordlist/modifiers/modifier'
2
+
3
+ module Wordlist
4
+ module Modifiers
5
+ #
6
+ # Lazily calls `String#tr` on every word in the wordlist.
7
+ #
8
+ # @since 1.0.0
9
+ #
10
+ class Tr < Modifier
11
+
12
+ # The characters or character range to translate.
13
+ #
14
+ # @return [String]
15
+ attr_reader :chars
16
+
17
+ # The characters or character range to translate to.
18
+ #
19
+ # @return [String]
20
+ attr_reader :replace
21
+
22
+ #
23
+ # Initializes the `String#tr` modifier.
24
+ #
25
+ # @param [String] chars
26
+ # The characters or character range to replace.
27
+ #
28
+ # @param [String] replace
29
+ # The characters or character range to use as the replacement.
30
+ #
31
+ def initialize(wordlist,chars,replace)
32
+ super(wordlist)
33
+
34
+ @chars = chars
35
+ @replace = replace
36
+ end
37
+
38
+ #
39
+ # Enumerates over every `tr`ed word in the wordlist.
40
+ #
41
+ # @yield [word]
42
+ # The given block will be passed each `tr`ed word.
43
+ #
44
+ # @yieldparam [String] word
45
+ # A `tr`ed word.
46
+ #
47
+ # @return [Enumerator]
48
+ # If no block is given, an Enumerator object will be returned.
49
+ #
50
+ # @example
51
+ # wordlist = Wordlist::List["foo", "bar", "baz"]
52
+ # wordlist.tr("oa", "0@").each do |word|
53
+ # puts word
54
+ # end
55
+ # # f00
56
+ # # b@r
57
+ # # b@z
58
+ #
59
+ # @api public
60
+ #
61
+ def each
62
+ return enum_for(__method__) unless block_given?
63
+
64
+ @wordlist.each do |word|
65
+ yield word.tr(@chars,@replace)
66
+ end
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,45 @@
1
+ require 'wordlist/modifiers/modifier'
2
+
3
+ module Wordlist
4
+ module Modifiers
5
+ #
6
+ # Lazily calls `String#upcase` on every word in the wordlist.
7
+ #
8
+ # @since 1.0.0
9
+ #
10
+ class Upcase < Modifier
11
+
12
+ #
13
+ # Enumerates over every `upcase`d word in the wordlist.
14
+ #
15
+ # @yield [word]
16
+ # The given block will be passed each `upcase`d word.
17
+ #
18
+ # @yieldparam [String] word
19
+ # A `upcase`d word.
20
+ #
21
+ # @return [Enumerator]
22
+ # If no block is given, an Enumerator object will be returned.
23
+ #
24
+ # @example
25
+ # wordlist = Wordlist::List["foo", "bar", "baz"]
26
+ # wordlist.upcase.each do |word|
27
+ # puts word
28
+ # end
29
+ # # FOO
30
+ # # BAR
31
+ # # BAZ
32
+ #
33
+ # @api public
34
+ #
35
+ def each
36
+ return enum_for(__method__) unless block_given?
37
+
38
+ @wordlist.each do |word|
39
+ yield word.upcase
40
+ end
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,8 @@
1
+ require 'wordlist/modifiers/tr'
2
+ require 'wordlist/modifiers/sub'
3
+ require 'wordlist/modifiers/gsub'
4
+ require 'wordlist/modifiers/capitalize'
5
+ require 'wordlist/modifiers/upcase'
6
+ require 'wordlist/modifiers/downcase'
7
+ require 'wordlist/modifiers/mutate'
8
+ require 'wordlist/modifiers/mutate_case'
@@ -0,0 +1,38 @@
1
+ require 'wordlist/operators/operator'
2
+
3
+ module Wordlist
4
+ module Operators
5
+ #
6
+ # Binary operator base class.
7
+ #
8
+ # @since 1.0.0
9
+ #
10
+ class BinaryOperator < Operator
11
+
12
+ # The left operand.
13
+ #
14
+ # @return [Enumerable]
15
+ attr_reader :left
16
+
17
+ # The right operand.
18
+ #
19
+ # @return [Enumerable]
20
+ attr_reader :right
21
+
22
+ #
23
+ # Initializes the binary operator.
24
+ #
25
+ # @param [Enumerable] left
26
+ # The left operand.
27
+ #
28
+ # @param [Enumerable] right
29
+ # The right operand.
30
+ #
31
+ def initialize(left,right)
32
+ @left = left
33
+ @right = right
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,47 @@
1
+ require 'wordlist/operators/binary_operator'
2
+
3
+ module Wordlist
4
+ module Operators
5
+ #
6
+ # Lazily enumerates over the first wordlist, then the second.
7
+ #
8
+ # @since 1.0.0
9
+ #
10
+ class Concat < BinaryOperator
11
+
12
+ #
13
+ # Enumerates over each word in both wordlists.
14
+ #
15
+ # @yield [word]
16
+ # The given block will be passed each word from both wordlists.
17
+ #
18
+ # @yieldparam [String] word
19
+ # A word from one of the wordlists.
20
+ #
21
+ # @return [Enumerator]
22
+ # If no block is given, an Enumerator object will be returned.
23
+ #
24
+ # @example
25
+ # wordlist1 = Wordlist::List["foo", "bar", "baz"]
26
+ # wordlist2 = Wordlist::List["abc", "xyz"]
27
+ # (wordlist1 + wordlist2).each do |word|
28
+ # puts word
29
+ # end
30
+ # # foo
31
+ # # bar
32
+ # # baz
33
+ # # abc
34
+ # # xyz
35
+ #
36
+ # @api public
37
+ #
38
+ def each(&block)
39
+ return enum_for(__method__) unless block
40
+
41
+ @left.each(&block)
42
+ @right.each(&block)
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,55 @@
1
+ require 'wordlist/operators/binary_operator'
2
+ require 'wordlist/unique_filter'
3
+
4
+ module Wordlist
5
+ module Operators
6
+ #
7
+ # Lazily enumerates over every word that belongs to both wordlists.
8
+ #
9
+ # @since 1.0.0
10
+ #
11
+ class Intersect < BinaryOperator
12
+
13
+ #
14
+ # Enumerates over the intersection between two wordlists.
15
+ #
16
+ # @yield [word]
17
+ # The given block will be passed each word from the intersection between
18
+ # the two wordlists.
19
+ #
20
+ # @yieldparam [String] word
21
+ # A word that belongs to both wordlists.
22
+ #
23
+ # @return [Enumerator]
24
+ # If no block is given, an Enumerator object will be returned.
25
+ #
26
+ # @example
27
+ # wordlist1 = Wordlist::List["foo", "bar", "baz", "qux"]
28
+ # wordlist2 = Wordlist::List["xyz", "bar", "abc", "qux"]
29
+ # (wordlist1 & wordlist2).each do |word|
30
+ # puts word
31
+ # end
32
+ # # bar
33
+ # # qux
34
+ #
35
+ # @api public
36
+ #
37
+ def each
38
+ return enum_for(__method__) unless block_given?
39
+
40
+ unique_filter = UniqueFilter.new
41
+
42
+ @left.each do |word|
43
+ unique_filter.add(word)
44
+ end
45
+
46
+ @right.each do |word|
47
+ if unique_filter.include?(word)
48
+ yield word
49
+ end
50
+ end
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,29 @@
1
+ module Wordlist
2
+ module Operators
3
+ #
4
+ # Operator base class.
5
+ #
6
+ # @since 1.0.0
7
+ #
8
+ class Operator
9
+
10
+ include Enumerable
11
+
12
+ #
13
+ # Place-holder method.
14
+ #
15
+ # @yield [word]
16
+ #
17
+ # @yieldparam [String] word
18
+ #
19
+ # @return [Enumerator]
20
+ #
21
+ # @abstract
22
+ #
23
+ def each(&block)
24
+ raise(NotImplementedError,"#{self.class}#each was not implemented")
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,72 @@
1
+ require 'wordlist/operators/binary_operator'
2
+ require 'wordlist/operators/product'
3
+
4
+ module Wordlist
5
+ module Operators
6
+ #
7
+ # Lazily enumerates over every combination of words in the wordlist.
8
+ #
9
+ # @since 1.0.0
10
+ #
11
+ class Power < BinaryOperator
12
+
13
+ # The product of the wordlist with itself.
14
+ #
15
+ # @return [Product]
16
+ attr_reader :wordlists
17
+
18
+ alias exponent right
19
+
20
+ #
21
+ # Initializes the power operator.
22
+ #
23
+ # @param [Enumerable] wordlist
24
+ #
25
+ # @param [Integer] exponent
26
+ #
27
+ def initialize(wordlist,exponent)
28
+ super(wordlist,exponent)
29
+
30
+ @wordlists = wordlist
31
+
32
+ (exponent - 1).times do
33
+ @wordlists = Product.new(wordlist,@wordlists)
34
+ end
35
+ end
36
+
37
+ #
38
+ # Enumerates over every combination of words from the wordlist.
39
+ #
40
+ # @yield [string]
41
+ # The given block will be passed each combination of words from the
42
+ # wordlist.
43
+ #
44
+ # @yieldparam [String] string
45
+ # A combination of words from the wordlist.
46
+ #
47
+ # @return [Enumerator]
48
+ # If no block is given, an Enumerator object will be returned.
49
+ #
50
+ # @example
51
+ # wordlist = Wordlist::List["foo", "bar"]
52
+ # (wordlist ** 3).each do |word|
53
+ # puts word
54
+ # end
55
+ # # foofoofoo
56
+ # # foofoobar
57
+ # # foobarfoo
58
+ # # foobarbar
59
+ # # barfoofoo
60
+ # # barfoobar
61
+ # # barbarfoo
62
+ # # barbarbar
63
+ #
64
+ # @api public
65
+ #
66
+ def each(&block)
67
+ @wordlists.each(&block)
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,50 @@
1
+ require 'wordlist/operators/binary_operator'
2
+
3
+ module Wordlist
4
+ module Operators
5
+ #
6
+ # Lazily enumerates over the combination of the words from two wordlists.
7
+ #
8
+ # @since 1.0.0
9
+ #
10
+ class Product < BinaryOperator
11
+
12
+ #
13
+ # Enumerates over every combination of the words from the two wordlist.
14
+ #
15
+ # @yield [string]
16
+ # The given block will be passed each combination of words from the
17
+ # two wordlist.
18
+ #
19
+ # @yieldparam [String] string
20
+ # A combination of two words from the two wordlists.
21
+ #
22
+ # @return [Enumerator]
23
+ # If no block is given, an Enumerator object will be returned.
24
+ #
25
+ # @example
26
+ # wordlist1 = Wordlist::List["foo", "bar"]
27
+ # wordlist2 = Wordlist::List["ABC", "XYZ"]
28
+ # (wordlist1 * wordlist2).each do |word|
29
+ # puts word
30
+ # end
31
+ # # fooABC
32
+ # # fooXYZ
33
+ # # barABC
34
+ # # barXYZ
35
+ #
36
+ # @api public
37
+ #
38
+ def each
39
+ return enum_for(__method__) unless block_given?
40
+
41
+ @left.each do |word1|
42
+ @right.each do |word2|
43
+ yield word1 + word2
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end