-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_core.py
146 lines (121 loc) · 4.55 KB
/
validate_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
Core validation script for cognitive metrics.
This script validates the core functionality of our cognitive metrics framework,
following British English standards and proper type safety.
"""
from pathlib import Path
import sys
from typing import Dict, List, Optional, Tuple
def validate_environment() -> Tuple[bool, Optional[str]]:
"""Validate Python environment and core dependencies.
Returns:
Tuple[bool, Optional[str]]: Success status and error message if any
"""
try:
import spacy
from src.metrics.cognitive_metrics import ReasoningEvaluator
from src.evaluation_protocols import TestCase, MetricResult
return True, None
except ImportError as e:
return False, f"Import error: {e}"
except Exception as e:
return False, f"Unexpected error: {e}"
def validate_nlp() -> Tuple[bool, Optional[str]]:
"""Validate NLP functionality.
Returns:
Tuple[bool, Optional[str]]: Success status and error message if any
"""
try:
import spacy
nlp = spacy.load("en_core_web_sm")
# Test basic processing
doc = nlp("First, we analyse. Therefore, we conclude.")
sentences = list(doc.sents)
if not sentences:
return False, "No sentences found in test text"
return True, None
except Exception as e:
return False, f"NLP error: {e}"
def validate_patterns() -> Tuple[bool, Optional[str], Dict[str, List[str]]]:
"""Validate pattern matching functionality.
Returns:
Tuple[bool, Optional[str], Dict[str, List[str]]]:
Success status, error message if any, and matched patterns
"""
try:
import spacy
nlp = spacy.load("en_core_web_sm")
test_text = """
First, let's examine the evidence carefully. According to recent studies,
the approach shows promising results. However, we must consider some
limitations. For example, the sample size was relatively small.
Therefore, while the data suggests positive outcomes, further validation
would be beneficial.
"""
patterns = {
"Logical Steps": [
"first", "therefore", "while"
],
"Evidence": [
"according to", "studies", "data suggests"
],
"Counterarguments": [
"however", "limitations"
]
}
matches: Dict[str, List[str]] = {}
for category, terms in patterns.items():
matches[category] = [
term for term in terms
if term.lower() in test_text.lower()
]
if not any(matches.values()):
return False, "No patterns matched", {}
return True, None, matches
except Exception as e:
return False, f"Pattern matching error: {e}", {}
def main() -> int:
"""Run core validation checks.
Returns:
int: Exit code (0 for success, 1 for failure)
"""
print("Core Functionality Validation")
print("===========================")
try:
# Validate environment
print("\nValidating environment...")
env_ok, env_error = validate_environment()
if not env_ok:
print(f"✗ Environment validation failed: {env_error}")
return 1
print("✓ Environment validated")
# Validate NLP
print("\nValidating NLP functionality...")
nlp_ok, nlp_error = validate_nlp()
if not nlp_ok:
print(f"✗ NLP validation failed: {nlp_error}")
return 1
print("✓ NLP functionality validated")
# Validate patterns
print("\nValidating pattern matching...")
patterns_ok, patterns_error, matches = validate_patterns()
if not patterns_ok:
print(f"✗ Pattern matching failed: {patterns_error}")
return 1
print("✓ Pattern matching validated")
print("\nMatched Patterns:")
for category, found in matches.items():
if found:
print(f"\n{category}:")
for pattern in found:
print(f"- {pattern}")
print("\n✓ All validation checks passed!")
return 0
except Exception as e:
print(f"\n✗ Validation failed: {e}")
import traceback
print("\nTraceback:")
traceback.print_exc()
return 1
if __name__ == "__main__":
sys.exit(main())