File size: 2,872 Bytes
b1b6791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Unit tests for Syntactic Parser
"""

import sys
from pathlib import Path

# Add parent to path
parent_dir = Path(__file__).parent.parent
sys.path.insert(0, str(parent_dir))

from pipeline.syntactic_parser import SyntacticParser


def test_parser_initialization():
    """Test parser initializes correctly."""
    parser = SyntacticParser()
    assert parser.nlp is not None
    print("βœ“ Parser initialization test passed")


def test_basic_parsing():
    """Test basic text parsing."""
    parser = SyntacticParser()
    result = parser.parse("I love my family")
    
    assert "tokens" in result
    assert "dependencies" in result
    assert len(result["tokens"]) > 0
    print("βœ“ Basic parsing test passed")


def test_negation_detection():
    """Test negation detection."""
    parser = SyntacticParser()
    result = parser.parse("I don't care about you")
    
    # Check negations found
    assert len(result["negations"]) > 0
    assert len(result["negation_scopes"]) > 0
    print("βœ“ Negation detection test passed")


def test_subject_detection():
    """Test subject detection."""
    parser = SyntacticParser()
    result = parser.parse("They protect the family")
    
    # Should have subjects
    subjects = result.get("subjects", [])
    assert len(subjects) > 0
    print("βœ“ Subject detection test passed")


def test_object_detection():
    """Test object detection."""
    parser = SyntacticParser()
    result = parser.parse("I love my family")
    
    # Should have objects
    objects = result.get("objects", [])
    assert len(objects) > 0
    print("βœ“ Object detection test passed")


def test_negation_reduces_score():
    """Test that negated moral words are detected."""
    parser = SyntacticParser()
    
    # Non-negated
    result1 = parser.parse("I care about family")
    tokens1 = result1["tokens"]
    
    # Negated  
    result2 = parser.parse("I don't care about family")
    tokens2 = result2["tokens"]
    negation_scopes2 = result2["negation_scopes"]
    
    # The words should be found in both, but negated version has negation
    assert "care" in [t.lower() for t in tokens1]
    assert "care" in [t.lower() for t in tokens2]
    assert len(negation_scopes2) > 0  # Has negation
    print("βœ“ Negation reduces score test passed")


def run_all_tests():
    """Run all tests."""
    print("\n" + "="*50)
    print("Running Parser Tests")
    print("="*50)
    
    try:
        test_parser_initialization()
        test_basic_parsing()
        test_negation_detection()
        test_subject_detection()
        test_object_detection()
        test_negation_reduces_score()
        
        print("\n" + "="*50)
        print("All parser tests passed! βœ“")
        print("="*50)
    except Exception as e:
        print(f"\nβœ— Test failed: {e}")
        raise


if __name__ == "__main__":
    run_all_tests()