Repository navigation
Expand file tree
/
Copy pathadvanced_pipeline.py
More file actions
154 lines (117 loc) · 4.27 KB
/
Copy pathadvanced_pipeline.py
File metadata and controls
154 lines (117 loc) · 4.27 KB
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
"""Advanced pipeline example with custom configuration.
Demonstrates creating custom text processing pipelines with
domain-specific stopwords, suffix attachment, and lemmatization.
"""
from dataclasses import dataclass, field
from durak import (
StopwordManager,
attach_detached_suffixes,
clean_text,
load_stopword_resource,
load_stopword_resources,
normalize_case,
tokenize,
)
@dataclass
class ProcessingContext:
text: str
metadata: list[str] = field(default_factory=list)
tokens: list[str] = field(default_factory=list)
def add_metadata(self, info: str):
self.metadata.append(info)
def main():
print("=" * 60)
print("Advanced Pipeline Example")
print("=" * 60)
# Sample text
text = """
Ankara'da bugün hava çok güzel. Ankara'nın tarihi yerlerini
gezmeye gittik. İstanbul'dan gelen arkadaşlarımız da vardı.
"""
print(f"\nOriginal text:\n{text}")
# 1. Custom Stopword Management
print("\n1. Custom Stopword Management")
print("-" * 60)
# Create a stopword manager with custom additions
stopword_mgr = StopwordManager(
additions=["bugün", "çok"], # Add custom stopwords
keep=["var", "git"], # Keep these even if they're stopwords
)
tokens = tokenize(clean_text(text))
print(f"Tokens: {tokens}")
filtered = [t for t in tokens if not stopword_mgr.is_stopword(t)]
print(f"Filtered: {filtered}")
# 2. Suffix Attachment
print("\n2. Detached Suffix Attachment")
print("-" * 60)
# Tokenize preserving apostrophes
text_with_suffixes = "Ankara'da İstanbul'dan geldi"
tokens_raw = tokenize(text_with_suffixes)
print(f"Raw tokens: {tokens_raw}")
# Attach detached suffixes
attached = attach_detached_suffixes(tokens_raw)
print(f"With attached suffixes: {attached}")
# 3. Custom Pipeline
print("\n3. Building a Custom Pipeline")
print("-" * 60)
class CustomTurkishPipeline:
"""Custom pipeline for Turkish text processing."""
def __init__(
self,
attach_suffixes=True,
remove_stopwords=True,
normalize=True,
custom_stopwords=None,
):
self.attach_suffixes = attach_suffixes
self.remove_stopwords = remove_stopwords
self.normalize = normalize
if custom_stopwords:
self.stopword_mgr = StopwordManager(additions=custom_stopwords)
else:
self.stopword_mgr = StopwordManager()
def __call__(self, text):
"""Process text through the pipeline."""
# Clean and normalize
processed = clean_text(text)
if self.normalize:
processed = normalize_case(processed)
# Tokenize
tokens = tokenize(processed)
# Attach suffixes
if self.attach_suffixes:
tokens = attach_detached_suffixes(tokens)
# Remove stopwords
if self.remove_stopwords:
tokens = [t for t in tokens if not self.stopword_mgr.is_stopword(t)]
return tokens
# Use the custom pipeline
custom_pipeline = CustomTurkishPipeline(
attach_suffixes=True,
remove_stopwords=True,
normalize=True,
custom_stopwords=["bugün"],
)
result = custom_pipeline(text)
print(f"Custom pipeline result: {result}")
# 4. Resource Loading Strategies
print("\n4. Resource Loading Strategies")
print("-" * 60)
# Load from embedded Rust resources (zero file I/O)
try:
from durak import _durak_core
rust_stopwords = set(_durak_core.get_stopwords_base())
print(f"Loaded {len(rust_stopwords)} stopwords from Rust (embedded)")
except ImportError:
print("Rust extension not available")
# Load from file resources (traditional)
file_stopwords = load_stopword_resource("base/turkish")
print(f"Loaded {len(file_stopwords)} stopwords from file")
# Load multiple resources
combined = load_stopword_resources(["base/turkish", "domains/social_media"])
print(f"Combined {len(combined)} stopwords from multiple sources")
print("\n" + "=" * 60)
print("Advanced example complete!")
print("=" * 60)
if __name__ == "__main__":
main()