import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; /** * Contains information about a Harry Potter script */ public class HarryPotterScript { /** all the lines in the script */ private ArrayList script = new ArrayList(); /** * Loads a Harry Potter Script from file and splits it into lines. */ public HarryPotterScript(String filename) { BufferedReader br = null; try { // load the script from file br = new BufferedReader(new FileReader(filename)); br.readLine(); // ignore the first line // read the script, line by line, processing each line String line = br.readLine(); while (line != null) { String[] splitLine = line.split(":"); switch (splitLine.length) { case 0: // no text (i.e., spacing between lines) case 1: // no speaker (i.e., non-spoken description) break; case 2: // regular spoken text String character = splitLine[0]; String text = splitLine[1]; this.addLine(new ScriptLine(character, text)); break; default: // error (this shouldn't happen) System.exit(1); } line = br.readLine(); // get the next line } } // error handling catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // clean up the file finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** add a new line of text to the script */ public void addLine(ScriptLine newLine) { int lastIndex = this.script.size() - 1; if (lastIndex >= 0) { ScriptLine lastLine = this.script.get(lastIndex); if (lastLine.getSpeaker().equals(newLine.getSpeaker())) { lastLine.addText(newLine.getText()); return; } } this.script.add(newLine); } /** * Loads a script from file and builds a "speaker graph". The nodes of the * graph are characters in the script, and an edge from one character to * another means that the first character speaks to the second */ public static void main(String[] args) { // create the script HarryPotterScript s = new HarryPotterScript("HarryPotter_PhilosophersStone.txt"); // TODO: Modify the type of this graph so that it stores different // information on the edges. Then, update the code below so // that it keeps track of this new information // create the initial graph DirectedWeightedGraph graph = new AdjacencyList(); // read through the script Iterator currentLines = s.script.iterator(); Iterator nextLines = s.script.iterator(); nextLines.next(); while (nextLines.hasNext()) { // get the current line (speaker and text) ScriptLine lineK = currentLines.next(); String speaker1 = lineK.getSpeaker(); String text = lineK.getText(); int textLength = text.length(); // get the next line (speaker) ScriptLine lineKplus1 = nextLines.next(); String speaker2 = lineKplus1.getSpeaker(); // add or update the edge from speaker1 to speaker 2 Integer currentEdge = graph.removeEdge(speaker1, speaker2); // speaker1 is talking to speaker2 for the first time if (currentEdge == null) { currentEdge = new Integer(textLength); } // speaker1 is saying more things to speaker2 else { currentEdge = new Integer(currentEdge.intValue() + textLength); } // update the graph with the new information graph.addEdge(speaker1, speaker2, currentEdge); } // Note: We lose the last line b/c there isn't a next speaker ScriptLine lastLine = currentLines.next(); System.out.println(lastLine.getSpeaker() + ": " + lastLine.getText()); // display some information about Hermione and Ron System.out.println("Hermione letter count to Ron: " + graph.getEdge("Hermione", "Ron")); System.out.println("Hermione letter count to Harry: " + graph.getEdge("Hermione", "Harry")); } }