카테고리 없음

input their current feeling and categorizes it into one of five sectors

DeNarO 2023. 1. 19. 14:18

1. Here is an example of an example of a Python script that allows a user to input their current feeling and categorizes it into one of five sectors:

feelings = {"happy": "Positive", "sad": "Negative", "angry": "Angry", "neutral": "Neutral", "excited": "Positive"}

feeling = input("How are you feeling today? ").lower()

if feeling in feelings:
    print("Your feeling is categorized as:", feelings[feeling])
else:
    print("Invalid feeling input. Please try again.")

This script uses a dictionary to map different feelings to different sectors. The user is prompted to input their current feeling, which is then converted to lowercase so that the script can match it to a key in the dictionary. If a match is found, the script prints the corresponding sector. If no match is found, the script will return an error message.

You can add or remove the sectors and feelings to your liking.

 

Please note that this is just a basic example and it's not necessarily the best way to classify feeling and you should be aware that the feeling classification is a complex task and it's not only rely on simple string matching.


2. Here is an example of a Python script that allows a user to input their current feeling and assigns it to one of five sectors: happy, neutral, sad, angry, or stressed:

feelings = input("What is your current feeling? ")

if "happy" in feelings:
    print("You are feeling happy!")
elif "neutral" in feelings:
    print("You are feeling neutral.")
elif "sad" in feelings:
    print("You are feeling sad.")
elif "angry" in feelings:
    print("You are feeling angry.")
elif "stressed" in feelings:
    print("You are feeling stressed.")
else:
    print("I'm sorry, I couldn't understand your feeling.")

This script prompts the user to input their current feeling, then uses an if-elif statement to check if certain keywords are present in the input and assigns the feeling to one of the five sectors. If none of the keywords are present, it will print that it couldn't understand the feeling.

You can further modify this script to include more keywords, or even use a machine learning model to classify the feeling with more accuracy.

 

Please note that this script is just a basic example and it's not necessarily the best way to classify the feeling. And, the accuracy of the script relies on how well the keywords are chosen and how well the user is able to express their feeling.


3. Here is an example of a Python script that classifies a given text into one of five emotional categories: happy, sad, angry, surprised, and neutral:

import nltk
from nltk.sentiment import SentimentIntensityAnalyzer

def classify_emotion(text):
    # Initialize SentimentIntensityAnalyzer
    sid = SentimentIntensityAnalyzer()

    # Get sentiment scores for text
    sentiment_scores = sid.polarity_scores(text)

    # Classify text based on sentiment scores
    if sentiment_scores['compound'] >= 0.5:
        return 'happy'
    elif sentiment_scores['compound'] > -0.5 and sentiment_scores['compound'] < 0.5:
        return 'neutral'
    elif sentiment_scores['compound'] <= -0.5 and sentiment_scores['neg']>sentiment_scores['pos']:
        return 'sad'
    elif sentiment_scores['compound'] <= -0.5 and sentiment_scores['neg']<sentiment_scores['pos']:
        return 'angry'
    elif sentiment_scores['compound'] <= -0.5 and sentiment_scores['neg']==sentiment_scores['pos']:
        return 'surprised'

# Test the function
text = "I am so happy today!"
print(classify_emotion(text)) # Output: happy

text = "I am so sad today!"
print(classify_emotion(text)) # Output: sad

text = "I am so angry today!"
print(classify_emotion(text)) # Output: angry

This script uses the NLTK library and its SentimentIntensityAnalyzer to calculate the sentiment scores for a given text, which includes positive, negative, neutral, and compound scores. Then, it compares the compound score to classify the text into one of five emotional categories.