random_text 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-11-03
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,18 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/random_text.rb
6
+ lib/random_text/random_strings.rb
7
+ lib/random_text/random_text.rb
8
+ resources/lorem.txt
9
+ resources/vesna.txt
10
+ script/console
11
+ script/destroy
12
+ script/generate
13
+ spec/random_text/random_string_spec.rb
14
+ spec/random_text/random_text_spec.rb
15
+ spec/random_text_spec.rb
16
+ spec/spec.opts
17
+ spec/spec_helper.rb
18
+ tasks/rspec.rake
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = random_text
2
+
3
+ * http://github.com/toy/random_text/tree/master
4
+
5
+ == DESCRIPTION:
6
+
7
+ Class to make usage of http://vesna.yandex.ru/ and http://lipsum.com/ easier
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * random words and sentences
12
+
13
+ == SYNOPSIS:
14
+
15
+ RandomText::Lorem.word
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * Activesupport (to work with non latin chars)
20
+
21
+ == INSTALL:
22
+
23
+ * sudo gem install random_text
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2008 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,47 +1,22 @@
1
- require 'rubygems'
2
- require 'rake/gempackagetask'
3
- require 'rubygems/specification'
4
- require 'date'
5
-
6
- GEM = "random_text"
7
- GEM_VERSION = "0.0.2"
8
- AUTHOR = "toy"
9
- EMAIL = "ivan@workisfun.ru"
10
- HOMEPAGE = ""
11
- SUMMARY = "Class to make access to http://vesna.yandex.ru/ and http://lipsum.com/ easier"
12
-
13
- spec = Gem::Specification.new do |s|
14
- s.name = GEM
15
- s.version = GEM_VERSION
16
- s.platform = Gem::Platform::RUBY
17
- s.has_rdoc = true
18
- s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
19
- s.summary = SUMMARY
20
- s.description = s.summary
21
- s.author = AUTHOR
22
- s.email = EMAIL
23
- s.homepage = HOMEPAGE
24
-
25
- # Uncomment this to add a dependency
26
- # s.add_dependency "foo"
27
-
28
- s.require_path = 'lib'
29
- s.autorequire = GEM
30
- s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
31
- end
32
-
33
- Rake::GemPackageTask.new(spec) do |pkg|
34
- pkg.gem_spec = spec
35
- end
36
-
37
- desc "install the gem locally"
38
- task :install => [:package] do
39
- sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
40
- end
41
-
42
- desc "create a gemspec file"
43
- task :make_spec do
44
- File.open("#{GEM}.gemspec", "w") do |file|
45
- file.puts spec.to_ruby
46
- end
47
- end
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/random_text'
3
+
4
+ $hoe = Hoe.new('random_text', RandomText::VERSION) do |p|
5
+ p.developer('toy', 'email')
6
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
7
+ p.rubyforge_name = 'random-text'
8
+ p.extra_deps = [
9
+ ['activesupport','>= 2.0.2'],
10
+ ]
11
+ p.extra_dev_deps = [
12
+ ['newgem', ">= #{::Newgem::VERSION}"]
13
+ ]
14
+
15
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
16
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
17
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
18
+ p.rsync_args = '-av --delete --ignore-errors'
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+
@@ -0,0 +1,12 @@
1
+ module RandomText
2
+ class RandomStrings < Array
3
+ def get(count = nil)
4
+ count ? Array.new(count){ rand } : rand
5
+ end
6
+
7
+ def uniq(n)
8
+ raise "Dictionary has only #{length} elements (you asked for n)" if n > length
9
+ sort_by{ Kernel.rand }.slice(0, n)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,29 @@
1
+ module RandomText
2
+ class RandomText
3
+ def initialize(text)
4
+ @words = RandomStrings.new(text.scan(/\w{3,}/).collect{ |w| w.chars.downcase.to_s }.reject{ |w| w =~ /^[0-9]/ }.uniq.map(&:chars))
5
+ @sentences = RandomStrings.new(text.split(/[\r\n]+/).uniq)
6
+ end
7
+
8
+ def word
9
+ @words.get
10
+ end
11
+
12
+ def words(n)
13
+ @words.get(n)
14
+ end
15
+
16
+ def uniq_words(n)
17
+ @words.uniq(n)
18
+ end
19
+ alias_method :unique_words, :uniq_words
20
+
21
+ def sentence
22
+ @sentences.get
23
+ end
24
+
25
+ def sentences(n)
26
+ @sentences.get(n)
27
+ end
28
+ end
29
+ end
data/lib/random_text.rb CHANGED
@@ -1,50 +1,23 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
1
4
  require 'rubygems'
2
5
  require 'activesupport'
3
6
 
4
- $KCODE = "u"
7
+ $KCODE = 'u'
5
8
 
6
- class RandomText
7
- class Randomized
8
- def initialize(data)
9
- @list = data
10
- end
11
- def get
12
- s = @list.slice!(rand(@list.length))
13
- @list << s
14
- s
15
- end
16
- def unique(n)
17
- raise "Dictionary has only #{@list.length} elements (you asked for n)" if n > @list.length
18
- @list.sort{ rand - 0.5 }.slice(0, n)
19
- end
20
- end
9
+ require 'random_text/random_text'
10
+ require 'random_text/random_strings'
21
11
 
22
- def initialize(*args)
23
- data = [*args].flatten.join(' ')
24
- @words = Randomized.new(data.scan(/\w{4,}/).collect{ |w| w.chars.downcase.to_s }.uniq.map(&:chars))
25
- @sentences = Randomized.new(data.scan(/\S[^.]*\./m).uniq)
26
- end
27
-
28
- def word
29
- @words.get
30
- end
12
+ module RandomText
13
+ VERSION = '0.0.3'
31
14
 
32
- def words(n)
33
- Array.new(n){ word }.join(' ').capitalize
34
- end
35
-
36
- def unique_words(n)
37
- @words.unique(n)
38
- end
39
-
40
- def sentence
41
- @sentences.get
42
- end
43
-
44
- def sentences(n)
45
- Array.new(n){ sentence }.join(' ')
15
+ def self.add_dictionary(path)
16
+ const_name = File.basename(path, File.extname(path)).classify
17
+ self.const_set(const_name, RandomText.new(File.read(path)))
46
18
  end
47
19
  end
48
20
 
49
- require 'lorem'
50
- require 'vesna'
21
+ Dir.glob(File.join(File.dirname(__FILE__), '..', 'resources', '*.txt')) do |path|
22
+ RandomText.add_dictionary(path)
23
+ end