scripts/TextStats.ps1
10.8 KB| 1 | # TextStats.ps1 - 文本统计与词频分析(PowerShell 7,离线) |
| 2 | # |
| 3 | # 统计字符/汉字/英文单词/数字串/行/段/句,估算阅读与口播时长,输出高频词榜。 |
| 4 | # 默认打印文字报告;-Json 输出 JSON;-OutFile 落盘;-InputPath 读文件。 |
| 5 | |
| 6 | [CmdletBinding()] |
| 7 | param( |
| 8 | [string]$InputText, |
| 9 | [string]$InputPath, |
| 10 | [int]$Top = 10, |
| 11 | [double]$ReadCpm = 300, |
| 12 | [double]$SpeakCpm = 240, |
| 13 | [int]$MinWordLength = 2, |
| 14 | [string]$OutFile, |
| 15 | [switch]$Json |
| 16 | ) |
| 17 | |
| 18 | $ErrorActionPreference = 'Stop' |
| 19 | |
| 20 | function Fail([string]$message, [int]$code) { |
| 21 | [Console]::Error.WriteLine("错误:$message") |
| 22 | exit $code |
| 23 | } |
| 24 | |
| 25 | function Format-Duration([double]$minutes) { |
| 26 | $seconds = [int][Math]::Round($minutes * 60) |
| 27 | if ($seconds -le 0) { return '不到 1 秒' } |
| 28 | $h = [int][Math]::Floor($seconds / 3600) |
| 29 | $m = [int][Math]::Floor(($seconds % 3600) / 60) |
| 30 | $s = [int]($seconds % 60) |
| 31 | if ($h -gt 0) { return "$h 小时 $m 分 $s 秒" } |
| 32 | if ($m -gt 0) { return "$m 分 $s 秒" } |
| 33 | return "$s 秒" |
| 34 | } |
| 35 | |
| 36 | # ---------- 参数校验 ---------- |
| 37 | $hasText = $PSBoundParameters.ContainsKey('InputText') |
| 38 | $hasPath = $PSBoundParameters.ContainsKey('InputPath') |
| 39 | if ($hasText -and $hasPath) { Fail '不能同时提供 -InputText 和 -InputPath' 3 } |
| 40 | if (-not $hasText -and -not $hasPath) { Fail '请用 -InputText 或 -InputPath 提供文本' 3 } |
| 41 | if ($Top -lt 0) { Fail '-Top 不能为负数' 3 } |
| 42 | if ($ReadCpm -le 0 -or $SpeakCpm -le 0) { Fail '-ReadCpm 与 -SpeakCpm 必须大于 0' 3 } |
| 43 | if ($MinWordLength -lt 1) { Fail '-MinWordLength 不能小于 1' 3 } |
| 44 | |
| 45 | # ---------- 取文本 ---------- |
| 46 | $sourceLabel = '内联文本' |
| 47 | $text = '' |
| 48 | if ($hasPath) { |
| 49 | if ([string]::IsNullOrWhiteSpace($InputPath)) { Fail '-InputPath 不能为空' 3 } |
| 50 | if (-not (Test-Path -LiteralPath $InputPath -PathType Leaf)) { Fail "找不到文件:$InputPath" 3 } |
| 51 | $item = Get-Item -LiteralPath $InputPath |
| 52 | if ($item.Length -gt 20MB) { Fail '文件超过 20 MB,请先切分' 3 } |
| 53 | $text = [IO.File]::ReadAllText($item.FullName, [Text.Encoding]::UTF8) |
| 54 | $sourceLabel = $item.FullName |
| 55 | } |
| 56 | else { |
| 57 | $text = $InputText |
| 58 | } |
| 59 | |
| 60 | # ---------- 基础计数 ---------- |
| 61 | $charsTotal = 0 |
| 62 | $charsNoSpace = 0 |
| 63 | if ($text.Length -gt 0) { |
| 64 | $charsTotal = ([Globalization.StringInfo]::new($text)).LengthInTextElements |
| 65 | $compact = [regex]::Replace($text, '\s', '') |
| 66 | if ($compact.Length -gt 0) { |
| 67 | $charsNoSpace = ([Globalization.StringInfo]::new($compact)).LengthInTextElements |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $cjkMatches = [regex]::Matches($text, '[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]') |
| 72 | $cjk = $cjkMatches.Count |
| 73 | $wordMatches = [regex]::Matches($text, "[A-Za-z]+(?:['’][A-Za-z]+)*") |
| 74 | $wordCount = $wordMatches.Count |
| 75 | $numbers = [regex]::Matches($text, '\d+(?:[.,]\d+)*').Count |
| 76 | |
| 77 | if ($text.Length -eq 0) { |
| 78 | $lines = 0 |
| 79 | } |
| 80 | else { |
| 81 | $lines = ([regex]::Matches($text, "`n")).Count + 1 |
| 82 | } |
| 83 | $nonEmptyLines = [regex]::Matches($text, '(?m)^[ \t]*\S').Count |
| 84 | |
| 85 | $paragraphs = 0 |
| 86 | $sentences = 0 |
| 87 | if (-not [string]::IsNullOrWhiteSpace($text)) { |
| 88 | $blocks = [regex]::Split($text.Trim(), '\r?\n[ \t]*\r?\n') |
| 89 | foreach ($block in $blocks) { |
| 90 | if ($block.Trim() -ne '') { $paragraphs++ } |
| 91 | } |
| 92 | $segments = [regex]::Split($text, '[。!?!?]+|\.{3,}|\.(?=\s|$)|…{2,}') |
| 93 | foreach ($segment in $segments) { |
| 94 | # 只含标点/空白的片段不算句子,避免“,。!”被数成一句 |
| 95 | if ($segment -match '[A-Za-z0-9\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]') { $sentences++ } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | $avgSentence = 0 |
| 100 | if ($sentences -gt 0) { |
| 101 | $avgSentence = [Math]::Round((($cjk + $wordCount) / $sentences), 1) |
| 102 | } |
| 103 | |
| 104 | # ---------- 时长估算 ---------- |
| 105 | $readWordCpm = $ReadCpm * 0.66 |
| 106 | $speakWordCpm = $SpeakCpm * 0.66 |
| 107 | $readMinutes = ($cjk / $ReadCpm) + ($wordCount / $readWordCpm) |
| 108 | $speakMinutes = ($cjk / $SpeakCpm) + ($wordCount / $speakWordCpm) |
| 109 | |
| 110 | # ---------- 词频 ---------- |
| 111 | $stopWords = @{ |
| 112 | 'the' = 1; 'a' = 1; 'an' = 1; 'and' = 1; 'or' = 1; 'of' = 1; 'to' = 1; 'in' = 1 |
| 113 | 'is' = 1; 'are' = 1; 'was' = 1; 'were' = 1; 'be' = 1; 'been' = 1; 'being' = 1 |
| 114 | 'it' = 1; 'its' = 1; 'this' = 1; 'that' = 1; 'these' = 1; 'those' = 1 |
| 115 | 'for' = 1; 'on' = 1; 'with' = 1; 'as' = 1; 'at' = 1; 'by' = 1; 'from' = 1 |
| 116 | 'but' = 1; 'not' = 1; 'no' = 1; 'you' = 1; 'your' = 1; 'i' = 1; 'we' = 1 |
| 117 | 'our' = 1; 'they' = 1; 'their' = 1; 'he' = 1; 'she' = 1; 'his' = 1; 'her' = 1 |
| 118 | 'do' = 1; 'does' = 1; 'did' = 1; 'have' = 1; 'has' = 1; 'had' = 1 |
| 119 | 'will' = 1; 'would' = 1; 'can' = 1; 'could' = 1; 'should' = 1; 'may' = 1 |
| 120 | 'might' = 1; 'must' = 1; 'if' = 1; 'then' = 1; 'than' = 1; 'so' = 1; 'too' = 1 |
| 121 | 'very' = 1; 'just' = 1; 'all' = 1; 'any' = 1; 'some' = 1; 'one' = 1; 'also' = 1 |
| 122 | 'more' = 1; 'most' = 1; 'such' = 1; 'only' = 1; 'own' = 1; 'same' = 1 |
| 123 | 'other' = 1; 'into' = 1; 'over' = 1; 'under' = 1; 'again' = 1; 'here' = 1 |
| 124 | 'there' = 1; 'when' = 1; 'where' = 1; 'why' = 1; 'how' = 1; 'what' = 1 |
| 125 | 'which' = 1; 'who' = 1; 'whom' = 1; 'whose' = 1; 'while' = 1; 'because' = 1 |
| 126 | 'about' = 1; 'up' = 1; 'out' = 1; 'off' = 1; 'down' = 1; 'between' = 1 |
| 127 | 'through' = 1; 'during' = 1; 'before' = 1; 'after' = 1; 'above' = 1; 'below' = 1 |
| 128 | 'both' = 1; 'each' = 1; 'few' = 1; 'many' = 1; 'much' = 1; 'nor' = 1 |
| 129 | 'once' = 1; 'now' = 1; 'yet' = 1; 'still' = 1; 'even' = 1; 'never' = 1 |
| 130 | 'always' = 1; 'often' = 1; 'usually' = 1; 'however' = 1; 'therefore' = 1 |
| 131 | 'they''re' = 1 |
| 132 | } |
| 133 | |
| 134 | $stopChars = @{} |
| 135 | foreach ($ch in @('的', '了', '是', '在', '和', '与', '有', '我', '你', '他', '她', '它', '们', |
| 136 | '这', '那', '个', '上', '下', '不', '也', '就', '都', '而', '及', '或', '以', '为', |
| 137 | '被', '把', '着', '过', '之', '其', '于', '中', '要', '会', '可', '对', '到', '从', |
| 138 | '后', '前', '里', '时', '多', '大', '小', '一', '二', '三', '四', '五', '六', '七', |
| 139 | '八', '九', '十', '呢', '吗', '吧', '啊', '呀', '很', '更', '最', '又', '再', '还', |
| 140 | '只', '才', '但', '却', '因', '所', '由', '让', '给', '向', '往', '些', '每', '各', |
| 141 | '某', '本', '该', '此', '何', '谁', '哪', '怎', '么', '样', '等', '已', '曾', '正', |
| 142 | '将', '并', '且', '若', '则', '虽', '然', '如', '果', '主', '义', '地', '得')) { |
| 143 | $stopChars[$ch] = 1 |
| 144 | } |
| 145 | |
| 146 | $wordFreq = @{} |
| 147 | foreach ($m in $wordMatches) { |
| 148 | $w = $m.Value.ToLowerInvariant() |
| 149 | if ($w.Length -lt $MinWordLength) { continue } |
| 150 | if ($stopWords.ContainsKey($w)) { continue } |
| 151 | if ($wordFreq.ContainsKey($w)) { $wordFreq[$w] = $wordFreq[$w] + 1 } |
| 152 | else { $wordFreq[$w] = 1 } |
| 153 | } |
| 154 | |
| 155 | $charFreq = @{} |
| 156 | foreach ($m in $cjkMatches) { |
| 157 | $ch = $m.Value |
| 158 | if ($stopChars.ContainsKey($ch)) { continue } |
| 159 | if ($charFreq.ContainsKey($ch)) { $charFreq[$ch] = $charFreq[$ch] + 1 } |
| 160 | else { $charFreq[$ch] = 1 } |
| 161 | } |
| 162 | |
| 163 | $topWordList = @() |
| 164 | $topCharList = @() |
| 165 | if ($Top -gt 0) { |
| 166 | if ($wordFreq.Count -gt 0) { |
| 167 | $topWordList = @($wordFreq.GetEnumerator() | |
| 168 | Sort-Object -Property @{ Expression = { $_.Value }; Descending = $true }, @{ Expression = { $_.Key }; Descending = $false } | |
| 169 | Select-Object -First $Top | |
| 170 | ForEach-Object { [pscustomobject]@{ word = $_.Key; count = $_.Value } }) |
| 171 | } |
| 172 | if ($charFreq.Count -gt 0) { |
| 173 | $topCharList = @($charFreq.GetEnumerator() | |
| 174 | Sort-Object -Property @{ Expression = { $_.Value }; Descending = $true }, @{ Expression = { $_.Key }; Descending = $false } | |
| 175 | Select-Object -First $Top | |
| 176 | ForEach-Object { [pscustomobject]@{ char = $_.Key; count = $_.Value } }) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | # ---------- 输出 ---------- |
| 181 | $readSeconds = [int][Math]::Round($readMinutes * 60) |
| 182 | $speakSeconds = [int][Math]::Round($speakMinutes * 60) |
| 183 | $readText = Format-Duration $readMinutes |
| 184 | if ($readText -ne '不到 1 秒') { $readText = "约 $readText" } |
| 185 | $speakText = Format-Duration $speakMinutes |
| 186 | if ($speakText -ne '不到 1 秒') { $speakText = "约 $speakText" } |
| 187 | |
| 188 | $jsonObject = [ordered]@{ |
| 189 | source = $sourceLabel |
| 190 | chars = $charsTotal |
| 191 | charsNoSpace = $charsNoSpace |
| 192 | cjk = $cjk |
| 193 | words = $wordCount |
| 194 | numbers = $numbers |
| 195 | lines = $lines |
| 196 | nonEmptyLines = $nonEmptyLines |
| 197 | paragraphs = $paragraphs |
| 198 | sentences = $sentences |
| 199 | avgSentence = $avgSentence |
| 200 | readingSeconds = $readSeconds |
| 201 | speakingSeconds = $speakSeconds |
| 202 | topWords = $topWordList |
| 203 | topChars = $topCharList |
| 204 | } |
| 205 | |
| 206 | if ($Json) { |
| 207 | $content = ($jsonObject | ConvertTo-Json -Depth 6) |
| 208 | } |
| 209 | else { |
| 210 | $report = @() |
| 211 | $report += '== 文本统计 ==' |
| 212 | $report += "来源:$sourceLabel" |
| 213 | $report += "字符总数:$charsTotal(不含空白:$charsNoSpace)" |
| 214 | $report += "汉字:$cjk" |
| 215 | $report += "英文单词:$wordCount" |
| 216 | $report += "数字串:$numbers" |
| 217 | $report += "行数:$lines(非空:$nonEmptyLines)" |
| 218 | $report += "段落:$paragraphs" |
| 219 | $report += "句子:$sentences" |
| 220 | $report += "平均每句:$avgSentence(汉字数+英文单词数)" |
| 221 | $report += "阅读时长:$readText(按 $([Math]::Round($ReadCpm)) 汉字/分钟、$([Math]::Round($readWordCpm)) 英文词/分钟)" |
| 222 | $report += "口播时长:$speakText(按 $([Math]::Round($SpeakCpm)) 汉字/分钟、$([Math]::Round($speakWordCpm)) 英文词/分钟)" |
| 223 | if ($Top -gt 0) { |
| 224 | if ($topWordList.Count -gt 0) { |
| 225 | $report += '' |
| 226 | $report += "高频英文词 Top $($topWordList.Count):" |
| 227 | $i = 0 |
| 228 | foreach ($w in $topWordList) { |
| 229 | $i++ |
| 230 | $report += " $i. $($w.word)($($w.count) 次)" |
| 231 | } |
| 232 | } |
| 233 | else { |
| 234 | $report += '' |
| 235 | $report += "高频英文词:无(已滤掉停用词与少于 $MinWordLength 个字母的词)" |
| 236 | } |
| 237 | if ($topCharList.Count -gt 0) { |
| 238 | $report += '' |
| 239 | $report += "高频汉字 Top $($topCharList.Count):" |
| 240 | $i = 0 |
| 241 | foreach ($c in $topCharList) { |
| 242 | $i++ |
| 243 | $report += " $i. $($c.char)($($c.count) 次)" |
| 244 | } |
| 245 | } |
| 246 | else { |
| 247 | $report += '' |
| 248 | $report += '高频汉字:无(已滤掉常见虚字)' |
| 249 | } |
| 250 | } |
| 251 | $content = ($report -join "`n") |
| 252 | } |
| 253 | |
| 254 | if ($OutFile) { |
| 255 | if ([IO.Path]::IsPathRooted($OutFile)) { |
| 256 | $full = [IO.Path]::GetFullPath($OutFile) |
| 257 | } |
| 258 | else { |
| 259 | $full = [IO.Path]::GetFullPath((Join-Path (Get-Location).Path $OutFile)) |
| 260 | } |
| 261 | $dir = [IO.Path]::GetDirectoryName($full) |
| 262 | if (-not (Test-Path -LiteralPath $dir)) { |
| 263 | New-Item -ItemType Directory -Path $dir -Force | Out-Null |
| 264 | } |
| 265 | # JSON 落盘不带 BOM,方便 jq / ConvertFrom-Json 等直接解析;文字报告带 BOM,Windows 记事本友好 |
| 266 | $bom = '' |
| 267 | if (-not $Json) { $bom = [char]0xFEFF } |
| 268 | $utf8NoBom = [Text.UTF8Encoding]::new($false) |
| 269 | [IO.File]::WriteAllText($full, ($bom + $content + "`r`n"), $utf8NoBom) |
| 270 | [Console]::Out.WriteLine("已写入:$full") |
| 271 | } |
| 272 | else { |
| 273 | [Console]::Out.WriteLine($content) |
| 274 | } |
| 275 | |
| 276 | exit 0 |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

