From 206153afea5cda1a5f7d19d089470bf9e92f53cd Mon Sep 17 00:00:00 2001 From: spark Date: Sat, 31 Oct 2026 01:05:52 +0000 Subject: [PATCH] spark: add text analyzer utility with verification test --- wizard-work/spark/test_text_analyzer.py | 34 +++++++++++++++++++ wizard-work/spark/text_analyzer.py | 44 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 wizard-work/spark/test_text_analyzer.py create mode 100644 wizard-work/spark/text_analyzer.py diff --git a/wizard-work/spark/test_text_analyzer.py b/wizard-work/spark/test_text_analyzer.py new file mode 100644 index 0000000..13e0443 --- /dev/null +++ b/wizard-work/spark/test_text_analyzer.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +"""Verification test for text_analyzer.py""" +import subprocess +import sys + +# Run the analyzer +result = subprocess.run( + ['python3', '/root/.hermes/wizard-outputs/spark/text_analyzer.py', + '/root/.hermes/wizard-outputs/spark/test_input.txt'], + capture_output=True, text=True +) + +output = result.stdout.strip() +print("Analyzer output:") +print(output) +print() + +# Verify against wc (ground truth) +lines = result.stdout.count('Lines: 1') +words = result.stdout.count('Words: 24') +chars = result.stdout.count('Characters: 125') + +print(f"Lines check: {lines > 0}") +print(f"Words check: {words > 0}") +print(f"Chars check: {chars > 0}") + +# Check exit code +assert result.returncode == 0, f"Expected exit code 0, got {result.returncode}" +assert lines > 0, "Missing Lines: 1" +assert words > 0, "Missing Words: 24" +assert chars > 0, "Missing Characters: 125" + +print("\nAll verifications passed!") +sys.exit(0) \ No newline at end of file diff --git a/wizard-work/spark/text_analyzer.py b/wizard-work/spark/text_analyzer.py new file mode 100644 index 0000000..1cfb58e --- /dev/null +++ b/wizard-work/spark/text_analyzer.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +"""Text file analyzer: count words, lines, characters.""" + +import sys +from pathlib import Path + + +def analyze_file(filepath: str) -> dict: + """Analyze a text file and return counts.""" + p = Path(filepath) + if not p.exists(): + raise FileNotFoundError(f"File not found: {filepath}") + + content = p.read_text(encoding='utf-8') + lines = content.splitlines() + + return { + 'filepath': filepath, + 'lines': len(lines), + 'words': len(content.split()), + 'characters': len(content), + 'bytes': len(content.encode('utf-8')) + } + + +def main(): + if len(sys.argv) < 2: + print("Usage: text_analyzer.py ", file=sys.stderr) + sys.exit(1) + + try: + result = analyze_file(sys.argv[1]) + print(f"File: {result['filepath']}") + print(f"Lines: {result['lines']}") + print(f"Words: {result['words']}") + print(f"Characters: {result['characters']}") + print(f"Bytes (UTF-8): {result['bytes']}") + except FileNotFoundError as e: + print(f"Error: {e}", file=sys.stderr) + sys.exit(2) + + +if __name__ == '__main__': + main() \ No newline at end of file