Chapter 6
Activity
This is just one way of doing it. If you got something different that worked, well done. That’s the right way – one of them, anyway. According to Exercises in Programming Style, there are at least 41 others:
$TheTrial = Get-Content -Path .\thetrial.txt -Raw $StopWords = Get-Content -Path .\stopwords.txt -Raw $TrialWords = $TheTrial.Split(" ", "`t", "`n", ",","`"",".", [System. StringSplitOptions]::RemoveEmptyEntries) $Words = [System.Collections.ArrayList]@() Foreach ($Word in $TrialWords) { $LWord = $Word.ToLower() if (!($StopWords.Contains($LWord))) { $Words.Add($Word) } } $Grouped = ($Words | Group-Object | Sort-Object Count) $Grouped[-1 .. -10]
Here it is running:
Figure A.5 – The ten most frequent words in an English translation of The Trial
Let’s step through it quickly:
- Lines 1 and 2 bring our two files into PowerShell...