XTRAN Example — Compute Fog Index for Prose Text
Scenario — you have created some documentation or sales literature, and your boss requires that it have a Fog Index of 10 or less, so it's understandable to its intended audience.
XTRAN to the rescue!
The following example uses an XTRAN rules file comprising 98 non-comment lines of XTRAN's rules language ("meta-code") to compute Gunning's Fog Index for prose text. The rules took 1¼ hours to create and 1 hour to debug. (That's right, only 2¼ hours total!)
Back in 1952, Robert Gunning developed his famous Fog Index, a measure of the educational grade level needed to understand prose.
The formula he used is
FI = 0.4 * (ASL + PHW)
|is the Fog Index
|is the average sentence length, in words
|is the percent of "hard words"
A hard word is one that:
- has 3 or more syllables
- is not a proper noun
- is not a hyphenation of easy words
- is not an easy word with a syllable added
by ending it in
ed(or, we added,
These XTRAN rules read prose from a specified text file, compute its Fog Index, and publish both it and the underlying statistics in XTRAN's run log.
How can such powerful and generalized text processing be automated in only 2¼ hours and 98 lines of XTRAN rules? Because there is so much capability already available as part of XTRAN's rules language. These rules take advantage of the following functionality:
- Text file input and output
- Text manipulation
- Text formatting
- Delimited list manipulation
- Environment variable manipulation
The input to and output from XTRAN are untouched.
Here is a flowchart for this process, in which the elements are color coded:
- BLUE for XTRAN versions (runnable programs)
- ORANGE for XTRAN rules (text files)
- PURPLE for text data files
Input to XTRAN:
Do you need to get somewhere discreetly, under the cover of darkness? Dangerous Dan's Nocturnal Aviation can get you there! Our airplanes are stealth equipped, so your adversary's radar won't find you. They run without lights, so our pilots are especially vigilant to avoid collisions with other aircraft, mountains, etc. They are also experts in evasive maneuvers. We serve snacks, assuming clear weather and no hostile fire. And if you need discreet ground transportation at your destination, be sure to check out Dangerous Dan's Nocturnal Limousine Service! Our limousines are also stealth equipped, to avoid police radar, and our drivers are thoroughly trained in evasive driving.
Extract from XTRAN's run log:
Number of words: 106 Number of hard words: 18 Number of sentences: 8 Average sentence length: 13.3 words Percent of hard words: 5.9% Fog Index: 7.7