jeremylongshore/tons-of-skills-marketplace / plugins/ai-ml/nlp-text-analyzer/skills/analyzing-text-with-nlp
analyzing-text-with-nlp
VerifiedExecute this skill enables AI assistant to perform natural language processing and text analysis using the nlp-text-analyzer plugin. it should be used when the user requests analysis of text, including sentiment analysis, keyword extraction, topic modeling, or ... Use when analyzing code or data. Trigger with phrases like 'analyze', 'review', or 'examine'.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 3.2 KB |
| assets/README.md | markdown | 434 B |
| assets/analysis_report_template.md | markdown | 3.3 KB |
| assets/error_handling_examples.md | markdown | 8.5 KB |
| assets/example_text_inputs.json | config | 2.8 KB |
| references/README.md | markdown | 60 B |
| scripts/README.md | markdown | 465 B |
| scripts/analyze_text.pyexecutable | script | 4.7 KB |
assets/error_handling_examples.md
View on GitHub# Error Handling Examples for NLP Text Analyzer Plugin
This document provides examples of how to handle common errors that may arise when using the NLP Text Analyzer plugin. Implementing robust error handling is crucial for building reliable and user-friendly applications.
## 1. API Rate Limits
API rate limits are a common occurrence, especially when using external NLP services. When you exceed the rate limit, the API will typically return an error code (e.g., 429 Too Many Requests).
**Handling Strategy:**
* **Retry Mechanism:** Implement an exponential backoff retry mechanism. This involves waiting for an increasing amount of time before retrying the request.
**Example:**
```
# Placeholder: Replace with your specific NLP API call
def analyze_text_with_retry(text, max_retries=5, initial_delay=1):
retries = 0
delay = initial_delay
while retries < max_retries:
try:
# Placeholder: Replace with your API call function
result = analyze_text(text)
return result
except Exception as e:
# Placeholder: Check for specific rate limit error code (e.g., 429)
if "rate limit exceeded" in str(e).lower():
retries += 1
print(f"Rate limit exceeded. Retrying in {delay} seconds...")
time.sleep(delay)
delay *= 2 # Exponential backoff
else:
raise # Re-raise other exceptions
raise Exception("Max retries reached. Unable to analyze text due to rate limits.")
# Example Usage:
# text = "Placeholder: Your text to analyze"
# try:
# analysis_result = analyze_text_with_retry(text)
# print(analysis_result)
# except Exception as e:
# print(f"Error during analysis: {e}")
```
**Instructions:**
1. Replace the placeholder comments with your actual code.
2. Adjust `max_retries` and `initial_delay` based on the API's recommendations.
3. Ensure the error check specifically targets rate-limiting errors. Different APIs have different error messages and codes.
## 2. Invalid Input
Invalid input can range from malformed text to unsupported languages.
**Handling Strategy:**
* **Input Validation:** Validate the input text before sending it to the NLP API.
* **Error Messages:** Provide informative error messages to the user.
**Example:**
```
# Placeholder: Replace with your specific NLP API call and language detection method
def analyze_text(text, language="en"):
# Placeholder: Add language detection here, if needed. Example:
# detected_language = detect_language(text)
# if detected_language != language:
# raise ValueError(f"Input language is not {language}. Detected: {detected_language}")
if not isinstance(text, str):
raise TypeError("Input must be a string.")
if not text:
raise ValueError("Input text cannot be empty.")
try:
# Placeholder: Replace with your API call function
result = perform_nlp_analysis(text, language)
return result
except Exception as e:
# Placeholder: Catch specific API errors related to invalid input
if "invalid input" in str(e).lower() or "bad request" in str(e).lower():
raise ValueError(f"Invalid input: {e}")
else:
raise # Re-raise other exceptions
# Example Usage:
# text = "Placeholder: Your text to analyze"
# try:
# analysis_result = analyze_text(text)
# print(analysis_result)
# except ValueError as e:
# print(f"Input error: {e}")
# except Exception as e:
# print(f"Other error: {e}")
```
**Instructions:**
1. Replace the placeholder comments with your actual code.
2. Implement input validation checks relevant to your specific NLP task (e.g., maximum text length, allowed characters).
3. Catch specific API errors related to invalid input and provide helpful error messages.
4. Consider adding language detection to automatically determine the input language and handle unsupported languages gracefully.
## 3. Network Errors
Network errors can occur due to connectivity issues or server problems.
**Handling Strategy:**
* **Retry Mechanism:** Similar to API rate limits, implement a retry mechanism with exponential backoff.
* **Timeout:** Set a timeout for API requests to prevent the application from hanging indefinitely.
**Example:**
```
import requests
import time
# Placeholder: Replace with your specific NLP API endpoint
API_ENDPOINT = "Placeholder: Your API endpoint URL"
def analyze_text_with_retry(text, max_retries=3, initial_delay=1, timeout=10):
retries = 0
delay = initial_delay
while retries < max_retries:
try:
# Placeholder: Replace with your API key or authentication method
headers = {"Authorization": "Bearer Placeholder: Your API Key"}
data = {"text": text}
response = requests.post(API_ENDPOINT, headers=headers, json=data, timeout=timeout)
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
return response.json()
except requests.exceptions.RequestException as e:
retries += 1
print(f"Network error: {e}. Retrying in {delay} seconds...")
time.sleep(delay)
delay *= 2
raise Exception("Max retries reached. Unable to analyze text due to network errors.")
# Example Usage:
# text = "Placeholder: Your text to analyze"
# try:
# analysis_result = analyze_text_with_retry(text)
# print(analysis_result)
# except Exception as e:
# print(f"Error during analysis: {e}")
```
**Instructions:**
1. Replace the placeholder comments with your actual code, including the API endpoint, authentication details, and request parameters.
2. Adjust `max_retries`, `initial_delay`, and `timeout` based on your network conditions and API requirements.
3. Use the `requests` library (or your preferred HTTP client) to make API requests.
4. Handle `requests.exceptions.RequestException` to catch various network-related errors.
## 4. Server-Side Errors
Server-side errors (e.g., 500 Internal Server Error) indicate problems on the NLP API's server.
**Handling Strategy:**
* **Retry Mechanism:** Implement a retry mechanism with exponential backoff, as these errors can be temporary.
* **Logging:** Log the error details for debugging purposes.
* **User Notification:** Inform the user that there is a problem with the service and that they should try again later.
**Example:**
```
import requests
import time
import logging
# Configure logging (optional)
logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
# Placeholder: Replace with your specific NLP API endpoint
API_ENDPOINT = "Placeholder: Your API endpoint URL"
def analyze_text_with_retry(text, max_retries=3, initial_delay=1, timeout=10):
retries = 0
delay = initial_delay
while retries < max_retries:
try:
# Placeholder: Replace with your API key or authentication method
headers = {"Authorization": "Bearer Placeholder: Your API Key"}
data = {"text": text}
response = requests.post(API_ENDPOINT, headers=headers, json=data, timeout=timeout)
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
return response.json()
except requests.exceptions.RequestException as e:
logging.error(f"API request failed: {e}") # Log the error
retries += 1
print(f"Server error. Retrying in {delay} seconds...")
time.sleep(delay)
delay *= 2
raise Exception("Max retries reached. Unable to analyze text due to server errors.")
# Example Usage:
# text = "Placeholder: Your text to analyze"
# try:
# analysis_result = analyze_text_with_retry(text)
# print(analysis_result)
# except Exception as e:
# print(f"Error during analysis: {e}")
# print("There was a problem with the service. Please try again later.") # User notification
```
**Instructions:**
1. Replace the placeholder comments with your actual code, including the API endpoint, authentication details, and request parameters.
2. Adjust `max_retries`, `initial_delay`, and `timeout` based on your network conditions and API requirements.
3. Implement logging to record error details for debugging.
4. Provide a user-friendly message to inform the user about the server-side error.
By implementing these error handling strategies, you can create more robust and reliable NLP applications. Remember to tailor these examples to your specific use case and the requirements of the NLP APIs you are using.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |