Docs

Python SDK Examples

This guide demonstrates comprehensive examples of using the Sentor Python SDK.

Installation

pip install sentor-python-sdk

Basic Usage

from sentor import SentorClient

# Initialize the client
client = SentorClient('your-api-key')

# Analyze sentiment
input_data = [
    {
      "doc": "In the competitive landscape of consumer electronics, Apple and Samsung continue to lead the market with innovative products and strong brand loyalty. While Apple focuses on a tightly integrated ecosystem with devices like the iPhone, iPad, and Mac, Samsung excels in offering a wide range of options across various price points, especially in its Galaxy smartphone lineup. Both companies push the boundaries of technology, from cutting-edge chipsets to advanced camera systems, often setting industry trends that others follow.",
      "doc_id": "0",
      "entities": [
        "Apple",
        "Samsung",
        "camera"
      ]
    },
    {
      "doc": "Apple's new iPhone is amazing!",
      "doc_id": "1",
      "entities": [
        "Apple",
        "iPhone"
      ]
    },
    {
      "doc": "Samsung's new phone is amazing!",
      "doc_id": "2",
      "entities": [
        "Samsung",
        "phone"
      ]
    }
  ]
result = client.predict(input_data)
print(result)

Sample Output

{
  "results": [
    {
      "doc_id": "0",
      "predicted_class": 2,
      "predicted_label": "positive",
      "probabilities": {
        "negative": 0.00007679959526285529,
        "neutral": 0.0002924697764683515,
        "positive": 0.9996306896209717
      },
      "details": [
        {
          "sentence_index": 0,
          "sentence_text": "In the competitive landscape of consumer electronics, Apple and Samsung continue to lead the market with innovative products and strong brand loyalty.",
          "predicted_class": 2,
          "predicted_label": "positive",
          "probabilities": {
            "negative": 0.00009389198385179043,
            "neutral": 0.00032428017584607005,
            "positive": 0.9995818734169006
          }
        },
        {
          "sentence_index": 1,
          "sentence_text": "While Apple focuses on a tightly integrated ecosystem with devices like the iPhone, iPad, and Mac, Samsung excels in offering a wide range of options across various price points, especially in its Galaxy smartphone lineup.",
          "predicted_class": 2,
          "predicted_label": "positive",
          "probabilities": {
            "negative": 0.00005746580063714646,
            "neutral": 0.00012963586777914315,
            "positive": 0.99981290102005
          }
        },
        {
          "sentence_index": 2,
          "sentence_text": "Both companies push the boundaries of technology, from cutting-edge chipsets to advanced camera systems, often setting industry trends that others follow.",
          "predicted_class": 2,
          "predicted_label": "positive",
          "probabilities": {
            "negative": 0.00006366783054545522,
            "neutral": 0.00044553453335538507,
            "positive": 0.9994907379150391
          }
        }
      ]
    },
    {
      "doc_id": "1",
      "predicted_class": 2,
      "predicted_label": "positive",
      "probabilities": {
        "negative": 0.00010637375817168504,
        "neutral": 0.0002509312762413174,
        "positive": 0.9996427297592163
      },
      "details": [
        {
          "sentence_index": 0,
          "sentence_text": "Apple's new iPhone is amazing!",
          "predicted_class": 2,
          "predicted_label": "positive",
          "probabilities": {
            "negative": 0.00010637375817168504,
            "neutral": 0.0002509312762413174,
            "positive": 0.9996427297592163
          }
        }
      ]
    },
    {
      "doc_id": "2",
      "predicted_class": 2,
      "predicted_label": "positive",
      "probabilities": {
        "negative": 0.00010637375817168504,
        "neutral": 0.0002509312762413174,
        "positive": 0.9996427297592163
      },
      "details": [
        {
          "sentence_index": 0,
          "sentence_text": "Samsung's new phone is amazing!",
          "predicted_class": 2,
          "predicted_label": "positive",
          "probabilities": {
            "negative": 0.00010637375817168504,
            "neutral": 0.0002509312762413174,
            "positive": 0.9996427297592163
          }
        }
      ]
    }
  ]
}

Clustering Documents

from sentor import SentorClient

client = SentorClient('your-api-key')

# Prepare documents for clustering (minimum 5 required)
documents = [
    {
        "doc_id": "doc1",
        "text": "Apple announced new iPhone features with improved camera.",
        "entities": ["Apple", "iPhone", "camera"]
    },
    {
        "doc_id": "doc2",
        "text": "Samsung launched Galaxy with advanced AI capabilities.",
        "entities": ["Samsung", "Galaxy", "AI"]
    },
    {
        "doc_id": "doc3",
        "text": "Apple plans to integrate AI into iOS ecosystem.",
        "entities": ["Apple", "AI", "iOS"]
    },
    {
        "doc_id": "doc4",
        "text": "SpaceX successfully launched Starlink satellites.",
        "entities": ["SpaceX", "Starlink", "satellites"]
    },
    {
        "doc_id": "doc5",
        "text": "Bitcoin price surged after ETF approval.",
        "entities": ["Bitcoin", "ETF"]
    }
]

# Cluster documents
clustering_result = client.cluster(documents, language='en')

print(f"Total clusters: {clustering_result['total_clusters']}")
print(f"Outliers: {clustering_result['outliers']}")

for cluster in clustering_result['clusters']:
    print(f"\nCluster {cluster['cluster_id']}:")
    print(f"  Documents: {len(cluster['documents'])}")
    print(f"  Top words: {', '.join(cluster['top_words'][:5])}")
    print(f"  Entities: {', '.join(cluster['entities'][:5])}")

Generating Topic Names

from sentor import SentorClient

client = SentorClient('your-api-key')

# After clustering, generate topic names for each cluster
clustering_result = client.cluster(documents, language='en')

for cluster in clustering_result['clusters']:
    topic_result = client.generate_topic_name(
        cluster_id=cluster['cluster_id'],
        documents=cluster['documents'],
        entities=cluster['entities'],
        top_words=cluster['top_words'],
        language='en'
    )
    
    print(f"Cluster {cluster['cluster_id']}: {topic_result['topic_name']}")

Complete Workflow Example

from sentor import SentorClient
import time

client = SentorClient('your-api-key')

# Step 1: Analyze sentiment
input_data = [
    {
        "doc": "Apple's new iPhone camera is incredible, but battery life is disappointing.",
        "doc_id": "review1",
        "entities": ["Apple", "iPhone", "camera", "battery life"]
    },
    {
        "doc": "Samsung Galaxy has amazing features and great performance.",
        "doc_id": "review2",
        "entities": ["Samsung", "Galaxy", "features", "performance"]
    },
    # ... more reviews (need at least 5 for clustering)
]

sentiment_results = client.predict(input_data)

# Step 2: Extract documents for clustering
cluster_documents = []
for result in sentiment_results['results']:
    original_doc = next(d for d in input_data if d['doc_id'] == result['doc_id'])
    cluster_documents.append({
        "doc_id": result['doc_id'],
        "text": original_doc['doc'],
        "entities": original_doc['entities']
    })

# Step 3: Cluster documents (if enough documents)
if len(cluster_documents) >= 5:
    clustering_result = client.cluster(cluster_documents)
    
    # Step 4: Generate topic names for each cluster
    for cluster in clustering_result['clusters']:
        topic_result = client.generate_topic_name(
            cluster_id=cluster['cluster_id'],
            documents=cluster['documents'],
            entities=cluster['entities'],
            top_words=cluster['top_words']
        )
        
        print(f"\n=== {topic_result['topic_name']} ===")
        print(f"Documents in cluster: {len(cluster['documents'])}")
        
        # Calculate sentiment distribution
        sentiments = {"positive": 0, "neutral": 0, "negative": 0}
        for doc in cluster['documents']:
            sentiment_data = next(
                r for r in sentiment_results['results'] 
                if r['doc_id'] == doc['doc_id']
            )
            sentiments[sentiment_data['predicted_label']] += 1
        
        print(f"Sentiment distribution: {sentiments}")

Error Handling

from sentor.exceptions import (
    SentorAPIError,
    RateLimitError,
    AuthenticationError
)

def handle_prediction(documents: list):
    try:
        result = client.predict(documents)
        return result
    except RateLimitError as e:
        print(f"Rate limit exceeded. Retry after {e.retry_after} seconds")
        time.sleep(e.retry_after)
        return client.predict(documents)
    except AuthenticationError:
        print("Invalid API key or authentication failed")
        raise
    except SentorAPIError as e:
        print(f"API Error: {e.message} (Code: {e.code})")
        raise

For more examples and detailed documentation, please refer to: