001package algs35;
002import stdlib.*;
003import algs13.Queue;
004/* ***********************************************************************
005 *  Compilation:  javac LookupIndex.java
006 *  Execution:    java LookupIndex movies.txt "/"
007 *  Dependencies: ST.java Queue.java In.java StdIn.java StdOut.java
008 *  Data files:   http://algs4.cs.princeton.edu/35applications/aminoI.txt
009 *                http://algs4.cs.princeton.edu/35applications/movies.txt
010 *
011 *  % java LookupIndex aminoI.txt ","
012 *  Serine
013 *    TCT
014 *    TCA
015 *    TCG
016 *    AGT
017 *    AGC
018 *  TCG
019 *    Serine
020 *
021 *  % java LookupIndex movies.txt "/"
022 *  Bacon, Kevin
023 *    Animal House (1978)
024 *    Apollo 13 (1995)
025 *    Beauty Shop (2005)
026 *    Diner (1982)
027 *    Few Good Men, A (1992)
028 *    Flatliners (1990)
029 *    Footloose (1984)
030 *    Friday the 13th (1980)
031 *    ...
032 *  Tin Men (1987)
033 *    DeBoy, David
034 *    Blumenfeld, Alan
035 *    ...
036 *
037 *************************************************************************/
038
039public class LookupIndex {
040
041        public static void main(String[] args) {
042                //args = new String[] { "data/aminoI.txt", "," }; StdIn.fromString ("Serine");
043                //args = new String[] { "data/aminoI.txt", "," }; //StdIn.fromString ("ATG");
044                //args = new String[] { "data/movies.txt", "/" }; StdIn.fromString ("Bacon, Kevin");
045                args = new String[] { "data/movies.txt", "/" }; StdIn.fromString ("12 Angry Men (1957)");
046
047                String filename  = args[0];
048                String separator = args[1];
049                In in = new In(filename);
050
051                ST<String, Queue<String>> st = new ST<>();
052                ST<String, Queue<String>> ts = new ST<>();
053
054                while (in.hasNextLine()) {
055                        String line = in.readLine();
056                        String[] fields = line.split(separator);
057                        String key = fields[0];
058                        for (int i = 1; i < fields.length; i++) {
059                                String val = fields[i];
060                                if (!st.contains(key)) st.put(key, new Queue<>());
061                                if (!ts.contains(val)) ts.put(val, new Queue<>());
062                                st.get(key).enqueue(val);
063                                ts.get(val).enqueue(key);
064                        }
065                }
066
067                StdOut.println("Done indexing");
068
069                // read queries from standard input, one per line
070                while (!StdIn.isEmpty()) {
071                        String query = StdIn.readLine();
072                        if (st.contains(query))
073                                for (String vals : st.get(query))
074                                        StdOut.println("  " + vals);
075                        if (ts.contains(query))
076                                for (String keys : ts.get(query))
077                                        StdOut.println("  " + keys);
078                }
079
080        }
081
082}