Ygvxqs_Text_Analyzer Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk
import re
from collections import Counter
import nltk
from nltk.corpus import stopwords

nltk.download('stopwords', quiet=True)
nltk.download('punkt', quiet=True)

class TextAnalyzerApp:
    def __init__(self, master):
        self.master = master
        master.title("Advanced Text Analyzer")

        # Notebook for Tabs
        self.notebook = ttk.Notebook(master)
        self.notebook.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)

        # Basic Analysis Tab
        self.basic_tab = ttk.Frame(self.notebook)
        self.notebook.add(self.basic_tab, text="Basic Analysis")

        # Advanced Analysis Tab
        self.advanced_tab = ttk.Frame(self.notebook)
        self.notebook.add(self.advanced_tab, text="Advanced Analysis")

        # Text Input Area
        self.text_input = tk.Text(self.basic_tab, height=15, width=70, wrap=tk.WORD)
        self.text_input.pack(pady=10, padx=10)

        # Analyze Button
        self.analyze_button = tk.Button(self.basic_tab, text="Analyze Text", command=self.analyze, bg="#4CAF50", fg="white", font=('Arial', 12))
        self.analyze_button.pack(pady=5)

        # Output Text Area
        self.output_text = tk.Text(self.basic_tab, height=10, width=70, wrap=tk.WORD, state=tk.DISABLED)
        self.output_text.pack(pady=10, padx=10)

        # Advanced Analysis Components
        self.sentiment_label = tk.Label(self.advanced_tab, text="Sentiment Analysis:", font=('Arial', 10, 'bold'))
        self.sentiment_label.pack(pady=5)

        self.sentiment_output = tk.Text(self.advanced_tab, height=2, width=60, wrap=tk.WORD, state=tk.DISABLED)
        self.sentiment_output.pack(pady=5)

        self.keyword_label = tk.Label(self.advanced_tab, text="Keyword Extraction:", font=('Arial', 10, 'bold'))
        self.keyword_label.pack(pady=5)

        self.keyword_output = tk.Text(self.advanced_tab, height=5, width=60, wrap=tk.WORD, state=tk.DISABLED)
        self.keyword_output.pack(pady=5)

    def analyze(self):
        text = self.text_input.get("1.0", tk.END)
        self.perform_basic_analysis(text)
        self.perform_advanced_analysis(text)

    def perform_basic_analysis(self, text):
        # Basic Analysis
        word_count = len(re.findall(r'\b\w+\b', text.lower()))
        sentence_count = len(re.split(r'[.!?]+', text))
        character_count = len(text)

        # Output to Text Area
        self.output_text.config(state=tk.NORMAL)
        self.output_text.delete("1.0", tk.END)
        self.output_text.insert(tk.END, f"Word Count: {word_count}\n")
        self.output_text.insert(tk.END, f"Sentence Count: {sentence_count}\n")
        self.output_text.insert(tk.END, f"Character Count: {character_count}\n")
        self.output_text.config(state=tk.DISABLED)

    def perform_advanced_analysis(self, text):
        # Sentiment Analysis (Basic)
        sentiment_score = self.simple_sentiment_analysis(text)
        sentiment = "Positive" if sentiment_score > 0 else "Negative" if sentiment_score < 0 else "Neutral"

        # Keyword Extraction
        keywords = self.extract_keywords(text)

        # Output to Advanced Tab
        self.sentiment_output.config(state=tk.NORMAL)
        self.sentiment_output.delete("1.0", tk.END)
        self.sentiment_output.insert(tk.END, f"Sentiment: {sentiment} (Score: {sentiment_score:.2f})")
        self.sentiment_output.config(state=tk.DISABLED)

        self.keyword_output.config(state=tk.NORMAL)
        self.keyword_output.delete("1.0", tk.END)
        self.keyword_output.insert(tk.END, ", ".join(keywords))
        self.keyword_output.config(state=tk.DISABLED)


    def simple_sentiment_analysis(self, text):
        # Very basic sentiment scoring
        positive_words = ['good', 'great', 'excellent', 'amazing', 'best']
        negative_words = ['bad', 'terrible', 'awful', 'worst', 'poor']

        score = 0
        words = text.lower().split()
        for word in words:
            if word in positive_words:
                score += 1
            elif word in negative_words:
                score -= 1
        return score

    def extract_keywords(self, text):
        stop_words = set(stopwords.words('english'))
        words = nltk.word_tokenize(text.lower())
        filtered_words = [w for w in words if w not in stop_words and w.isalnum()]
        word_counts = Counter(filtered_words)
        most_common = word_counts.most_common(10)
        return [word for word, count in most_common]


root = tk.Tk()
app = TextAnalyzerApp(root)
root.mainloop()
👁️ Viewed: 7

Comments