روبیکا من : https://rubika.ir/artinkarimian3 سایت من : https://sites.google.com/view/artin-karimian
پروژه نهم پایتون : تشخیص احساسات از روی متن

خب اول وسایل لازم که در زیر نوشته شده را تهیه کنید .
پایتون
کتابخانه sys
کتابخانه PyQt5
کتابخانه textblob

خب پس از نصب کتابخانه های لازم کد زیر را در پایتون میزنید .
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont, QIcon
from PyQt5.QtCore import Qt
from textblob import TextBlob # Import TextBlob
# --- برای نصب TextBlob ---
# pip install PyQt5
# pip install textblob
# python -m textblob.download_corpora # این دستور رو یک بار اجرا کنید
class SentimentApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Sentiment Analyzer (TextBlob)')
# self.setWindowIcon(QIcon('/path/to/your/icon.png')) # Optional: Set an icon
self.setGeometry(150, 150, 650, 450) # Adjusted size slightly
# Main layout
layout = QVBoxLayout()
# Title
title = QLabel('Sentiment Analysis with TextBlob')
title.setFont(QFont('Arial', 18, QFont.Bold))
title.setAlignment(Qt.AlignCenter)
title.setStyleSheet("color: #333;")
layout.addWidget(title)
# Input Label
input_label = QLabel('Enter your text (English):')
input_label.setFont(QFont('Arial', 12))
input_label.setStyleSheet("margin-top: 15px;")
layout.addWidget(input_label)
# Text Edit for input
self.text_edit = QTextEdit()
self.text_edit.setFont(QFont('Arial', 11))
self.text_edit.setPlaceholderText("Type or paste your English text here...")
self.text_edit.setMinimumHeight(100) # Give it some space
self.text_edit.setStyleSheet("padding: 10px; border: 1px solid #ccc; border-radius: 5px;")
layout.addWidget(self.text_edit)
# Button Layout
btn_layout = QHBoxLayout()
analyze_btn = QPushButton('Analyze Sentiment')
analyze_btn.clicked.connect(self.analyze_sentiment)
analyze_btn.setStyleSheet("""
QPushButton {
background-color: #4CAF50; color: white; padding: 10px 20px;
border: none; border-radius: 5px; font-size: 12pt;
}
QPushButton:hover {
background-color: #45a049;
}
""")
btn_layout.addWidget(analyze_btn)
clear_btn = QPushButton('Clear All')
clear_btn.clicked.connect(self.clear_all)
clear_btn.setStyleSheet("""
QPushButton {
background-color: #f44336; color: white; padding: 10px 20px;
border: none; border-radius: 5px; font-size: 12pt;
}
QPushButton:hover {
background-color: #da190b;
}
""")
btn_layout.addWidget(clear_btn)
layout.addLayout(btn_layout)
# Result Display Area
result_label_title = QLabel('Analysis Result:')
result_label_title.setFont(QFont('Arial', 14, QFont.Bold))
result_label_title.setStyleSheet("margin-top: 20px; color: #555;")
layout.addWidget(result_label_title)
self.result_display = QLabel('—')
self.result_display.setFont(QFont('Arial', 16))
self.result_display.setAlignment(Qt.AlignCenter)
self.result_display.setStyleSheet("background-color: #e0e0e0; padding: 20px; border-radius: 8px; font-weight: bold;")
layout.addWidget(self.result_display)
# Polarity and Subjectivity Scores
score_layout = QHBoxLayout()
self.polarity_label = QLabel('Polarity: 0.0')
self.polarity_label.setFont(QFont('Arial', 11))
score_layout.addWidget(self.polarity_label)
self.subjectivity_label = QLabel('Subjectivity: 0.0')
self.subjectivity_label.setFont(QFont('Arial', 11))
score_layout.addWidget(self.subjectivity_label)
layout.addLayout(score_layout)
# Footer/Explanation
footer = QLabel('Powered by TextBlob. Polarity ranges from -1 (negative) to 1 (positive). Subjectivity ranges from 0 (objective) to 1 (subjective).')
footer.setFont(QFont('Arial', 9))
footer.setWordWrap(True)
footer.setAlignment(Qt.AlignCenter)
footer.setStyleSheet("color: #777; margin-top: 10px;")
layout.addWidget(footer)
self.setLayout(layout)
def analyze_sentiment(self):
text = self.text_edit.toPlainText().strip()
if not text:
QMessageBox.warning(self, 'Empty Input', 'Please enter some English text to analyze.')
return
try:
# Use TextBlob for sentiment analysis
blob = TextBlob(text)
sentiment = blob.sentiment
polarity = sentiment.polarity
subjectivity = sentiment.subjectivity
# Determine sentiment category
if polarity > 0.1: # Threshold can be adjusted
sentiment_category = 'POSITIVE 😊'
color = '#4CAF50' # Green
elif polarity < -0.1: # Threshold can be adjusted
sentiment_category = 'NEGATIVE 😞'
color = '#f44336' # Red
else:
sentiment_category = 'NEUTRAL 😐'
color = '#ff9800' # Orange
# Update result display
self.result_display.setText(f'{sentiment_category}')
self.result_display.setStyleSheet(f"background-color: #e0e0e0; padding: 20px; border-radius: 8px; font-weight: bold; color: {color};")
# Update score labels
self.polarity_label.setText(f'Polarity: {polarity:.3f}')
self.subjectivity_label.setText(f'Subjectivity: {subjectivity:.3f}')
except Exception as e:
QMessageBox.critical(self, 'Error', f'An error occurred during analysis: {e}\n\nMake sure you have run "python -m textblob.download_corpora".')
self.result_display.setText('Error')
self.polarity_label.setText('Polarity: N/A')
self.subjectivity_label.setText('Subjectivity: N/A')
def clear_all(self):
self.text_edit.clear()
self.result_display.setText('—')
self.polarity_label.setText('Polarity: 0.0')
self.subjectivity_label.setText('Subjectivity: 0.0')
if __name__ == '__main__':
app = QApplication(sys.argv)
# --- IMPORTANT: Download necessary corpora for TextBlob ---
# You only need to run this part once.
# If you haven't run it before, uncomment the next line and run the script.
# Then, comment it out again and run the script normally.
# import nltk
# nltk.download('punkt')
# nltk.download('averaged_perceptron_tagger')
# nltk.download('brown') # Needed for some TextBlob features
# nltk.download('wordnet') # Needed for WordNetLemmatizer
# nltk.download('movie_reviews') # Example corpus used by TextBlob
# nltk.download('stopwords') # If you plan to use stopword removal
# Or simply run: python -m textblob.download_corpora in your terminal
# It's better to run this in terminal: python -m textblob.download_corpora
window = SentimentApp()
window.show()
sys.exit(app.exec_())پس از اجرای کد بالا تصویر زیر را میبینید .

برای دانلود همین پایتون یا فایل تبدیل شده به برنامه روی نوشته لینک بزنید و 15ثانیه صبر کنید و فایل را دریافت کنید .
اینم از این پروژه اگر ایده یا پروژه ای دارید در نظرت بنویسید.
مطلبی دیگر از این انتشارات
ساخت یک آنتی ویروس به نام Chitartin با پایتون
مطلبی دیگر از این انتشارات
ایده پروژه پایتون 2025 | ۱۲ ایده عملی در ۴ حوزه کلیدی
مطلبی دیگر از این انتشارات
آموزش ساخت ویروس کامپیوتری (Fork Bomb) به 3 روش