001package algs35; 002import stdlib.*; 003import algs13.Bag; 004/* *********************************************************************** 005 * Compilation: javac MovieIndex.java 006 * Execution: java MovieIndex movies.txt 007 * Dependencies: ST.java Bag.java In.java StdIn.java 008 * 009 * % java MovieIndex movies-top-grossing.txt 010 * Stallone, Sylvester 011 * Rambo: First Blood Part II (1985) 012 * Rocky (1976) 013 * Rocky III (1982) 014 * Rocky IV (1985) 015 * 016 * Hanks, Tom 017 * Apollo 13 (1995) 018 * Big (1988) 019 * Forrest Gump (1994) 020 * Green Mile, The (1999) 021 * League of Their Own, A (1992) 022 * Saving Private Ryan (1998) 023 * Sleepless in Seattle (1993) 024 * Toy Story (1995) 025 * Toy Story 2 (1999) 026 * 027 * Apollo 13 (1995) 028 * Allen, Ivan 029 * Andrews, David 030 * Bacon, Kevin 031 * Barry, Thom 032 * Berkeley, Xander 033 * ... 034 * 035 *************************************************************************/ 036 037 038public class XMovieIndex { 039 040 public static void main(String[] args) { 041 042 // key = actor / movie, value = list of movies / actors 043 ST<String, Bag<String>> st = new ST<>(); 044 045 // create inverted index of all files 046 In in = new In(args[0]); 047 while (in.hasNextLine()) { 048 String line = in.readLine(); 049 String[] names = line.split("/"); 050 String movie = names[0]; 051 for (int i = 1; i < names.length; i++) { 052 String actor = names[i]; 053 if (!st.contains(actor)) st.put(actor, new Bag<>()); 054 if (!st.contains(movie)) st.put(movie, new Bag<>()); 055 st.get(actor).add(movie); 056 st.get(movie).add(actor); 057 } 058 } 059 StdOut.println("Done indexing files"); 060 StdOut.println(); 061 062 StdOut.println("Type the name of a performer or movie"); 063 while (!StdIn.isEmpty()) { 064 String name = StdIn.readLine(); 065 if (st.contains(name)) { 066 for (String s : st.get(name)) 067 StdOut.println(" " + s); 068 } 069 StdOut.println(); 070 } 071 } 072 073}