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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List, Dict, Optional, Tuple, Union
|
| 2 |
+
|
| 3 |
+
class OutputClipper:
|
| 4 |
+
"""
|
| 5 |
+
كلاس للتحكم في قص وتصفية المخرجات النصية
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
def __init__(self):
|
| 9 |
+
pass
|
| 10 |
+
|
| 11 |
+
def clip_by_ranges(
|
| 12 |
+
self,
|
| 13 |
+
data: List[Dict[str, str]],
|
| 14 |
+
ranges: List[Tuple[int, int]]
|
| 15 |
+
) -> List[Dict[str, str]]:
|
| 16 |
+
"""
|
| 17 |
+
قص البيانات حسب النطاقات المحددة
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
data: البيانات الأصلية [{'text': 'content'}, ...]
|
| 21 |
+
ranges: النطاقات [(start, end), (start, end), ...]
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
البيانات المقصوصة حسب النطاقات
|
| 25 |
+
"""
|
| 26 |
+
if not ranges or not data:
|
| 27 |
+
return data
|
| 28 |
+
|
| 29 |
+
total_items = len(data)
|
| 30 |
+
clipped_data = []
|
| 31 |
+
|
| 32 |
+
for start, end in ranges:
|
| 33 |
+
# تنظيف النطاقات
|
| 34 |
+
start = max(0, start)
|
| 35 |
+
end = min(total_items, end)
|
| 36 |
+
|
| 37 |
+
# تجاهل النطاقات غير الصحيحة
|
| 38 |
+
if start >= end:
|
| 39 |
+
continue
|
| 40 |
+
|
| 41 |
+
# إضافة العناصر من النطاق المحدد
|
| 42 |
+
range_items = data[start:end]
|
| 43 |
+
clipped_data.extend(range_items)
|
| 44 |
+
|
| 45 |
+
return clipped_data
|
| 46 |
+
|
| 47 |
+
def clip_by_count(
|
| 48 |
+
self,
|
| 49 |
+
data: List[Dict[str, str]],
|
| 50 |
+
start: int = 0,
|
| 51 |
+
count: Optional[int] = None
|
| 52 |
+
) -> List[Dict[str, str]]:
|
| 53 |
+
"""
|
| 54 |
+
قص البيانات حسب نقطة البداية وعدد العناصر
|
| 55 |
+
|
| 56 |
+
Args:
|
| 57 |
+
data: البيانات الأصلية
|
| 58 |
+
start: نقطة البداية (افتراضي 0)
|
| 59 |
+
count: عدد العناصر المطلوبة (None للكل)
|
| 60 |
+
|
| 61 |
+
Returns:
|
| 62 |
+
البيانات المقصوصة
|
| 63 |
+
"""
|
| 64 |
+
if not data:
|
| 65 |
+
return data
|
| 66 |
+
|
| 67 |
+
start = max(0, start)
|
| 68 |
+
|
| 69 |
+
if count is None:
|
| 70 |
+
return data[start:]
|
| 71 |
+
|
| 72 |
+
count = max(0, count)
|
| 73 |
+
end = start + count
|
| 74 |
+
|
| 75 |
+
return data[start:end]
|
| 76 |
+
|
| 77 |
+
def clip_by_text_length(
|
| 78 |
+
self,
|
| 79 |
+
data: List[Dict[str, str]],
|
| 80 |
+
min_length: int = 0,
|
| 81 |
+
max_length: Optional[int] = None
|
| 82 |
+
) -> List[Dict[str, str]]:
|
| 83 |
+
"""
|
| 84 |
+
قص البيانات حسب طول النص
|
| 85 |
+
|
| 86 |
+
Args:
|
| 87 |
+
data: البيانات الأصلية
|
| 88 |
+
min_length: الحد الأدنى لطول النص
|
| 89 |
+
max_length: الحد الأقصى لطول النص (None بلا حد أقصى)
|
| 90 |
+
|
| 91 |
+
Returns:
|
| 92 |
+
البيانات المفلترة حسب طول النص
|
| 93 |
+
"""
|
| 94 |
+
if not data:
|
| 95 |
+
return data
|
| 96 |
+
|
| 97 |
+
filtered_data = []
|
| 98 |
+
|
| 99 |
+
for item in data:
|
| 100 |
+
text_length = len(item['text'])
|
| 101 |
+
|
| 102 |
+
# فحص الحد الأدنى
|
| 103 |
+
if text_length < min_length:
|
| 104 |
+
continue
|
| 105 |
+
|
| 106 |
+
# فحص الحد الأقصى
|
| 107 |
+
if max_length is not None and text_length > max_length:
|
| 108 |
+
continue
|
| 109 |
+
|
| 110 |
+
filtered_data.append(item)
|
| 111 |
+
|
| 112 |
+
return filtered_data
|
| 113 |
+
|
| 114 |
+
def clip_by_keywords(
|
| 115 |
+
self,
|
| 116 |
+
data: List[Dict[str, str]],
|
| 117 |
+
include_keywords: Optional[List[str]] = None,
|
| 118 |
+
exclude_keywords: Optional[List[str]] = None,
|
| 119 |
+
case_sensitive: bool = False
|
| 120 |
+
) -> List[Dict[str, str]]:
|
| 121 |
+
"""
|
| 122 |
+
قص البيانات حسب الكلمات المفتاحية
|
| 123 |
+
|
| 124 |
+
Args:
|
| 125 |
+
data: البيانات الأصلية
|
| 126 |
+
include_keywords: كلمات يجب تضمينها
|
| 127 |
+
exclude_keywords: كلمات يجب استبعادها
|
| 128 |
+
case_sensitive: حساسية الأحرف الكبيرة والصغيرة
|
| 129 |
+
|
| 130 |
+
Returns:
|
| 131 |
+
البيانات المفلترة حسب الكلمات المفتاحية
|
| 132 |
+
"""
|
| 133 |
+
if not data:
|
| 134 |
+
return data
|
| 135 |
+
|
| 136 |
+
filtered_data = []
|
| 137 |
+
|
| 138 |
+
for item in data:
|
| 139 |
+
text = item['text']
|
| 140 |
+
|
| 141 |
+
if not case_sensitive:
|
| 142 |
+
text_lower = text.lower()
|
| 143 |
+
|
| 144 |
+
# فحص كلمات التضمين
|
| 145 |
+
if include_keywords:
|
| 146 |
+
found_include = False
|
| 147 |
+
for keyword in include_keywords:
|
| 148 |
+
check_keyword = keyword.lower() if not case_sensitive else keyword
|
| 149 |
+
check_text = text_lower if not case_sensitive else text
|
| 150 |
+
|
| 151 |
+
if check_keyword in check_text:
|
| 152 |
+
found_include = True
|
| 153 |
+
break
|
| 154 |
+
|
| 155 |
+
if not found_include:
|
| 156 |
+
continue
|
| 157 |
+
|
| 158 |
+
# فحص كلمات الاستبعاد
|
| 159 |
+
if exclude_keywords:
|
| 160 |
+
found_exclude = False
|
| 161 |
+
for keyword in exclude_keywords:
|
| 162 |
+
check_keyword = keyword.lower() if not case_sensitive else keyword
|
| 163 |
+
check_text = text_lower if not case_sensitive else text
|
| 164 |
+
|
| 165 |
+
if check_keyword in check_text:
|
| 166 |
+
found_exclude = True
|
| 167 |
+
break
|
| 168 |
+
|
| 169 |
+
if found_exclude:
|
| 170 |
+
continue
|
| 171 |
+
|
| 172 |
+
filtered_data.append(item)
|
| 173 |
+
|
| 174 |
+
return filtered_data
|
| 175 |
+
|
| 176 |
+
def remove_duplicates(
|
| 177 |
+
self,
|
| 178 |
+
data: List[Dict[str, str]],
|
| 179 |
+
preserve_order: bool = True
|
| 180 |
+
) -> List[Dict[str, str]]:
|
| 181 |
+
"""
|
| 182 |
+
إزالة التكرارات من البيانات
|
| 183 |
+
|
| 184 |
+
Args:
|
| 185 |
+
data: البيانات الأصلية
|
| 186 |
+
preserve_order: الحفاظ على الترتيب الأصلي
|
| 187 |
+
|
| 188 |
+
Returns:
|
| 189 |
+
البيانات بدون تكرارات
|
| 190 |
+
"""
|
| 191 |
+
if not data:
|
| 192 |
+
return data
|
| 193 |
+
|
| 194 |
+
seen = set()
|
| 195 |
+
unique_data = []
|
| 196 |
+
|
| 197 |
+
for item in data:
|
| 198 |
+
text_content = item['text']
|
| 199 |
+
if text_content not in seen:
|
| 200 |
+
seen.add(text_content)
|
| 201 |
+
unique_data.append(item)
|
| 202 |
+
|
| 203 |
+
return unique_data
|
| 204 |
+
|
| 205 |
+
def combine_clips(
|
| 206 |
+
self,
|
| 207 |
+
data: List[Dict[str, str]],
|
| 208 |
+
ranges: Optional[List[Tuple[int, int]]] = None,
|
| 209 |
+
min_length: Optional[int] = None,
|
| 210 |
+
max_length: Optional[int] = None,
|
| 211 |
+
include_keywords: Optional[List[str]] = None,
|
| 212 |
+
exclude_keywords: Optional[List[str]] = None,
|
| 213 |
+
remove_duplicates: bool = True,
|
| 214 |
+
max_results: Optional[int] = None
|
| 215 |
+
) -> List[Dict[str, str]]:
|
| 216 |
+
"""
|
| 217 |
+
تطبيق عدة فلاتر معاً
|
| 218 |
+
|
| 219 |
+
Args:
|
| 220 |
+
data: البيانات الأصلية
|
| 221 |
+
ranges: النطاقات
|
| 222 |
+
min_length: الحد الأدنى لطول النص
|
| 223 |
+
max_length: الحد الأقصى لطول النص
|
| 224 |
+
include_keywords: كلمات للتضمين
|
| 225 |
+
exclude_keywords: كلمات للاستبعاد
|
| 226 |
+
remove_duplicates: إزالة التكرارات
|
| 227 |
+
max_results: الحد الأقصى للنتائج
|
| 228 |
+
|
| 229 |
+
Returns:
|
| 230 |
+
البيانات المفلترة نهائياً
|
| 231 |
+
"""
|
| 232 |
+
result = data.copy()
|
| 233 |
+
|
| 234 |
+
# تطبيق فلتر النطاقات
|
| 235 |
+
if ranges:
|
| 236 |
+
result = self.clip_by_ranges(result, ranges)
|
| 237 |
+
|
| 238 |
+
# تطبيق فلتر طول النص
|
| 239 |
+
if min_length is not None or max_length is not None:
|
| 240 |
+
result = self.clip_by_text_length(
|
| 241 |
+
result,
|
| 242 |
+
min_length or 0,
|
| 243 |
+
max_length
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
# تطبيق فلتر الكلمات المفتاحية
|
| 247 |
+
if include_keywords or exclude_keywords:
|
| 248 |
+
result = self.clip_by_keywords(
|
| 249 |
+
result,
|
| 250 |
+
include_keywords,
|
| 251 |
+
exclude_keywords
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
# إزالة التكرارات
|
| 255 |
+
if remove_duplicates:
|
| 256 |
+
result = self.remove_duplicates(result)
|
| 257 |
+
|
| 258 |
+
# تحديد عدد النتائج النهائي
|
| 259 |
+
if max_results is not None and max_results > 0:
|
| 260 |
+
result = result[:max_results]
|
| 261 |
+
|
| 262 |
+
return result
|
| 263 |
+
|
| 264 |
+
# دوال مستقلة للاستخدام السريع
|
| 265 |
+
def clip_by_ranges(data: List[Dict[str, str]], ranges: List[Tuple[int, int]]) -> List[Dict[str, str]]:
|
| 266 |
+
"""دالة مستقلة لقص البيانات حسب النطاقات"""
|
| 267 |
+
clipper = OutputClipper()
|
| 268 |
+
return clipper.clip_by_ranges(data, ranges)
|
| 269 |
+
|
| 270 |
+
def clip_by_count(data: List[Dict[str, str]], start: int = 0, count: Optional[int] = None) -> List[Dict[str, str]]:
|
| 271 |
+
"""دالة مستقلة لقص البيانات حسب العدد"""
|
| 272 |
+
clipper = OutputClipper()
|
| 273 |
+
return clipper.clip_by_count(data, start, count)
|
| 274 |
+
|
| 275 |
+
def clip_by_keywords(
|
| 276 |
+
data: List[Dict[str, str]],
|
| 277 |
+
include: Optional[List[str]] = None,
|
| 278 |
+
exclude: Optional[List[str]] = None
|
| 279 |
+
) -> List[Dict[str, str]]:
|
| 280 |
+
"""دالة مستقلة لفلترة الكلمات المفتاحية"""
|
| 281 |
+
clipper = OutputClipper()
|
| 282 |
+
return clipper.clip_by_keywords(data, include, exclude)
|
| 283 |
+
|
| 284 |
+
def advanced_clip(
|
| 285 |
+
data: List[Dict[str, str]],
|
| 286 |
+
**kwargs
|
| 287 |
+
) -> List[Dict[str, str]]:
|
| 288 |
+
"""دالة مستقلة للفلترة المتقدمة"""
|
| 289 |
+
clipper = OutputClipper()
|
| 290 |
+
return clipper.combine_clips(data, **kwargs)
|