Skip to content

Instantly share code, notes, and snippets.

@gatosyocora
Last active July 19, 2020 12:41
Show Gist options
  • Save gatosyocora/52b57d3cf456697b99f926825ad48407 to your computer and use it in GitHub Desktop.
Save gatosyocora/52b57d3cf456697b99f926825ad48407 to your computer and use it in GitHub Desktop.
pdfをtxtしたレポートの文字数を数えるPowerShellスクリプト
#[使い方]
# 1. ショートカットを作成する。リンク先は以下のような感じにする
# [powershell.exeの絶対パス] -ExecutionPolicy RemoteSigned -File [text_counter.ps1(このファイル)の絶対パス]
# 2. 採点するpdfファイルをAdobeAcrobatReaderDCの"ファイル(F)>テキストとして保存"でutf-8のtxtファイルに変換する
# 3. 1で作ったショートカットに2で作ったtxtファイルをD&Dする
#自動文字数検索では小節以下のタイトルも文字数に含めている
#図タイトルは文字数に含めないようにしている
#英数列は1文字ずつに分けて文字数として計算している(count = 5文字、0.1km = 5文字)
#このps1ファイルはShilf JISで保存する必要がある(そうでないと日本語を変数にいれるところで落ちる)
#また読み込まれるtxtファイルはutf-8を想定している
$reports = @()
foreach($path in $args) {
if ($path -eq "") {
Write-Host "txtファイルをドラッグ&ドロップしてください"
pause
exit
}
Write-Host $path
#検索に使う文字がshift-jisなので一度shift-jisに変換する
Get-Content -Encoding UTF8 -Path $path | Set-Content tmp.txt
$lines = @(Get-Content tmp.txt)
Write-Host $lines
Write-Host ""
# 学籍番号
[string]$schoolNumber = ""
for ($i = 0; $i -lt $lines.Count; $i++) {
if(([regex]"^(学籍番号)? *1[0-9]{6}[^0-9]*").IsMatch($lines[$i])) {
$schoolNumber = ([regex]"^(学籍番号)? *(?<number>1[0-9]{6}?)[^0-9]*").Match($lines[$i]).Groups["number"].value
break
}
}
Write-Host ("SchoolNumber: "+$schoolNumber)
# タイトル一覧を表示
for ($i = 0; $i -lt $lines.Count; $i++) {
if (([regex]"^[0-9][0-9.]*[^( |;|0-9|,|a-z|A-Z)]+").IsMatch($lines[$i])) {
Write-Host $lines[$i]
}
}
Write-Host ""
# 図一覧を表示
for ($i = 0; $i -lt $lines.Count-1; $i++) {
if ($lines[$i].StartsWith("図")) {
$figure_title = "図"
for ($j = 1; $j -lt $lines.Length-$i; $j++) {
if ($lines[$i+$j] -ne ""){
$figure_title += $lines[$i+$j]
}
else{break}
}
Write-Host $figure_title
}
}
Write-Host ""
# 章ごとの文字数を計算
#ps1ファイルの文字コードがshift-jis以外の場合、日本語を変数にいれようとすると落ちる
$sections = @("^[0-9]概要.*", "^[0-9]目的.*", "^[0-9](装置|[^(,|.)]*方法).*", "^[0-9]結果.*", "^[0-9]結論.*")
$sectionText = [string]""
$sectionCounts = [string]""
$sectionIndex = [int]0
for ($i = 0; $i -lt $lines.Count; $i++) {
if (([regex]($sections[$sectionIndex])).IsMatch($lines[$i])) {
if ($sectionIndex -ne 0) {
# 章情報の書き出し
Write-Host $sections[$sectionIndex-1]
Write-Host $sectionText
Write-Host $sectionText.Length
Write-Host ""
$sectionCounts += [string]$sectionText.Length + " "
}
$sectionText = [string]""
$sectionIndex++
if ($sectionIndex -ge $sections.Length) {break}
}
# 章の文字を追加していく
else {
# 図タイトルはカウントから除外
if ($lines[$i].StartsWith("図")) {
for ($j = 1; $j -lt $lines.Length-$i; $j++) {
if ($lines[$i+$j] -ne ""){
$lines[$i+$j] = ""
}
else{break}
}
}
else {
# 全角と半角スペースはカウントから除外
$lineText = [string]($lines[$i] -replace " +","")
$lineText = $lineText -replace " +",""
$sectionText += $lineText
}
}
}
# 関連語句
Write-Host "---関連語句---"
$relatedTerms = @("^[0-9]関連語句.*", "^[0-9. ]*FIRフィルタ.*", "^[0-9. ]*ノイズキャンセリングヘッド(ホン|フォン).*", "^[0-9. ]*.*マスキング.*", "^[0-9. ]*音声認識.*", "^.*参考文献.*")
$relatedTermsIndex = [int]0
$relatedTermsText = [string]""
for ($i = 0; $i -lt $lines.Count; $i++) {
if (([regex]($relatedTerms[$relatedTermsIndex])).IsMatch($lines[$i])) {
if ($relatedTermsIndex -gt 1) {
# 関連語句情報の書き出し
Write-Host $relatedTerms[$relatedTermsIndex-1]
Write-Host $relatedTermsText
Write-Host $relatedTermsText.Length
Write-Host ""
$sectionCounts += [string]$relatedTermsText.Length + " "
}
$relatedTermsText = [string]""
$relatedTermsIndex++
if ($relatedTermsIndex -ge $relatedTerms.Length) {break}
}
# 章の文字を追加していく
else {
# 全角と半角スペースはカウントから除外
$lineText = [string]($lines[$i] -replace " +","")
$lineText = $lineText -replace " +",""
$relatedTermsText += $lineText
}
}
Write-Host ""
# すべての文字数カウントを表示
Write-Host ($schoolNumber+": "+ $sectionCounts)
$reports += ($schoolNumber+": "+ $sectionCounts)
}
if ($args.Length -gt 1) {
Write-Host ""
$reports | ForEach-Object{Write-Host $_}
}
if ($args.Length -eq 1) {
# 任意の章範囲の文字数をカウント
while($true) {
$from = Read-Host "From"
if (($from -eq "exit") -or ($from -eq "quit") ) {break}
$to = Read-Host "To"
if (($to -eq "exit") -or ($to -eq "quit") ) {break}
for ($i = 0; $i -lt $lines.Count; $i++) {
if (([regex]("^[0-9.]+"+$from+".*")).IsMatch($lines[$i])) {
$sectionText = [string]""
}
elif (([regex]("^[0-9.]+"+$to+".*")).IsMatch($lines[$i])) {
# 章情報の書き出し
Write-Host $from
Write-Host $sectionText
Write-Host $sectionText.Length
Write-Host ""
break
}
# 章の文字を追加していく
else {
# 図タイトルはカウントから除外
if ($lines[$i].StartsWith("図")) {
for ($j = 1; $j -lt $lines.Length-$i; $j++) {
if ($lines[$i+$j] -ne ""){
$lines[$i+$j] = ""
}
else{break}
}
}
else {
# 全角と半角スペースはカウントから除外
$lineText = [string]($lines[$i] -replace " +","")
$lineText = $lineText -replace " +",""
$sectionText += $lineText
}
}
}
}
}
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment