/**
   * 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;
  }
Beispiel #2
0
 /**
  * Test if a Node has all Edges to the same Nodes and another Node.
  *
  * @param n the node of interest
  * @return true if this Node has all Edges to the sames Nodes as n
  */
 public boolean hasEquivalentLeavingEdges(Node n) {
   if (leavingEdges.size() != n.getLeavingEdges().size()) {
     return false;
   }
   for (Edge e : leavingEdges) {
     Node toNode = e.getToNode();
     if (!n.hasEdgeToNode(toNode)) {
       return false;
     }
   }
   return true;
 }
Beispiel #3
0
 /**
  * Test if a Node has all Edges from the same Nodes and another Node.
  *
  * @param n node to check
  * @return true if this Node has Edges from the same Nodes as n
  */
 protected boolean hasEquivalentEnteringEdges(Node n) {
   if (enteringEdges.size() != n.getEnteringEdges().size()) {
     return false;
   }
   for (Edge e : enteringEdges) {
     Node fromNode = e.getFromNode();
     if (!n.hasEdgeFromNode(fromNode)) {
       return false;
     }
   }
   return true;
 }
Beispiel #4
0
 protected boolean isAncestorHelper(List<Node> children, Node node, Set<Node> seenNodes) {
   for (Node n : children) {
     if (seenNodes.contains(n)) {
       continue;
     }
     seenNodes.add(n);
     if (n.equals(node)) {
       return true;
     }
     if (isAncestorHelper(n.getChildNodes(), node, seenNodes)) {
       return true;
     }
   }
   return false;
 }
Beispiel #5
0
 protected void cacheDescendantsHelper(Node n) {
   for (Node child : n.getChildNodes()) {
     if (descendants.contains(child)) {
       continue;
     }
     descendants.add(child);
     cacheDescendantsHelper(child);
   }
 }
Beispiel #6
0
 /**
  * Returns true if the given node is equivalent to this node. Two nodes are equivalent only if
  * they have the same word, the same number of entering and leaving edges, and that their begin
  * and end times are the same.
  *
  * @param other the Node we're comparing to
  * @return true if the Node is equivalent; false otherwise
  */
 public boolean isEquivalent(Node other) {
   return ((word.getSpelling().equals(other.getWord().getSpelling())
           && (getEnteringEdges().size() == other.getEnteringEdges().size()
               && getLeavingEdges().size() == other.getLeavingEdges().size()))
       && (getBeginTime() == other.getBeginTime() && endTime == other.getEndTime()));
 }
Beispiel #7
0
 /**
  * Check whether this node has an ancestral relationship with another node (i.e. either this node
  * is an ancestor of the other node, or vice versa)
  *
  * @param node the Node to check for a relationship
  * @return whether a relationship exists
  */
 public boolean hasAncestralRelationship(Node node) {
   return this.isAncestorOf(node) || node.isAncestorOf(this);
 }