#K65137. Word Frequency Counter
Word Frequency Counter
Word Frequency Counter
You are tasked with creating a program that reads text from the standard input and counts the number of times each unique word appears in the text. The program should ignore case and punctuation. For each test case, the words must be output in alphabetical order along with their frequency counts in the format <word>: <count>
.
Input Format:
- The first line contains an integer T, the number of test cases.
- Each of the following T lines contains a paragraph of text.
Output Format:
- For each test case, print a single line that contains each unique word and its corresponding count, formatted as
<word>: <count>
and separated by a single space.
Details:
- Punctuation marks should be removed using the regular expression
[^a-z\s]
after converting the text to lowercase. - If the input string is empty, output an empty line.
The task can be formalized using the following formula for a given word w in the cleaned text:
[ \text{frequency}(w) = \sum_{i=1}^{n} \mathbf{1}_{{w_i = w}} ]
where \(w_i\) represents each word in the text and \(\mathbf{1}\) is the indicator function.
inputFormat
The input is provided via standard input in the following format:
- The first line contains an integer T, representing the number of test cases.
- This is followed by T lines, each containing a paragraph of text. Each paragraph may include letters (both uppercase and lowercase), spaces, and punctuation.
outputFormat
For each test case, output a single line containing the unique words and their frequency, formatted as <word>: <count>
, with the words sorted in alphabetical order. If the test case is an empty string, print an empty line.
1
Hello world! Hello.
hello: 2 world: 1
</p>