File size: 1,205 Bytes
2f879b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import requests
from textblob import TextBlob
import matplotlib.pyplot as plt
import os

# Replace with your API endpoint and parameters
from dotenv import load_dotenv


# Load environment variables from .env file
load_dotenv()

headers = {
    "X-RapidAPI-Key": os.getenv("PARAM1"),
    "X-RapidAPI-Host": os.getenv("PARAM2")
}



# Make the API request
url = "https://twinword-sentiment-analysis.p.rapidapi.com/analyze/"

querystring = {"text":input("Enter the text to analyze:")}


# Make the API request
response = requests.get(url, headers=headers, params=querystring)

# Check if the request was successful
if response.status_code == 200:
    # Extract the text data from the API response
    text_data = response.text

    # Analyze sentiment using TextBlob
    blob = TextBlob(text_data)
    sentiment = blob.sentiment.polarity

    # Determine sentiment category
    if sentiment > 0:
        sentiment_category = "positive"
    elif sentiment < 0:
        sentiment_category = "negative"
    else:
        sentiment_category = "neutral"

    print(f"Sentiment: {sentiment_category} ({sentiment})")

else:
    print(f"Error: API request failed with status code {response.status_code}")