A text analyser
The problem
Build a tool that reads a text file and reports its word count, its most common words and its longest lines.
Requirements
- Reads any text file named by the user, and reports a missing file clearly
- Counts lines, words and characters
- Ignores case and punctuation when counting words
- Lists the ten most common words, with ties in alphabetical order
- Uses standard algorithms and containers rather than hand-written sorting
- Prints the results in aligned columns
Technologies
- C++
- File streams
- map and vector
- Standard algorithms
- iomanip
Step by step
- Read the file line by line with getline, and count the lines and characters.
- Write a function that cleans a word by lower-casing it and removing punctuation.
- Split each line with a string stream, and count the words in a map.
- Copy the map into a vector of pairs.
- Sort the vector with a compare function, by count and then by word.
- Print the top ten with setw.
- Test with an empty file and with a file that does not exist.
Expected result
A useful tool that summarises any text file, built almost entirely from the standard library.
How it is evaluated
- The counts are correct for an empty file
- Words differing only in case count as one
- Ties are ordered correctly
- The output lines up
Ways to improve it
- Accept several files at once
- Exclude common words such as the and and
- Show a text histogram of word lengths