lattice.java
来自「It is the Speech recognition software. 」· Java 代码 · 共 1,093 行 · 第 1/3 页
JAVA
1,093 行
Edge e = new Edge(fromNode, toNode, acousticScore, lmScore); fromNode.addLeavingEdge(e); toNode.addEnteringEdge(e); edges.add(e); return e; } /** * Add a Node that represents the theory that a given word was spoken * over a given period of time. * * @param word * @param beginTime * @param endTime * @return the new Node */ public Node addNode(Word word, int beginTime, int endTime) { Node n = new Node(word, beginTime, endTime); addNode(n); return n; } /** * Add a Node that represents the theory that a given word was spoken * over a given period of time. * * @param word * @param beginTime * @param endTime * @return the new Node */ public Node addNode(String word, int beginTime, int endTime) { Word w = new Word(word, new Pronunciation[0], false); return addNode(w, beginTime, endTime); } /** * Add a Node with a given ID that represents the theory that a * given word was spoken over a given period of time. * This method is used when loading Lattices from .LAT files. * * @param word * @param beginTime * @param endTime * @return the new Node */ protected Node addNode(String id, Word word, int beginTime, int endTime) { Node n = new Node(id, word, beginTime, endTime); addNode(n); return n; } /** * Add a Node with a given ID that represents the theory that a * given word was spoken over a given period of time. * This method is used when loading Lattices from .LAT files. * * @param word * @param beginTime * @param endTime * @return the new Node */ protected Node addNode(String id, String word, int beginTime, int endTime) { Word w = new Word(word, new Pronunciation[0], false); return addNode(id, w, beginTime, endTime); } /** * Add a Node corresponding to a Token from the result Token tree. * Usually, the Token should reference a search state that is a * WordSearchState, although other Tokens may be used for debugging. * @param token * @return the new Node */ protected Node addNode(Token token, int beginTime, int endTime) { assert (token.getSearchState() instanceof WordSearchState); Word word = ((WordSearchState) (token.getSearchState())) .getPronunciation().getWord(); return addNode(Integer.toString(token.hashCode()), word, beginTime, endTime); } /** * Test to see if the Lattice contains a Node * * @param node * @return true if yes */ boolean hasNode(Node node) { return nodes.containsValue(node); } /** * Test to see if the Lattice contains an Edge * * @param edge * @return true if yes */ boolean hasEdge(Edge edge) { return edges.contains(edge); } /** * Test to see if the Lattice already contains a Node corresponding * to a given Token. * * @param ID the ID of the Node to find * @return true if yes */ protected boolean hasNode(String ID) { return nodes.containsKey(ID); } /** * Add a Node to the set of all Nodes * * @param n */ protected void addNode(Node n) { assert !hasNode(n.getId()); nodes.put(n.getId(), n); } /** * Remove a Node from the set of all Nodes * * @param n */ protected void removeNode(Node n) { assert hasNode(n.getId()); nodes.remove(n.getId()); } /** * Get the Node associated with an ID * * @param id * @return the Node */ protected Node getNode(String id) { return (Node) (nodes.get(id)); } /** * Get a copy of the Collection of all Nodes. * Used by LatticeOptimizer to avoid Concurrent modification of the * nodes list. * * @return a copy of the collection of Nodes */ protected Collection getCopyOfNodes() { return new Vector(nodes.values()); } /** * Get the Collection of all Nodes. * * @return the colllection of all Nodes */ public Collection getNodes() { return nodes.values(); } /** * Remove an Edge from the set of all Edges. * @param e */ protected void removeEdge(Edge e) { edges.remove(e); } /** * Get the set of all Edges. * * @return the set of all edges */ public Collection getEdges() { return edges; } /** * Dump the Lattice in the form understood by AiSee * (a graph visualization tool). See http://www.AbsInt.com * * @param fileName * @param title */ public void dumpAISee(String fileName, String title) { try { System.err.println("Dumping " + title + " to " + fileName); FileWriter f = new FileWriter(fileName); f.write("graph: {\n"); f.write("title: \"" + title + "\"\n"); f.write("display_edge_labels: yes\n"); /* f.write( "colorentry 32: 25 225 0\n"); f.write( "colorentry 33: 50 200 0\n"); f.write( "colorentry 34: 75 175 0\n"); f.write( "colorentry 35: 100 150 0\n"); f.write( "colorentry 36: 125 125 0\n"); f.write( "colorentry 37: 150 100 0\n"); f.write( "colorentry 38: 175 75 0\n"); f.write( "colorentry 39: 200 50 0\n"); f.write( "colorentry 40: 225 25 0\n"); f.write( "colorentry 41: 250 0 0\n"); f.write( "color: black\n"); f.write( "orientation: left_to_right\n"); f.write( "xspace: 10\n"); f.write( "yspace: 10\n"); */ for (Iterator i = nodes.values().iterator(); i.hasNext();) { ((Node) (i.next())).dumpAISee(f); } for (Iterator i = edges.iterator(); i.hasNext();) { ((Edge) (i.next())).dumpAISee(f); } f.write("}\n"); f.close(); } catch (IOException e) { throw new Error(e.toString()); } } /** * Dump the Lattice as a .LAT file * * @param out * @throws IOException */ protected void dump(PrintWriter out) throws IOException { //System.err.println( "Dumping to " + out ); for (Iterator i = nodes.values().iterator(); i.hasNext();) { ((Node) (i.next())).dump(out); } for (Iterator i = edges.iterator(); i.hasNext();) { ((Edge) (i.next())).dump(out); } out.println("initialNode: " + initialNode.getId()); out.println("terminalNode: " + terminalNode.getId()); out.println("logBase: " + logMath.getLogBase()); out.flush(); } /** * Dump the Lattice as a .LAT file. Used to save Lattices as * ASCII files for testing and experimentation. * * @param file */ public void dump(String file) { try { dump(new PrintWriter(new FileWriter(file))); } catch (IOException e) { throw new Error(e.toString()); } } /** * Remove a Node and all Edges connected to it. Also remove those * Edges from all connected Nodes. * * @param n */ protected void removeNodeAndEdges(Node n) { //System.err.println("Removing node " + n + " and associated edges"); for (Iterator i = n.getLeavingEdges().iterator(); i.hasNext();) { Edge e = (Edge) (i.next()); e.getToNode().removeEnteringEdge(e); //System.err.println( "\tRemoving " + e ); edges.remove(e); } for (Iterator i = n.getEnteringEdges().iterator(); i.hasNext();) { Edge e = (Edge) (i.next()); e.getFromNode().removeLeavingEdge(e); //System.err.println( "\tRemoving " + e ); edges.remove(e); } //System.err.println( "\tRemoving " + n ); nodes.remove(n.getId()); assert checkConsistency(); } /** * Remove a Node and cross connect all Nodes with Edges to it. * * For example given * * Nodes A, B, X, M, N * Edges A-->X, B-->X, X-->M, X-->N * * Removing and cross connecting X would result in * * Nodes A, B, M, N * Edges A-->M, A-->N, B-->M, B-->N * * @param n */ protected void removeNodeAndCrossConnectEdges(Node n) { System.err.println("Removing node " + n + " and cross connecting edges"); for (Iterator i = n.getEnteringEdges().iterator(); i.hasNext();) { Edge ei = (Edge) (i.next()); for (Iterator j = n.getLeavingEdges().iterator(); j.hasNext();) { Edge ej = (Edge) (j.next()); addEdge(ei.getFromNode(), ej.getToNode(), ei.getAcousticScore(), ei.getLMScore()); } } removeNodeAndEdges(n); assert checkConsistency(); } /** * Get the initialNode for this Lattice. This corresponds usually to * the <s> symbol * * @return the initial Node */ public Node getInitialNode() { return initialNode; } /** * Set the initialNode for this Lattice. This corresponds usually to * the <s> symbol * * @param p_initialNode */ public void setInitialNode(Node p_initialNode) { initialNode = p_initialNode; } /** * Get the terminalNode for this Lattice. This corresponds usually to * the </s> symbol * * @return the initial Node */ public Node getTerminalNode() { return terminalNode; } /** * Set the terminalNode for this Lattice. This corresponds usually to * the </s> symbol * * @param p_terminalNode */ public void setTerminalNode(Node p_terminalNode) { terminalNode = p_terminalNode; } /** * Edge scores are usually log-likelyhood. Get the log base. * * @return the log base */ public double getLogBase() { return logMath.getLogBase(); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?