-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_whdload_details.ps1
460 lines (357 loc) · 13.2 KB
/
build_whdload_details.ps1
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
# Build WhdLoad Details
# ---------------------
#
# Author: Henrik Nørfjand Stengaard
# Date: 2017-12-19
#
# A PowerShell script to build whdload details by finding exact and best matching detail items from multiple sources.
# Lucene is used to query and find best matching detail items using keywords built from detail item id, name, publisher and languages.
using assembly Lucene.Net.dll
using namespace System.IO
using namespace Lucene.Net.Analysis
using namespace Lucene.Net.Analysis.Standard
using namespace Lucene.Net.Documents
using namespace Lucene.Net.Index
using namespace Lucene.Net.QueryParsers
using namespace Lucene.Net.Store
using namespace Lucene.Net.Util
using namespace Lucene.Net.Search
Param(
[Parameter(Mandatory=$true)]
[string]$entriesFile,
[Parameter(Mandatory=$true)]
[string]$detailsSourcesFile,
[Parameter(Mandatory=$true)]
[int32]$minScore,
[Parameter(Mandatory=$true)]
[string]$entriesDetailsFile,
[Parameter(Mandatory=$false)]
[switch]$noExactEntryNameMatching,
[Parameter(Mandatory=$false)]
[switch]$noExactWhdloadSlaveNameMatching,
[Parameter(Mandatory=$false)]
[switch]$noExactFilteredNameMatching
)
# lucene
$analyzer = [StandardAnalyzer]::new("LUCENE_CURRENT")
$directory = [RAMDirectory]::new()
# index items
function IndexItems($items)
{
$writer = [IndexWriter]::new($directory,$analyzer,$true,[IndexWriter+MaxFieldLength]::new(25000))
foreach ($item in $items)
{
$document = [Document]::new()
$document.Add([Field]::new("Keywords",$item._Keywords,"YES","ANALYZED"))
$document.Add([Field]::new("Data",($item | ConvertTo-Json),"YES","NOT_ANALYZED"))
$writer.AddDocument($document)
}
$writer.close()
}
# search items
function SearchItems($q)
{
if (!$q)
{
return $null
}
if ($q -notmatch ' 1\s*')
{
$q += ' 1'
}
try
{
$searcher = [IndexSearcher]::new($directory, $true)
$parser = [QueryParser]::new("LUCENE_CURRENT", "Keywords", $analyzer)
$query = $parser.Parse($q)
$result = $searcher.Search($query, $null, 100)
$hits = $result.ScoreDocs
$results = @()
foreach($hit in $hits)
{
$document = $searcher.Doc($hit.doc)
$data = $document.Get("Data") | ConvertFrom-Json
$results += , @{ "Score" = $hit.score; "Item" = $data }
}
}
catch
{
Write-Error "Failed to search items with query '$q': $($_.Exception.Message)"
}
return $results
}
# get index name from first character in name
function GetIndexName($name)
{
$name = $name -replace '^[^a-z0-9]+', ''
if ($name -match '^[0-9]')
{
$indexName = "0"
}
else
{
$indexName = $name.Substring(0,1)
}
return $indexName
}
function FindBestMatchingItems($query)
{
$screenshots = $null
$strictQuery = [string]::join(' ', ($query -split ' ' | % { "+{0}*" -f $_ }))
$simpleQuery = [string]::join(' ', ($query -split ' ' | % { "{0}*" -f $_ }))
$bestMatchingItems = @()
$bestMatchingItems += SearchItems $strictQuery
$bestMatchingItems += SearchItems $simpleQuery
$bestMatchingItems += SearchItems $query
if ($minScore)
{
$screenshots = $bestMatchingItems | Where { $_.Score -ge $minScore } | sort @{expression={$_.Score};Ascending=$false}
}
else
{
$screenshots = $bestMatchingItems | sort @{expression={$_.Score};Ascending=$false}
}
return $screenshots
}
function ConvertRoman([string]$text)
{
$text = $text -replace " vii ", " 7 " -replace " vi ", " 6 " -replace " v ", " 5 " -replace " iv ", " 4 " -replace " iii ", " 3 " -replace " ii ", " 2 " -replace " i ", " 1 "
return $text
}
function MakeKeywords([string]$text)
{
$text = " " + $text + " "
$text = $text -replace '[\.]', ''
# change odd chars to space
$text = $text -replace "[&\-_\(\):\.,!\\/+\*\?\[\]]", " "
# remove the and demo
#$text = $text -replace "the", " " -replace "demo", " "
# replace roman numbers
# $text = $text -replace " vii ", " 7 " -replace " vi ", " 6 " -replace " v ", " 5 " -replace " iv ", " 4 " -replace " iii ", " 3 " -replace " ii ", " 2 " -replace " i ", " 1 "
$text = ConvertRoman $text
# remove odd chars
$text = $text -creplace "[']", ""
# add space between number and letters, if not the character 'D'
$text = $text -replace "(\d+)([^d\d])", "`$1 `$2"
# add space before and after 3D or 4D
$text = $text -replace "([34]D)", " `$1 "
# add space between lower and upper case letters or numbers
$text = $text -creplace "([a-z])([A-Z0-9])", "`$1 `$2"
# add space between upper case letters or numbers
$text = $text -creplace "([A-Z])([0-9])", "`$1 `$2"
# replace multiple space with a single space
$text = $text -replace "\s+", " "
return $text.ToLower().Trim()
}
function RemoveDiacritics([string]$text)
{
$textFormD = $text.Normalize([System.Text.NormalizationForm]::FormD).ToCharArray()
$sb = New-Object -TypeName "System.Text.StringBuilder"
ForEach ($c in $textFormD)
{
$uc = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($c)
if ($uc -ne [System.Globalization.UnicodeCategory]::NonSpacingMark)
{
[void]$sb.Append($c)
}
}
return $sb.ToString().Normalize([System.Text.NormalizationForm]::FormC)
}
function ConvertSuperscript([string]$text)
{
$textFormKd = $text.Normalize([System.Text.NormalizationForm]::FormKD).ToCharArray()
$sb = New-Object -TypeName "System.Text.StringBuilder"
ForEach ($c in $textFormKd)
{
$uc = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($c)
if ($uc -ne [System.Globalization.UnicodeCategory]::NonSpacingMark)
{
[void]$sb.Append($c)
}
}
return $sb.ToString().Normalize([System.Text.NormalizationForm]::FormKC)
}
function PatchDiacritics([string]$text)
{
return $text -replace 'æ', 'ae' -replace 'ø', 'oe' -replace 'å', 'aa'
}
function Normalize([string]$text)
{
return RemoveDiacritics (ConvertSuperscript (PatchDiacritics $text))
}
function MakeFileName([string]$text)
{
$text = $text.ToLower()
$text = $text -replace "[^0-9a-z_\-\.']", " "
$text = $text -replace "['\.]", ""
$text = $text.Trim()
$text = $text -replace "\s+", "-"
$text = $text -replace "\-+", "-"
return $text
}
function AddDetailItemColumns($entry, $detailItem)
{
foreach($property in $detailItem.psobject.Properties)
{
if ($property.Value -eq $null)
{
continue
}
$entry | Add-Member -MemberType NoteProperty -Name ('Detail' + $property.Name) -Value $property.Value -Force
}
}
# resolve paths
$entriesFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($entriesFile)
$detailsSourcesFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($detailsSourcesFile)
$entriesDetailsFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($entriesDetailsFile)
# read whdload slaves
$entries = @()
$entries += Import-Csv -Delimiter ';' $entriesFile | Sort-Object @{expression={$_.EntryName};Ascending=$true}
# read screenshot sources
$detailSources = @()
$detailSources += Import-Csv -Delimiter ';' $detailsSourcesFile
$detailSourceIndex = @{}
# Process detail items for exact matching
for ($priority = 0; $priority -lt $detailSources.Count;$priority++)
{
$detailSource = $detailSources[$priority]
$detailsFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($detailSource.DetailsFile)
$detailItems = Import-Csv -Delimiter ';' $detailsFile
Write-Host ("Indexing " + $detailItems.Count + " detail items from '" + $detailSource.SourceName + "' for exact matching...")
$detailItemsIndex = @{}
foreach($detailItem in $detailItems)
{
$nameKeywords = MakeKeywords (Normalize $detailItem.Name)
$comparableName = $nameKeywords -replace '\s+', ''
$comparableFileName = (MakeFileName (Normalize $detailItem.Name)) -replace '[-]+', ''
$keywords = @($nameKeywords, $comparableName)
if ($detailItem.Id)
{
$keywords +=, $detailItem.Id
}
if ($detailItem.Publisher)
{
$keywords +=, (MakeKeywords (Normalize $detailItem.Publisher))
}
if ($detailItem.Groups)
{
$comparableFileName += (MakeFileName (Normalize $detailItem.Groups)) -replace '[-]+', ''
$keywords +=, (MakeKeywords (Normalize $detailItem.Groups))
}
$keywords += $comparableFileName
if ($detailItem.Languages)
{
$keywords +=, ($detailItem.Languages.ToLower())
}
$detailItem | Add-Member -MemberType NoteProperty -Name '_Keywords' -Value ([string]::Join(" ", $keywords))
# add comparableFileName
if ($detailItemsIndex.ContainsKey($comparableFileName))
{
$identicalDetailItems = $detailItemsIndex.Get_Item($comparableFileName)
}
else
{
$identicalDetailItems = @()
}
$identicalDetailItems +=, $detailItem
$detailItemsIndex.Set_Item($comparableFileName, $identicalDetailItems)
# add comparableName
if ($detailItemsIndex.ContainsKey($comparableName))
{
$identicalDetailItems = $detailItemsIndex.Get_Item($comparableName)
}
else
{
$identicalDetailItems = @()
}
$identicalDetailItems +=, $detailItem
$detailItemsIndex.Set_Item($comparableName, $identicalDetailItems)
}
Write-Host ("Done")
Write-Host ("Finding exact matching detail items from '" + $detailSource.SourceName + "'...")
foreach($entry in ($entries | Where-Object { $_.DetailMatch -eq $null }))
{
$name = $entry.EntryName.ToLower()
$detailItem = $null
$matchingDetailItems = $null
# use whdload name to get exact matching detail items
if (!$noExactEntryNameMatching -and $detailItemsIndex.ContainsKey($name))
{
$matchingDetailItems = $detailItemsIndex.Get_Item($name)
}
# use query to get exact matching detail items
if (!$matchingDetailItems)
{
$query = $entry.Query -replace '[-\s]+', ''
$matchingDetailItems = $detailItemsIndex.Get_Item($query)
}
# use filtered name to get exact matching detail items
if (!$noExactFilteredNameMatching -and !$matchingDetailItems -and $entry.FilteredName)
{
$filteredName = $entry.FilteredName.ToLower()
$matchingDetailItems = $detailItemsIndex.Get_Item($filteredName)
}
# use whdload slave name to get exact matching detail items
if (!$noExactWhdloadSlaveNameMatching -and !$matchingDetailItems -and $entry.WhdloadSlaveName)
{
$whdloadSlaveNameKeywords = (MakeKeywords (Normalize $entry.WhdloadSlaveName)) -replace '\s+', ''
$matchingDetailItems = $detailItemsIndex.Get_Item($whdloadSlaveNameKeywords)
}
# use first part of whdload name to get exact matching detail items, if it contains a question mark
if (!$matchingDetailItems -and ($name -match '&'))
{
$nameFirstPart = $name -replace '^([^&]+).+', '$1'
$matchingDetailItems = $detailItemsIndex.Get_Item($nameFirstPart)
}
# skip whdload slave, if no matching detail items exist
if (!$matchingDetailItems)
{
continue
}
# get first matching detail item
$detailItem = $matchingDetailItems | Select-Object -First 1
# add exact matching detail item match, score and source
$entry | Add-Member -MemberType NoteProperty -Name 'DetailMatch' -Value 'Exact'
$entry | Add-Member -MemberType NoteProperty -Name 'DetailScore' -Value '100'
$entry | Add-Member -MemberType NoteProperty -Name 'DetailSource' -Value $detailSource.SourceName
# add detail item columns to entry
AddDetailItemColumns $entry $detailItem
}
Write-Host ("Done")
$detailSourceIndex.Set_Item($detailSource.SourceName, $detailItems)
}
# Process detail items for best matching
for ($priority = 0; $priority -lt $detailSources.Count;$priority++)
{
$detailSource = $detailSources[$priority]
$detailItems = $detailSourceIndex.Get_Item($detailSource.SourceName)
Write-Host ("Indexing " + $detailItems.Count + " detail items from '" + $detailSource.SourceName + "' for best matching...")
IndexItems $detailItems
Write-Host ("Done")
Write-Host ("Finding best matching detail items from '" + $detailSource.SourceName + "'...")
foreach($entry in ($entries | Where-Object { $_.DetailMatch -eq $null }))
{
$matchingDetailItems = FindBestMatchingItems $entry.Query
# skip whdload slave, if no matching detail items exist
if (!$matchingDetailItems)
{
continue
}
# get first matching detail item
$firstMatchingDetailItem = $matchingDetailItems | Select-Object -First 1
$detailItem = $firstMatchingDetailItem.Item
# add best matching detail item match, score and source
$entry | Add-Member -MemberType NoteProperty -Name 'DetailMatch' -Value 'Best'
$entry | Add-Member -MemberType NoteProperty -Name 'DetailScore' -Value $firstMatchingDetailItem.Score
$entry | Add-Member -MemberType NoteProperty -Name 'DetailSource' -Value $detailSource.SourceName
# add detail item columns to entry
AddDetailItemColumns $entry $detailItem
}
Write-Host ("Done")
}
# Write number of entries that doesn't have a detail match
$entriesNoMatch = @()
$entriesNoMatch += $entries | Where-Object { $_.DetailMatch -eq $null }
Write-Host ("{0} whdload slaves doesn't have a detail match" -f $entriesNoMatch.Count)
# Write entries details file
$entries | export-csv -delimiter ';' -path $entriesDetailsFile -NoTypeInformation -Encoding UTF8