Files
EnglishNotes/_annotate.ps1
T

82 lines
3.1 KiB
PowerShell
Raw Normal View History

# 词根盘点标注脚本:为 01/02/03 词条打 ✅(已收录)/ ⭐(新词)标注并统计
$ErrorActionPreference = 'Stop'
$base = 'D:\考研\英语'
$coveredPath = Join-Path $base '_vcb_covered.txt'
$folder = Join-Path $base '英语词汇\词根盘点'
$files = @(
'01-同根异义·一个词根十个面孔.md',
'02-形近异源·假同根与同源双词.md',
'03-同形多义·一词多义与同形异义速查.md'
)
$readme = Join-Path $folder 'README-总览与复习指引.md'
# 1. 读覆盖词表(全表 38 组 + 法律政治专题,去重 3734 词)
$covered = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
foreach ($line in [System.IO.File]::ReadAllLines($coveredPath, [System.Text.Encoding]::UTF8)) {
$w = $line.Trim()
if ($w -ne '') { [void]$covered.Add($w) }
}
# 2. 逐文件标注 + 统计
$stats = @{}
foreach ($f in $files) {
$path = Join-Path $folder $f
$text = [System.IO.File]::ReadAllText($path, [System.Text.Encoding]::UTF8)
$allWords = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
$coveredWords = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
$newWords = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
$evaluator = [System.Text.RegularExpressions.MatchEvaluator]{
param($m)
$word = $m.Groups[1].Value.Trim()
if ($word -eq '') { return $m.Value }
[void]$allWords.Add($word)
if ($covered.Contains($word)) {
[void]$coveredWords.Add($word)
return "**$word**✅"
} else {
[void]$newWords.Add($word)
return "**$word**⭐"
}
}
$newText = [regex]::Replace($text, '\{\{([^}]+)\}\}', $evaluator)
# 追加标注统计脚注(幂等:先移除旧的统计块)
$footerMark = "## 本文件标注统计(由脚本生成)"
$idx = $newText.IndexOf($footerMark)
if ($idx -ge 0) { $newText = $newText.Substring(0, $idx).TrimEnd() + "`r`n" }
$total = $allWords.Count
$cov = $coveredWords.Count
$new = $newWords.Count
$stats[$f] = @{ total = $total; covered = $cov; news = $new }
$footer = @"
$footerMark
- 词条总数(去重):$total
- ✅ 已收录(词汇全表+法律政治专题):$cov
- ⭐ 新词(建议补录):$new
"@
$newText = $newText.TrimEnd() + $footer
[System.IO.File]::WriteAllText($path, $newText, (New-Object System.Text.UTF8Encoding($false)))
Write-Output ("{0} total={1} covered={2} new={3}" -f $f, $total, $cov, $new)
}
# 3. 更新 README 统计表
$rt = [System.IO.File]::ReadAllText($readme, [System.Text.Encoding]::UTF8)
foreach ($f in $files) {
$name = $f -replace '\.md$',''
$s = $stats[$f]
$row = "| $name | $($s.covered) | $($s.news) | $($s.total) |"
# 替换该文件名开头的统计行
$rt = [regex]::Replace($rt, "(?m)^\|\s*$([regex]::Escape($name))\s*\|[^\r\n]*$", $row)
}
[System.IO.File]::WriteAllText($readme, $rt, (New-Object System.Text.UTF8Encoding($false)))
Write-Output 'README stats updated.'