randomperson 0.0.19 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/CHANGELOG +2 -0
- data/Manifest +22 -2
- data/README.markdown +475 -0
- data/lib/randomperson/Loader.rb +62 -0
- data/lib/randomperson/Names/AmericanFemaleFirst.rb +10 -8
- data/lib/randomperson/Names/AmericanLast.rb +13 -10
- data/lib/randomperson/Names/AmericanMaleFirst.rb +11 -8
- data/lib/randomperson/Names/AmericanPrefix.rb +16 -18
- data/lib/randomperson/Names/AmericanSuffix.rb +26 -16
- data/lib/randomperson/Names/AncientGreekFemaleFirst.rb +12 -8
- data/lib/randomperson/Names/AncientGreekLast.rb +11 -8
- data/lib/randomperson/Names/AncientGreekMaleFirst.rb +11 -8
- data/lib/randomperson/Names/AnyLast.rb +11 -9
- data/lib/randomperson/Names/BasqueFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/BasqueLast.rb +9 -8
- data/lib/randomperson/Names/BasqueMaleFirst.rb +10 -9
- data/lib/randomperson/Names/BritishPrefix.rb +10 -16
- data/lib/randomperson/Names/BritishSuffix.rb +21 -16
- data/lib/randomperson/Names/EnglishFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/EnglishLast.rb +12 -11
- data/lib/randomperson/Names/EnglishMaleFirst.rb +10 -9
- data/lib/randomperson/Names/EnglishPrefix.rb +11 -15
- data/lib/randomperson/Names/FinnishFemaleFirst.rb +10 -8
- data/lib/randomperson/Names/FinnishLast.rb +9 -8
- data/lib/randomperson/Names/FinnishMaleFirst.rb +10 -9
- data/lib/randomperson/Names/FinnishPrefix.rb +16 -18
- data/lib/randomperson/Names/FrenchFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/FrenchLast.rb +12 -11
- data/lib/randomperson/Names/FrenchMaleFirst.rb +11 -10
- data/lib/randomperson/Names/GermanFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/GermanLast.rb +9 -8
- data/lib/randomperson/Names/GermanMaleFirst.rb +10 -9
- data/lib/randomperson/Names/ScottishFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/ScottishLast.rb +11 -10
- data/lib/randomperson/Names/ScottishMaleFirst.rb +13 -12
- data/lib/randomperson/Names/ScottishPrefix.rb +10 -16
- data/lib/randomperson/Names/SpanishFemaleFirst.rb +11 -10
- data/lib/randomperson/Names/SpanishLast.rb +15 -12
- data/lib/randomperson/Names/SpanishMaleFirst.rb +12 -11
- data/lib/randomperson/Names/SpanishPrefix.rb +17 -19
- data/lib/randomperson/Names/SwedishFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/SwedishLast.rb +10 -9
- data/lib/randomperson/Names/SwedishMaleFirst.rb +15 -14
- data/lib/randomperson/Names/ThaiFemaleFirst.rb +9 -8
- data/lib/randomperson/Names/ThaiFirst.rb +9 -8
- data/lib/randomperson/Names/ThaiLast.rb +9 -8
- data/lib/randomperson/Names/ThaiMaleFirst.rb +9 -8
- data/lib/randomperson/Names/ThaiRomanisedFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/ThaiRomanisedLast.rb +11 -10
- data/lib/randomperson/Names/ThaiRomanisedMaleFirst.rb +10 -9
- data/lib/randomperson/Names/WelshFemaleFirst.rb +9 -8
- data/lib/randomperson/Names/WelshLast.rb +11 -10
- data/lib/randomperson/Names/WelshMaleFirst.rb +9 -8
- data/lib/randomperson/Names/WelshPrefix.rb +10 -16
- data/lib/randomperson/Outputter.rb +53 -0
- data/lib/randomperson/demographic.rb +75 -0
- data/lib/randomperson/ext/Array.rb +6 -2
- data/lib/randomperson/generator.rb +54 -77
- data/lib/randomperson/name.rb +18 -13
- data/lib/randomperson/person.rb +2 -0
- data/lib/randomperson/version.rb +1 -1
- data/lib/randomperson.rb +126 -1
- data/spec/AmericanSuffix_spec.rb +37 -0
- data/spec/AnyLast_spec.rb +25 -0
- data/spec/Last_and_First_Names_spec.rb +113 -0
- data/spec/array_spec.rb +49 -0
- data/spec/demographic_spec.rb +15 -0
- data/spec/generator_spec.rb +1 -4
- data/spec/name_spec.rb +3 -2
- data/spec/randomperson_spec.rb +321 -0
- data/spec/rspec_helper.rb +22 -0
- data/spec/support/helpers.rb +13 -0
- data/spec/support/matchers/all.rb +14 -0
- data/spec/support/matchers/be_in.rb +9 -0
- data/spec/support/shared_examples/names.rb +59 -0
- data/test/benchmarks/concat_or_add.rb +30 -0
- data/test/benchmarks/my_assoc_or_select.rb +60 -0
- data.tar.gz.sig +0 -0
- metadata +35 -7
- metadata.gz.sig +2 -3
- data/README +0 -324
- data/lib/randomperson/choice.rb +0 -113
- data/spec/choice_spec.rb +0 -13
data/README
DELETED
@@ -1,324 +0,0 @@
|
|
1
|
-
=RandomPerson
|
2
|
-
|
3
|
-
A port to Ruby of Perl's Data::RandomPerson.
|
4
|
-
|
5
|
-
http://search.cpan.org/~peterhi/Data-RandomPerson-0.4/
|
6
|
-
|
7
|
-
Initially just the Names portion. In fact, probably just that. I'm not a masochist. Well, just a bit.
|
8
|
-
|
9
|
-
==Why did I write this?
|
10
|
-
|
11
|
-
Because the Faker library and a few of the other random ones are very good, but they don't have the precision I needed. I wanted believable male names between a certain age range for a football game I was writing. Producing names like Mrs Albert Wiggins wasn't really going to cut it.
|
12
|
-
|
13
|
-
And I'm a masochist.
|
14
|
-
|
15
|
-
It also has unicode characters, so it'll give your database a real test.
|
16
|
-
|
17
|
-
|
18
|
-
==Installation:
|
19
|
-
|
20
|
-
You'll need at least Ruby v1.9.1
|
21
|
-
|
22
|
-
Get the public key from http://github.com/yb66/RandomPerson/blob/master/gem-public_cert.pem and do:
|
23
|
-
|
24
|
-
gem cert --add /path/to/wherever/you/downloaded/this/to/public_cert.pem
|
25
|
-
|
26
|
-
then install it:
|
27
|
-
|
28
|
-
gem install randomperson -P HighSecurity
|
29
|
-
|
30
|
-
|
31
|
-
==USAGE:
|
32
|
-
|
33
|
-
So, to generate 1000 random people with Spanish names, between the ages of 16 and 35 with a ratio of 3 males to every 5 females:
|
34
|
-
|
35
|
-
require 'randomperson'
|
36
|
-
|
37
|
-
choice = RandomPerson::Choice.new( {gender_ratio: [3,5] , age_lower:16, age_upper:35 } )
|
38
|
-
|
39
|
-
choice.add_Spanish
|
40
|
-
|
41
|
-
g = RandomPerson::Generator.new
|
42
|
-
|
43
|
-
g.make_generator choice
|
44
|
-
|
45
|
-
people = [ ]
|
46
|
-
|
47
|
-
1000.times { people << g.generators[0].call }
|
48
|
-
|
49
|
-
10.times { |i| puts "#{people[i].first} #{people[i].last} age: #{people[i].age} born: #{people[i].dob.strftime("%d-%b-%Y")}" }
|
50
|
-
|
51
|
-
Bartolomé Andrés de Elixaeberna age: 20 born: 28-Apr-1990
|
52
|
-
Fabiana Cordero Balmaceda age: 21 born: 14-Jun-1989
|
53
|
-
Jorge Alas Albarracin age: 29 born: 16-Apr-1981
|
54
|
-
Eufemia Berlanga de Sergi age: 33 born: 25-Jan-1977
|
55
|
-
David Puig Villaroel age: 19 born: 17-Apr-1991
|
56
|
-
Alba Henchoz de Hurtado age: 30 born: 14-Feb-1980
|
57
|
-
Natalia Jara de Guevara age: 33 born: 28-Jul-1977
|
58
|
-
Tito Cresaco Campo age: 20 born: 14-Feb-1990
|
59
|
-
Susana Pitillas de Vaime age: 32 born: 12-Aug-1978
|
60
|
-
Gustavo Hierro Carbajal y Plazas age: 31 born: 08-Feb-1979
|
61
|
-
|
62
|
-
Here's an example using the Thai Romanised data:
|
63
|
-
|
64
|
-
choice = RandomPerson::Choice.new
|
65
|
-
choice.add_Thai_Romanised
|
66
|
-
g = RandomPerson::Generator.new
|
67
|
-
g.make_generator choice
|
68
|
-
people = [ ]
|
69
|
-
10.times { people << g.generators[0].call }
|
70
|
-
10.times { |i| puts "#{people[i].first} #{people[i].last} age: #{people[i].age} born: #{people[i].dob.strftime("%d-%b-%Y")}" }
|
71
|
-
|
72
|
-
Yongchaiyuth Sripituksakul Puntasrima age: 39 born: 29-Mar-1971
|
73
|
-
Sri-Patana Wattanasin age: 77 born: 04-Feb-1933
|
74
|
-
Wattana Rojjanasukchai age: 7 born: 27-Jun-2003
|
75
|
-
Ban Sukbunsung age: 85 born: 07-Sep-1925
|
76
|
-
Daeng Prasongsanti age: 65 born: 27-Mar-1945
|
77
|
-
Proi Paowsong Sriwarunyu age: 60 born: 06-May-1950
|
78
|
-
Ban Yongjaiyut age: 4 born: 06-Nov-2006
|
79
|
-
Siam Narkhirunkanok age: 15 born: 12-Aug-1995
|
80
|
-
Tong Punyawong Kadesadayurat age: 64 born: 01-Sep-1946
|
81
|
-
Sarai Wattanapanit Maneerattana age: 44 born: 21-Dec-1966
|
82
|
-
|
83
|
-
==CHOICE
|
84
|
-
|
85
|
-
The choice class sets the parameters that will be used to generate people.
|
86
|
-
|
87
|
-
choice = RandomPerson::Choice.new
|
88
|
-
|
89
|
-
is the same as
|
90
|
-
|
91
|
-
choice = RandomPerson::Choice.new( gender_ratio:[1,1] , age_lower: 0, age_upper: 100 )
|
92
|
-
|
93
|
-
So you'd end up with a population of roughly 50:50 male/female with ages from 0 to 100 years.
|
94
|
-
|
95
|
-
==LOADING NAMEFILES
|
96
|
-
|
97
|
-
choice.add_Spanish_Female would just load the SpanishFemaleFirst into choice.female
|
98
|
-
|
99
|
-
choice.add_Spanish would load:
|
100
|
-
SpanishFemaleFirst into choice.female
|
101
|
-
SpanishMaleFirst into choice.male
|
102
|
-
SpanishLast into choice.last
|
103
|
-
etc etc
|
104
|
-
|
105
|
-
or you can do things the old fashioned way
|
106
|
-
|
107
|
-
require 'namefiles/spanishfemalefirst' #obviously you need to use the path from where you are or where the script will run from
|
108
|
-
|
109
|
-
choice.female = RandomPerson::SpanishFemaleFirst.new
|
110
|
-
|
111
|
-
The rule is, put_underscores_between_the_important_words
|
112
|
-
|
113
|
-
_and_
|
114
|
-
|
115
|
-
always begin with *add_*
|
116
|
-
|
117
|
-
_and_
|
118
|
-
|
119
|
-
make sure each word is capitalised, add_male will actually pick up Fe_male_ whereas Female and Male will get what you want.
|
120
|
-
|
121
|
-
If you want EnglishLast names loaded:
|
122
|
-
|
123
|
-
choice.add_English_Last
|
124
|
-
|
125
|
-
All English files:
|
126
|
-
|
127
|
-
choice.add_English
|
128
|
-
|
129
|
-
English males:
|
130
|
-
|
131
|
-
choice.add_English_Male
|
132
|
-
|
133
|
-
|
134
|
-
Do you see how this is working? If you need to check what's loaded, have a look in the instance variables of choice (or whatever you named your Choice instance):
|
135
|
-
|
136
|
-
choice.male
|
137
|
-
choice.female
|
138
|
-
choice.last
|
139
|
-
choice.prefix
|
140
|
-
choice.suffix
|
141
|
-
|
142
|
-
==NEGATIONS
|
143
|
-
|
144
|
-
Sometimes you'll want to load something but not another, you can do this by prepending _not_ to the things you don't want. For example, to get the Thai names that are in Thai script and not romanised:
|
145
|
-
|
146
|
-
choice = RandomPerson::Choice.new
|
147
|
-
choice.add_Thai_notRomanised
|
148
|
-
|
149
|
-
or
|
150
|
-
|
151
|
-
choice.add_Thai_notRomanised_notFemale
|
152
|
-
|
153
|
-
to also get rid of the female names.
|
154
|
-
|
155
|
-
This is an experimental thing. Seems to work, but may change. You cannot do this (at the moment)
|
156
|
-
|
157
|
-
choice.notRomanised_add_Thai
|
158
|
-
|
159
|
-
*Always begin with add_*
|
160
|
-
|
161
|
-
==RATIOS VS ODDS
|
162
|
-
|
163
|
-
The default gender ratio is 1:1. The male part is the left side (or Array#first), the female is the right side (or Array#last). All ratios are given as an array i.e. [1,1] or [3,2]
|
164
|
-
|
165
|
-
A ratio of 1:1 will not necessarily give you a population with exactly half male, half female. The ratio is the odds of getting that. So a ratio of [1,3] will give a 25% chance of producing a male and a 75% chance of being a female.
|
166
|
-
|
167
|
-
If you wish to have an exact ratio within the population then create two choices each with a 0% chance of producing the other. For example:
|
168
|
-
|
169
|
-
men = RandomPerson::Choice.new( gender_ratio:[1,0] )
|
170
|
-
women = RandomPerson::Choice.new( gender_ratio:[0,1] )
|
171
|
-
...more code here...
|
172
|
-
g.make_generator men # pushes the generator onto @generators in 0 position
|
173
|
-
g.make_generator women # pushes the generator onto @generators in 1 position
|
174
|
-
25.times { people << g.generators[0].call }
|
175
|
-
75.times { people << g.generators[1].call }
|
176
|
-
|
177
|
-
This would give you a population of 25 males and 75 females. Which sounds great unless you really think about it.
|
178
|
-
|
179
|
-
==FORMATTING
|
180
|
-
|
181
|
-
Each culture has it's own conventions around names. This makes sticking some monolithic algorithm in the centre of the code to sort out how names should be displayed impossible, so each data file describes how it thinks the names should be formatted.
|
182
|
-
|
183
|
-
For example, in the EnglishLast.rb file:
|
184
|
-
|
185
|
-
@formats = {
|
186
|
-
:single => ->(n) { n.rand },
|
187
|
-
:double_barrelled_hyphenated=> ->(n) { n.rand + '-' + n.rand },
|
188
|
-
}
|
189
|
-
|
190
|
-
This tells us that there are two ways of formatting last names defined in the file. All formats are described as lambda functions in a hash, the key being a description of what it's trying to achieve.
|
191
|
-
|
192
|
-
==BIT MORE ON RATIOS
|
193
|
-
|
194
|
-
Behind the scenes, ratios like this [1,3] are converted to an array of ranges like this [0..24, 25..99], called ratiod. Just so you know for this next bit.
|
195
|
-
|
196
|
-
In the EnglishLast.rb file:
|
197
|
-
@formats_ratiod = [ (0..96), (97..99) ]
|
198
|
-
|
199
|
-
This says that the chances of a name being single barrelled is 97%, and double barrelled is 3%. I made up those figures from my own experience, but if you disagree with either the ratios or the formatting then you can change it. Either directly in the file or while running the code. It's your choice. Just make sure the numbers are right, length of arrays should be the same ( e.g. four formatting options should have a ratio with four parts like [a,b,c,d]) or it will break.
|
200
|
-
|
201
|
-
==PREFIXES AND SUFFIXES
|
202
|
-
|
203
|
-
Each generator starts by producing a person with a gender, an age and a date of birth. These are then passed on to the subsequent name portions so that a male always gets a male name and a male prefix (if you've specified a prefix file).
|
204
|
-
|
205
|
-
Suffix files will also (I hope) do a bit of checking at what's already been set, so you won't get Dr. Bobby Horliton PhD, as it should be either Dr. or PhD (apparently). Stuff like that should get caught.
|
206
|
-
|
207
|
-
==ADDING YOUR OWN NAMEFILES
|
208
|
-
|
209
|
-
My suggestion is to find the type of name that is closest (if you're looking for Spanishy names then look at the Spanish files as they'll have similar formatting rules there for free...) and copy and paste it in to a new one. It's easier that way.
|
210
|
-
|
211
|
-
These are current name files in the library:
|
212
|
-
|
213
|
-
AmericanFemaleFirst
|
214
|
-
AmericanLast
|
215
|
-
AmericanMaleFirst
|
216
|
-
AmericanPrefix
|
217
|
-
AmericanSuffix
|
218
|
-
AncientGreekFemaleFirst
|
219
|
-
AncientGreekLast
|
220
|
-
AncientGreekMaleFirst
|
221
|
-
AnyLast
|
222
|
-
BasqueFemaleFirst
|
223
|
-
BasqueLast
|
224
|
-
BasqueMaleFirst
|
225
|
-
BritishPrefix
|
226
|
-
BritishSuffix
|
227
|
-
EnglishFemaleFirst
|
228
|
-
EnglishLast
|
229
|
-
EnglishMaleFirst
|
230
|
-
EnglishPrefix
|
231
|
-
FinnishFemaleFirst
|
232
|
-
FinnishLast
|
233
|
-
FinnishMaleFirst
|
234
|
-
FinnishPrefix
|
235
|
-
FrenchFemaleFirst
|
236
|
-
FrenchLast
|
237
|
-
FrenchMaleFirst
|
238
|
-
GermanFemaleFirst
|
239
|
-
GermanLast
|
240
|
-
GermanMaleFirst
|
241
|
-
ScottishFemaleFirst
|
242
|
-
ScottishLast
|
243
|
-
ScottishMaleFirst
|
244
|
-
ScottishPrefix
|
245
|
-
SpanishFemaleFirst
|
246
|
-
SpanishLast
|
247
|
-
SpanishMaleFirst
|
248
|
-
SpanishPrefix
|
249
|
-
SwedishFemaleFirst
|
250
|
-
SwedishLast
|
251
|
-
SwedishMaleFirst
|
252
|
-
ThaiFemaleFirst
|
253
|
-
ThaiFirst
|
254
|
-
ThaiLast
|
255
|
-
ThaiMaleFirst
|
256
|
-
ThaiRomanisedFemaleFirst
|
257
|
-
ThaiRomanisedLast
|
258
|
-
ThaiRomanisedMaleFirst
|
259
|
-
WelshFemaleFirst
|
260
|
-
WelshLast
|
261
|
-
WelshMaleFirst
|
262
|
-
WelshPrefix
|
263
|
-
|
264
|
-
|
265
|
-
==ACCURACY OF NAMES AND RATIOS
|
266
|
-
|
267
|
-
I've taken bits and pieces from wherever I could get them, so if you see something is wrong then either let me know or produce a patch. I've no idea what the process is for that on GitHub but I'm sure there's a tutorial if you look for it. I'll also add you name to this readme, and worldly fame will be yours.
|
268
|
-
|
269
|
-
==TODO:
|
270
|
-
|
271
|
-
There's lots to do. Lot of repetition and ugly bits here and there, but it works so I'll get round to it when I can.
|
272
|
-
|
273
|
-
==THANKS GO TO
|
274
|
-
|
275
|
-
Peter Hickman for writing the original library in Perl that inspired this on in Ruby.
|
276
|
-
|
277
|
-
My good friends:
|
278
|
-
Johan Bergsten for helping me out with the Swedish names.
|
279
|
-
HALLOJULIA (hayl yeah!) for helping me with the German names.
|
280
|
-
Aino Rissanen for helping me with the Finnish names.
|
281
|
-
Fabiola Fernández Gutiérrez for help with the Spanish prefixes.
|
282
|
-
|
283
|
-
Special thanks to my agent, all the people at Marvel and DC for such fine comics, my wife and my mom for supporting me when no one else believed in me, but (sniff) most thanks goes to Noel Edmonds and Jeremy Kyle, for making daytime TV so creepy and inane that I rarely waste time watching it.
|
284
|
-
|
285
|
-
==THE END UNLESS YOU WANT TO READ THE LICENCE
|
286
|
-
|
287
|
-
*Iain Barnett*
|
288
|
-
|
289
|
-
==LICENCE:
|
290
|
-
|
291
|
-
It's an MIT Licence, I didn't take any code from the Perl one just names and a slight idea on how to structure things, so this ain't gonna be under the GPL. MIT is better anyway ;)
|
292
|
-
|
293
|
-
Copyright (c) 2010 Iain Barnett
|
294
|
-
|
295
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
296
|
-
of this software and associated documentation files (the "Software"), to deal
|
297
|
-
in the Software without restriction, including without limitation the rights
|
298
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
299
|
-
copies of the Software, and to permit persons to whom the Software is
|
300
|
-
furnished to do so, subject to the following conditions:
|
301
|
-
|
302
|
-
The above copyright notice and this permission notice shall be included in
|
303
|
-
all copies or substantial portions of the Software.
|
304
|
-
|
305
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
306
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
307
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
308
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
309
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
310
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
311
|
-
THE SOFTWARE.
|
312
|
-
|
313
|
-
In other words, be good.
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
data/lib/randomperson/choice.rb
DELETED
@@ -1,113 +0,0 @@
|
|
1
|
-
module RandomPerson
|
2
|
-
|
3
|
-
class Choice
|
4
|
-
require 'set'
|
5
|
-
|
6
|
-
attr_accessor :malefirst, :femalefirst, :last, :gender_ratio, :age_lower, :age_upper, :prefix, :suffix #,:age_ratio
|
7
|
-
|
8
|
-
def available_classes
|
9
|
-
@@available_classes
|
10
|
-
end
|
11
|
-
|
12
|
-
|
13
|
-
@@available_classes = Set.new
|
14
|
-
|
15
|
-
# Initialize the class with the parameters for the population you want.
|
16
|
-
# @example
|
17
|
-
# choice = RandomPerson::Choice.new( {gender_ratio: [3,5] , age_lower:16, age_upper:35 } )
|
18
|
-
def initialize( h={} )
|
19
|
-
@gender_ratio = h[:gender_ratio] || [1,1] #default
|
20
|
-
@age_lower = h[:age_lower] || 0
|
21
|
-
@age_upper = h[:age_upper] || 100
|
22
|
-
|
23
|
-
load_names
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
def load_names( patterns=['*.rb'] )
|
28
|
-
lib_dir = File.dirname(__FILE__)
|
29
|
-
patterns.each do |pat|
|
30
|
-
full_pattern = File.join( lib_dir, 'Names', pat )
|
31
|
-
Dir.glob( full_pattern ).each do |file|
|
32
|
-
@@available_classes << file
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
# tribe, gender, position
|
38
|
-
def method_missing( name, *args )
|
39
|
-
return super( name, *args ) unless name.to_s =~ /^add/
|
40
|
-
words = name.to_s.split( "_" )
|
41
|
-
words.shift #get rid of the "add"
|
42
|
-
|
43
|
-
#get the negations prepended with "not", then lop it off"
|
44
|
-
nots = words.select{ |w| w =~ /^not/ }.map {|s| s[3..-1] }
|
45
|
-
#get just the positives
|
46
|
-
words.reject! {|w| w =~ /^not/ }
|
47
|
-
|
48
|
-
#TODO: check the beginning of each word has an uc letter
|
49
|
-
|
50
|
-
#get a set of nots
|
51
|
-
n = nots.map{|word| @@available_classes.classify_true(word)}.fold(:&)
|
52
|
-
#get a set of wanteds
|
53
|
-
cs = words.map{|word| @@available_classes.classify_true(word)}.fold(:&)
|
54
|
-
|
55
|
-
cs = cs - n unless n.nil? #remove nots from wanteds
|
56
|
-
|
57
|
-
cs.each do |c|
|
58
|
-
require c
|
59
|
-
fn = File.basename( c, File.extname( c ) ) #remove the extension
|
60
|
-
fn = 'RandomPerson::Names::' + fn
|
61
|
-
addklass( fn, %w{ Male First } )
|
62
|
-
addklass( fn, %w{ Female First } )
|
63
|
-
addklass( fn, %w{ Last } )
|
64
|
-
addklass( fn, %w{ Prefix } )
|
65
|
-
addklass( fn, %w{ Suffix } )
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
|
70
|
-
def addklass( fn, ps )
|
71
|
-
if ps.map { |p| fn =~ /#{p}/ }.all?
|
72
|
-
instance_variable_set( "@#{ps.join.downcase}", qualified_const_get(fn).new )
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
#set all the nameclasses for this instance to nil
|
77
|
-
def reset_names
|
78
|
-
@suffix = nil
|
79
|
-
@prefix = nil
|
80
|
-
@last = nil
|
81
|
-
@malefirst = nil
|
82
|
-
@femalefirst = nil
|
83
|
-
end
|
84
|
-
|
85
|
-
#set all parameters and nameclasses for this instance to nil
|
86
|
-
def reset_all
|
87
|
-
reset_names
|
88
|
-
reset_parameters
|
89
|
-
end
|
90
|
-
|
91
|
-
#set the age and gender parameters for this instance to nil
|
92
|
-
def reset_parameters
|
93
|
-
@age_upper = nil
|
94
|
-
@age_lower = nil
|
95
|
-
@gender_ratio = nil
|
96
|
-
end
|
97
|
-
|
98
|
-
# while n > 0
|
99
|
-
# r = rand(rat.last.last)
|
100
|
-
# puts "n: #{n} r: #{r}"
|
101
|
-
# i = rat.index_in_range( r )
|
102
|
-
# puts "i: #{i}"
|
103
|
-
#
|
104
|
-
# person = @choices[i].call
|
105
|
-
# puts person.inspect
|
106
|
-
# @people << person
|
107
|
-
# n -= 1
|
108
|
-
# end
|
109
|
-
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
data/spec/choice_spec.rb
DELETED