>
import Char

input/output sequence:
  displays a table of measures of the diction of a passage
  that is recorded in the file "McCarthy.txt"

> main =
>   do
>     textOfMcCarthy <- readFile "McCarthy.txt"
>     putStr "Text:\n"
>     putStr textOfMcCarthy
>     putStr "\n"
>     putStr(displayDictionSignature textOfMcCarthy)


function: displayDictionSignature
  delivers a string, properly formatted with newline characters,
  that will display as a table showing some measured traits of
  a sample of text, which is supplied as a string

> displayDictionSignature =
>   unlines . makeDictionSignatureLines . words .
>     makeAllLowerCase . removePunctuation


function: makeDictionSignatureLines
  delivers a sequence of strings, each of which is a properly
  formatted line for a tabular display showing some measured traits
  of a sample of words supplied as an argument

> makeDictionSignatureLines ws =
>   ["Bar Chart:",
>    makeLine labelDefiniteArticles   (matchingAny["the"])     ws,
>    makeLine labelIndefiniteArticles (matchingAny["a", "an"]) ws,
>    makeLine labelShorts             (shorterThan 5)          ws,
>    makeLine labelLowMediums         (between 4  9)           ws,
>    makeLine labelHighMediums        (between 8 13)           ws,
>    makeLine labelLongs              (longerThan 12)          ws,
>    makeLine labelManyLetterI's      manyLetterI's            ws ]


function: makeLine
  delivers one line of bar graph, given a label, a function
  that builds a bar from a word sample, and a word sample
  the corresponding portion of the bar graph

> makeLine label makeBar ws = label ++ "|" ++ (bar . makeBar) ws


function: matchingAny
  delivers those elements of its second argument that
  are equal to one of the elements of its first argument

> matchingAny ofThese fromThese =
>   concat[matchingElements wd fromThese | wd <- ofThese]

function: matchingElements
  delivers those elements of a sequence
  that are equal to a given value

> matchingElements matchingThis fromThese =
>   [x | x <- fromThese, x == matchingThis]


function: longerThan
  delivers those elements of a sequence of sequences
  whose lengths exceed a given length

> longerThan thisLength fromTheseSequences =
>   [x | x <- fromTheseSequences, length x > thisLength]


function: shorterThan
  delivers those elements of a sequence of sequences
  whose lengths are less than or equal to a given length

> shorterThan thisLength fromTheseSequences =
>   [x | x <- fromTheseSequences, length x < thisLength]


function: bar
  delivers a string of asterisks whose length matches the
  number of elements in a given sequence

> bar xs = ['*' | x <- xs]


function: removePunctuation
  delivers a string like its argument but without periods,
  commas, question marks, colons, semicolons, parentheses,
  quotation marks, or apostrophes

> removePunctuation =
>   remove '.' . remove ',' . remove '?' . remove ':' . remove ';' .
>     remove '(' . remove '(' . remove '"' . remove  '\''


function: remove
  delivers a sequence like its second argument, but with all
  occurances of its first argument removed

> remove x xs = [y | y <- xs, y /= x]


function: between
  delivers those elements of a sequence of sequences
  whose lengths lie strictly between given lower and upper bounds

> between lowerBound upperBound =
>   longerThan lowerBound . shorterThan upperBound


function: manyLetterI's
  delivers the strings from its argument that contain 3 or more i's

> manyLetterI's ws =
>   [w | w <- ws, (length . matchingElements 'i') w > 2]


function: makeAllLowerCase 
  delivers a string like its argument, but with all capital
  letters changed to lower case

> makeAllLowerCase cs =
>   [toLower c | c <- cs]


variables: labels for lines in bar graph

> labelDefiniteArticles   = "  the's           "
> labelIndefiniteArticles = "  a's or an's     "
> labelShorts             = "  under 5 letters "
> labelLowMediums         = "  5 to 8 letters  "
> labelHighMediums        = "  9 to 12 letters "
> labelLongs              = "  over 12 letters "
> labelManyLetterI's      = "  3 or more i's   "
Last Modified: