Text Analysis Tool in Java with GUI

Discover an advanced Text Analysis Tool in Java with GUI that goes beyond simple word counting. Learn how to handle stop words, toggle case sensitivity, support multiple languages, and build a user-friendly GUI with Java Swing in this comprehensive guide.

Key Features

Our advanced Multilingual Word Counter is designed with the following features to provide a comprehensive text analysis tool in java with GUI:

  • Multi-file Selection: Users can select multiple text files simultaneously, allowing batch processing.
  • Case Sensitivity Toggle: Users can choose whether the word count should be case-sensitive.
  • Language-Specific Stop Words: The application supports stop words filtering for multiple languages, starting with English and Spanish.
  • Detailed Word and Letter Counts: The application provides counts for both words and individual letters, filtering out stop words.
  • Progress Indicator: A progress bar informs users of the ongoing analysis status, improving user experience, especially with large files.

NOTE

You can get the code of Text Analysis Tool in Java with GUI from my GitHub repository

You can also Download the Zip file

User Interface Components

The GUI of our Project Text Analysis Tool in Java Swing, comprises several panels and components, each serving a specific function:

  • File Selection Panel:
    • Text Field: Displays the paths of selected files.
    • Browse Button: Opens a file chooser dialog, allowing users to select multiple files.
  • Case Sensitivity Panel:
    • Checkbox: Lets users toggle case sensitivity on or off.
  • Language Selection Panel:
    • Dropdown Menu: Enables users to select the language for stop word filtering. Currently supports English and Spanish.
  • Analyze Button: Initiates the analysis process.
  • Progress Bar: Provides visual feedback on the progress of the file analysis.
  • Result Area: A non-editable text area where the results of the word and letter count are displayed.

Functional Breakdown

  1. file handling in java:
    • Users initiate file selection by clicking the “Browse” button. A JFileChooser dialog allows users to select one or more files, and the paths of these files are displayed in the text field.
  2. Case Sensitivity and Language Selection:
    • The case sensitivity checkbox lets users decide if the analysis should distinguish between uppercase and lowercase words.
    • The language dropdown menu allows users to select the language whose stop words will be filtered during the analysis. This is crucial for accurate word counting in different languages.
  3. Analysis Process:
    • Upon clicking the “Analyze” button, the application reads the selected files and processes each line to extract words and letters.
    • Using regular expressions, each line is split into words, which are then checked against the stop words list of the selected language.
    • Words and letters are counted, and their frequencies are stored in maps. This data is later sorted and displayed in the result area.
  4. Stop Words Handling:
    • Stop words are common words that are usually filtered out in text analysis because they occur frequently and carry less meaningful information. The application uses predefined lists of stop words for supported languages, which can be expanded as needed.
  5. Displaying Results:
    • After processing, the application displays the word and letter counts in the result area. The words are sorted by frequency, making it easier to identify the most common words and letters in the text. The total counts for words and letters are also displayed, providing a summary of the analysis.

Multilingual Word Counter in java

To add support for additional Multilingual Word Counter in java, the stop words map (STOP_WORDS_MAP) can be updated with new entries. For instance, to add French stop words, you can add:

STOP_WORDS_MAP.put("French", new HashSet<>(Arrays.asList(
    "le", "la", "les", "et", "ou", "mais", "est", "sont", "\u00e9tait", "\u00e9taient", "il", "ils", "de", "\u00e0", "dans", "sur", "avec", "comme", "par"
)));

This flexibility allows the application to be tailored to various linguistic requirements, enhancing its utility in multilingual environments.

Text Analysis Tool in Java with GUI

Conclusion of Case Sensitive Word Counter Java

The advanced word counter application built with Java Swing offers a powerful tool for text analysis tool in java with GUI (user-friendly interface). By incorporating features like multi-file selection, case sensitivity options, and language-specific stop words, it provides a robust solution for detailed word and letter counting tasks. Its modular design ensures that the application can be easily extended and customized to meet additional requirements or support more languages.

Example of project on Interactive Word Counter in java

To use the Text Analysis Tool in Java with GUI, click the “Browse” button to open a file chooser where you can select one or more text files. The paths of the chosen files will appear in the text field. You can then choose whether the word count should be case-sensitive by checking or unchecking the box and select a language for stop word filtering from the dropdown menu. Click the “Analyze” button to start the analysis. While the app processes the files, a progress bar will show how much of the analysis is done. When finished, the results, including word and letter counts (excluding stop words), will be displayed in the result area, sorted by frequency, with total counts for both words and letters.

Text Analysis Tool in Java with GUI

Source Code of Text Analysis Tool in Java with GUI

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

class WordCounterSwing {

    private static final Map<String, Set<String>> STOP_WORDS_MAP = new HashMap<>();

    static {
        STOP_WORDS_MAP.put("English", new HashSet<>(Arrays.asList(
                "a", "an", "the", "and", "or", "but", "is", "are", "was", "were", "it", "they", "of", "to", "in", "on", "for", "with", "about", "as", "by"
        )));
        STOP_WORDS_MAP.put("Spanish", new HashSet<>(Arrays.asList(
                "un", "una", "el", "la", "y", "o", "pero", "es", "son", "fue", "fueron", "lo", "ellos", "de", "para", "en", "sobre", "con", "como", "por"
        )));
        // Add more languages as needed
    }

    private JTextArea resultArea;
    private JTextField fileInputField;
    private JCheckBox caseSensitiveCheckbox;
    private JComboBox<String> languageComboBox;
    private JProgressBar progressBar;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new WordCounterSwing().createAndShowGUI();
        });
    }

    private void createAndShowGUI() {
        JFrame frame = new JFrame("Advanced Word Counter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);

        frame.setLayout(new BorderLayout());

        JPanel filePanel = new JPanel();
        filePanel.add(new JLabel("Select Input Files:"));
        fileInputField = new JTextField(30);
        filePanel.add(fileInputField);
        JButton browseButton = new JButton("Browse");
        filePanel.add(browseButton);

        JPanel casePanel = new JPanel();
        casePanel.add(new JLabel("Case Sensitive:"));
        caseSensitiveCheckbox = new JCheckBox();
        casePanel.add(caseSensitiveCheckbox);

        JPanel languagePanel = new JPanel();
        languagePanel.add(new JLabel("Select Language:"));
        languageComboBox = new JComboBox<>(new String[]{"English", "Spanish"});
        languagePanel.add(languageComboBox);

        JPanel bottomPanel = new JPanel();
        JButton analyzeButton = new JButton("Analyze");
        analyzeButton.addActionListener(new AnalyzeActionListener());
        bottomPanel.add(analyzeButton);

        progressBar = new JProgressBar();
        progressBar.setIndeterminate(true);
        progressBar.setVisible(false);
        bottomPanel.add(progressBar);

        resultArea = new JTextArea();
        resultArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(resultArea);

        frame.add(filePanel, BorderLayout.NORTH);
        frame.add(casePanel, BorderLayout.CENTER);
        frame.add(languagePanel, BorderLayout.WEST);
        frame.add(bottomPanel, BorderLayout.SOUTH);
        frame.add(scrollPane, BorderLayout.CENTER);

        browseButton.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setMultiSelectionEnabled(true);
            int returnValue = fileChooser.showOpenDialog(frame);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File[] selectedFiles = fileChooser.getSelectedFiles();
                StringBuilder sb = new StringBuilder();
                for (File file : selectedFiles) {
                    sb.append(file.getAbsolutePath()).append("\n");
                }
                fileInputField.setText(sb.toString().trim());
            }
        });

        frame.setVisible(true);
    }

    private class AnalyzeActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String filePaths = fileInputField.getText();
            if (filePaths.isEmpty()) {
                JOptionPane.showMessageDialog(null, "Please select files.", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }

            String selectedLanguage = (String) languageComboBox.getSelectedItem();
            Set<String> stopWords = STOP_WORDS_MAP.get(selectedLanguage);

            String[] paths = filePaths.split("\n");
            Map<String, Integer> wordCounts = new HashMap<>();
            Map<Character, Integer> letterCounts = new HashMap<>();

            progressBar.setVisible(true);

            for (int i = 0; i < paths.length; i++) {
                File file = new File(paths[i].trim());
                if (file.exists()) {
                    countWordsAndLettersInFile(file, wordCounts, letterCounts, stopWords);
                } else {
                    JOptionPane.showMessageDialog(null, "File not found: " + paths[i].trim(), "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                progressBar.setValue((i + 1) * 100 / paths.length);  // Update progress bar
            }

            boolean caseSensitive = caseSensitiveCheckbox.isSelected();
            if (!caseSensitive) {
                wordCounts = convertWordCountsToLowerCase(wordCounts);
                letterCounts = convertLetterCountsToLowerCase(letterCounts);
            }

            removeStopWords(wordCounts, stopWords);
            String output = formatWordCountsAndLetters(wordCounts, letterCounts);
            resultArea.setText(output);

            progressBar.setVisible(false);  // Hide progress bar
        }
    }

    private void countWordsAndLettersInFile(File file, Map<String, Integer> wordCounts, Map<Character, Integer> letterCounts, Set<String> stopWords) {
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] words = line.split("\\W+");
                for (String word : words) {
                    if (!word.isEmpty() && !stopWords.contains(word.toLowerCase())) {
                        word = word.toLowerCase();
                        wordCounts.put(word, wordCounts.getOrDefault(word, 0) + 1);
                    }
                }

                // Count letters (excluding non-alphabetical characters)
                for (char c : line.toCharArray()) {
                    if (Character.isLetter(c)) {
                        char lowerChar = Character.toLowerCase(c);
                        letterCounts.put(lowerChar, letterCounts.getOrDefault(lowerChar, 0) + 1);
                    }
                }
            }
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Error reading the file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private Map<String, Integer> convertWordCountsToLowerCase(Map<String, Integer> wordCounts) {
        Map<String, Integer> lowerCaseCounts = new HashMap<>();
        for (Map.Entry<String, Integer> entry : wordCounts.entrySet()) {
            lowerCaseCounts.put(entry.getKey().toLowerCase(), entry.getValue());
        }
        return lowerCaseCounts;
    }

    private Map<Character, Integer> convertLetterCountsToLowerCase(Map<Character, Integer> letterCounts) {
        Map<Character, Integer> lowerCaseCounts = new HashMap<>();
        for (Map.Entry<Character, Integer> entry : letterCounts.entrySet()) {
            lowerCaseCounts.put(Character.toLowerCase(entry.getKey()), entry.getValue());
        }
        return lowerCaseCounts;
    }

    private void removeStopWords(Map<String, Integer> wordCounts, Set<String> stopWords) {
        wordCounts.keySet().removeIf(word -> stopWords.contains(word));
    }

    private String formatWordCountsAndLetters(Map<String, Integer> wordCounts, Map<Character, Integer> letterCounts) {
        if (wordCounts.isEmpty() && letterCounts.isEmpty()) {
            return "No words or letters found after processing the files (or they were all stop words).";
        }

        StringBuilder output = new StringBuilder("Word Counts:\n");
        wordCounts.entrySet().stream()
                .sorted((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()))
                .forEach(entry -> output.append("- ").append(entry.getKey()).append(": ").append(entry.getValue()).append("\n"));

        int totalWords = wordCounts.values().stream().mapToInt(Integer::intValue).sum();
        output.append("\nTotal Words (excluding stop words): ").append(totalWords);

        output.append("\n\nLetter Counts:\n");
        letterCounts.entrySet().stream()
                .sorted((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()))
                .forEach(entry -> output.append("- ").append(entry.getKey()).append(": ").append(entry.getValue()).append("\n"));

        int totalLetters = letterCounts.values().stream().mapToInt(Integer::intValue).sum();
        output.append("\nTotal Letters: ").append(totalLetters);

        return output.toString();
    }
}

Leave a comment