0% found this document useful (0 votes)
14 views5 pages

Text Analysis Tool Overview

Uploaded by

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

Text Analysis Tool Overview

Uploaded by

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

Code:

import [Link];
import [Link];
import [Link];

public class TextAnalysisTool {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

// User Input
[Link]("Please enter a paragraph or a lengthy text:");
String text = [Link]();

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

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

// Most Common Character


char mostCommonChar = findMostCommonCharacter(text);
[Link]("Most common character: " + mostCommonChar);

// Character Frequency
[Link]("Please enter a character to find its frequency:");
char characterToFind = [Link]().charAt(0);
int charFrequency = findCharacterFrequency(text, characterToFind);
[Link]("Frequency of '" + characterToFind + "': " + charFrequency);

// Word Frequency
[Link]("Please enter a word to find its frequency:");
[Link](); // consume the leftover newline
String wordToFind = [Link]();
int wordFrequency = findWordFrequency(words, wordToFind);
[Link]("Frequency of \"" + wordToFind + "\": " + wordFrequency);

// Unique Words
int uniqueWordsCount = findUniqueWordsCount(words);
[Link]("Number of unique words: " + uniqueWordsCount);

[Link]();
}

private static char findMostCommonCharacter(String text) {


Map<Character, Integer> charFrequencyMap = new HashMap<>();
for (char c : [Link]()) {
char lowerC = [Link](c);
[Link](lowerC, [Link](lowerC, 0) + 1);
}

char mostCommonChar = ' ';


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

return mostCommonChar;
}

private static int findCharacterFrequency(String text, char character) {


int frequency = 0;
char lowerCharacter = [Link](character);
for (char c : [Link]()) {
if ([Link](c) == lowerCharacter) {
frequency++;
}
}
return frequency;
}

private static int findWordFrequency(String[] words, String word) {


int frequency = 0;
String lowerWord = [Link]();
for (String w : words) {
if ([Link]().equals(lowerWord)) {
frequency++;
}
}
return frequency;
}

private static int findUniqueWordsCount(String[] words) {


Map<String, Integer> wordFrequencyMap = new HashMap<>();
for (String word : words) {
String lowerWord = [Link]();
[Link](lowerWord, [Link](lowerWord, 0) +
1);
}
return [Link]();
}
}

Explanation:
1. User Input: The program first prompts the user to input a paragraph or lengthy text and
stores it.
2. Character Count: The length of the text is calculated using [Link]().
3. Word Count: The text is split into words using split("\\s+"), and the length of the
resulting array gives the word count.
4. Most Common Character: A frequency map is built for all characters, and the most
frequent character is found.
5. Character Frequency: The program prompts the user to enter a character and calculates
its frequency in the text.
6. Word Frequency: The program prompts the user to enter a word and calculates its
frequency in the text.
7. Unique Words: A frequency map is built for all words, and the number of unique words
is determined.
Screenshot:

Common questions

Powered by AI

The TextAnalysisTool converts all words in the text and the user-specified word to lowercase to ensure case insensitivity. It iterates over the array of words split from the text, incrementing a counter each time it finds a match with the user-provided word. The final count is the frequency of that word in the text .

The split("\\s+") method is significant because it divides the input text into an array of strings (words) based on whitespace, which includes spaces, tabs, and new lines. This allows the tool to accurately count words by providing a distinct separation point between them, enabling an accurate count of word occurrences .

The TextAnalysisTool takes a user-specified character and converts it to lowercase. It iterates through each character in the text, also converted to lowercase, to check for matches with the user-provided character. Each match increments a frequency counter, which is returned as the character frequency .

The TextAnalysisTool employs the Scanner class to prompt the user and read input. For character frequency, it reads a character directly and processes it case-insensitively by converting it to lowercase. For word frequency, it uses scanner.nextLine() after a previous next() to clear the buffer, ensuring correct reading of the full word input. This prevents errors due to leftover newline characters in the input stream .

The TextAnalysisTool uses a frequency map to count occurrences of each character in the input text. It converts characters to lowercase to ensure case insensitivity. As it iterates through the characters, it updates the count in the frequency map. It then finds the character with the highest frequency by iterating over the entry set of the map. The character with the maximum count is returned as the most common character .

The TextAnalysisTool's method of using text.length() is effective for counting the total number of characters as it includes all characters in the string regardless of their type (letters, numbers, spaces, punctuation). However, it cannot distinguish between different types of characters, such as ignoring whitespace if only non-whitespace characters are needed. Additionally, it treats multibyte characters individually, which could cause discrepancies in languages with such characters .

To optimize the performance of the findMostCommonCharacter method, one could employ an array of size 128 to track ASCII character frequencies directly, as characters are mapped to integer values within this range. This approach can reduce overhead associated with HashMap operations and potentially improve execution speed. Additionally, utilizing parallel processing for frequency counting in massive texts could further enhance performance by reducing processing time .

Using a HashMap for frequency analysis in the TextAnalysisTool can potentially lead to high memory usage when processing large texts, as each unique character or word requires a map entry. The performance could be affected if the hashmap grows large, leading to increased time in computing operations such as searching or inserting. However, its average O(1) time complexity for these operations generally offers efficient performance unless the hash function poorly distributes data, which could cause collisions and degrade performance .

The TextAnalysisTool determines the number of unique words by using a frequency map to track occurrences of each word, all converted to lowercase for case insensitivity. Each word is added to the map with its frequency incremented as it appears. The size of this map, representing unique words, is then returned .

You might also like