genebrand 0.1.0

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,14 @@
1
+ module Genebrand
2
+ class Command
3
+ class << self
4
+ def subclasses
5
+ @subclasses ||= []
6
+ end
7
+
8
+ def inherited(base)
9
+ subclasses << base
10
+ super(base)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,142 @@
1
+ module Genebrand
2
+ class New < Command
3
+ class << self
4
+ require 'json'
5
+
6
+ def load_command(p)
7
+ p.command(:new) do |c|
8
+ c.syntax 'new'
9
+ c.description 'Generates new brand name'
10
+ c.alias(:brand)
11
+
12
+ c.action do |args, options|
13
+ process(args, options)
14
+ end
15
+ end
16
+ end
17
+
18
+ def process(_args, _options)
19
+ puts "Hello! Let's generate some brands!".cyan
20
+ puts
21
+
22
+ brand = []
23
+ stopit = false
24
+ @gen = Genebrand::Generator.new
25
+
26
+ catch :parts_done do
27
+ loop do
28
+ choose do |menu|
29
+ menu.prompt = 'What should we add?'.yellow
30
+
31
+ menu.choice('String') do
32
+ brand.push(type: :word,
33
+ word: ask('Enter word (English, no spaces and punctiation, 1-10 symbols)')) do |q|
34
+ q.validate = /\A[a-zA-Z\d]{1,10}\z/
35
+ end
36
+ puts
37
+ end
38
+
39
+ menu.choice('Specific part of speech') { brand.push(pickpart) }
40
+
41
+ if brand.length > 1
42
+ menu.choice('Enough, show me some brands!') { throw :parts_done }
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ @gen.generate(brand)
49
+ end
50
+
51
+ def pickpart
52
+ puts
53
+
54
+ data = { type: :part }
55
+ part = ''
56
+
57
+ choose do |menu|
58
+ menu.prompt = 'What part of speech should we add?'.yellow
59
+
60
+ menu.choice("Noun (#{@gen.words['noun'].count} in db)") { part = 'noun' }
61
+ menu.choice("Plural (#{@gen.words['plur'].count} in db)") { part = 'plur' }
62
+ menu.choice("Verb participle (#{@gen.words['verb_part'].count} in db)") { part = 'verb_part' }
63
+ menu.choice("Verb transitive (#{@gen.words['verb_trans'].count} in db)") { part = 'verb_trans' }
64
+ menu.choice("Verb intransitive (#{@gen.words['verb_intrans'].count} in db)") { part = 'verb_intrans' }
65
+ menu.choice("Adjective (#{@gen.words['adj'].count} in db)") { part = 'adj' }
66
+ end
67
+
68
+ data[:part] = part
69
+
70
+ addfilters(data)
71
+ end
72
+
73
+ def check_filter_conflict(data)
74
+ filters = data[:filters]
75
+ if !filters[:minlen].nil? && !filters[:maxlen].nil?
76
+ if filters[:minlen] > filters[:maxlen]
77
+ filters[:minlen] = nil
78
+ filters[:maxlen] = nil
79
+ Genebrand::Logger.error 'Minimum length should be lesser than maximum, min and max filters was reset'
80
+ return false
81
+ end
82
+ end
83
+ true
84
+ end
85
+
86
+ def addfilters(data)
87
+ puts
88
+
89
+ data[:filters] = {} if data[:filters].nil?
90
+
91
+ catch :filter_done do
92
+ loop do
93
+ puts
94
+ choose do |menu|
95
+ menu.prompt = 'Choose filters for this words'.yellow
96
+
97
+ check_filter_conflict(data)
98
+
99
+ if data[:filters][:minlen].nil?
100
+ menu.choice('Minimum length') do
101
+ data[:filters][:minlen] = ask('Enter minimum word length (between 1 and 10)', Integer) do |q|
102
+ q.in = 1..10
103
+ end
104
+ end
105
+ end
106
+
107
+ if data[:filters][:maxlen].nil?
108
+ menu.choice('Maximum length') do
109
+ data[:filters][:maxlen] = ask('Enter maximum word length (between 1 and 10)', Integer) do |q|
110
+ q.in = 1..10
111
+ end
112
+ end
113
+ end
114
+
115
+ if data[:filters][:starts].nil?
116
+ menu.choice('Starts with') do
117
+ data[:filters][:starts] = ask('Enter symbols word should start with')
118
+ end
119
+ end
120
+
121
+ if data[:filters][:ends].nil?
122
+ menu.choice('Ends with') do
123
+ data[:filters][:ends] = ask('Enter symbols word should end with')
124
+ end
125
+ end
126
+
127
+ if data[:filters][:contains].nil?
128
+ menu.choice('Contains') do
129
+ data[:filters][:contains] = ask('Enter symbols word should contain')
130
+ end
131
+ end
132
+
133
+ menu.choice('Enough filters, next') { throw :filter_done if check_filter_conflict(data) }
134
+ end
135
+ end
136
+ end
137
+
138
+ data
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,135 @@
1
+ module Genebrand
2
+ class Generator
3
+ require 'resolv'
4
+
5
+ attr_reader :words
6
+
7
+ def initialize
8
+ @words = JSON.parse(File.read(File.join(Gem::Specification.find_by_name('genebrand').gem_dir, 'lib/data/posinfo.json')))
9
+ end
10
+
11
+ def prettyoutput(domain)
12
+ data = '['
13
+
14
+ # A bit hacky, but pretty fast method to guess domain available or not
15
+ resolv = Resolv::DNS.open
16
+ data << (resolv.getresources("#{domain}.com", Resolv::DNS::Resource::IN::NS).count == 0 ? "com".green : "com".red)
17
+ data << ' '
18
+ data << (resolv.getresources("#{domain}.new", Resolv::DNS::Resource::IN::NS).count == 0 ? "net".green : "net".red)
19
+ data << ' '
20
+ data << (resolv.getresources("#{domain}.org", Resolv::DNS::Resource::IN::NS).count == 0 ? "org".green : "org".red)
21
+ data << "]\t"
22
+ data << domain.bold
23
+
24
+ return data
25
+ end
26
+
27
+ def generate(info)
28
+ out = []
29
+
30
+ puts
31
+ puts 'Generating brands with these parameters:'.cyan
32
+ puts
33
+ i = 1
34
+
35
+ info.each do |item|
36
+ if item[:type] == :word
37
+ puts "#{i}. Word: #{item[:word]}".green
38
+ elsif item[:type] == :part
39
+ puts "#{i}. Part of speech: #{item[:part]}".green
40
+ puts 'Filters:'
41
+ item[:filters].each do |filter, value|
42
+ if filter == :minlen
43
+ puts "Minimum length: #{value}"
44
+ elsif filter == :maxlen
45
+ puts "Maximum length: #{value}"
46
+ elsif filter == :starts
47
+ puts "Starts with: #{value}"
48
+ elsif filter == :ends
49
+ puts "Ends with: #{value}"
50
+ elsif filter == :contains
51
+ puts "Contains: #{value}"
52
+ end
53
+ end
54
+ end
55
+
56
+ i += 1
57
+ end
58
+
59
+ puts
60
+ puts 'Press any key to continue'.cyan
61
+ puts
62
+
63
+ puts 'Fetching variants...'
64
+ gener = []
65
+ wordscount = 0
66
+ info.each do |item|
67
+ if item[:type] == :word
68
+ wordscount += 1
69
+ gener.push(item[:word])
70
+ elsif item[:type] == :part
71
+ parts = @words[item[:part]]
72
+ item[:filters].each do |filter, value|
73
+ if filter == :minlen
74
+ parts = parts.select { |i| i[/^.{#{value},}$/] }
75
+ elsif filter == :maxlen
76
+ parts = parts.select { |i| i[/^.{,#{value}}$/] }
77
+ elsif filter == :starts
78
+ parts = parts.select { |i| i[/^#{value}.*$/i] }
79
+ elsif filter == :ends
80
+ parts = parts.select { |i| i[/^.*#{value}$/i] }
81
+ elsif filter == :contains
82
+ parts = parts.select { |i| i[/^.*(#{value}).*$/i] }
83
+ end
84
+ end
85
+
86
+ if parts.count > 0
87
+ gener.push(parts)
88
+ else
89
+ Genebrand::Logger.warning "0 variants for that part, will be skipped"
90
+ end
91
+ end
92
+ end
93
+
94
+ approx = 1
95
+
96
+ gener.each do |item|
97
+ if item.is_a? Array
98
+ approx *= item.count
99
+ end
100
+ end
101
+
102
+ #arrdata.reverse!
103
+
104
+ puts 'Available variants: '.yellow + approx.to_s.bold
105
+ puts
106
+
107
+ puts "Whois info\tBrand"
108
+
109
+ finish = false
110
+ i = 0
111
+ while not finish
112
+ itemd = ''
113
+
114
+ gener.each_with_index do |item, index|
115
+ if item.is_a? Array
116
+ ind = rand(gener[index].count)
117
+ itemd << gener[index][ind].capitalize
118
+ else
119
+ itemd << gener[index].capitalize
120
+ end
121
+ end
122
+
123
+ puts prettyoutput(itemd)
124
+
125
+ i += 1
126
+
127
+ if i%15 == 0
128
+ puts
129
+ puts "Press any key to see next variants".cyan
130
+ gets
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,23 @@
1
+ module Genebrand
2
+ class Logger
3
+ def self.error(text)
4
+ puts "ERROR: #{text}".red
5
+ end
6
+
7
+ def self.warning(text)
8
+ puts "WARNING: #{text}".yellow
9
+ end
10
+
11
+ def self.info(text)
12
+ puts text.bold.cyan
13
+ end
14
+
15
+ def self.hint(text)
16
+ puts text.bold
17
+ end
18
+
19
+ def self.log(text)
20
+ puts text
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ module Genebrand
2
+ class PosParser
3
+ require 'json'
4
+ require 'fileutils'
5
+
6
+ def initialize
7
+ @parsed = {}
8
+ @table = {}
9
+ # Сущ
10
+ @table['N'] = @parsed['noun'] = []
11
+ # Мн. число
12
+ @table['P'] = @parsed['plur'] = []
13
+ # Глаг. прич, пер, непер
14
+ @table['V'] = @parsed['verb_part'] = []
15
+ @table['t'] = @parsed['verb_trans'] = []
16
+ @table['i'] = @parsed['verb_intrans'] = []
17
+ # Прилаг
18
+ @table['A'] = @parsed['adj'] = []
19
+ end
20
+
21
+ def parse(filename)
22
+ unless File.exist?(filename)
23
+ fail "File not found: #{filename}"
24
+ return
25
+ end
26
+
27
+ File.open(filename, 'r').each_line do |line|
28
+ data = line.split("\t")
29
+
30
+ if !is_numeric?(data[0]) && (!/\A[a-zA-Z0-9]{2,10}\z/.match(data[0]).nil?)
31
+ data[1].split('').each do |partofsp|
32
+ @table[partofsp].push(data[0]) if @table.key?(partofsp)
33
+ end
34
+ end
35
+ end
36
+
37
+ @parsed
38
+ end
39
+
40
+ def parseandsave(filename)
41
+ FileUtils.mkdir_p 'lib/data'
42
+ File.open('lib/data/posinfo.json', 'w+') { |f| f.write(parse(filename).to_json) }
43
+ end
44
+
45
+ def is_numeric?(obj)
46
+ obj.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil? ? false : true
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Genebrand
2
+ VERSION = '0.1.0'
3
+ end
data/lib/genebrand.rb ADDED
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+
3
+ def require_all(path)
4
+ glob = File.join(File.dirname(__FILE__), path, '*.rb')
5
+ Dir[glob].each do |f|
6
+ require f
7
+ end
8
+ end
9
+
10
+ require 'rubygems'
11
+ require 'colorator'
12
+ require 'highline/import'
13
+
14
+ module Genebrand
15
+ require 'genebrand/command'
16
+ require_all 'genebrand/commands'
17
+
18
+ autoload :PosParser, 'genebrand/posparser'
19
+ autoload :VERSION, 'genebrand/version'
20
+ autoload :Logger, 'genebrand/logger'
21
+ autoload :Generator, 'genebrand/generator'
22
+
23
+ class << self
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: genebrand
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Viktorov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorator
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mercenary
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: highline
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.2'
97
+ description: CLI brand names generator
98
+ email:
99
+ - andv@outlook.com
100
+ executables:
101
+ - genebrand
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - exe/genebrand
115
+ - genebrand.gemspec
116
+ - lib/data/posinfo.json
117
+ - lib/genebrand.rb
118
+ - lib/genebrand/command.rb
119
+ - lib/genebrand/commands/new.rb
120
+ - lib/genebrand/generator.rb
121
+ - lib/genebrand/logger.rb
122
+ - lib/genebrand/posparser.rb
123
+ - lib/genebrand/version.rb
124
+ homepage: https://github.com/andreyviktorov/genebrand
125
+ licenses:
126
+ - MIT
127
+ metadata:
128
+ allowed_push_host: https://rubygems.org
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.4.7
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Genebrand is a small brands generator
149
+ test_files: []