powershell - Powershell 中优雅的词频

标签 powershell knuth

Donald Knuth 曾经接到过编写一个计算文件词频的文字程序的任务。

Read a file of text, determine the n most frequently used words, and print out a sorted list of those words along with their frequencies.

Doug McIlroy 用几行 sh 重写了 10 页的 Pascal:

tr -cs A-Za-z '\n' |
tr A-Z a-z |
sort |
uniq -c |
sort -rn |
sed ${1}q

作为一个小练习,我将其转换为 Powershell:

(-split ((Get-Content -Raw test.txt).ToLower() -replace '[^a-zA-Z]',' ')) |
  Group-Object |
  Sort-Object -Property count -Descending |
  Select-Object -First $Args[0] |
  Format-Table count, name

我喜欢 Powershell 结合 sort | uniq -c 到单个 Group-Object 中。

第一行看起来很丑,不知道能不能写得优雅点?也许有办法以某种方式加载带有正则表达式分隔符的文件?

缩短代码的一个明显方法是使用别名,但这无助于可读性。

最佳答案

我会这样做。

PS C:\users\me> Get-Content words.txt
One one
two
two
three,three.
two;two


PS C:\users\me> (Get-Content words.txt) -Split '\W' | Group-Object

Count Name                      Group
----- ----                      -----
    2 One                       {One, one}
    4 two                       {two, two, two, two}
    2 three                     {three, three}
    1                           {}

编辑:Bruce Payette 的 Windows Powershell 的一些实际代码

# top 10 most frequent words, hash table
$s = gc songlist.txt
$s = [string]::join(" ", $s)
$words = $s.Split(" `t", [stringsplitoptions]::RemoveEmptyEntries)
$uniq = $words | sort -Unique
$words | % {$h=@{}} {$h[$_] += 1}
$frequency = $h.keys | sort {$h[$_]}
-1..-10 | %{ $frequency[$_]+" "+$h[$frequency[$_]]}

# or
$grouped = $words | group | sort count
$grouped[-1..-10]

关于powershell - Powershell 中优雅的词频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56378391/

相关文章:

windows - 自动安装 DCOM 并配置其启动设置

powershell - 跨多个服务器的PowerShell文件比较

c - 根据 Knuth [2, 41-79] 评估随机数生成器

c - 试图理解 Knuth 的排列算法

windows - 为什么这个简单的 Powershell 脚本在需要时不退出?

powershell - 如何在powershell中忽略foreach中的空值?

csv - 导出列表中的所有列及其数据值

c - C中的Knuth列表插入方法

string-matching - KMP失效函数计算

c - 内存字长异常的 "char*"(Knuth 的 MIX 架构)