Spaces:
Sleeping
Sleeping
updates to Validators + priority based validators
Browse files- validators.py +19 -15
validators.py
CHANGED
|
@@ -3,6 +3,8 @@ from abc import ABC, abstractmethod
|
|
| 3 |
|
| 4 |
class Validator(ABC):
|
| 5 |
"""Abstract base class for a validator."""
|
|
|
|
|
|
|
| 6 |
@abstractmethod
|
| 7 |
def can_handle(self, task_description: str) -> bool:
|
| 8 |
"""Whether this validator can handle the given task."""
|
|
@@ -15,6 +17,8 @@ class Validator(ABC):
|
|
| 15 |
|
| 16 |
class PunctuationValidator(Validator):
|
| 17 |
"""Validator to remove trailing punctuation."""
|
|
|
|
|
|
|
| 18 |
def can_handle(self, task_description: str) -> bool:
|
| 19 |
"""This validator can handle any task."""
|
| 20 |
return True
|
|
@@ -25,41 +29,40 @@ class PunctuationValidator(Validator):
|
|
| 25 |
|
| 26 |
class BotanyValidator(Validator):
|
| 27 |
"""Validator for the botany question."""
|
|
|
|
|
|
|
| 28 |
def can_handle(self, task_description: str) -> bool:
|
| 29 |
"""Handles the botany question about fruits and vegetables."""
|
| 30 |
return "botany" in task_description.lower() and "vegetable" in task_description.lower()
|
| 31 |
|
| 32 |
def validate(self, answer: str) -> str:
|
| 33 |
"""Ensures no botanical fruits are in the vegetable list."""
|
| 34 |
-
# List of common botanical fruits often mistaken for vegetables
|
| 35 |
botanical_fruits = [
|
| 36 |
"corn", "zucchini", "green beans", "bell pepper", "tomato",
|
| 37 |
"pepper", "cucumber", "beans", "peas", "okra", "eggplant", "acorns"
|
| 38 |
]
|
| 39 |
-
|
| 40 |
-
# Split the answer string into a list of items
|
| 41 |
vegetables = [veg.strip().lower() for veg in answer.split(',')]
|
| 42 |
-
|
| 43 |
-
# Filter out the botanical fruits
|
| 44 |
final_vegetables = [veg for veg in vegetables if veg not in botanical_fruits]
|
| 45 |
-
|
| 46 |
-
# Return the alphabetized, comma-separated string
|
| 47 |
return ", ".join(sorted(final_vegetables))
|
| 48 |
|
| 49 |
class CaseValidator(Validator):
|
| 50 |
-
"""Validator to capitalize single-word answers."""
|
|
|
|
|
|
|
| 51 |
def can_handle(self, task_description: str) -> bool:
|
| 52 |
-
"""
|
| 53 |
-
return
|
| 54 |
|
| 55 |
def validate(self, answer: str) -> str:
|
| 56 |
-
"""Capitalizes the answer if it's a single word."""
|
| 57 |
-
if len(answer.split()) == 1:
|
| 58 |
return answer.capitalize()
|
| 59 |
return answer
|
| 60 |
|
| 61 |
class NumericValidator(Validator):
|
| 62 |
"""Validator to ensure numeric answers are just numbers."""
|
|
|
|
|
|
|
| 63 |
def can_handle(self, task_description: str) -> bool:
|
| 64 |
"""Handles tasks that ask 'how many'."""
|
| 65 |
return "how many" in task_description.lower()
|
|
@@ -77,11 +80,12 @@ class ValidatorRegistry:
|
|
| 77 |
self.validators = []
|
| 78 |
|
| 79 |
def register(self, validator: Validator):
|
| 80 |
-
"""Register a validator."""
|
| 81 |
self.validators.append(validator)
|
|
|
|
| 82 |
|
| 83 |
def process(self, task_description: str, answer: str) -> str:
|
| 84 |
-
"""Process the answer through all applicable validators."""
|
| 85 |
processed_answer = answer
|
| 86 |
for validator in self.validators:
|
| 87 |
if validator.can_handle(task_description):
|
|
@@ -93,4 +97,4 @@ validator_registry = ValidatorRegistry()
|
|
| 93 |
validator_registry.register(PunctuationValidator())
|
| 94 |
validator_registry.register(BotanyValidator())
|
| 95 |
validator_registry.register(CaseValidator())
|
| 96 |
-
validator_registry.register(NumericValidator())
|
|
|
|
| 3 |
|
| 4 |
class Validator(ABC):
|
| 5 |
"""Abstract base class for a validator."""
|
| 6 |
+
priority = 100 # Default priority
|
| 7 |
+
|
| 8 |
@abstractmethod
|
| 9 |
def can_handle(self, task_description: str) -> bool:
|
| 10 |
"""Whether this validator can handle the given task."""
|
|
|
|
| 17 |
|
| 18 |
class PunctuationValidator(Validator):
|
| 19 |
"""Validator to remove trailing punctuation."""
|
| 20 |
+
priority = 999 # Should run last
|
| 21 |
+
|
| 22 |
def can_handle(self, task_description: str) -> bool:
|
| 23 |
"""This validator can handle any task."""
|
| 24 |
return True
|
|
|
|
| 29 |
|
| 30 |
class BotanyValidator(Validator):
|
| 31 |
"""Validator for the botany question."""
|
| 32 |
+
priority = 10 # High priority for content-specific validation
|
| 33 |
+
|
| 34 |
def can_handle(self, task_description: str) -> bool:
|
| 35 |
"""Handles the botany question about fruits and vegetables."""
|
| 36 |
return "botany" in task_description.lower() and "vegetable" in task_description.lower()
|
| 37 |
|
| 38 |
def validate(self, answer: str) -> str:
|
| 39 |
"""Ensures no botanical fruits are in the vegetable list."""
|
|
|
|
| 40 |
botanical_fruits = [
|
| 41 |
"corn", "zucchini", "green beans", "bell pepper", "tomato",
|
| 42 |
"pepper", "cucumber", "beans", "peas", "okra", "eggplant", "acorns"
|
| 43 |
]
|
|
|
|
|
|
|
| 44 |
vegetables = [veg.strip().lower() for veg in answer.split(',')]
|
|
|
|
|
|
|
| 45 |
final_vegetables = [veg for veg in vegetables if veg not in botanical_fruits]
|
|
|
|
|
|
|
| 46 |
return ", ".join(sorted(final_vegetables))
|
| 47 |
|
| 48 |
class CaseValidator(Validator):
|
| 49 |
+
"""Validator to capitalize single-word, all-lowercase answers."""
|
| 50 |
+
priority = 900 # Low priority, but before final punctuation strip
|
| 51 |
+
|
| 52 |
def can_handle(self, task_description: str) -> bool:
|
| 53 |
+
"""This validator can handle any task."""
|
| 54 |
+
return True
|
| 55 |
|
| 56 |
def validate(self, answer: str) -> str:
|
| 57 |
+
"""Capitalizes the answer if it's a single, all-lowercase word."""
|
| 58 |
+
if len(answer.split()) == 1 and answer.islower():
|
| 59 |
return answer.capitalize()
|
| 60 |
return answer
|
| 61 |
|
| 62 |
class NumericValidator(Validator):
|
| 63 |
"""Validator to ensure numeric answers are just numbers."""
|
| 64 |
+
priority = 50 # Medium priority
|
| 65 |
+
|
| 66 |
def can_handle(self, task_description: str) -> bool:
|
| 67 |
"""Handles tasks that ask 'how many'."""
|
| 68 |
return "how many" in task_description.lower()
|
|
|
|
| 80 |
self.validators = []
|
| 81 |
|
| 82 |
def register(self, validator: Validator):
|
| 83 |
+
"""Register a validator and maintain sorted order by priority."""
|
| 84 |
self.validators.append(validator)
|
| 85 |
+
self.validators.sort(key=lambda v: v.priority)
|
| 86 |
|
| 87 |
def process(self, task_description: str, answer: str) -> str:
|
| 88 |
+
"""Process the answer through all applicable validators in order of priority."""
|
| 89 |
processed_answer = answer
|
| 90 |
for validator in self.validators:
|
| 91 |
if validator.can_handle(task_description):
|
|
|
|
| 97 |
validator_registry.register(PunctuationValidator())
|
| 98 |
validator_registry.register(BotanyValidator())
|
| 99 |
validator_registry.register(CaseValidator())
|
| 100 |
+
validator_registry.register(NumericValidator())
|