Inverted index: Difference between revisions

Added PowerShell
(Added PowerShell)
Line 2,232:
: (searchFor "it" "is")
-> ("file3" "file2" "file1")</pre>
 
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<lang PowerShell>function Index-File ( [string[]]$FileList )
{
# Create index hashtable, as needed
If ( -not $Script:WordIndex ) { $Script:WordIndex = @{} }
# For each file to be indexed...
ForEach ( $File in $FileList )
{
# Find any previously existing entries for this file
$ExistingEntries = $Script:WordIndex.Keys | Where { $Script:WordIndex[$_] -contains $File }
# For any previously existing entries
# Delete them (prior to reindexing the file)
ForEach ( $Key in $ExistingEntries )
{
$Script:WordIndex[$Key] = @( $Script:WordIndex[$Key] | Where { $_ -ne $File } )
}
# Get the contents of the file, split on non-alphanumeric characters, and remove duplicates
$Words = ( Get-Content $File ) -split '[^a-zA-Z\d]' | Sort -Unique
# For each word in the file...
ForEach ( $Word in $Words )
{
# If the entry for the word already exists...
If ( $Script:WordIndex[$Word] )
{
# Add the file name to the entry
$Script:WordIndex[$Word] += $File
}
Else
{
# Create a new entry
$Script:WordIndex[$Word] = @( $File )
}
}
}
}
function Find-Word ( [string]$Word )
{
return $WordIndex[$Word]
}</lang>
<lang PowerShell># Populate test files
@'
Files full of
various words.
'@ | Out-File -FilePath C:\Test\File1.txt
@'
Create an index
of words.
'@ | Out-File -FilePath C:\Test\File2.txt
@'
Use the index
to find the files.
'@ | Out-File -FilePath C:\Test\File3.txt</lang>
<lang PowerShell># Index files
Index-File C:\Test\File1.txt, C:\Test\File2.txt, C:\Test\File3.txt</lang>
Because PowerShell is a shell language, it is "a user interface to do a search". After running the script defining the functions and running a command to index the files, the user can simply run the search function at the PowerShell command prompt.
Alternatively, one could create a more complex custom UI or GUI if desired.
<lang PowerShell># Query index
Find-Word files</lang>
{{out}}
<pre>C:\Test\File1.txt
C:\Test\File3.txt</pre>
 
=={{header|Python}}==