Demonstrates core Veritext functionality: metrics, validators, composites, and constraint validators with runnable code.
136 lines
4.3 KiB
Python
136 lines
4.3 KiB
Python
"""Basic text validation examples.
|
|
|
|
Demonstrates core Veritext functionality:
|
|
- Single metric scoring (BLEU, ROUGE)
|
|
- Validator usage with thresholds
|
|
- Composite validators (all_of, any_of)
|
|
- Constraint validators (length, readability)
|
|
"""
|
|
|
|
from veritext.core.types import ValidationContext
|
|
from veritext.metrics import Bleu, Rouge
|
|
from veritext.validators import (
|
|
all_of,
|
|
any_of,
|
|
bleu,
|
|
contains,
|
|
excludes,
|
|
length,
|
|
readability,
|
|
rouge,
|
|
)
|
|
|
|
|
|
def metric_scoring_example() -> None:
|
|
"""Score text using individual metrics."""
|
|
candidate = "The quick brown fox jumps over the lazy dog."
|
|
reference = "A fast brown fox leaps over a sleepy dog."
|
|
|
|
# BLEU scoring (translation quality)
|
|
bleu_metric = Bleu()
|
|
bleu_result = bleu_metric.score(candidate, reference)
|
|
print("BLEU Scores:")
|
|
print(f" BLEU-1: {bleu_result.bleu1:.3f}")
|
|
print(f" BLEU-4: {bleu_result.bleu4:.3f}")
|
|
print(f" Brevity penalty: {bleu_result.brevity_penalty:.3f}")
|
|
|
|
# ROUGE scoring (summary quality)
|
|
rouge_metric = Rouge()
|
|
rouge_result = rouge_metric.score(candidate, reference)
|
|
print("\nROUGE Scores:")
|
|
print(f" ROUGE-1 F1: {rouge_result.rouge1.fmeasure:.3f}")
|
|
print(f" ROUGE-L F1: {rouge_result.rouge_l.fmeasure:.3f}")
|
|
|
|
|
|
def validator_example() -> None:
|
|
"""Use validators to make pass/fail decisions."""
|
|
reference = "Machine learning models require training data."
|
|
candidate = "ML models need training data to learn patterns."
|
|
|
|
context = ValidationContext(reference=reference)
|
|
|
|
# BLEU validator with minimum threshold
|
|
bleu_validator = bleu(min_score=0.3)
|
|
result = bleu_validator.check(candidate, context)
|
|
print(f"\nBLEU validation (min 0.3): {'PASS' if result.passed else 'FAIL'}")
|
|
|
|
# ROUGE validator
|
|
rouge_validator = rouge(min_score=0.5)
|
|
result = rouge_validator.check(candidate, context)
|
|
print(f"ROUGE validation (min 0.5): {'PASS' if result.passed else 'FAIL'}")
|
|
|
|
|
|
def composite_validator_example() -> None:
|
|
"""Combine validators with all_of and any_of."""
|
|
reference = "The product launch exceeded all expectations."
|
|
candidate = "The product release performed beyond expectations."
|
|
|
|
context = ValidationContext(reference=reference)
|
|
|
|
# All checks must pass
|
|
strict_validator = all_of(
|
|
[
|
|
bleu(min_score=0.2),
|
|
rouge(min_score=0.4),
|
|
length(max_chars=100),
|
|
]
|
|
)
|
|
result = strict_validator.check(candidate, context)
|
|
print(f"\nStrict (all_of): {'PASS' if result.passed else 'FAIL'}")
|
|
if not result.passed:
|
|
print(f" Failures: {result.failure_summary}")
|
|
|
|
# At least one check must pass
|
|
flexible_validator = any_of(
|
|
[
|
|
bleu(min_score=0.8), # Unlikely to pass
|
|
rouge(min_score=0.4), # More likely
|
|
]
|
|
)
|
|
result = flexible_validator.check(candidate, context)
|
|
print(f"Flexible (any_of): {'PASS' if result.passed else 'FAIL'}")
|
|
|
|
|
|
def constraint_validator_example() -> None:
|
|
"""Use constraint validators for text properties."""
|
|
text = "This short guide explains the basics clearly."
|
|
context = ValidationContext() # No reference needed for constraints
|
|
|
|
# Length constraints
|
|
length_validator = length(min_chars=20, max_chars=100, min_words=5, max_words=20)
|
|
result = length_validator.check(text, context)
|
|
print(f"\nLength check: {'PASS' if result.passed else 'FAIL'}")
|
|
|
|
# Readability (Flesch-Kincaid)
|
|
readability_validator = readability(max_grade=10.0)
|
|
result = readability_validator.check(text, context)
|
|
print(f"Readability (grade <= 10): {'PASS' if result.passed else 'FAIL'}")
|
|
|
|
# Content patterns
|
|
contains_validator = contains(patterns=["guide", "basics"])
|
|
result = contains_validator.check(text, context)
|
|
print(f"Contains required terms: {'PASS' if result.passed else 'FAIL'}")
|
|
|
|
excludes_validator = excludes(patterns=["error", "warning"])
|
|
result = excludes_validator.check(text, context)
|
|
print(f"Excludes forbidden terms: {'PASS' if result.passed else 'FAIL'}")
|
|
|
|
|
|
def main() -> None:
|
|
"""Run all examples."""
|
|
print("=" * 60)
|
|
print("Veritext Basic Validation Examples")
|
|
print("=" * 60)
|
|
|
|
metric_scoring_example()
|
|
validator_example()
|
|
composite_validator_example()
|
|
constraint_validator_example()
|
|
|
|
print("\n" + "=" * 60)
|
|
print("All examples completed.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|