К основному содержанию
правило

spacy — правило для Cursor: что делает и как установить

Задаёт Cursor правила работы со spaCy в коде Python: требует аннотации типов, пакетную обработку через nlp.pipe и декларативные конфиги.

Установить в Cursor · Вручную: положить файлы Все способы и платформы ↓
.cursor/rules/spacy.mdc
Открыть на GitHub

Что делает spacy

Правило применяется к файлам по маске **/*.py. Оно предписывает собирать пайплайны spaCy через spacy project с декларативным config.cfg и оркестрацией в project.yml вместо разрозненных скриптов с зашитыми путями к моделям.

В коде ассистент обязан выставлять аннотации типов для объектов Language, Doc, Span и Token. Обработку текстов правило требует выполнять пачками через nlp.pipe, отключая лишние этапы пайплайна с помощью контекстного менеджера nlp.select_pipes(enable=...) ради экономии памяти.

Кастомные шаги обработки регистрируются декоратором @Language.factory, а пользовательские данные привязываются к объектам через Doc.set_extension. Версии библиотек в requirements.txt фиксируются жёстко, чтобы избежать конфликтов при развёртывании.

Какие задачи решает

  • Организовать пайплайн spaCy через project.yml и config.cfg
  • Ускорить обработку текстов с помощью nlp.pipe и select_pipes
  • Добавить аннотации типов для объектов Language, Doc и Token
  • Зарегистрировать кастомный компонент через Language.factory

Примеры запросов агенту

  • Напиши функцию для извлечения сущностей из текста с точными типами spaCy
  • Добавь кастомный компонент оценки тональности через Language.factory в наш config.cfg
  • Оптимизируй обработку списка документов через nlp.pipe с размером батча 50

Как установить spacy

Вручную: положить файлы

.cursor/rules/spacy.mdc

Во frontmatter: description, globs, alwaysApply

По шагам, с командами для каждой платформы: Cursor rules: файлы .mdc, четыре режима и замена .cursorrules

Вопросы о spacy

Как установить spacy в Cursor?

Создайте в корне проекта файл .cursor/rules/spacy.mdc и скопируйте в него содержимое правила.

Что делает правило spacy?

Оно направляет Cursor на генерацию типизированного кода spaCy, использование пакетного инференса и хранение параметров в config.cfg.

Зачем правило требует nlp.select_pipes?

Этот метод временно отключает неиспользуемые компоненты пайплайна при инференсе, снижая расход памяти и процессора.

Исходный текст

spacy.mdc · CC0-1.0
---
description: This guide provides definitive, opinionated best practices for writing maintainable, performant, and robust spaCy code in Python, focusing on modern patterns and avoiding common pitfalls.
globs: **/*.py
---
# spaCy Best Practices

spaCy is the backbone of our NLP systems. Adhering to these guidelines ensures our pipelines are performant, reproducible, and easy to maintain.

## 1. Project Organization & Configuration

Always structure your spaCy projects using `spacy project` and define all pipeline settings in a declarative YAML config. This is non-negotiable for reproducibility and scalability.

**✅ GOOD: Use `spacy project` and declarative configs.**
Organize your pipeline logic in a dedicated `src/pipeline/` package. Use `project.yml` to manage workflows and `config.cfg` for all spaCy pipeline settings.

```python
# src/pipeline/custom_component.py
from spacy.language import Language
from spacy.tokens import Doc

@Language.factory("my_custom_component")
def create_my_component(nlp: Language, name: str):
    return MyCustomComponent(nlp, name)

class MyCustomComponent:
    def __init__(self, nlp: Language, name: str):
        self.nlp = nlp
        self.name = name

    def __call__(self, doc: Doc) -> Doc:
        # Custom logic here
        return doc

# project.yml (simplified)
# ...
workflows:
  train:
    - "python -m spacy train config.cfg --output models/"
  package:
    - "python -m spacy package models/en_core_web_v1.0.0 ./dist --build wheel"

# config.cfg (simplified)
[nlp]
lang = "en"
pipeline = ["tok2vec", "ner", "my_custom_component"]

[components.my_custom_component]
factory = "my_custom_component"
```

**❌ BAD: Ad-hoc scripts and hardcoded parameters.**
Avoid scattering pipeline logic across multiple scripts or hardcoding model paths and hyperparameters. This makes experiments non-reproducible and deployment fragile.

```python
# bad_script.py
import spacy
# Parameters hardcoded or passed via CLI args, not in a central config
MODEL_PATH = "path/to/my/model"
THRESHOLD = 0.7

nlp = spacy.load(MODEL_PATH)
# ... pipeline components added programmatically ...
```

## 2. Type Hinting

Strictly use type hints for all spaCy objects (`Language`, `Doc`, `Span`, `Token`). This improves code readability, enables static analysis, and reduces runtime errors.

**✅ GOOD: Comprehensive type hints.**
Annotate function arguments and return types with precise spaCy types.

```python
import spacy
from spacy.language import Language
from spacy.tokens import Doc, Span, Token
from typing import List

def process_document(nlp: Language, text: str) -> Doc:
    """Processes text with a spaCy pipeline."""
    return nlp(text)

def extract_entities(doc: Doc) -> List[Span]:
    """Extracts named entities from a processed Doc."""
    return list(doc.ents)

def get_token_lemma(token: Token) -> str:
    """Returns the lemma of a single token."""
    return token.lemma_
```

**❌ BAD: Untyped spaCy code.**
Omitting type hints makes code harder to understand and refactor.

```python
# bad_types.py
import spacy

def process_document(nlp, text): # What are nlp and text? What does it return?
    return nlp(text)

def extract_entities(doc): # What is 'doc'?
    return list(doc.ents)
```

## 3. Performance Considerations

Optimize for speed and memory by disabling unused pipeline components and processing text in batches.

**✅ GOOD: Efficient pipeline usage.**
Use `nlp.select_pipes` for inference to only run necessary components. Process multiple documents with `nlp.pipe`.

```python
import spacy
from spacy.language import Language
from spacy.tokens import Doc
from typing import Iterator

def analyze_text_batch(nlp: Language, texts: List[str]) -> Iterator[Doc]:
    """Processes a batch of texts, only running NER."""
    # Temporarily disable unused pipes for performance during inference
    with nlp.select_pipes(enable=["ner"]):
        yield from nlp.pipe(texts, batch_size=50)

# Example usage
nlp_full = spacy.load("en_core_web_sm")
documents = ["Apple is looking at buying U.K. startup.", "Tim Cook is CEO of Apple."]
for doc in analyze_text_batch(nlp_full, documents):
    print(f"Text: {doc.text}, Entities: {[(ent.text, ent.label_) for ent in doc.ents]}")
```

**❌ BAD: Inefficient processing.**
Loading full pipelines for simple tasks or processing documents one by one in a loop is wasteful.

```python
# bad_performance.py
import spacy

nlp = spacy.load("en_core_web_lg") # Loads all components, even if only tokenization is needed
texts = ["Text 1", "Text 2", "Text 3"]

for text in texts: # Processes one by one, no batching
    doc = nlp(text)
    # ... only use doc.text or doc.tokens, ignoring NER, POS, etc.
```

## 4. Virtual Environments & Dependencies

Always use a virtual environment and pin your `spacy` version in `requirements.txt`. This prevents dependency conflicts and ensures consistent environments.

**✅ GOOD: Pinned dependencies in a virtual environment.**

```bash
# Terminal
python -m venv .venv
source .venv/bin/activate
pip install -U pip setuptools wheel
pip install -r requirements.txt
python -m spacy download en_core_web_sm
```

```ini
# requirements.txt
spacy==3.8.*
spacy-llm==0.8.* # If using LLM integration
# Other dependencies...
```

**❌ BAD: Global installs or unpinned versions.**
Installing `spacy` globally or using `spacy>=3.0` can lead to unexpected behavior and "works on my machine" issues.

```ini
# requirements.txt (BAD)
spacy # No version specified, will install latest
```

## 5. Custom Components & Extension Attributes

Extend spaCy's `Doc`, `Span`, and `Token` objects using custom components and extension attributes. This keeps your custom logic integrated and accessible.

**✅ GOOD: Custom components and extension attributes.**

```python
# src/pipeline/custom_component.py
from spacy.language import Language
from spacy.tokens import Doc, Span, Token

# Register extension attribute
if not Doc.has_extension("custom_sentiment"):
    Doc.set_extension("custom_sentiment", default=0.0)

@Language.factory("sentiment_analyzer")
def create_sentiment_analyzer(nlp: Language, name: str):
    return SentimentAnalyzer(nlp, name)

class SentimentAnalyzer:
    def __init__(self, nlp: Language, name: str):
        self.nlp = nlp
        self.name = name

    def __call__(self, doc: Doc) -> Doc:
        # Simple sentiment logic
        score = sum(1 for token in doc if token.text == "good") - sum(1 for token in doc if token.text == "bad")
        doc._.custom_sentiment = float(score)
        return doc

# config.cfg
# ...
[nlp]
pipeline = ["tok2vec", "ner", "sentiment_analyzer"]

[components.sentiment_analyzer]
factory = "sentiment_analyzer"

# Usage
nlp = spacy.load("path/to/your/trained_model") # Load model with custom component
doc = nlp("This is a good movie, not bad at all.")
print(f"Sentiment: {doc._.custom_sentiment}") # Access custom attribute
```

**❌ BAD: Storing custom data separately or modifying core attributes.**
Avoid maintaining parallel data structures or attempting to directly modify `Doc` attributes that are meant to be read-only.

```python
# bad_extensions.py
import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("Some text.")

# ❌ BAD: Storing custom data in a separate dictionary
custom_data = {doc.text: {"sentiment": 0.5}}

# ❌ BAD: Attempting to modify read-only attributes (will raise error)
# doc.user_data = {"my_field": "value"}
```

## 6. Testing

Write focused unit tests for custom components and integration tests for pipeline behavior using `pytest`. Use minimal `nlp` objects for unit tests.

**✅ GOOD: Targeted unit tests with `pytest`.**

```python
# tests/test_custom_component.py
import pytest
import spacy
from spacy.language import Language
from spacy.tokens import Doc

# Assuming custom_component.py is in src/pipeline/
# from src.pipeline.custom_component import create_my_component, MyCustomComponent

# Register factory for testing
@Language.factory("test_my_custom_component")
def create_test_my_component(nlp: Language, name: str):
    return MyCustomComponent(nlp, name)

class MyCustomComponent: # Define a minimal version for testing
    def __init__(self, nlp: Language, name: str):
        self.nlp = nlp
        self.name = name
    def __call__(self, doc: Doc) -> Doc:
        doc.set_extension("processed_by_custom", default=True, force=True)
        return doc

def test_my_custom_component_adds_extension():
    nlp = spacy.blank("en")
    nlp.add_pipe("test_my_custom_component")
    doc = nlp("Hello world.")
    assert doc._.processed_by_custom is True

def test_my_custom_component_returns_doc():
    nlp = spacy.blank("en")
    nlp.add_pipe("test_my_custom_component")
    doc = nlp("Another test.")
    assert isinstance(doc, Doc)
```

**❌ BAD: Untested components or overly broad tests.**
Avoid relying solely on manual testing or writing integration tests that load full, complex models for every small change.

```python
# tests/bad_test.py
import spacy

def test_full_pipeline_output():
    # ❌ BAD: Loads a large, slow model for a simple test
    nlp = spacy.load("en_core_web_lg")
    doc = nlp("This is a test sentence.")
    assert len(doc.ents) > 0 # Fragile, depends on model behavior
```

## 7. LLM Integration

For integrating Large Language Models, leverage the `spacy-llm` package. It provides a structured, component-based approach that aligns with spaCy's pipeline philosophy.

**✅ GOOD: Use `spacy-llm` for structured LLM integration.**
Define LLM components in your `config.cfg` just like any other spaCy component.

```ini
# config.cfg (simplified for spacy-llm)
[nlp]
lang = "en"
pipeline = ["llm_ner"]

[components.llm_ner]
factory = "spacy.LLM"
task = {"@llm_tasks": "spacy.NER.v3"}
model = {"@llm_models": "spacy.GPT-3-5.v1"}
```

**❌ BAD: Ad-hoc LLM calls outside the pipeline.**
Directly calling LLM APIs in your application code bypasses spaCy's structured pipeline, making it harder to manage, test, and swap models.

```python
# bad_llm.py
import openai # or other LLM client
import spacy

def process_with_llm_and_spacy(text: str):
    # ❌ BAD: LLM call is separate from the spaCy pipeline
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt=f"Extract entities from: {text}",
        max_tokens=100
    )
    llm_entities = parse_llm_response(response)

    nlp = spacy.load("en_core_web_sm")
    doc = nlp(text)
    # Now you have two separate sets of entities, difficult to reconcile
    return doc, llm_entities
```

© sanjeed5 · CC0-1.0 · sanjeed5/awesome-cursor-rules-mdc

Похожие правила IDE

Все правила IDE по теме «ИИ и ML» →
nlp-natural-language-processing Mindrally Набор инструкций для NLP-разработки: токенизация, классификация, spaCy, NLTK и поиск по эмбеддингам. Помогает агенту писать чистый Python-код по стандартам PEP 8. CX Codex +1 980установок transformers-huggingface Mindrally Скилл для работы с библиотекой Hugging Face Transformers: загрузка моделей, токенизация, файнтюнинг через Trainer и оптимизация инференса. CX Codex +1 758установок azure-ai-textanalytics-py Microsoft Скилл для работы с Azure AI Language из Python: тональность, сущности, ключевые фразы, определение языка, поиск персональных данных и медицинский NLP. 2установок azure-ai-translation-text-py Microsoft Скилл по Python SDK Azure AI Translator: перевод текста, транслитерация, определение языка и словарные статьи. Пригодится, когда в приложении нужно переводить строки на лету. 2установок chunking-strategy giuseppe-trisciuoglio Помогает настроить нарезку текста для RAG-систем и векторных баз данных. Подбирает размер чанков от 256 до 1024 токенов и проверяет метрики извлечения. CX Codex +1 3 900установок Memory Vault MihaiBuilds Локальная база памяти для ИИ-агентов на PostgreSQL и pgvector. Сохраняет контекст проектов, решений и заметок между сессиями с гибридным поиском. CX Codex +1 ★ 65звёзд репо

Из того же набора240

Весь набор →
sqlalchemy Правило для Cursor задаёт стиль SQLAlchemy 2.x: типизированные модели, сессии через контекстный менеджер, select() вместо устаревшего Query. ★ 3 580звёзд репо springboot Настраивает Cursor на стандарты Spring Boot 3.x и Java 17+. Помогает генерировать чистую доменную архитектуру, DTO и обработку ошибок. ★ 3 580звёзд репо spring Правило подсказывает, как организовать Spring Boot-код, внедрять зависимости и обрабатывать типичные проблемы. Пригодится, когда агент пишет или пересматривает Java-код проекта. ★ 3 580звёзд репо sphinx Правило задаёт Cursor порядок работы с документацией Python: структура docs/, MyST, docstring в стиле Google и автогенерация API. Пригодится, когда проект уже оброс модулями, а документация живёт в голове автора. ★ 3 580звёзд репо solidjs Правило задаёт практики для SolidJS 1.x: реактивность, компоненты, состояние и очистка ресурсов. Применяется к JavaScript и JSX-файлам. ★ 3 580звёзд репо solidity Правило задаёт стиль и требования к Solidity-коду: фиксированный pragma, кастомные ошибки, защита от реентрантности. Пригодится, когда пишете контракты в Cursor. ★ 3 580звёзд репо socket-io Задаёт стандарты работы с Socket.IO v4.x в Cursor: защищённая инициализация, разделение по комнатам, обработка сбоев сети и подтверждение доставки. ★ 3 580звёзд репо smolagents Правило задаёт рекомендации по структуре приложений на smolagents и безопасности инструментов. Оно срабатывает для файлов Python. ★ 3 580звёзд репо

Спонсоры проекта

Компании, которые помогают держать аналитику открытой