0% found this document useful (0 votes)
10 views3 pages

Java Text Analysis Tool Code

The TextAnalysisTool is a Java program that analyzes user-provided text for various metrics. It calculates the total number of characters, words, the most common character, and the frequency of a specified character and word. Additionally, it counts the number of unique words in the text.

Uploaded by

belovedvince
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Java Text Analysis Tool Code

The TextAnalysisTool is a Java program that analyzes user-provided text for various metrics. It calculates the total number of characters, words, the most common character, and the frequency of a specified character and word. Additionally, it counts the number of unique words in the text.

Uploaded by

belovedvince
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;

public class TextAnalysisTool {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

// User Input: Get paragraph


[Link]("Enter a paragraph or lengthy text:");
String text = [Link]().trim();

while ([Link]()) {
[Link]("Text cannot be empty. Please enter again:");
text = [Link]().trim();
}

// Character Count
int charCount = [Link]();
[Link]("\nTotal number of characters: " + charCount);

// Word Count
String[] words = [Link]().split("\\s+");
int wordCount = [Link];
[Link]("Total number of words: " + wordCount);

// Most Common Character (excluding spaces, case-insensitive)


Map<Character, Integer> charFrequency = new HashMap<>();
for (char ch : [Link]().toCharArray()) {
if (ch != ' ') {
[Link](ch, [Link](ch, 0) + 1);
}
}

char mostCommonChar = ' ';


int maxFreq = 0;
for ([Link]<Character, Integer> entry : [Link]()) {
if ([Link]() > maxFreq) {
maxFreq = [Link]();
mostCommonChar = [Link]();
}
}

[Link]("Most common character: " + mostCommonChar);

// Character Frequency
[Link]("\nEnter a character to find its frequency: ");
String charInput = [Link]().trim().toLowerCase();

while ([Link]() || [Link]() != 1 ||


![Link]([Link](0))) {
[Link]("Please enter a single valid character: ");
charInput = [Link]().trim().toLowerCase();
}

char targetChar = [Link](0);


long charFreq = [Link]().chars().filter(c -> c == targetChar).count();
[Link]("Frequency of character '" + targetChar + "': " + charFreq);

// Word Frequency
[Link]("\nEnter a word to find its frequency: ");
String wordInput = [Link]().trim().toLowerCase();

while ([Link]()) {
[Link]("Please enter a valid word: ");
wordInput = [Link]().trim().toLowerCase();
}

int wordFreq = 0;
for (String word : words) {
if ([Link]().equals(wordInput)) {
wordFreq++;
}
}
[Link]("Frequency of word \"" + wordInput + "\": " + wordFreq);

// Unique Words
Set<String> uniqueWords = new HashSet<>();
for (String word : words) {
[Link]([Link]());
}
[Link]("Number of unique words: " + [Link]());

[Link]();
}
}

Common questions

Powered by AI

Yes, the TextAnalysisTool can handle both numerical and alphabetical characters equally. This is achieved through the use of Java's Character.isLetterOrDigit method during character input validation and the toLowerCase method to ensure case-insensitivity for character counting, irrespective of the character type .

The TextAnalysisTool handles word frequency analysis by converting the entire text input and the word input to lowercase to prevent case sensitivity issues. It then counts occurrences of the exact word match within the list of words split from the text, differing from character analysis which uses a frequency map for individual characters .

Data structures such as HashMap and HashSet play critical roles in the TextAnalysisTool. The HashMap is used to map characters to their frequencies efficiently, allowing quick frequency lookups and updates. The HashSet is employed to count unique words, leveraging its property of storing only distinct elements. These structures facilitate efficient data manipulation and retrieval essential for text analysis tasks .

The TextAnalysisTool ensures case-insensitivity by converting both the text and the user inputs (for character and word searches) to lowercase using the toLowerCase method. This conversion occurs during the processing of both character frequency mapping and word comparison tasks .

The TextAnalysisTool prompts the user until they enter a valid single character by checking if the input is non-empty, has a length of one, and is alphanumeric. The tool uses a loop to repeatedly ask for the input until these conditions are satisfied .

The TextAnalysisTool calculates the most common character by first iterating through the text to create a frequency map of all characters excluding spaces, using a case-insensitive approach. It then iterates through the frequency map to identify the character with the highest frequency count. This process determines the most common character .

The TextAnalysisTool uses a HashSet to store words by converting all to lowercase, ensuring case-insensitivity. This class naturally enforces uniqueness, allowing the tool to calculate the number of unique words by simply checking the size of the HashSet. This method is significant because it efficiently removes duplicates, considering different case representations as the same word .

The '\\s+' pattern as a regex in the TextAnalysisTool splits input text on one or more whitespace characters, effectively handling multiple consecutive spaces and ensuring accurate word splits. This technique is crucial for accurate word counting, preventing the miscount of words caused by accidental extra spaces in the input, thus enhancing robustness in text processing .

The check for an empty text input is crucial to prevent the tool from performing calculations on invalid data, which could lead to misleading results such as zero values for character, word counts, and frequencies. Skipping this step could also cause runtime errors in processing steps that assume at least some input .

The TextAnalysisTool employs a loop mechanism to continuously prompt the user until valid character or word input is received. It then uses the toLowerCase method to ensure case-insensitive matching and count occurrences of the input value. Accuracy is ensured by strictly checking the validity of the input realm (character must be alphanumeric, word must not be empty) before analysis .

You might also like