/**
   * Creates the grammar from the language model. This Grammar contains one word per grammar node.
   * Each word (and grammar node) is connected to all other words with the given probability
   *
   * @return the initial grammar node
   */
  @Override
  protected GrammarNode createGrammar() throws IOException {
    if (lattice == null) {
      return createGrammarNode("<s>");
    }

    lattice.removeFillers();

    GrammarNode firstNode = null;
    HashMap<Node, GrammarNode> nodeMap = new HashMap<Node, GrammarNode>();
    for (Node n : lattice.getNodes()) {
      String word = n.getWord().toString();
      GrammarNode node = createGrammarNode(word);
      if (n.equals(lattice.getInitialNode())) firstNode = node;
      if (n.equals(lattice.getTerminalNode())) node.setFinalNode(true);
      nodeMap.put(n, node);
    }
    if (firstNode == null) {
      throw new Error("No lattice start found");
    }

    for (Edge e : lattice.getEdges()) {
      float logProbability = (float) e.getLMScore();
      GrammarNode prevNode = nodeMap.get(e.getFromNode());
      GrammarNode toNode = nodeMap.get(e.getToNode());
      prevNode.add(toNode, logProbability);
    }

    return firstNode;
  }
  /**
   * Main method for running the LatticeCompTest demo.
   *
   * @throws IOException
   * @throws UnsupportedAudioFileException
   */
  @Test
  public void testLatticeComp() throws UnsupportedAudioFileException, IOException {

    URL audioFileURL = new File("src/test/edu/cmu/sphinx/result/test/green.wav").toURI().toURL();
    URL configURL = new File("src/test/edu/cmu/sphinx/result/test/config.xml").toURI().toURL();

    ConfigurationManager cm = new ConfigurationManager(configURL);

    Recognizer recognizer = (Recognizer) cm.lookup("recognizer");

    StreamDataSource reader = (StreamDataSource) cm.lookup("streamDataSource");

    AudioInputStream ais = AudioSystem.getAudioInputStream(audioFileURL);

    /* set the stream data source to read from the audio file */
    reader.setInputStream(ais, audioFileURL.getFile());

    /* allocate the resource necessary for the recognizer */
    recognizer.allocate();

    /* decode the audio file */
    Result result = recognizer.recognize();

    /* print out the results */
    Lattice lattice = new Lattice(result);
    lattice.dumpAISee("lattice.gdl", "lattice");

    recognizer.deallocate();

    cm = new ConfigurationManager(configURL);

    ConfigurationManagerUtils.setProperty(cm, "keepAllTokens", "true");

    recognizer = (Recognizer) cm.lookup("recognizer");
    recognizer.allocate();

    reader = (StreamDataSource) cm.lookup("streamDataSource");
    reader.setInputStream(AudioSystem.getAudioInputStream(audioFileURL), audioFileURL.getFile());

    Result allResult = recognizer.recognize();

    Lattice allLattice = new Lattice(allResult);
    allLattice.dumpAISee("allLattice.gdl", "All Lattice");

    assertTrue(lattice.isEquivalent(allLattice));
  }