-
Notifications
You must be signed in to change notification settings - Fork 28
/
NameIT.psm1
496 lines (367 loc) · 12.3 KB
/
NameIT.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<#
.Synopsis
Utilize Invoke-Generate to create a random value type
.DESCRIPTION
NameIt returns strings including unecessary zeros in numbers. Get-RandomValue returns a specified values type.
.PARAMETER Template
A Nameit template string.
[alpha]; selects a random character (constrained by the -Alphabet parameter).
[numeric]; selects a random numeric (constrained by the -Numbers parameter).
[vowel]; selects a vowel from a, e, i, o or u.
[phoneticVowel]; selects a vowel sound, for example ou.
[consonant]; selects a consonant from the entire alphabet.
[syllable]; generates (usually) a pronouncable single syllable.
[synonym word]; finds a synonym to match the provided word.
[person]; generate random name of female or male based on provided culture like <FirstName><Space><LastName>.
[person female]; generate random name of female based on provided culture like <FirstName><Space><LastName>.
[person male]; generate random name of male based on provided culture like <FirstName><Space><LastName>.
[address]; generate a random street address. Formatting is biased to US currently.
[space]; inserts a literal space. Spaces are striped from the templates string by default.
.PARAMETER Count
The number of random items to return.
.PARAMETER Alphabet
A set of alpha characters used to generate random strings.
.PARAMETER Numbers
A set of digit characters used to generate random numerics.
.EXAMPLE
PS C:\> Invoke-Generate
lhcqalmf
.EXAMPLE
PS C:\> Invoke-Generate -alphabet abc
cabccbca
.EXAMPLE
PS C:\> Invoke-Generate "cafe###"
cafe176
.EXAMPLE
PS C:\> Invoke-Generate "???###"
yhj561
.EXAMPLE
PS C:\> Invoke-Generate -count 5 "[synonym cafe]_[syllable][syllable]"
restaurant_owkub
coffeebar_otqo
eatingplace_umit
coffeeshop_fexuz
coffeebar_zuvpe
.Notes
Inspired by
http://mitchdenny.com/introducing-namerer-for-naming-things/
#>
$ApprovedVerb=$false
function Invoke-Generate {
[CmdletBinding()]
param (
[Parameter(Position=0)]
[String]
$Template = '????????',
[Parameter(Position=1)]
[int]
$Count = 1,
[Parameter(Position=2)]
[string]
$Alphabet = 'abcdefghijklmnopqrstuvwxyz',
[Parameter(Position=3)]
[string]
$Numbers = '0123456789',
[Parameter(Position=4)]
[HashTable]$CustomData,
[Switch]$ApprovedVerb,
[Switch]$AsTabDelimited,
[Switch]$AsPSObject
)
$script:alphabet = $alphabet
$script:numbers = $number
$functionList = 'alpha|synonym|numeric|syllable|vowel|phoneticvowel|consonant|person|address|space|noun|adjective|verb|cmdlet|state|dave|guid'.Split('|')
$customDataFile="$PSScriptRoot\customData\customData.ps1"
if(Test-Path $customDataFile){
$CustomData+=. $customDataFile
}
if($CustomData) {
foreach ($key in $CustomData.Keys) {
$functionList+=$key
"function $key { echo $($CustomData.$key) | Get-Random }" | Invoke-Expression
}
}
$functionList=$functionList.ToLower()
$template = $template -replace '\?', '[alpha]' -replace '#', '[numeric]'
$unitOfWork = $template -split "\[(.+?)\]" | Where-Object -FilterScript { $_ }
$ApprovedVerb=$ApprovedVerb
1..$count | ForEach-Object -Process {
$r=$($unitOfWork | ForEach-Object -Process {
$fn = $_.split(' ')[0]
if ($functionList -notcontains $fn) {
$_
}
else {
$_ | Invoke-Expression
}
}) -join ''
if($AsPSObject) {
[pscustomobject](ConvertFrom-StringData $r)
} elseif($AsTabDelimited) {
[pscustomobject](ConvertFrom-StringData $r) |
ConvertTo-Csv -Delimiter "`t" -NoTypeInformation
} else {
$r
}
}
}
<#
.Synopsis
Utilize Invoke-Generate to create a random value with a specified type.
.DESCRIPTION
NameIt returns strings including unecessary zeros in numbers. Get-RandomValue returns a specified values type.
.PARAMETER Template
A Nameit template string.
.PARAMETER As
A type name specifying the return type of the command.
.PARAMETER Alphabet
A set of alpha characters used to generate random strings.
.PARAMETER Numbers
A set of digit characters used to generate random numerics.
.EXAMPLE
PS C:\> 1..3 | % {Get-RandomValue "###.##" -as double}
75.41
439.92
195.55
.EXAMPLE
PS C:\> 1..3 | % {Get-RandomValue "#.#.#" -as version}
Major Minor Build Revision
----- ----- ----- --------
1 3 1 -1
2 2 5 -1
7 1 0 -1
#>
function Get-RandomValue
{
[CmdletBinding()]
[Alias()]
Param
(
[Parameter(Mandatory,Position=0)]
[string]
$Template,
[Parameter(Position=1)]
[Type]
$As,
[Parameter(Position=2)]
[string]
$Alphabet,
[Parameter(Position=3)]
[string]
$Numbers
)
$type = $null
if ( $PSBoundParameters.ContainsKey('As') )
{
$type = $As
}
$null = $PSBoundParameters.Remove('As')
$stringValue = Invoke-Generate @PSBoundParameters
if ( -not $null -eq $type )
{
$returnValue = $stringValue -as $type
if ($null -eq $returnValue)
{
Write-Warning "Could not cast '$stringValue' to [$($type.Name)]"
}
}
else
{
$returnValue = $stringValue
}
$returnValue
}
function Get-RandomChoice {
param (
$list,
[int]$length = 1
)
$max = $list.Length
$(
for ($i = 0; $i -lt $length; $i++) {
$list[(Get-Random -Minimum 0 -Maximum $max)]
}
) -join ''
}
$nouns=Get-Content -Path "$PSScriptRoot\cultures\en-US.nouns.txt"
$adjectives=Get-Content -Path "$PSScriptRoot\cultures\en-US.adjectives.txt"
$verbs=Get-Content -Path "$PSScriptRoot\cultures\en-US.verbs.txt"
function noun {
$nouns | Get-Random
}
function adjective {
$adjectives | Get-Random
}
function verb {
$verbs | Get-Random
}
function baseVerbNoun {
if($ApprovedVerb) {
$verb = (Get-Verb | Get-Random).verb
} else {
$verb = (verb)
}
"{0}-{1}" -f $verb,(noun)
}
function cmdlet {
baseVerbNoun
}
function address {
Param
(
[String]$Culture = "en-US"
)
$numberLength = Get-Random -Minimum 3 -Maximum 6
$streetLenth = Get-Random -Minimum 2 -Maximum 6
$houseNumber = Get-RandomValue -Template "[numeric $numberLength]" -As int
$streetTemplate = "[syllable]" * $streetLenth
$street = Invoke-Generate $streetTemplate
$suffix = Get-Content -Path "$PSScriptRoot\cultures\$Culture.streetsuffix.txt" | Get-Random
$address = $houseNumber,$street,$suffix -join ' '
(culture).TextInfo.ToTitleCase($address)
}
function space {
param (
[int]$length = 1
)
' ' * $length
}
function alpha {
param ([int]$length = 1)
Get-RandomChoice $alphabet $length
}
function numeric {
param ([int]$length = 1)
(Get-RandomChoice $numbers $length) -as [int]
}
function synonym {
param ([string]$word)
$url = "http://words.bighugelabs.com/api/2/78ae52fd37205f0bad5f8cd349409d16/$($word)/json"
$synonyms = $(foreach ($item in (Invoke-RestMethod $url)) {
$names = $item.psobject.Properties.name
foreach ($name in $names) {
$item.$name.syn -replace ' ', ''
}
}) | Where-Object -FilterScript { $_ }
$max = $synonyms.Length
$synonyms[(Get-Random -Minimum 0 -Maximum $max)]
}
function consonant { Get-RandomChoice 'bcdfghjklmnpqrstvwxyz' }
function vowel { Get-RandomChoice 'aeiou' }
function phoneticVowel {
Get-RandomChoice 'a', 'ai', 'ay', 'au', 'aw', 'augh', 'wa', 'all', 'ald', 'alk', 'alm', 'alt',
'e', 'ee', 'ea', 'eu', 'ei', 'ey', 'ew', 'eigh', 'i', 'ie', 'ye', 'igh', 'ign',
'ind', 'o', 'oo', 'oa', 'oe', 'oi', 'oy', 'old', 'olk', 'olt', 'oll', 'ost',
'ou', 'ow', 'u', 'ue', 'ui'
}
function syllable {
param ([Switch]$usePhoneticVowels)
$syllables = ((vowel) + (consonant)), ((consonant) + (vowel)), ((consonant) + (vowel) + (consonant))
if ($usePhoneticVowels) {
$syllables += ((consonant) + (phoneticVowel))
}
Get-RandomChoice $syllables
}
function person {
<#
.SYNOPSIS
The function intended to generate string with name of a random person
.DESCRIPTION
The function generate a random name of females or mailes based on provided culture like <FirstName><Space><LastName>.
The first and last names are randomly selected from the file delivered with for the culture
.PARAMETER Sex
The sex of random person.
.PARAMETER Culture
The culture used for generate - default is en-US.
.LINK
https://www.linkedin.com/in/sciesinskiwojciech
.NOTES
AUTHOR: Wojciech Sciesinski, wojciech[at]sciesinski[dot]net
KEYWORDS: PowerShell
VERSIONS HISTORY
0.1.0 - 2016-01-16 - The first version published as a part of NameIT powershell module https://github.com/dfinke/NameIT
0.1.1 - 2016-01-16 - Mistakes corrected, support for additional templates added, the paremeter Count removed
0.1.2 - 2016-01-18 - Incorrect usage Get-Random in the templates [person*] corrected, the example corrected
The future version will be developed under https://github.com/it-praktyk/New-RandomPerson
The source of last names for the en-US culture - taken the first 100 on the state 2016-01-15
http://names.mongabay.com/data/1000.html
The source of the first names for the en-US culture - taken the first 100 on the state 2016-01-15
http://www.behindthename.com/top/lists/united-states-decade/1980/100
.EXAMPLE
[PS] > person
Justin Carter
The one name returned with random sex.
.EXAMPLE
[PS] > 1..3 | Foreach-Object -Process { person -Sex 'female' }
Jacqueline Walker
Julie Richardson
Stacey Powell
Three names returned, only women.
#>
param (
[parameter(Mandatory = $false, Position = 0)]
[ValidateSet("both", "female", "male")]
[String]$Sex = "both",
[String]$Culture = "en-US"
)
$ModulePath = Split-Path $script:MyInvocation.MyCommand.Path
[String]$CultureFileName = "{0}\cultures\{1}.csv" -f $ModulePath, $Culture
$AllNames = Import-Csv -Path $CultureFileName -Delimiter ","
$AllNamesCount = ($AllNames | Measure-Object).Count
$LastName = $AllNames[(Get-Random -Minimum 0 -Maximum $AllNamesCount)].LastName
If ($Sex -eq 'both') {
$RandomSex = (Get-Random @('Female', 'Male'))
$FirstNameFieldName = "{0}FirstName" -f $RandomSex
$FirstName = $AllNames[(Get-Random -Minimum 0 -Maximum $AllNamesCount)].$FirstNameFieldName
}
elseif ($Sex -eq 'female') {
$FirstName = $AllNames[(Get-Random -Minimum 0 -Maximum $AllNamesCount)].FemaleFirstName
}
else {
$FirstName = $AllNames[(Get-Random -Minimum 0 -Maximum $AllNamesCount)].MaleFirstName
}
Return $([String]"{0} {1}" -f $FirstName, $LastName)
}
function State {
param(
$property="name",
[String]$Culture = "en-US"
)
$CultureFileName = "$($PSScriptRoot)\cultures\{0}.States.csv" -f $Culture
$states=Import-Csv $CultureFileName
switch($property) {
"name" {$property="statename"}
"abbr" {$property="abbreviation"}
"capital" {$property="capital"}
"zip" {$property="zip"}
"all" {
$targetState=$states | Get-Random
"{0},{1},{2},{3}" -f $targetState.Capital,$targetState.StateName,$targetState.Abbreviation,$targetState.Zip
}
default { throw "property [$($property)] not supported"}
}
$states | get-random | % $property
}
# Too Many Daves by Dr. Seuss
# https://www.poetryfoundation.org/poems-and-poets/poems/detail/42882
function Dave {
param(
[String]$Culture = "en-US"
)
$CultureFileName = "$($PSScriptRoot)\cultures\{0}.daves.txt" -f $Culture
$daves=Get-Content $CultureFileName
$daves | get-random
}
function guid {
param(
$part
)
$guid=[guid]::NewGuid().guid
if($part -ne $null) {
($guid -split '-')[$part]
} else {
$guid
}
}
Set-Alias ig Invoke-Generate
Export-ModuleMember -Function * -Alias *