grep
, sort
, tr
, and
uniq
).
cat document | tr -cs a-zA-Z '[\n*]' | tr A-Z a-z | sort | uniq(Provided we put it all on the same line.)
tr -cs a-zA-Z '[\n*]'
indicates "translate any character
that is not a letter to a carriage return, collapsing sequences".
tr
is "translate"
-c
is "complement" ("is not")
-s
is "collapse sequence"
\n
is "carriage return"
tr A-Z a-z
indicates "translate uppercase to lowercase"
uniq
is "print only the first line of lines that repeat"
(note that in order to get only one copy, the other copies must be
on subsequent lines)
Iterator doc = new DocumentIterator(document); Iterator words = new TranslateIterator(doc,cs,"a-zA-Z","\n"); Iterator lcwords = new TranslateIterator(words,"A-Z","a-z"); Iterator sorted = new SortIterator(lcwords); Iterator unique = new UniqueIterator(sorted); Iterator misspelled = new FilterOut(unique,DICTIONARY);
Iterator misspelled = new FilterOut( new UniqueIterator( new SortIterator( new TranslateIterator( new TranslateIterator( new DocumentIterator(document) , cs, "a-zA-Z", "\n" ) // words only , "A-Z", "a-z" ) // lowercase only ) // sorted ) // unique , DICTONARY ) // filtered out dictonary
Predicate
interface. We'll
pretend that means that the predicate supports
okay(Object o)
.
PredicateFilter()
, will take two
parameters: an iterator to filter and a predicate to use for the
filtering.
base
).
pred
).
reset()
,
hasMoreElements()
, value()
, and
nextElement()
)?
reset()
simply requires us to reset the base
iterator.
value()
, nextElement()
, and
hasMoreElements()
require us to advance the base iterator until
we find an element that the predicate accepts. We'll use a helper function
to do this.
/** An iterator that filters out unwanted items. */ public class PredicateFilter implements Iterator { /** The base iterator. */ protected Iterator base; /** The predicate used for filtering. */ protected Predicate pred; /** Construct a new filter. pre: The base iterator is defined. pre: The predicate is defined. post: A new filter iterator is returned. post: The base iterator may be affected by uses of this filter. */ public PredicateFilter(Iterator base, Predicate pred) { this.base = base; this.pred = pred; } // PredicateFilter(Iterator,Predicate) /** Reset the filter. post: The filter is reset to the beginning (all elements are marked as unseen). post: The base iterator may also be affected.  */ public void reset() { base.reset(); } // reset /** Peek at the next element. pre: There are unseen elements remaining. post: The next unseen element is returned. post: That element is not marked as seen. post: The base iterator may be advanced. @throws IteratorException if no unseen elements remain */ public Object value() throws IteratorException { // Advance the base iterator until acceptable element found if (!advance()) { throw new IteratorException("No acceptable elements remain"); } return base.value(); } // value /** Get the next element. pre: There are unseen elements remaining. post: The next unseen element is returned. post: That element is not marked as seen. post: The base iterator may be advanced. @throws IteratorException if no unseen elements remain */ public Object nextElement() throws IteratorException { // Advance the base iterator until acceptable element found if (!advance()) { throw new IteratorException("No acceptable elements remain"); } return base.nextElement(); } // nextElement() /** Are there acceptable elements remaining? pre: (none) post: True is returned if there are acceptable elements remaining. False is returned o/w. post: The base iterator may be advanced. */ public boolean hasMoreElements() { return advance(); } // advance() /** Advance the base iterator until an acceptable element is found (one that matches the base predicate). Returns a boolean value that indicates whether or not such an element is found. pre: (none) post: If acceptable unseen elements remain, the current value of the base iterator is one such element. post: If no acceptable unseen elements remain, the current value of the bsae iterator is undefined. post: The base iterator may be advanced. */ private boolean advance() { while ( (base.hasMoreElements()) && (!pred.okay(base.value())) ) { base.nextElement(); } // while // Are there elements remaining in the base predicate. If so, // the advance was successful. return base.hasMoreElements(); } // advance() } // PredicateFilter
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Wed Nov 5 12:24:49 1997.
This page generated on Wed Nov 5 12:38:25 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu