Write a function named word_stats_json
that accepts as its parameter a string
$filename
holding a file name, opens that file and reads its contents as a
sequence of words, and ouputs JSON data with statistics about the input.
The JSON output for your function should include 3 keys:
word_count
- maps to the number of words in the file (as an integer).
avg_word_length
- maps to the average word length (as a real number rounded with a precision of at most 2 decimal digits)
unique_letters
- maps to the number of unique alphabetic (A-Z) letters used, ignoring letter-casing
Use json_encode
to encode an associative array with these keys/values - your function should print out this result.
For example, suppose the file tobe.txt
contains the following text:
To be or not TO BE, THAT IS the question.
For the purposes of this problem, we will use whitespace to separate words - this means that "123 321" is considered two words, even though neither word has alphabetical characters, and that some words include punctuation, as in "be,".
For the input above, your function should produce exactly the following output.
The number of "unique letters" is 12 because the file contains 12 distinct letters of the alphabet from A-Z: a, b, e, h, i, n, o, q, r, s, t, and u.
So the call of word_stats("tobe.txt");
would print the following:
{"word_count":10,"avg_word_length":3.2,"unique_letters":12}
Assumptions:
You may assume that the input file exists and is readable.
Constraints:
Your solution should read the file only once, not make multiple passes over the file data.