random_text 0.0.6.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +4 -2
- data/Rakefile +9 -6
- data/VERSION.yml +1 -1
- data/bin/lorem +5 -0
- data/bin/vesna +5 -0
- data/lib/random_text/dictionary.rb +42 -0
- data/lib/random_text/random_strings.rb +5 -1
- data/lib/random_text.rb +56 -13
- data/random_text.gemspec +6 -8
- data/spec/random_text/dictionary_spec.rb +107 -0
- data/spec/random_text/random_string_spec.rb +44 -45
- data/spec/random_text_spec.rb +11 -1
- metadata +15 -19
- data/lib/random_text/random_text.rb +0 -38
- data/spec/random_text/random_text_spec.rb +0 -59
data/Manifest
CHANGED
@@ -2,13 +2,15 @@ Manifest
|
|
2
2
|
README.rdoc
|
3
3
|
Rakefile
|
4
4
|
VERSION.yml
|
5
|
+
bin/lorem
|
6
|
+
bin/vesna
|
5
7
|
lib/random_text.rb
|
8
|
+
lib/random_text/dictionary.rb
|
6
9
|
lib/random_text/random_strings.rb
|
7
|
-
lib/random_text/random_text.rb
|
8
10
|
resources/lorem.txt
|
9
11
|
resources/vesna.txt
|
12
|
+
spec/random_text/dictionary_spec.rb
|
10
13
|
spec/random_text/random_string_spec.rb
|
11
|
-
spec/random_text/random_text_spec.rb
|
12
14
|
spec/random_text_spec.rb
|
13
15
|
spec/spec.opts
|
14
16
|
spec/spec_helper.rb
|
data/Rakefile
CHANGED
@@ -1,20 +1,23 @@
|
|
1
|
+
require 'pathname'
|
1
2
|
require 'rubygems'
|
2
3
|
require 'rake'
|
3
4
|
require 'rake/clean'
|
4
5
|
require 'fileutils'
|
5
6
|
require 'echoe'
|
6
7
|
|
7
|
-
version = YAML.load_file(
|
8
|
+
version = YAML.load_file(Pathname(__FILE__).dirname + 'VERSION.yml').join('.') rescue nil
|
8
9
|
|
9
10
|
echoe = Echoe.new('random_text', version) do |p|
|
10
11
|
p.author = 'toy'
|
11
12
|
p.summary = 'A library to generate random strings.'
|
12
|
-
p.
|
13
|
+
p.project = 'toytoy'
|
13
14
|
end
|
14
15
|
|
15
16
|
desc "Replace system gem with symlink to this folder"
|
16
|
-
task
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
task 'ghost' do
|
18
|
+
gem_path = Pathname(Gem.searcher.find(echoe.name).full_gem_path)
|
19
|
+
current_path = Pathname('.').expand_path
|
20
|
+
cmd = gem_path.writable? && gem_path.parent.writable? ? %w() : %w(sudo)
|
21
|
+
system(*cmd + %W[rm -r #{gem_path}])
|
22
|
+
system(*cmd + %W[ln -s #{current_path} #{gem_path}])
|
20
23
|
end
|
data/VERSION.yml
CHANGED
@@ -1 +1 @@
|
|
1
|
-
[
|
1
|
+
[1, 0, 0]
|
data/bin/lorem
ADDED
data/bin/vesna
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
class RandomText
|
2
|
+
class Dictionary
|
3
|
+
def initialize(text)
|
4
|
+
@words = RandomStrings.new(text.scan(/\w{3,}/u).
|
5
|
+
collect{ |w| RandomText.downcase(w) }.
|
6
|
+
reject{ |w| w =~ /^[0-9]/u }.
|
7
|
+
uniq.map{ |w| w })
|
8
|
+
@sentences = RandomStrings.new(text.split(/[\r\n]+/u).
|
9
|
+
map(&:strip).compact.
|
10
|
+
delete_if(&:empty?).uniq)
|
11
|
+
end
|
12
|
+
|
13
|
+
def word
|
14
|
+
@words.get
|
15
|
+
end
|
16
|
+
|
17
|
+
def words(n = :all)
|
18
|
+
@words.get(n)
|
19
|
+
end
|
20
|
+
|
21
|
+
def uniq_words(n = :all)
|
22
|
+
@words.uniq(n)
|
23
|
+
end
|
24
|
+
alias_method :unique_words, :uniq_words
|
25
|
+
|
26
|
+
def sentence
|
27
|
+
@sentences.get
|
28
|
+
end
|
29
|
+
|
30
|
+
def sentences(n = :all)
|
31
|
+
@sentences.get(n)
|
32
|
+
end
|
33
|
+
|
34
|
+
def paragraph
|
35
|
+
@sentences.get(5).join(' ')
|
36
|
+
end
|
37
|
+
|
38
|
+
def paragraphs(n)
|
39
|
+
Array.new(n){ paragraph }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/random_text.rb
CHANGED
@@ -1,23 +1,66 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
$:.unshift(File.dirname(__FILE__)) unless
|
2
3
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
4
|
|
4
|
-
require '
|
5
|
-
require '
|
5
|
+
require 'random_text/dictionary'
|
6
|
+
require 'random_text/random_strings'
|
6
7
|
|
7
|
-
|
8
|
+
class RandomText
|
9
|
+
class << self
|
10
|
+
def dictionaries
|
11
|
+
@dictionaries ||= {}
|
12
|
+
end
|
8
13
|
|
9
|
-
|
10
|
-
|
14
|
+
def add_dictionary(path)
|
15
|
+
const_name = classify(File.basename(path, File.extname(path)))
|
16
|
+
dictionary = Dictionary.new(File.read(path))
|
17
|
+
self.const_set(const_name, dictionary)
|
18
|
+
dictionaries[const_name] = dictionary
|
19
|
+
end
|
20
|
+
|
21
|
+
def run(args)
|
22
|
+
arg = args.join(' ').strip.downcase
|
23
|
+
arg = 'p' if arg.empty?
|
24
|
+
binary = File.basename($0)
|
25
|
+
if m = arg.match(/^(\d+)?\s*(p|w|s|u)/)
|
26
|
+
number = m[1] && m[1].to_i
|
27
|
+
dictionary = dictionaries[classify(binary)]
|
28
|
+
puts case m[2]
|
29
|
+
when 'p'
|
30
|
+
number ? dictionary.paragraphs(number) : dictionary.paragraph
|
31
|
+
when 's'
|
32
|
+
number ? dictionary.sentences(number) : dictionary.sentence
|
33
|
+
when 'w'
|
34
|
+
number ? dictionary.words(number) : dictionary.word
|
35
|
+
when 'u'
|
36
|
+
number ? dictionary.uniq_words(number) : dictionary.uniq_words
|
37
|
+
end
|
38
|
+
else
|
39
|
+
abort <<-help
|
40
|
+
#{binary} [specifier]
|
41
|
+
without specifier returns one paragraph
|
42
|
+
p, paragraph, s, sentence, w, word - one paragraph, sentence, word
|
43
|
+
X p, X paragraph, X s, X sentence, X w, X word - X paragraphs, sentences, words
|
44
|
+
u, uniq_words - all words
|
45
|
+
X u, X uniq_words - X uniq_words
|
46
|
+
help
|
47
|
+
end
|
48
|
+
end
|
11
49
|
|
12
|
-
|
13
|
-
|
50
|
+
def classify(s)
|
51
|
+
s.split('_').map(&:capitalize).join
|
52
|
+
end
|
14
53
|
|
15
|
-
|
16
|
-
|
17
|
-
|
54
|
+
OTHER_LOWER = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'.scan(/./u)
|
55
|
+
OTHER_UPPER = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'.scan(/./u)
|
56
|
+
DOWNCASE_TABLE = Hash[*(OTHER_UPPER.zip(OTHER_LOWER)).flatten]
|
57
|
+
|
58
|
+
def downcase(s)
|
59
|
+
s.downcase.gsub(/./u){ |c| DOWNCASE_TABLE[c] || c }
|
60
|
+
end
|
18
61
|
end
|
19
|
-
end
|
20
62
|
|
21
|
-
Dir.glob(File.join(File.dirname(__FILE__), '..', 'resources', '*.txt')) do |path|
|
22
|
-
|
63
|
+
Dir.glob(File.join(File.dirname(__FILE__), '..', 'resources', '*.txt')) do |path|
|
64
|
+
add_dictionary(path)
|
65
|
+
end
|
23
66
|
end
|
data/random_text.gemspec
CHANGED
@@ -2,19 +2,20 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{random_text}
|
5
|
-
s.version = "0.0
|
5
|
+
s.version = "1.0.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["toy"]
|
9
|
-
s.date = %q{2009-
|
9
|
+
s.date = %q{2009-10-16}
|
10
10
|
s.description = %q{A library to generate random strings.}
|
11
11
|
s.email = %q{}
|
12
|
-
s.
|
13
|
-
s.
|
12
|
+
s.executables = ["lorem", "vesna"]
|
13
|
+
s.extra_rdoc_files = ["README.rdoc", "bin/lorem", "bin/vesna", "lib/random_text.rb", "lib/random_text/dictionary.rb", "lib/random_text/random_strings.rb"]
|
14
|
+
s.files = ["Manifest", "README.rdoc", "Rakefile", "VERSION.yml", "bin/lorem", "bin/vesna", "lib/random_text.rb", "lib/random_text/dictionary.rb", "lib/random_text/random_strings.rb", "resources/lorem.txt", "resources/vesna.txt", "spec/random_text/dictionary_spec.rb", "spec/random_text/random_string_spec.rb", "spec/random_text_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "random_text.gemspec"]
|
14
15
|
s.homepage = %q{}
|
15
16
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Random_text", "--main", "README.rdoc"]
|
16
17
|
s.require_paths = ["lib"]
|
17
|
-
s.rubyforge_project = %q{
|
18
|
+
s.rubyforge_project = %q{toytoy}
|
18
19
|
s.rubygems_version = %q{1.3.5}
|
19
20
|
s.summary = %q{A library to generate random strings.}
|
20
21
|
|
@@ -23,11 +24,8 @@ Gem::Specification.new do |s|
|
|
23
24
|
s.specification_version = 3
|
24
25
|
|
25
26
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
-
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
27
27
|
else
|
28
|
-
s.add_dependency(%q<activesupport>, [">= 0"])
|
29
28
|
end
|
30
29
|
else
|
31
|
-
s.add_dependency(%q<activesupport>, [">= 0"])
|
32
30
|
end
|
33
31
|
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
WORD_R = /^\w+$/
|
4
|
+
SENTENCE_RS = '\w+([,;]?\W\w+)+[.!?]'
|
5
|
+
SENTENCE_R = /^#{SENTENCE_RS}$/
|
6
|
+
PARAGRAPH_R = /^(#{SENTENCE_RS} ){4}#{SENTENCE_RS}$/
|
7
|
+
|
8
|
+
Dictionary = RandomText::Dictionary
|
9
|
+
describe Dictionary do
|
10
|
+
before :each do
|
11
|
+
@text = %Q{
|
12
|
+
Typi non habent claritatem insitam, est usus legentis in iis qui facit eorum claritatem.
|
13
|
+
Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius!
|
14
|
+
Claritas est etiam processus dynamicus, qui sequitur Brahmes mutationem consuetudium lectorum?
|
15
|
+
Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
|
16
|
+
}
|
17
|
+
@lorem = Dictionary.new(@text)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "getting word(s)" do
|
21
|
+
|
22
|
+
it "should return word" do
|
23
|
+
@lorem.word.should =~ WORD_R
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return n words" do
|
27
|
+
words = @lorem.words(5)
|
28
|
+
words.length.should == 5
|
29
|
+
words.each{ |word| word.should =~ WORD_R }
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return all words" do
|
33
|
+
words = @lorem.words
|
34
|
+
words.length.should == 39
|
35
|
+
words.each{ |word| word.should =~ WORD_R }
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return n uniq words" do
|
39
|
+
words = @lorem.uniq_words(30)
|
40
|
+
words.length.should == 30
|
41
|
+
words.uniq.should == words
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return all uniq words (it is same as all words)" do
|
45
|
+
words = @lorem.uniq_words
|
46
|
+
words.length.should == 39
|
47
|
+
words.uniq.should == words
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "getting sentence(s)" do
|
52
|
+
it "should return sentence" do
|
53
|
+
@lorem.sentence.should =~ SENTENCE_R
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return n sentences" do
|
57
|
+
sentences = @lorem.sentences(10)
|
58
|
+
sentences.length.should == 10
|
59
|
+
sentences.each{ |sentence| sentence.should =~ SENTENCE_R }
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return all sentences" do
|
63
|
+
sentences = @lorem.sentences
|
64
|
+
sentences.length.should == 4
|
65
|
+
sentences.each{ |sentence| sentence.should =~ SENTENCE_R }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "paragraph(s)" do
|
70
|
+
it "should return paragraph" do
|
71
|
+
@lorem.paragraph.should =~ PARAGRAPH_R
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should return n paragraphs" do
|
75
|
+
paragraphs = @lorem.paragraphs(10)
|
76
|
+
paragraphs.length.should == 10
|
77
|
+
paragraphs.each{ |paragraph| paragraph.should =~ PARAGRAPH_R }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "binaries" do
|
83
|
+
it "should return 1 paragraph by default" do
|
84
|
+
IO.popen('bin/lorem', &:read).strip.should =~ PARAGRAPH_R
|
85
|
+
end
|
86
|
+
|
87
|
+
{
|
88
|
+
%w(paragraph paragraphs p para) => PARAGRAPH_R,
|
89
|
+
%w(sentence sentences s sent) => SENTENCE_R,
|
90
|
+
%w(word words w) => WORD_R,
|
91
|
+
%w(uniq_word uniq_words u uniq) => WORD_R,
|
92
|
+
}.each do |specifiers, reg|
|
93
|
+
specifiers.each do |specifier|
|
94
|
+
describe "with specifier #{specifier}" do
|
95
|
+
it "should return #{specifiers[0]}" do
|
96
|
+
IO.popen("bin/lorem #{specifier}", &:read).strip.should =~ reg
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should return 5 #{specifiers[1]}" do
|
100
|
+
lines = IO.popen("bin/lorem 5 #{specifier}", &:readlines).map(&:strip)
|
101
|
+
lines.length.should == 5
|
102
|
+
lines.each{ |line| line.should =~ reg }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -1,65 +1,64 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
RandomStrings = RandomText::RandomStrings
|
4
|
+
describe RandomStrings do
|
5
|
+
before :each do
|
6
|
+
@words = %w(Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua lol)
|
7
|
+
@random_words = RandomStrings.new(@words)
|
8
|
+
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
it "should be equal to initialization array" do
|
11
|
+
@random_words.should == @words
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
14
|
+
describe "with get" do
|
15
|
+
it "should return word" do
|
16
|
+
@random_words.get.should =~ /^\w+$/
|
18
17
|
end
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
describe "with get(n)" do
|
21
|
+
it "should return words" do
|
22
|
+
@random_words.get(10).all?{ |w| w.should =~ /^\w+$/ }
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
end
|
25
|
+
it "should return n words" do
|
26
|
+
@random_words.get(10).length.should == 10
|
28
27
|
end
|
28
|
+
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
describe "with get(:all)" do
|
31
|
+
it "should return words" do
|
32
|
+
@random_words.get(:all).all?{ |w| w.should =~ /^\w+$/ }
|
33
|
+
end
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
it "should return all words" do
|
36
|
+
@random_words.get(:all).sort.should == @words.sort
|
37
|
+
end
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
end
|
39
|
+
it "should return shuffled words" do
|
40
|
+
@random_words.get(:all).should_not == @random_words.get(:all)
|
42
41
|
end
|
42
|
+
end
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
describe "with uniq(n)" do
|
45
|
+
it "should return words" do
|
46
|
+
@random_words.uniq(10).all?{ |w| w.should =~ /^\w+$/ }
|
47
|
+
end
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
it "should return n words" do
|
50
|
+
@random_words.uniq(10).length.should == 10
|
51
|
+
end
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
end
|
53
|
+
it "should return n uniq words" do
|
54
|
+
@random_words.uniq(10).uniq.length.should == 10
|
56
55
|
end
|
56
|
+
end
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
58
|
+
describe "with uniq(:all)" do
|
59
|
+
it "should call get(:all)" do
|
60
|
+
@random_words.should_receive(:get).with(:all).and_return(:uniq_words)
|
61
|
+
@random_words.uniq(:all).should == :uniq_words
|
63
62
|
end
|
64
63
|
end
|
65
64
|
end
|
data/spec/random_text_spec.rb
CHANGED
@@ -1,9 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
3
|
|
3
4
|
describe RandomText do
|
4
5
|
it "should create RandomText objects for each text file" do
|
5
6
|
Dir.glob(File.join(File.dirname(__FILE__), '..', 'resources', '*.txt')) do |path|
|
6
|
-
RandomText.const_get(File.basename(path, File.extname(path))
|
7
|
+
RandomText.const_get(RandomText.classify(File.basename(path, File.extname(path)))).class.should == RandomText::Dictionary
|
7
8
|
end
|
8
9
|
end
|
10
|
+
|
11
|
+
it "should classify" do
|
12
|
+
RandomText.classify('hello_world').should == 'HelloWorld'
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should downcase" do
|
16
|
+
RandomText.downcase('ABCDEFGHIJKLMNOPQRSTUVWXYZ-АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ:abcdefghijklmnopqrstuvwxyz-абвгдеёжзийклмнопрстуфхцчшщъыьэюя').should ==
|
17
|
+
'abcdefghijklmnopqrstuvwxyz-абвгдеёжзийклмнопрстуфхцчшщъыьэюя:abcdefghijklmnopqrstuvwxyz-абвгдеёжзийклмнопрстуфхцчшщъыьэюя'
|
18
|
+
end
|
9
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random_text
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- toy
|
@@ -9,42 +9,38 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-10-16 00:00:00 +04:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
name: activesupport
|
17
|
-
type: :runtime
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: "0"
|
24
|
-
version:
|
14
|
+
dependencies: []
|
15
|
+
|
25
16
|
description: A library to generate random strings.
|
26
17
|
email: ""
|
27
|
-
executables:
|
28
|
-
|
18
|
+
executables:
|
19
|
+
- lorem
|
20
|
+
- vesna
|
29
21
|
extensions: []
|
30
22
|
|
31
23
|
extra_rdoc_files:
|
32
24
|
- README.rdoc
|
25
|
+
- bin/lorem
|
26
|
+
- bin/vesna
|
33
27
|
- lib/random_text.rb
|
28
|
+
- lib/random_text/dictionary.rb
|
34
29
|
- lib/random_text/random_strings.rb
|
35
|
-
- lib/random_text/random_text.rb
|
36
30
|
files:
|
37
31
|
- Manifest
|
38
32
|
- README.rdoc
|
39
33
|
- Rakefile
|
40
34
|
- VERSION.yml
|
35
|
+
- bin/lorem
|
36
|
+
- bin/vesna
|
41
37
|
- lib/random_text.rb
|
38
|
+
- lib/random_text/dictionary.rb
|
42
39
|
- lib/random_text/random_strings.rb
|
43
|
-
- lib/random_text/random_text.rb
|
44
40
|
- resources/lorem.txt
|
45
41
|
- resources/vesna.txt
|
42
|
+
- spec/random_text/dictionary_spec.rb
|
46
43
|
- spec/random_text/random_string_spec.rb
|
47
|
-
- spec/random_text/random_text_spec.rb
|
48
44
|
- spec/random_text_spec.rb
|
49
45
|
- spec/spec.opts
|
50
46
|
- spec/spec_helper.rb
|
@@ -77,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
73
|
version:
|
78
74
|
requirements: []
|
79
75
|
|
80
|
-
rubyforge_project:
|
76
|
+
rubyforge_project: toytoy
|
81
77
|
rubygems_version: 1.3.5
|
82
78
|
signing_key:
|
83
79
|
specification_version: 3
|
@@ -1,38 +0,0 @@
|
|
1
|
-
module RandomText
|
2
|
-
class RandomText
|
3
|
-
def initialize(text)
|
4
|
-
@words = RandomStrings.new(text.scan(/\w{3,}/).
|
5
|
-
collect{ |w| chars(w).downcase.to_s }.
|
6
|
-
reject{ |w| w =~ /^[0-9]/ }.
|
7
|
-
uniq.map{ |w| chars(w) })
|
8
|
-
@sentences = RandomStrings.new(text.split(/[\r\n]+/).
|
9
|
-
map(&:strip).compact.
|
10
|
-
delete_if(&:blank?).uniq)
|
11
|
-
end
|
12
|
-
|
13
|
-
def chars(s)
|
14
|
-
s.respond_to?(:mb_chars) ? s.mb_chars : s.chars
|
15
|
-
end
|
16
|
-
|
17
|
-
def word
|
18
|
-
@words.get
|
19
|
-
end
|
20
|
-
|
21
|
-
def words(n = :all)
|
22
|
-
@words.get(n)
|
23
|
-
end
|
24
|
-
|
25
|
-
def uniq_words(n = :all)
|
26
|
-
@words.uniq(n)
|
27
|
-
end
|
28
|
-
alias_method :unique_words, :uniq_words
|
29
|
-
|
30
|
-
def sentence
|
31
|
-
@sentences.get
|
32
|
-
end
|
33
|
-
|
34
|
-
def sentences(n = :all)
|
35
|
-
@sentences.get(n)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
-
|
3
|
-
module RandomText
|
4
|
-
describe RandomText do
|
5
|
-
before :each do
|
6
|
-
@text = %Q{
|
7
|
-
Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem.
|
8
|
-
Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius!
|
9
|
-
Claritas est etiam processus dynamicus, qui sequitur J.Brahmes mutationem consuetudium lectorum?
|
10
|
-
Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
|
11
|
-
}
|
12
|
-
@lorem = RandomText.new(@text)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should return word" do
|
16
|
-
@lorem.word.should =~ /^\w+$/
|
17
|
-
end
|
18
|
-
|
19
|
-
it "should return n words" do
|
20
|
-
words = @lorem.words(5)
|
21
|
-
words.length.should == 5
|
22
|
-
words.each{ |word| word.should =~ /^\w+$/ }
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should return all words" do
|
26
|
-
words = @lorem.words
|
27
|
-
words.length.should == 39
|
28
|
-
words.each{ |word| word.should =~ /^\w+$/ }
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should return n uniq words" do
|
32
|
-
words = @lorem.uniq_words(30)
|
33
|
-
words.length.should == 30
|
34
|
-
words.uniq.should == words
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should return all uniq words (it is same as all words)" do
|
38
|
-
words = @lorem.uniq_words
|
39
|
-
words.length.should == 39
|
40
|
-
words.uniq.should == words
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should return sentence" do
|
44
|
-
@lorem.sentence.should =~ /\w+(\W\w+)+/
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should return n sentences" do
|
48
|
-
sentences = @lorem.sentences(10)
|
49
|
-
sentences.length.should == 10
|
50
|
-
sentences.each{ |sentence| sentence.should =~ /\w+(\W\w+)+/ }
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should return all sentences" do
|
54
|
-
sentences = @lorem.sentences
|
55
|
-
sentences.length.should == 4
|
56
|
-
sentences.each{ |sentence| sentence.should =~ /\w+(\W\w+)+/ }
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|