nickname_generator 0.0.1

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.
@@ -0,0 +1,34 @@
1
+ require 'getoptlong'
2
+
3
+ class ArgumentParser
4
+ attr_reader :data_file, :words_to_generate
5
+
6
+ def initialize
7
+ @opts = GetoptLong.new(
8
+ ["--datafile", "-d", GetoptLong::OPTIONAL_ARGUMENT],
9
+ ["--number-of-words", "-n", GetoptLong::OPTIONAL_ARGUMENT]
10
+ )
11
+ @data_file = "data.txt"
12
+ @words_to_generate = 10
13
+ end
14
+
15
+ def parse_arguments
16
+ @opts.each do |opt, arg|
17
+ case opt
18
+ when '--datafile'
19
+ @data_file = arg
20
+ when '--number-of-words'
21
+ @words_to_generate = arg
22
+ end
23
+ end
24
+ end
25
+
26
+
27
+ # def display_usage
28
+ # puts "Sample usage:"
29
+ # puts "ruby name_generator_main.rb -d <data-file>.txt -n <x>"
30
+ # puts "<data-file>.txt is the name of a file with sample names to use as input"
31
+ # puts "<x> - the number of words to generate"
32
+ # puts "both arguments are optional"
33
+ # end
34
+ end
@@ -0,0 +1,26 @@
1
+ class DataHandler
2
+ attr_reader :follower_letters, :start_pairs
3
+ def initialize
4
+ @start_pairs = []
5
+ @follower_letters = Hash.new('')
6
+ end
7
+
8
+ def read_data_file(data_file)
9
+ File.open(data_file, 'r') do |file|
10
+ chars = file.read.chomp.downcase.gsub(/\s/, ' ').chars.to_a
11
+ chars.push(chars[0], chars[1])
12
+ populate_followers_and_start_pairs(chars)
13
+ end
14
+ end
15
+
16
+ def populate_followers_and_start_pairs(chars)
17
+ (chars.length-2).times do |i|
18
+ if chars[i] =~ /\s/
19
+ @start_pairs.push(chars[i+1, 2].join)
20
+ end
21
+ @follower_letters[chars[i, 2].join]=@follower_letters[chars[i,2].join]+chars[i+2,1].join
22
+ end
23
+ end
24
+
25
+ private :populate_followers_and_start_pairs
26
+ end
@@ -0,0 +1,27 @@
1
+ class NameGenerator
2
+ def initialize(follower_letters, min_length = 3, max_length = 9)
3
+ @min_word_length = min_length
4
+ @max_word_length = max_length
5
+ @follower_letters = follower_letters
6
+ end
7
+
8
+ def generate_name(word)
9
+ last_pair = word[-2, 2]
10
+ letter = @follower_letters[last_pair].slice(rand(@follower_letters[last_pair].length), 1)
11
+ if word =~ /\s$/
12
+ return word[0, @max_word_length] unless word.length <= @min_word_length
13
+ return generate_name(word[-1, 1]+letter)
14
+ else
15
+ word = word.gsub(/^\s/, '')
16
+ return generate_name(word+letter)
17
+ end
18
+ end
19
+
20
+ def generate_names(start_pairs, count = 10)
21
+ names = []
22
+ count.times do |i|
23
+ names.push(generate_name(start_pairs[rand start_pairs.length]).capitalize)
24
+ end
25
+ return names
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nickname_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rodi Alessandro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: The library helps you building random nicknames based on a list of real
15
+ names. You can change the source and obtain nickanmes that sound simliar to the
16
+ original ones. Thanks to Chris Pound for the original Perl alghoritm and Alan Snorkin
17
+ for he ruby porting.
18
+ email:
19
+ - coorasse@gmail.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - lib/argument_parser.rb
25
+ - lib/data_handler.rb
26
+ - lib/name_generator.rb
27
+ homepage: http://www.airesis.it
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.22
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Random nickname generator
51
+ test_files: []