34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
"""Препроцессор: фильтрация пустого и слишком короткого текста."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from src.prepare.base_prepare import PreProcessor
|
|
|
|
DEFAULT_MIN_WORDS = 20
|
|
|
|
|
|
class MinWordsPreProcessor(PreProcessor):
|
|
"""Пропускает пустой текст и текст короче ``min_words`` слов.
|
|
|
|
Слова разделяются пробельными символами (``str.split``).
|
|
|
|
Args:
|
|
min_words: Минимальное количество слов в тексте.
|
|
Текст с меньшим числом слов отбрасывается.
|
|
"""
|
|
|
|
def __init__(self, min_words: int = DEFAULT_MIN_WORDS) -> None:
|
|
if min_words <= 0:
|
|
raise ValueError("min_words должен быть положительным")
|
|
|
|
self._min_words = min_words
|
|
|
|
async def prepare(self, text: str, metadata: dict) -> str | None:
|
|
if not text:
|
|
return None
|
|
|
|
if len(text.split()) < self._min_words:
|
|
return None
|
|
|
|
return text
|