#C12241. Word Frequency Analyzer

    ID: 41647 Type: Default 1000ms 256MiB

Word Frequency Analyzer

Word Frequency Analyzer

You are given a block of text, and your task is to analyze its word frequency. The text should be processed by converting all characters to lowercase and removing all punctuation. Optionally, you may be asked to remove common stop words. After processing, you must determine the top ( N ) most frequent words. The output should list the words in descending order of frequency, formatted as follows:

$$Output: \; i.\; word \; - \; frequency\quad (1 \leq i \leq N) $$

For instance, if the input text is "Hello, World! Hello..." and ( N = 2 ), the expected output is:

  1. hello - 2
  2. world - 1

Your program must read input from standard input (stdin) and print the result to standard output (stdout).

inputFormat

The input is provided via standard input (stdin) in the following format:

  1. The first line contains an integer ( L ) representing the number of lines in the text block.
  2. The next ( L ) lines each contain a portion of the text.
  3. The following line contains an integer ( N ) denoting the number of top frequent words to display.
  4. An optional line follows containing a flag (either "true"/"1" to ignore stop words or "false"/"0" otherwise). If omitted, the flag should be considered as false.

For example:

3 Python is fun. Isn't it? Python, Python, and more Python! Maybe it's fun, really fun. 3 true

outputFormat

The output should be printed to standard output (stdout) and must consist of exactly ( N ) lines. Each line should be in the format:

. word - frequency

where is the rank (starting from 1), 'word' is one of the top N words after processing the text, and 'frequency' is the number of occurrences of that word.## sample

1
Hello, World! Hello...
2
false
1. hello - 2
  1. world - 1 </code></pre>