Installing Prerequisites and Running the Qt NLP Analysis App

Introduction

Before launching your desktop NLP analysis app, you need to ensure your Python environment is set up with the required libraries. These include PyQt5 for the GUI and Matplotlib for plotting. Let’s start with a quick guide to install everything.


Step-by-step: Installing prerequisites

1. Create a virtual environment (recommended)

python -m venv .venv
# Activate environment (Linux/Mac)
source .venv/bin/activate
# On Windows:
# .venv\Scripts\activate

2. Upgrade pip (best practice)

pip install --upgrade pip

3. Install required packages

pip install pyqt5 matplotlib

Running the Qt NLP Analysis App

Copy the following script into a file named, for example, step11_nlp_qt_app.py.

“`python name=step11_nlp_qt_app.py
import sys
import string
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QTextEdit, QPushButton, QLabel, QTabWidget
from PyQt5.QtCore import Qt
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

class NLPApp(QWidget):
def init(self):
super().init()
self.setWindowTitle(“Python NLP Analyzer”)
self.setGeometry(100, 100, 900, 500)
layout = QHBoxLayout(self)

    # Left: Input
    left_layout = QVBoxLayout()
    self.input_box = QTextEdit()
    self.input_box.setPlaceholderText("Paste your text here...")
    left_layout.addWidget(QLabel("Input Text"))
    left_layout.addWidget(self.input_box)
    self.process_btn = QPushButton("Process")
    left_layout.addWidget(self.process_btn)

    layout.addLayout(left_layout)

    # Right: Tabs for Results
    self.tabs = QTabWidget()
    self.token_tab = QTextEdit()
    self.token_tab.setReadOnly(True)
    self.freq_tab = QTextEdit()
    self.freq_tab.setReadOnly(True)
    self.graph_tab = QWidget()
    self.tabs.addTab(self.token_tab, "Tokens & Frequencies")
    self.tabs.addTab(self.freq_tab, "Analysis")
    self.tabs.addTab(self.graph_tab, "Graph")
    layout.addWidget(self.tabs)

    self.process_btn.clicked.connect(self.process_text)

    # Graph elements
    self.figure = plt.figure(figsize=(5,4))
    self.canvas = FigureCanvas(self.figure)
    graph_layout = QVBoxLayout(self.graph_tab)
    graph_layout.addWidget(self.canvas)

def process_text(self):
    text = self.input_box.toPlainText()
    if not text.strip():
        self.token_tab.setPlainText("No input detected.")
        self.freq_tab.setPlainText("No analysis.")
        self.figure.clf()
        self.canvas.draw()
        return

    # Clean and tokenize
    clean = text.strip().lower()
    translator = str.maketrans('', '', string.punctuation)
    no_punct = clean.translate(translator)
    tokens = no_punct.split()

    stop_words = set([
        'the','is','in','it','by','and','a','of','to','for','on',
        'o','a','de','em','para','with','as','an','at','this','that'
    ])
    filtered = [w for w in tokens if w not in stop_words]

    # Word Frequency
    word_freq = {}
    for w in filtered:
        word_freq[w] = word_freq.get(w, 0) + 1

    # Display tokens & frequencies
    self.token_tab.setPlainText(
        f"Tokens (stopwords removed):\n{filtered}\n\nFrequencies:\n{word_freq}"
    )

    # Analysis: Top N words
    sorted_words = sorted(word_freq.items(), key=lambda item: item[1], reverse=True)
    analysis = ""
    top_n = 8
    for i, (word, count) in enumerate(sorted_words[:top_n]):
        analysis += f"{i+1}. {word}: {count}\n"
    analysis += "\nTry different inputs for varied results."
    self.freq_tab.setPlainText(analysis)

    # Bar plot (Matplotlib)
    self.figure.clf()
    if sorted_words:
        words = [w for w,c in sorted_words[:top_n]]
        counts = [c for w,c in sorted_words[:top_n]]
        ax = self.figure.add_subplot(111)
        ax.bar(words, counts, color='skyblue')
        ax.set_xlabel('Word')
        ax.set_ylabel('Frequency')
        ax.set_title(f"Top {top_n} Words")
        self.figure.tight_layout()
    self.canvas.draw()

if name == “main“:
app = QApplication(sys.argv)
win = NLPApp()
win.show()
sys.exit(app.exec_())

---
## Running the application
1. Start your virtual environment.
2. Run the script:

bash
python step11_nlp_qt_app.py
“`

  1. The GUI will open. Paste any text into the left pane, then click “Process”. See tokens, frequencies, analysis, and a bar chart on the right.

Practical notes

  • You may expand the stopword list for richer analysis.
  • The app works with any pasted or typed text—use it for English, Portuguese, Spanish, or other simple experiments.

Edvaldo Guimrães Filho Avatar

Published by