fix: code cleanup
This commit is contained in:
@@ -12,10 +12,9 @@ import time
|
||||
|
||||
import httpx
|
||||
|
||||
BASE_URL = "http://localhost:8000"
|
||||
BASE_URL = os.environ.get("GATEWAY_URL", "http://localhost:8000")
|
||||
CORPUS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "corpus", "fonts"))
|
||||
|
||||
# PDFs to test (in priority order)
|
||||
TARGET_PDFS = [
|
||||
"utf-8.pdf",
|
||||
"vertical_text.pdf",
|
||||
@@ -106,7 +105,6 @@ def validate_pdf(client: httpx.Client, pdf_filename: str) -> dict:
|
||||
result["errors"].append(f"PDF file not found: {pdf_path}")
|
||||
return result
|
||||
|
||||
# ========== STEP 1: Upload ==========
|
||||
sub_separator(f"Step 1: Upload {pdf_filename}")
|
||||
try:
|
||||
with open(pdf_path, "rb") as f:
|
||||
@@ -131,7 +129,6 @@ def validate_pdf(client: httpx.Client, pdf_filename: str) -> dict:
|
||||
|
||||
doc_id = result["doc_id"]
|
||||
|
||||
# ========== STEP 2: Document Font Extraction ==========
|
||||
sub_separator("Step 2: Document Font Extraction")
|
||||
try:
|
||||
resp = client.get(f"/documents/{doc_id}/fonts")
|
||||
@@ -145,26 +142,21 @@ def validate_pdf(client: httpx.Client, pdf_filename: str) -> dict:
|
||||
result["doc_font_count"] = len(fonts)
|
||||
print(f" Font count: {len(fonts)}")
|
||||
|
||||
# Print each font in full
|
||||
for i, f in enumerate(fonts):
|
||||
print(f"\n Font #{i}:")
|
||||
print(f" {json.dumps(f, indent=4)}")
|
||||
|
||||
# Validate font count > 0
|
||||
if len(fonts) == 0:
|
||||
result["issues"].append("Document fonts: count is 0")
|
||||
|
||||
# Validate no empty names
|
||||
empty = check_empty_names(fonts)
|
||||
if empty:
|
||||
result["issues"].append(f"Document fonts: empty font names at indices {empty}")
|
||||
|
||||
# Validate no duplicates
|
||||
dupes = check_duplicates(fonts)
|
||||
if dupes:
|
||||
result["issues"].append(f"Document fonts: duplicate fonts: {dupes}")
|
||||
|
||||
# Validate all fields present
|
||||
for i, f in enumerate(fonts):
|
||||
field_issues = validate_font_fields(f, pdf_filename, i)
|
||||
result["issues"].extend(field_issues)
|
||||
@@ -172,7 +164,6 @@ def validate_pdf(client: httpx.Client, pdf_filename: str) -> dict:
|
||||
except Exception as e:
|
||||
result["errors"].append(f"Document fonts exception: {e}")
|
||||
|
||||
# ========== STEP 3: Page Font Extraction ==========
|
||||
sub_separator("Step 3: Page Font Extraction (page 0)")
|
||||
try:
|
||||
resp = client.get(f"/documents/{doc_id}/pages/0/fonts")
|
||||
@@ -193,7 +184,6 @@ def validate_pdf(client: httpx.Client, pdf_filename: str) -> dict:
|
||||
if len(page_fonts) == 0:
|
||||
result["issues"].append("Page fonts: count is 0")
|
||||
|
||||
# Compare with document fonts
|
||||
if result["doc_fonts"] is not None:
|
||||
doc_font_names = {f["fontName"] for f in result["doc_fonts"]}
|
||||
page_font_names = {f["fontName"] for f in page_fonts}
|
||||
@@ -201,7 +191,6 @@ def validate_pdf(client: httpx.Client, pdf_filename: str) -> dict:
|
||||
print(f"\n Document font names: {sorted(doc_font_names)}")
|
||||
print(f" Page font names: {sorted(page_font_names)}")
|
||||
|
||||
# Page fonts should be a subset of document fonts
|
||||
extra_in_page = page_font_names - doc_font_names
|
||||
if extra_in_page:
|
||||
result["issues"].append(f"Page fonts not in document fonts: {extra_in_page}")
|
||||
@@ -212,7 +201,6 @@ def validate_pdf(client: httpx.Client, pdf_filename: str) -> dict:
|
||||
except Exception as e:
|
||||
result["errors"].append(f"Page fonts exception: {e}")
|
||||
|
||||
# ========== STEP 4: Text Extraction ==========
|
||||
sub_separator("Step 4: Text Extraction (page 0)")
|
||||
try:
|
||||
resp = client.get(f"/documents/{doc_id}/pages/0/text")
|
||||
@@ -236,12 +224,10 @@ def validate_pdf(client: httpx.Client, pdf_filename: str) -> dict:
|
||||
if len(glyphs) == 0:
|
||||
result["issues"].append("Text extraction: no glyphs returned")
|
||||
else:
|
||||
# Show first 5 glyphs as samples
|
||||
print("\n First 5 glyphs (sample):")
|
||||
for i, g in enumerate(glyphs[:5]):
|
||||
print(f" Glyph #{i}: {json.dumps(g, indent=6)}")
|
||||
|
||||
# Check glyph structure
|
||||
for i, g in enumerate(glyphs):
|
||||
font_size = g.get("fontSize", 0)
|
||||
if font_size > 0:
|
||||
@@ -249,7 +235,6 @@ def validate_pdf(client: httpx.Client, pdf_filename: str) -> dict:
|
||||
elif font_size == 0 and i < 5:
|
||||
result["issues"].append(f"Glyph #{i}: fontSize is 0")
|
||||
|
||||
# Validate coordinate fields exist
|
||||
for coord in ["x", "y", "right", "bottom"]:
|
||||
if coord not in g and i < 3:
|
||||
result["issues"].append(f"Glyph #{i}: missing coordinate '{coord}'")
|
||||
@@ -259,7 +244,6 @@ def validate_pdf(client: httpx.Client, pdf_filename: str) -> dict:
|
||||
except Exception as e:
|
||||
result["errors"].append(f"Text extraction exception: {e}")
|
||||
|
||||
# ========== STEP 5: Content-Specific Validation ==========
|
||||
sub_separator("Step 5: Content-Specific Validation")
|
||||
|
||||
if "vertical" in pdf_filename.lower() and result["doc_fonts"]:
|
||||
@@ -289,7 +273,6 @@ def validate_pdf(client: httpx.Client, pdf_filename: str) -> dict:
|
||||
if "utf" in pdf_filename.lower() and result["text_result"]:
|
||||
text = result["text_result"].get("text", "")
|
||||
print(f" UTF-8 PDF - extracted text: {text[:300]!r}")
|
||||
# Check for non-ASCII characters
|
||||
non_ascii = [c for c in text if ord(c) > 127]
|
||||
if non_ascii:
|
||||
print(f" Non-ASCII characters found: {len(non_ascii)} chars")
|
||||
@@ -311,7 +294,6 @@ def main():
|
||||
|
||||
client = httpx.Client(base_url=BASE_URL, timeout=30.0)
|
||||
|
||||
# Verify server is up
|
||||
try:
|
||||
r = client.get("/")
|
||||
print(f"\n Server status: OK ({r.status_code})")
|
||||
@@ -319,7 +301,6 @@ def main():
|
||||
print(f"\n [FATAL] Cannot connect to server: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
# Verify corpus directory
|
||||
if not os.path.isdir(CORPUS_DIR):
|
||||
print(f"\n [FATAL] Corpus directory not found: {CORPUS_DIR}")
|
||||
sys.exit(1)
|
||||
@@ -333,7 +314,6 @@ def main():
|
||||
result = validate_pdf(client, pdf)
|
||||
results.append(result)
|
||||
|
||||
# ========== FINAL REPORT ==========
|
||||
separator("FINAL REPORT")
|
||||
|
||||
for r in results:
|
||||
@@ -352,7 +332,6 @@ def main():
|
||||
for err in r["errors"]:
|
||||
print(f" XX {err}")
|
||||
|
||||
# Summary Answers
|
||||
separator("SUMMARY ANSWERS")
|
||||
|
||||
total_issues = sum(len(r["issues"]) for r in results)
|
||||
@@ -372,7 +351,6 @@ def main():
|
||||
print("\n 3. Are font sizes being extracted correctly?")
|
||||
print(f" {'YES' if font_sizes_ok else 'NO'}")
|
||||
|
||||
# Check vertical/subset
|
||||
vertical_ok = True
|
||||
subset_ok = True
|
||||
for r in results:
|
||||
|
||||
Reference in New Issue
Block a user