spark: add text analyzer utility with verification test
This commit is contained in:
1 parent
27eef6faae
commit
206153afea
2 files changed
+78
No files matched your search
@@ -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)
|
||||
@@ -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>", 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()
|
||||
Reference in new issue
Block a user