#!/bin/bash # Determines the medium of *-un.pdf (unknown) books empirically: # OCRs two mid-book pages with the English model and counts common English # stopwords — an Urdu page OCR'd as English yields almost none. # Renames the file to -en.pdf or -ur.pdf accordingly. set -u ROOT="$(cd "$(dirname "$0")/.." && pwd)" export TESSDATA_PREFIX="$ROOT/scripts/tessdata" find "$ROOT" -name "*-un.pdf" -not -path "*/extracted-text/*" | sort | while read -r f; do pages=$(pdfinfo "$f" 2>/dev/null | awk '/^Pages:/{print $2}') mid=$(( ${pages:-20} / 2 )) tmp=$(mktemp -d) pdftoppm -gray -r 150 -f "$mid" -l $((mid+1)) "$f" "$tmp/pg" 2>/dev/null hits=0 for img in "$tmp"/pg-*; do t=$(tesseract "$img" - -l eng --psm 3 2>/dev/null) h=$(echo "$t" | tr '[:upper:]' '[:lower:]' | grep -owE "the|and|of|in|to|is|are|was|which" | wc -l) hits=$((hits + h)) done rm -rf "$tmp" if [ "$hits" -ge 10 ]; then new="${f%-un.pdf}-en.pdf"; med=English; else new="${f%-un.pdf}-ur.pdf"; med=Urdu; fi mv "$f" "$new" echo "$(basename "$f") -> $med (stopword hits: $hits)" done