Turkish
File size: 7,069 Bytes
7c16940
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Property-Based Testing Guide

Durak uses [Hypothesis](https://hypothesis.readthedocs.io/) for property-based testing to ensure robustness across a wide range of Turkish text inputs.

## What is Property-Based Testing?

Unlike traditional unit tests that check specific examples, property-based tests verify **mathematical properties** that should hold for **all inputs**.

### Example: Traditional vs Property-Based

**Traditional test:**
```python
def test_normalize_case():
    assert normalize_case("İSTANBUL") == "istanbul"
    assert normalize_case("ANKARA") == "ankara"
```

**Property-based test:**
```python
@given(turkish_word())
def test_normalize_case_is_idempotent(word):
    """Normalizing twice should equal normalizing once."""
    normalized_once = normalize_case(word)
    normalized_twice = normalize_case(normalized_once)
    assert normalized_once == normalized_twice
```

The property test generates **hundreds of random Turkish words** and verifies the idempotence property holds for all of them.

## Why Property-Based Testing for Turkish NLP?

Turkish has unique challenges that make property testing especially valuable:

1. **Diacritic complexity**: İ/I vs i/ı conversion must be correct in all cases
2. **Apostrophe handling**: `İstanbul'a`, `Ankara'da` create complex token boundaries
3. **Unicode edge cases**: Zero-width characters, combining diacritics, RTL marks
4. **Morphological richness**: Agglutination creates extremely long, unpredictable words
5. **Capitalization rules**: Turkish-specific uppercase/lowercase conversions

Traditional unit tests can't cover the combinatorial space of these features.

## Text Generation Strategies

Durak provides specialized Hypothesis strategies in `tests/strategies.py`:

### Basic Strategies

```python
from tests.strategies import turkish_word, turkish_sentence

# Generate random Turkish words
turkish_word()  # "çalışmak", "görmek", "İstanbul", etc.

# Generate words with possessive/case markers
turkish_word_with_suffix()  # "İstanbul'a", "Ankara'dan", etc.

# Generate full sentences
turkish_sentence()  # "İstanbul'a gittik ve müze'yi gördük."
```

### Advanced Strategies

```python
# Unicode edge cases (zero-width chars, combining diacritics)
turkish_text_with_unicode_edge_cases()

# Lists of Turkish stopwords
turkish_stopword_list()
```

## Writing Property Tests

### 1. Import the Strategy

```python
from hypothesis import given
from tests.strategies import turkish_sentence
```

### 2. Define the Property

```python
@given(turkish_sentence())
def test_tokenize_always_returns_list(text):
    """Tokenization must always return a list."""
    tokens = tokenize(text)
    assert isinstance(tokens, list)
```

### 3. Run with Hypothesis

Hypothesis will:
- Generate 100-200 random examples (configurable with `@settings(max_examples=N)`)
- Run your test on each example
- If a failure occurs, **shrink** to find the minimal failing case
- Cache failures for regression testing

## Common Properties to Test

### Idempotence

Functions that should give the same result when applied twice:

```python
@given(turkish_sentence())
def test_normalize_is_idempotent(text):
    assert normalize_case(text) == normalize_case(normalize_case(text))
```

### Inverse Functions

Functions that should undo each other:

```python
@given(turkish_sentence())
def test_tokenize_rejoins(text):
    tokens = tokenize(text)
    rejoined = "".join(tokens)
    # Should preserve non-whitespace content
    assert set(rejoined.replace(" ", "")) == set(text.replace(" ", ""))
```

### Monotonicity

Functions that should never increase size:

```python
@given(turkish_word())
def test_normalization_not_expanding(word):
    assert len(normalize_case(word)) <= len(word)
```

### Consistency

Related functions should agree:

```python
@given(turkish_sentence())
def test_offset_consistency(text):
    tokens_with_offsets = tokenize_with_offsets(text)
    for token, start, end in tokens_with_offsets:
        # Offset must be valid
        assert 0 <= start < end <= len(text)
        # Extracted text should match token
        extracted = text[start:end]
        assert extracted.lower() == token.lower()
```

## Controlling Test Execution

### Number of Examples

```python
from hypothesis import settings

@given(turkish_sentence())
@settings(max_examples=500)  # Generate 500 random examples
def test_something(text):
    ...
```

### Filtering Invalid Inputs

```python
from hypothesis import assume

@given(turkish_sentence())
def test_non_empty_tokenization(text):
    assume(len(text.strip()) > 0)  # Skip empty strings
    tokens = tokenize(text)
    assert len(tokens) > 0
```

### Debugging Failures

When a property test fails, Hypothesis prints:
1. The **minimal failing example** (after shrinking)
2. The **random seed** to reproduce the failure

```
Falsifying example: test_something(text='İ')
You can reproduce this by running:
    pytest --hypothesis-seed=12345
```

## Running Property Tests

### Run all property tests:

```bash
pytest tests/test_properties.py -v
```

### Show Hypothesis statistics:

```bash
pytest tests/test_properties.py --hypothesis-show-statistics
```

Output:
```
  - 200 passing examples, 0 failing examples, 0 invalid examples
  - Typical runtimes: 0-1ms, ~ 50% in data generation
  - Stopped because settings.max_examples=200
```

### Run with more examples (stress test):

```bash
pytest tests/test_properties.py --hypothesis-profile=ci
```

## Best Practices

### ✅ Do:

- Test **mathematical properties**, not specific examples
- Use `assume()` to filter out invalid inputs
- Keep properties simple and focused (one property per test)
- Use meaningful assertion messages
- Combine traditional and property-based tests

### ❌ Don't:

- Test implementation details
- Make tests too complex (they become hard to debug)
- Over-constrain input generation (reduces coverage)
- Ignore failures (Hypothesis finds real edge cases!)

## Example: Real Bug Caught by Property Testing

**Property test:**
```python
@given(turkish_word())
def test_normalize_case_removes_uppercase(word):
    normalized = normalize_case(word)
    assert "İ" not in normalized
```

**Bug found:**
```
Falsifying example: test_normalize_case_removes_uppercase(word='İİİ')
AssertionError: 'İ' found in normalized output
```

**Root cause:** Normalization was using Python's `.lower()` instead of Turkish-aware lowercase conversion. Property testing caught it instantly!

## Resources

- [Hypothesis Documentation](https://hypothesis.readthedocs.io/)
- [Property-Based Testing Patterns](https://hypothesis.works/articles/what-is-property-based-testing/)
- [Hypothesis for Python Developers](https://hypothesis.works/articles/quickcheck-in-every-language/)

## Contributing

When adding new features to Durak:

1. **Add traditional unit tests** for specific examples
2. **Add property tests** for general behavior
3. **Document invariants** that your function guarantees

Property tests serve as **executable specifications** and protect against regressions.