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