Text Statistics Tool
Build a Perl tool that analyzes any text file and prints interesting statistics: the number of words, lines and sentences, the most common words (ignoring common words such as the and and), the average and longest line and sentence, and phrases of two or three words that are repeated often, which is handy for spotting repetition in your own writing. Perl is famous for text processing, and this project shows why: you'll learn regular expressions for finding words and sentences, hashes for counting, sorting by value, reading files line by line, handling Unicode text, command-line options with Getopt::Long, and testing with Test::More.
- Estimate
- ~6.5h
- Steps
- 5
- Completed by
- 0
- Proposed by
- codeseed.app
Perl core modules · Getopt::Long · Test::More
Project roadmap
- 01
Define the report
~1hWrite README.md: the command (textstats.pl FILE, with --top N, --min-length and --phrases 2 or 3), what counts as a word (letters, including accented ones, and apostrophes inside words), what ends a sentence, and an example of the full report. Pick a public-domain book from Project Gutenberg to test with. Done when: README.md defines the rules and shows example output.
- 02
Read the text
~1hOpen the file with UTF-8 decoding (open with <:encoding(UTF-8)) and read it line by line, counting lines and characters and tracking the longest line. Read from standard input when no file is given. Stop with a clear message when the file can't be opened. Done when: line and character counts match wc on a test file.
- 03
Count words
~1.5hExtract words with a regular expression such as /(\p{L}+(?:'\p{L}+)*)/g, lowercase them, and count them in a hash. Skip stop words from a list (in a file, so it can be changed) and words shorter than --min-length. Sort by count and then alphabetically, and print the top N with their counts and percentages. Done when: the top words for a book make sense and exclude stop words.
- 04
Find sentences and repeated phrases
~1.5hSplit the text into sentences at ., ! and ? followed by a space and a capital letter or the end (knowing common abbreviations like Mr. can fool this, and noting it). Report the number of sentences and the average and longest sentence in words. Count two- and three-word phrases (n-grams) within sentences, and list those repeated at least 3 times. Done when: sentence counts are reasonable, and repeated phrases are listed.
- 05
Test it
~1.5hMove the counting into subroutines and write tests with Test::More on small texts with known answers: word extraction (apostrophes, accents and numbers), stop words, sentence splitting, n-gram counting, and an empty file. Done when: prove -l t/ passes.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack