mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+231
-241
@@ -1,304 +1,294 @@
|
||||
---
|
||||
description: Provides comprehensive guidance on best practices for coding standards, performance, security, and testing in NLTK projects. This rule helps developers write clean, maintainable, and efficient NLP code using NLTK.
|
||||
globs: *.py
|
||||
description: This guide provides definitive, actionable best practices for writing maintainable, performant, and type-safe NLTK code in Python, emphasizing modern patterns and avoiding common pitfalls.
|
||||
globs: **/*.py
|
||||
---
|
||||
- Follow these guidelines for consistency and maintainability of your code.
|
||||
# `nltk` Best Practices
|
||||
|
||||
# NLTK Coding Standards and Best Practices
|
||||
|
||||
This document outlines coding standards and best practices for developing applications using the Natural Language Toolkit (NLTK) in Python.
|
||||
NLTK remains a cornerstone for classic NLP tasks. Integrate it effectively with modern Python by following these guidelines.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
### 1.1 Directory Structure Best Practices
|
||||
Organize NLTK operations into modular, reusable components.
|
||||
|
||||
Adopt a clear and logical directory structure to organize your NLTK project. A typical structure might look like this:
|
||||
|
||||
|
||||
my_nltk_project/
|
||||
├── data/ # Raw data files
|
||||
├── processed_data/ # Processed data files (e.g., tokenized, stemmed)
|
||||
├── models/ # Trained models (e.g., sentiment analysis models)
|
||||
├── scripts/ # Python scripts for data processing, training, etc.
|
||||
│ ├── __init__.py # Make the scripts directory a Python package
|
||||
│ ├── data_processing.py
|
||||
│ ├── model_training.py
|
||||
│ └── utils.py
|
||||
├── tests/ # Unit and integration tests
|
||||
│ ├── __init__.py
|
||||
│ ├── test_data_processing.py
|
||||
│ └── test_model_training.py
|
||||
├── notebooks/ # Jupyter notebooks for experimentation
|
||||
├── requirements.txt # Project dependencies
|
||||
├── README.md
|
||||
└── .gitignore
|
||||
|
||||
|
||||
* `data/`: Stores raw data used for training and analysis. Keep this separate and under version control if the dataset is small. For larger datasets, use DVC (Data Version Control) or similar.
|
||||
* `processed_data/`: Save intermediate and final processed data here. This avoids recomputing the same steps repeatedly. Consider using Parquet or Feather format for efficient storage and retrieval of processed dataframes.
|
||||
* `models/`: Stores trained NLTK models. Use a consistent naming convention (e.g., `sentiment_model_v1.pkl`) and consider using a model registry (e.g., MLflow, Weights & Biases) for managing model versions and metadata.
|
||||
* `scripts/`: Contains reusable Python modules for data processing, model training, and utility functions. Structure this directory into submodules if the project becomes complex.
|
||||
* `tests/`: Holds unit tests and integration tests to ensure code correctness. Aim for high test coverage.
|
||||
* `notebooks/`: Jupyter notebooks are useful for exploring data and prototyping code. Keep notebooks clean and well-documented, and consider refactoring useful code into the `scripts/` directory.
|
||||
* `requirements.txt`: Lists all project dependencies, allowing for easy installation via `pip install -r requirements.txt`. Use `pip freeze > requirements.txt` to generate this file.
|
||||
|
||||
### 1.2 File Naming Conventions
|
||||
|
||||
Follow consistent file naming conventions to improve readability and maintainability:
|
||||
|
||||
* Python scripts: Use snake_case (e.g., `data_processing.py`).
|
||||
* Data files: Use descriptive names that indicate the contents (e.g., `raw_text_data.csv`, `processed_data.parquet`).
|
||||
* Model files: Include versioning and relevant information in the name (e.g., `sentiment_model_v1.pkl`).
|
||||
* Test files: Prefix with `test_` and mirror the naming of the modules they test (e.g., `test_data_processing.py`).
|
||||
|
||||
### 1.3 Module Organization Best Practices
|
||||
|
||||
Organize your code into logical modules within the `scripts/` directory. Each module should focus on a specific task or set of related functionalities. Here's an example:
|
||||
|
||||
python
|
||||
# scripts/data_processing.py
|
||||
### 1.1 Modularize NLP Stages
|
||||
Encapsulate each NLP step (tokenization, stemming, POS tagging) in a dedicated, pure function or small class. This promotes testability and allows easy swapping of implementations (e.g., NLTK to spaCy).
|
||||
|
||||
❌ **BAD: Monolithic Script**
|
||||
```python
|
||||
import nltk
|
||||
from nltk.corpus import stopwords
|
||||
from nltk.stem import PorterStemmer
|
||||
|
||||
def tokenize_text(text):
|
||||
# Tokenize text using NLTK
|
||||
tokens = nltk.word_tokenize(text)
|
||||
return tokens
|
||||
|
||||
def remove_stopwords(tokens):
|
||||
# Remove stopwords using NLTK
|
||||
stopwords = nltk.corpus.stopwords.words('english')
|
||||
filtered_tokens = [token for token in tokens if token.lower() not in stopwords]
|
||||
return filtered_tokens
|
||||
|
||||
# scripts/model_training.py
|
||||
text = "NLTK is a powerful library for natural language processing."
|
||||
tokens = nltk.word_tokenize(text)
|
||||
filtered_tokens = [word for word in tokens if word.lower() not in stopwords.words('english')]
|
||||
stemmer = PorterStemmer()
|
||||
stemmed_tokens = [stemmer.stem(word) for word in filtered_tokens]
|
||||
print(stemmed_tokens)
|
||||
```
|
||||
|
||||
✅ **GOOD: Modular Functions**
|
||||
```python
|
||||
import nltk
|
||||
import pickle
|
||||
from nltk.corpus import stopwords
|
||||
from nltk.stem import PorterStemmer
|
||||
from typing import List
|
||||
|
||||
def train_sentiment_model(data):
|
||||
# Train a sentiment analysis model using NLTK
|
||||
# ...
|
||||
model = ...
|
||||
return model
|
||||
def tokenize_text(text: str) -> List[str]:
|
||||
"""Tokenizes text into words."""
|
||||
return nltk.word_tokenize(text)
|
||||
|
||||
def save_model(model, filepath):
|
||||
# Save the trained model to a file
|
||||
with open(filepath, 'wb') as f:
|
||||
pickle.dump(model, f)
|
||||
def remove_stopwords(tokens: List[str], lang: str = 'english') -> List[str]:
|
||||
"""Removes common stopwords from a list of tokens."""
|
||||
stop_words = set(stopwords.words(lang))
|
||||
return [word for word in tokens if word.lower() not in stop_words]
|
||||
|
||||
def stem_tokens(tokens: List[str]) -> List[str]:
|
||||
"""Applies Porter Stemming to a list of tokens."""
|
||||
stemmer = PorterStemmer()
|
||||
return [stemmer.stem(word) for word in tokens]
|
||||
|
||||
### 1.4 Component Architecture Recommendations
|
||||
|
||||
Design your application with a clear component architecture. Consider using a layered architecture:
|
||||
|
||||
* **Data Layer:** Handles data loading, cleaning, and preprocessing. Responsible for interacting with data sources (e.g., files, databases, APIs).
|
||||
* **Processing Layer:** Implements NLP tasks such as tokenization, stemming, lemmatization, POS tagging, and parsing. This layer relies heavily on NLTK functionalities.
|
||||
* **Model Layer:** Trains and evaluates NLP models. Includes code for feature extraction, model selection, and hyperparameter tuning.
|
||||
* **Application Layer:** Provides the user interface or API for interacting with the NLP application. Handles user input and presents results.
|
||||
|
||||
### 1.5 Code Splitting Strategies
|
||||
|
||||
Break down large code files into smaller, more manageable chunks. Use functions, classes, and modules to improve code organization and reusability.
|
||||
|
||||
* **Functions:** Encapsulate specific tasks into well-defined functions with clear inputs and outputs. Use docstrings to document function purpose, arguments, and return values.
|
||||
* **Classes:** Group related data and functions into classes. Use inheritance and polymorphism to create reusable and extensible code. For example, you could create a base `TextProcessor` class and subclasses for different text processing tasks (e.g., `SentimentAnalyzer`, `TopicModeler`).
|
||||
* **Modules:** Organize code into modules based on functionality. Use relative imports to access modules within the same package.
|
||||
# Usage
|
||||
document = "NLTK is a powerful library for natural language processing."
|
||||
tokens = tokenize_text(document)
|
||||
filtered = remove_stopwords(tokens)
|
||||
stemmed = stem_tokens(filtered)
|
||||
print(stemmed)
|
||||
```
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
### 2.1 Design Patterns
|
||||
### 2.1 Lazy Data Loading
|
||||
Download NLTK corpora *once* during setup or on first run, not repeatedly. Guard `nltk.download()` calls.
|
||||
|
||||
* **Strategy Pattern:** Use this pattern to implement different text processing algorithms (e.g., stemming algorithms). Define a common interface for all algorithms and allow the client to choose the desired algorithm at runtime.
|
||||
* **Factory Pattern:** Use this pattern to create different types of NLTK objects (e.g., different types of tokenizers or stemmers). This promotes loose coupling and makes it easier to switch between different implementations.
|
||||
* **Observer Pattern:** Use this pattern to notify interested components when data changes (e.g., when a new document is added to a corpus). This can be useful for real-time NLP applications.
|
||||
❌ **BAD: Repeated Downloads**
|
||||
```python
|
||||
import nltk
|
||||
# This will run every time the script executes
|
||||
nltk.download('punkt')
|
||||
nltk.download('stopwords')
|
||||
|
||||
### 2.2 Recommended Approaches for Common Tasks
|
||||
def process_text(text: str) -> List[str]:
|
||||
# ...
|
||||
pass
|
||||
```
|
||||
|
||||
* **Text Preprocessing:** Create a reusable pipeline for text preprocessing. This pipeline should include tokenization, lowercasing, punctuation removal, stopword removal, stemming/lemmatization, and any other necessary steps.
|
||||
* **Feature Extraction:** Use NLTK's feature extraction capabilities (e.g., bag-of-words, TF-IDF) to convert text data into numerical features suitable for machine learning models.
|
||||
* **Model Training:** Use scikit-learn or other machine learning libraries in conjunction with NLTK to train and evaluate NLP models. Use cross-validation to estimate model performance and hyperparameter tuning to optimize model parameters.
|
||||
* **Sentiment Analysis:** Use pre-trained sentiment analysis models (e.g., VADER) or train your own model using NLTK and scikit-learn.
|
||||
* **Topic Modeling:** Use NLTK and Gensim to perform topic modeling on text data.
|
||||
✅ **GOOD: Lazy, Guarded Download**
|
||||
```python
|
||||
import nltk
|
||||
import os
|
||||
|
||||
### 2.3 Anti-patterns and Code Smells
|
||||
def ensure_nltk_data(corpus_name: str):
|
||||
"""Downloads NLTK data if not already present."""
|
||||
try:
|
||||
nltk.data.find(f'corpora/{corpus_name}')
|
||||
except nltk.downloader.DownloadError:
|
||||
print(f"Downloading NLTK data: {corpus_name}...")
|
||||
nltk.download(corpus_name)
|
||||
print(f"Downloaded {corpus_name}.")
|
||||
|
||||
* **Global Variables:** Avoid using global variables, as they can lead to unexpected side effects and make code difficult to debug. Use dependency injection or other techniques to pass data between components.
|
||||
* **Hardcoded Values:** Avoid hardcoding values in your code. Use configuration files or environment variables to store configurable parameters.
|
||||
* **Long Functions:** Avoid writing long, complex functions. Break down large functions into smaller, more manageable chunks.
|
||||
* **Code Duplication:** Avoid duplicating code. Create reusable functions or classes to encapsulate common functionality.
|
||||
* **Ignoring Errors:** Don't ignore errors. Implement proper error handling to prevent unexpected crashes and provide informative error messages.
|
||||
* **Over-commenting/Under-commenting:** Strike a balance. Comments should explain *why* you're doing something, not *what* the code is already clearly showing. Conversely, don't under-comment complex logic.
|
||||
# In your application's entry point or setup script:
|
||||
# ensure_nltk_data('punkt')
|
||||
# ensure_nltk_data('stopwords')
|
||||
|
||||
### 2.4 State Management Best Practices
|
||||
# In your processing logic, assume data is available
|
||||
def process_text(text: str) -> List[str]:
|
||||
# ...
|
||||
pass
|
||||
```
|
||||
|
||||
* **Stateless Functions:** Prefer stateless functions whenever possible. Stateless functions are easier to test and reason about.
|
||||
* **Immutable Data Structures:** Use immutable data structures whenever possible. Immutable data structures prevent accidental modification of data and make code more robust.
|
||||
* **Configuration Management:** Use a configuration management library (e.g., `configparser`) to store application settings and parameters. This makes it easier to change settings without modifying code.
|
||||
### 2.2 Avoid Global State
|
||||
Pass NLTK objects (like tokenizers, stemmers, lemmatizers) explicitly to functions. This improves testability and thread safety.
|
||||
|
||||
### 2.5 Error Handling Patterns
|
||||
❌ **BAD: Global Stemmer Instance**
|
||||
```python
|
||||
from nltk.stem import PorterStemmer
|
||||
_GLOBAL_STEMMER = PorterStemmer() # Global state
|
||||
|
||||
* **Try-Except Blocks:** Use `try-except` blocks to handle potential exceptions. Catch specific exceptions rather than using a generic `except` block. Log exceptions for debugging purposes.
|
||||
* **Logging:** Use the `logging` module to log events, errors, and warnings. Configure logging levels to control the amount of information that is logged. Use a consistent logging format.
|
||||
* **Custom Exceptions:** Define custom exceptions for specific error conditions in your application. This makes it easier to handle errors in a consistent and informative way.
|
||||
def process_document(text: str) -> List[str]:
|
||||
tokens = nltk.word_tokenize(text)
|
||||
return [_GLOBAL_STEMMER.stem(t) for t in tokens]
|
||||
```
|
||||
|
||||
✅ **GOOD: Pass Objects Explicitly**
|
||||
```python
|
||||
from nltk.stem import PorterStemmer
|
||||
from typing import List, Callable
|
||||
|
||||
# Define a type alias for clarity
|
||||
StemmerFunc = Callable[[str], str]
|
||||
|
||||
def process_document(text: str, stemmer: StemmerFunc) -> List[str]:
|
||||
tokens = nltk.word_tokenize(text)
|
||||
return [stemmer(t) for t in tokens]
|
||||
|
||||
# Usage
|
||||
porter_stemmer = PorterStemmer().stem # Get the bound method
|
||||
processed_text = process_document("running ran runner", stemmer=porter_stemmer)
|
||||
print(processed_text)
|
||||
```
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### 3.1 Optimization Techniques
|
||||
### 3.1 Choose the Right Tokenizer
|
||||
For large corpora, `RegexpTokenizer` or `ToktokTokenizer` are significantly faster than `nltk.word_tokenize`.
|
||||
|
||||
* **Profiling:** Use profiling tools (e.g., `cProfile`) to identify performance bottlenecks in your code.
|
||||
* **Vectorization:** Use NumPy's vectorized operations to perform calculations on arrays of data. Vectorization is significantly faster than looping over individual elements.
|
||||
* **Caching:** Use caching to store frequently accessed data and avoid redundant calculations. Use the `functools.lru_cache` decorator for simple caching.
|
||||
* **Parallelization:** Use multiprocessing or threading to parallelize computationally intensive tasks.
|
||||
* **Efficient Data Structures:** Choose appropriate data structures for your needs. Use sets for fast membership testing and dictionaries for fast key-value lookups.
|
||||
* **Lazy Loading:** Defer the loading of large datasets or models until they are actually needed. This can significantly reduce startup time.
|
||||
❌ **BAD: Default `word_tokenize` for Large Scale**
|
||||
```python
|
||||
import nltk
|
||||
# Slow for very large texts
|
||||
tokens = nltk.word_tokenize(long_text_data)
|
||||
```
|
||||
|
||||
### 3.2 Memory Management
|
||||
✅ **GOOD: Faster Tokenizers for Production**
|
||||
```python
|
||||
from nltk.tokenize import RegexpTokenizer, ToktokTokenizer
|
||||
from typing import List
|
||||
|
||||
* **Generators:** Use generators to process large datasets in chunks. Generators produce data on demand, which reduces memory consumption.
|
||||
* **Data Type Optimization:** Use the most appropriate data types for your data. For example, use `int8` instead of `int64` if your integers are within the range of `int8`.
|
||||
* **Garbage Collection:** Be aware of Python's garbage collection mechanism. Avoid creating circular references that can prevent objects from being garbage collected.
|
||||
* **Del Statements:** Use `del` statements to explicitly release memory when objects are no longer needed.
|
||||
def tokenize_regex(text: str) -> List[str]:
|
||||
"""Uses RegexpTokenizer for faster word tokenization."""
|
||||
tokenizer = RegexpTokenizer(r'\w+')
|
||||
return tokenizer.tokenize(text)
|
||||
|
||||
### 3.3 Rendering Optimization (If Applicable)
|
||||
def tokenize_toktok(text: str) -> List[str]:
|
||||
"""Uses ToktokTokenizer for robust, faster word tokenization."""
|
||||
tokenizer = ToktokTokenizer()
|
||||
return tokenizer.tokenize(text)
|
||||
|
||||
NLTK itself doesn't typically involve direct rendering like a UI framework, but visualization of NLTK outputs (e.g., parse trees) might. In such cases, libraries like Matplotlib or Graphviz might be used. Follow their respective optimization guides.
|
||||
long_text_data = "This is some very long text that needs efficient tokenization." * 1000
|
||||
# Prefer one of these for performance
|
||||
tokens_regex = tokenize_regex(long_text_data)
|
||||
tokens_toktok = tokenize_toktok(long_text_data)
|
||||
```
|
||||
|
||||
### 3.4 Bundle Size Optimization
|
||||
## 4. Common Pitfalls and Gotchas
|
||||
|
||||
This is more relevant for web applications using NLTK in the backend. For such cases:
|
||||
### 4.1 Lemmatization Requires POS Tags
|
||||
`WordNetLemmatizer` performs better when provided with a Part-of-Speech (POS) tag. Without it, it defaults to noun ('n').
|
||||
|
||||
* **Dependency Management:** Only include the NLTK modules that are actually needed by your application. Avoid importing entire modules if you only need a few functions.
|
||||
* **Code Minification:** Minify your Python code to reduce its size. This can be done using tools like `pyminify`.
|
||||
❌ **BAD: Lemmatizing without POS**
|
||||
```python
|
||||
from nltk.stem import WordNetLemmatizer
|
||||
lemmatizer = WordNetLemmatizer()
|
||||
print(lemmatizer.lemmatize("running")) # Output: 'running' (incorrect for verb)
|
||||
```
|
||||
|
||||
### 3.5 Lazy Loading Strategies
|
||||
✅ **GOOD: Lemmatizing with POS Tags**
|
||||
```python
|
||||
from nltk.stem import WordNetLemmatizer
|
||||
from nltk.corpus import wordnet
|
||||
from typing import List, Tuple
|
||||
|
||||
* **Delayed Imports:** Import NLTK modules only when they are needed. This can reduce startup time if your application doesn't use all of NLTK's functionalities.
|
||||
* **Conditional Loading:** Load different NLTK modules based on the application's configuration. This allows you to only load the modules that are relevant to the current task.
|
||||
def get_wordnet_pos(tag: str) -> str:
|
||||
"""Map NLTK POS tag to WordNet POS tag."""
|
||||
if tag.startswith('J'): return wordnet.ADJ
|
||||
elif tag.startswith('V'): return wordnet.VERB
|
||||
elif tag.startswith('N'): return wordnet.NOUN
|
||||
elif tag.startswith('R'): return wordnet.ADV
|
||||
else: return wordnet.NOUN # Default to noun
|
||||
|
||||
## 4. Security Best Practices
|
||||
def lemmatize_with_pos(tokens: List[str]) -> List[str]:
|
||||
"""Lemmatizes tokens using WordNetLemmatizer with POS tags."""
|
||||
lemmatizer = WordNetLemmatizer()
|
||||
tagged_tokens = nltk.pos_tag(tokens) # Requires 'averaged_perceptron_tagger'
|
||||
return [lemmatizer.lemmatize(word, pos=get_wordnet_pos(tag)) for word, tag in tagged_tokens]
|
||||
|
||||
### 4.1 Common Vulnerabilities and Prevention
|
||||
# Ensure data is downloaded:
|
||||
# ensure_nltk_data('wordnet')
|
||||
# ensure_nltk_data('averaged_perceptron_tagger')
|
||||
|
||||
* **Arbitrary Code Execution:** Be careful when loading data from untrusted sources. Avoid using `eval()` or `pickle.load()` on untrusted data, as these can be exploited to execute arbitrary code. Use safer alternatives such as JSON or CSV.
|
||||
* **Denial of Service (DoS):** Protect your application from DoS attacks by limiting the size of input data and using appropriate timeouts. Avoid unbounded loops or recursive functions that can consume excessive resources.
|
||||
* **Regular Expression Denial of Service (ReDoS):** Be careful when using regular expressions, as complex regular expressions can be exploited to cause ReDoS attacks. Use simple and efficient regular expressions, and limit the backtracking depth.
|
||||
tokens = ["The", "cats", "are", "running", "fast"]
|
||||
lemmas = lemmatize_with_pos(tokens)
|
||||
print(lemmas) # Output: ['The', 'cat', 'be', 'run', 'fast']
|
||||
```
|
||||
|
||||
### 4.2 Input Validation
|
||||
## 5. Type Hints
|
||||
|
||||
* **Data Type Validation:** Validate the data type of input values to prevent type errors and unexpected behavior.
|
||||
* **Range Validation:** Validate that input values are within acceptable ranges.
|
||||
* **Format Validation:** Validate that input data is in the expected format (e.g., using regular expressions).
|
||||
* **Sanitization:** Sanitize input data to remove potentially harmful characters or code. Use appropriate escaping techniques to prevent injection attacks.
|
||||
Always use type hints for NLTK functions, especially for inputs (`str`, `List[str]`) and outputs (`List[str]`, `nltk.Tree`). This improves readability, enables static analysis, and enhances IDE support.
|
||||
|
||||
### 4.3 Authentication and Authorization
|
||||
```python
|
||||
import nltk
|
||||
from nltk.tree import Tree
|
||||
from typing import List, Tuple
|
||||
|
||||
* **Authentication:** Implement authentication to verify the identity of users. Use strong passwords and multi-factor authentication.
|
||||
* **Authorization:** Implement authorization to control access to resources. Use role-based access control (RBAC) to assign permissions to users based on their roles.
|
||||
def pos_tag_sentence(tokens: List[str]) -> List[Tuple[str, str]]:
|
||||
"""Performs Part-of-Speech tagging on a list of tokens."""
|
||||
# Requires 'averaged_perceptron_tagger'
|
||||
return nltk.pos_tag(tokens)
|
||||
|
||||
### 4.4 Data Protection
|
||||
def chunk_entities(tagged_tokens: List[Tuple[str, str]]) -> Tree:
|
||||
"""Chunks named entities from POS-tagged tokens."""
|
||||
# Requires 'maxent_ne_chunker' and 'words'
|
||||
return nltk.chunk.ne_chunk(tagged_tokens)
|
||||
|
||||
* **Encryption:** Encrypt sensitive data at rest and in transit. Use strong encryption algorithms and manage keys securely.
|
||||
* **Data Masking:** Mask sensitive data when displaying it to users. This can prevent unauthorized access to sensitive information.
|
||||
* **Access Control:** Implement strict access control policies to limit access to sensitive data. Only grant access to users who need it.
|
||||
# Usage
|
||||
sentence_tokens = ["Apple", "Inc.", "is", "headquartered", "in", "Cupertino", "."]
|
||||
tagged = pos_tag_sentence(sentence_tokens)
|
||||
entities = chunk_entities(tagged)
|
||||
print(entities)
|
||||
```
|
||||
|
||||
### 4.5 Secure API Communication
|
||||
## 6. Virtual Environments and Packaging
|
||||
|
||||
* **HTTPS:** Use HTTPS to encrypt communication between your application and the API. This prevents eavesdropping and man-in-the-middle attacks.
|
||||
* **API Keys:** Use API keys to authenticate your application with the API. Protect your API keys and do not embed them in your code.
|
||||
* **Rate Limiting:** Implement rate limiting to prevent abuse of your API. This can protect your API from DoS attacks.
|
||||
### 6.1 Use Virtual Environments
|
||||
Always develop NLTK projects within a virtual environment. This isolates dependencies and prevents conflicts.
|
||||
|
||||
## 5. Testing Approaches
|
||||
```bash
|
||||
# Create a virtual environment
|
||||
python -m venv .venv
|
||||
# Activate it
|
||||
source .venv/bin/activate # Linux/macOS
|
||||
.venv\Scripts\activate # Windows
|
||||
# Install NLTK and other dependencies
|
||||
pip install nltk
|
||||
```
|
||||
|
||||
### 5.1 Unit Testing
|
||||
### 6.2 Manage Dependencies with `pyproject.toml`
|
||||
Use `pyproject.toml` for modern dependency management and project configuration.
|
||||
|
||||
* **Test-Driven Development (TDD):** Write unit tests before writing code. This helps you to design your code in a testable way and ensures that your code meets the requirements.
|
||||
* **Test Coverage:** Aim for high test coverage. Use a test coverage tool to measure the percentage of code that is covered by tests.
|
||||
* **Assertions:** Use assertions to verify that your code is behaving as expected. Use informative error messages to help you debug your code.
|
||||
```toml
|
||||
# pyproject.toml
|
||||
[project]
|
||||
name = "my-nltk-project"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"nltk>=3.8",
|
||||
"regex", # Often useful with NLTK
|
||||
]
|
||||
|
||||
### 5.2 Integration Testing
|
||||
[tool.black]
|
||||
line-length = 79
|
||||
|
||||
* **Test Dependencies:** Test the integration of your code with external dependencies (e.g., databases, APIs). Use mock objects or test doubles to isolate your code from external dependencies during unit testing.
|
||||
* **Test Scenarios:** Test different scenarios to ensure that your code handles different inputs and edge cases correctly.
|
||||
[tool.ruff]
|
||||
line-length = 79
|
||||
select = ["E", "F", "W", "I"] # Common rules
|
||||
```
|
||||
|
||||
### 5.3 End-to-End Testing
|
||||
## 7. Testing Approaches
|
||||
|
||||
* **User Interface Testing:** Test the user interface of your application to ensure that it is working as expected. Use automated testing tools to automate UI testing.
|
||||
* **System Testing:** Test the entire system to ensure that all components are working together correctly.
|
||||
### 7.1 Unit Test Pure Functions
|
||||
Since NLTK operations are often encapsulated in pure functions, unit testing is straightforward.
|
||||
|
||||
### 5.4 Test Organization
|
||||
```python
|
||||
# test_nlp_utils.py
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from my_module import remove_stopwords, tokenize_text # Assuming your functions are in my_module
|
||||
|
||||
* **Test Directory:** Create a separate `tests/` directory to store your tests.
|
||||
* **Test Modules:** Create separate test modules for each module in your application. Use the same naming conventions as your application modules.
|
||||
* **Test Classes:** Group related tests into test classes. Use descriptive names for your test classes.
|
||||
* **Test Functions:** Create separate test functions for each test case. Use descriptive names for your test functions.
|
||||
class TestNlpUtils(unittest.TestCase):
|
||||
def test_remove_stopwords(self):
|
||||
tokens = ["This", "is", "a", "test", "sentence", "for", "stopwords"]
|
||||
expected = ["test", "sentence", "stopwords"]
|
||||
# Mock stopwords to ensure test isolation
|
||||
with patch('nltk.corpus.stopwords.words', return_value=['this', 'is', 'a', 'for']):
|
||||
self.assertEqual(remove_stopwords(tokens), expected)
|
||||
|
||||
### 5.5 Mocking and Stubbing
|
||||
def test_tokenize_text(self):
|
||||
text = "Hello, world!"
|
||||
# Mock nltk.word_tokenize to control behavior
|
||||
with patch('nltk.word_tokenize', return_value=['Hello', ',', 'world', '!']):
|
||||
self.assertEqual(tokenize_text(text), ['Hello', ',', 'world', '!'])
|
||||
|
||||
* **Mock Objects:** Use mock objects to replace external dependencies during unit testing. This allows you to isolate your code from external dependencies and test it in a controlled environment.
|
||||
* **Stubbing:** Use stubbing to replace complex or time-consuming operations with simple or fast operations during testing. This can improve the speed and reliability of your tests.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### 6.1 Frequent Mistakes
|
||||
|
||||
* **Incorrect Data Preparation:** Failing to properly clean and prepare data can lead to inaccurate results.
|
||||
* **Overfitting:** Training a model that is too complex for the data can lead to overfitting. Use regularization techniques and cross-validation to prevent overfitting.
|
||||
* **Ignoring Rare Words:** Ignoring rare words can lead to a loss of information. Use techniques such as stemming or lemmatization to group related words together.
|
||||
* **Using Default Parameters:** Relying on default parameters without understanding their implications can lead to unexpected behavior.
|
||||
* **Not Documenting Code:** Failing to document code makes it difficult to understand and maintain.
|
||||
|
||||
### 6.2 Edge Cases
|
||||
|
||||
* **Empty Strings:** Handle empty strings gracefully.
|
||||
* **Special Characters:** Handle special characters correctly.
|
||||
* **Unicode:** Handle Unicode characters correctly. Use UTF-8 encoding for all text files.
|
||||
* **Missing Data:** Handle missing data appropriately.
|
||||
|
||||
### 6.3 Version-Specific Issues
|
||||
|
||||
* **API Changes:** Be aware of API changes between different versions of NLTK. Check the NLTK documentation for migration guides.
|
||||
* **Dependency Conflicts:** Be aware of potential dependency conflicts between NLTK and other libraries. Use a virtual environment to isolate your project's dependencies.
|
||||
|
||||
### 6.4 Compatibility Concerns
|
||||
|
||||
* **Python Version:** Ensure that your code is compatible with the desired version of Python.
|
||||
* **Operating System:** Be aware of potential compatibility issues between different operating systems.
|
||||
|
||||
### 6.5 Debugging Strategies
|
||||
|
||||
* **Print Statements:** Use `print` statements to debug your code. Print the values of variables and the results of calculations.
|
||||
* **Debuggers:** Use a debugger to step through your code and inspect the values of variables.
|
||||
* **Logging:** Use the `logging` module to log events, errors, and warnings. Review the logs to identify the cause of problems.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### 7.1 Recommended Development Tools
|
||||
|
||||
* **IDE:** Use an IDE such as Visual Studio Code, PyCharm, or Spyder. These IDEs provide features such as code completion, debugging, and refactoring.
|
||||
* **Virtual Environment Manager:** Use a virtual environment manager such as `venv` or `conda` to isolate your project's dependencies.
|
||||
* **Package Manager:** Use a package manager such as `pip` or `conda` to install and manage your project's dependencies.
|
||||
* **Version Control System:** Use a version control system such as Git to track changes to your code.
|
||||
* **Data Version Control (DVC):** For larger datasets that are under version control use DVC.
|
||||
|
||||
### 7.2 Build Configuration
|
||||
|
||||
* **Makefile:** Use a `Makefile` to automate common tasks such as building, testing, and deploying your application.
|
||||
* **Setup Script:** Use a `setup.py` script to package your application for distribution.
|
||||
|
||||
### 7.3 Linting and Formatting
|
||||
|
||||
* **Linting:** Use a linter such as `pylint` or `flake8` to check your code for style errors and potential problems.
|
||||
* **Formatting:** Use a code formatter such as `black` or `autopep8` to automatically format your code according to a consistent style guide.
|
||||
|
||||
### 7.4 Deployment
|
||||
|
||||
* **Containerization:** Use containerization technologies such as Docker to package your application and its dependencies into a self-contained unit. This makes it easier to deploy your application to different environments.
|
||||
* **Cloud Platform:** Deploy your application to a cloud platform such as AWS, Google Cloud, or Azure.
|
||||
|
||||
### 7.5 CI/CD Integration
|
||||
|
||||
* **Continuous Integration:** Use a continuous integration (CI) system such as Jenkins, Travis CI, or CircleCI to automatically build and test your code whenever changes are pushed to your version control system.
|
||||
* **Continuous Deployment:** Use a continuous deployment (CD) system to automatically deploy your application to production after it has passed all tests.
|
||||
|
||||
By adhering to these coding standards and best practices, you can develop NLTK applications that are clean, maintainable, efficient, and secure.
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
```
|
||||
Reference in New Issue
Block a user