001package algs35;
002import stdlib.*;
003/* ***********************************************************************
004 *  Compilation:  javac WhiteFilter.java
005 *  Execution:    java WhiteFilter whitelist.txt < input.txt
006 *  Dependencies: SET In.java StdIn.java StdOut.java
007 *  Data files:   http://algs4.cs.princeton.edu/35applications/tinyTale.txt
008 *                http://algs4.cs.princeton.edu/35applications/list.txt
009 *
010 *  Read in a whitelist of words from a file. Then read in a list of
011 *  words from standard input and print out all those words that
012 *  are in the first file.
013 *
014 *  % more tinyTale.txt
015 *  it was the best of times it was the worst of times
016 *  it was the age of wisdom it was the age of foolishness
017 *  it was the epoch of belief it was the epoch of incredulity
018 *  it was the season of light it was the season of darkness
019 *  it was the spring of hope it was the winter of despair
020 *
021 *  % more list.txt
022 *  was it the of
023 *
024 *  % java WhiteFilter list.txt < tinyTale.txt
025 *  it was the of it was the of
026 *  it was the of it was the of
027 *  it was the of it was the of
028 *  it was the of it was the of
029 *  it was the of it was the of
030 *
031 *************************************************************************/
032
033public class WhiteFilter {
034        public static void main(String[] args) {
035                args = new String[] { "data/list.txt" };
036                StdIn.fromFile ("data/tinyTale.txt");
037
038                SET<String> set = new SET<>();
039
040                // read in strings and add to set
041                In in = new In(args[0]);
042                while (!in.isEmpty()) {
043                        String word = in.readString();
044                        set.add(word);
045                }
046
047                // read in string from standard input, printing out all exceptions
048                while (!StdIn.isEmpty()) {
049                        String word = StdIn.readString();
050                        if (set.contains(word))
051                                StdOut.println(word);
052                }
053        }
054}