Powershell, kind of inline intersection inline?

For some kind of game where it would be necessary to find anagrams from a handful of scattered letters, I ended up implementing a permutation algorithm to find all possible anagrams and filter them out if necessary for known letter positions ( -match fine, by the way). But for longer words, this turned out to be very error prone, since dropping a large list of gibberish does not really show the correct words that were hidden inside.

So I thought that if I had a large list of English words (it should be available somewhere), I could just cross the permutation list with the list of correct words and get (hopefully) all real words from the permutation list.

Since many operators in PS work differently with collections, I thought I could just do something like

 $wordlist -contains $permlist 

and get the intersection back. Unfortunately, this is not so simple. Other options I was thinking about are to -contains over one list and do -contains for each item:

 $permlist | ? { $wordlist -contains $_ } 

Maybe this will work, but very slowly, I think (especially when $wordlist is the result of gc wordlist.txt ). Or I could build a giant regex:

 $wordlist -matches (($permlist | %{ "^$_`$" }) -join "|") 

But this is probably not very fast either. I could also use findstr with over a gigantic regex, but this is simply wrong.

Are there any built-in solutions that I could use that are better than my attempts? Otherwise, I would probably put the list of words in the hash table and use the iterative -contains approach, which should be fast enough.

+6
set powershell intersection
source share
2 answers
 $left = New-HashSet string $left.Add("foo") $left.Add("bar") $right = New-HashSet string $right.Add("bar") $right.Add("baz") $left.IntersectWith($right) $left.UnionWith($right) 

(borrowing New-HashSet from Josh Einstein )

Warning: these methods in HashSet are built-in algorithms that modify the original collection. If you need to convert a functional style to immutable objects, you need to bring LINQ to the side:

 add-type system.core $asqueryable = [system.linq.queryable].getmethods() | ? { $_.name -eq "AsQueryable" } | select -first 1 $asqueryable = $asqueryable.MakeGenericMethod([string]) $leftAsQueryable = $asqueryable.Invoke($null, (,$left)) $intersect = [system.linq.queryable].getmethods() | ? { $_.name -eq "Intersect" } | select -first 1 $intersect = $intersect.MakeGenericMethod([string]) $result = $intersect.Invoke($null, ($leftAsQueryable, $right)) 

It's clear that someone needs to wrap this shit from a static generator into a friendly cmdlet! Don’t worry, I’m working on it ...

+6
source share

You can check your word list and eliminate all spelling errors regarding the standard dictionary.

With GNU aspell installed

  cat text.txt | aspell list 

will provide you with a list of all the missing words.
You can work with other dictionaries using aspell .


Or just grab an anagram generator like this made for Scrabble players .

The word "word revolution" has two options; Anagram Finder and Scrabble Solver. Anagram Finder accepts a list of letters and returns all valid anagrams that can be created using their relatively fixed list of words. Each anagram is validated against the SOWPODS word list, which is the word list used in current international Scrabble tournaments.

0
source share

All Articles