Package org.maltparserx.core.syntaxgraph.node

Examples of org.maltparserx.core.syntaxgraph.node.PhraseStructureNode


          }
        }
      }
    } else if (direction == Direction.RIGHT) {
      for (int i = nt.nChildren()-1; i >= 0; i--) {
        PhraseStructureNode child = nt.getChild(i);
        for (int j = 0; j < size(); j++) {
          match = matchHeadChild(child, get(j));
          if (match == true) {
            if (j+1 >= size()) {
              return child;
View Full Code Here


  }
 
  private void writeTerminals(PhraseStructure phraseStructure) throws MaltChainedException {
    try {
      for (int index : phraseStructure.getTokenIndices()) {
        final PhraseStructureNode terminal = phraseStructure.getTokenNode(index);
        final Iterator<ColumnDescription> columns = dataFormatInstance.iterator();
        ColumnDescription column = null;
        int ti = 1;
        while (columns.hasNext()) {
          column = columns.next();
          if (column.getCategory() == ColumnDescription.INPUT) {
            writer.write(terminal.getLabelSymbol(column.getSymbolTable()));
            int nTabs = 1;
            if (ti == 1 || ti == 2) {
              nTabs = 3 - (terminal.getLabelSymbol(column.getSymbolTable()).length() / 8);
            } else if (ti == 3) {
              nTabs = 1;
            } else if (ti == 4) {
              nTabs = 2 - (terminal.getLabelSymbol(column.getSymbolTable()).length() / 8);
            }
            if (nTabs < 1) {
              nTabs = 1;
            }
            for (int j = 0; j < nTabs; j++) {
              writer.write('\t');
            }
            ti++;
          } else if (column.getCategory() == ColumnDescription.PHRASE_STRUCTURE_EDGE_LABEL) {
            if (terminal.getParent() != null && terminal.hasParentEdgeLabel(column.getSymbolTable())) {
              writer.write(terminal.getParentEdgeLabelSymbol(column.getSymbolTable()));
              writer.write('\t');
            } else {
              writer.write("--\t");
            }
          } else if (column.getCategory() == ColumnDescription.PHRASE_STRUCTURE_NODE_LABEL) {
            if (terminal.getParent() == null || terminal.getParent() == phraseStructure.getPhraseStructureRoot()) {
              writer.write('0');
            } else {
              writer.write(Integer.toString(nonTerminalIndexMap.get(terminal.getParent().getIndex())));
//              writer.write(Integer.toString(terminal.getParent().getIndex()+START_ID_OF_NONTERMINALS-1));
            }
          }
        }
        for (Edge e : terminal.getIncomingSecondaryEdges()) {
          if (e.hasLabel(column.getSymbolTable())) {
            writer.write('\t');
            writer.write(e.getLabelSymbol(column.getSymbolTable()));
            writer.write('\t');
            if (e.getSource() instanceof NonTerminalNode) {
View Full Code Here

    }
  }
 
  private void updateDependenyLabels(MappablePhraseStructureGraph graph) throws MaltChainedException {
    for (int index :graph.getTokenIndices()) {
      PhraseStructureNode top = (PhraseStructureNode)graph.getTokenNode(index);
     
      while (top != null && top.getParent() != null &&graph.getTokenNode(index) == ((NonTerminalNode)top.getParent()).getLexicalHead(headRules)) {
        top = top.getParent();
      }
      lockUpdate = true;
      labelDependencyEdge(graph, graph.getTokenNode(index).getHeadEdge(), top);
      lockUpdate = false;
   
View Full Code Here

        e.addLabel(symbolTables.getSymbolTable(DEPREL), deprelDefaultRootLabel);
      } else {
        e.addLabel(symbolTables.getSymbolTable(DEPREL), EMPTY_LABEL);
      }
    }
    PhraseStructureNode tmp = (PhraseStructureNode)e.getTarget();
    while (tmp != top && tmp.getParent() != null) { // && !tmp.getParent().isRoot()) {
      i=0;
      for (String name : edgeLabelSymbolTables.keySet()) {
        if (tmp.hasParentEdgeLabel(symbolTables.getSymbolTable(name))) {
          headrel.append(tmp.getParentEdgeLabelSymbol(symbolTables.getSymbolTable(name)));
        } else {
          headrel.append(EMPTY_LABEL);
        }
        i++;
        if (i < edgeLabelSymbolTables.size()) {
          headrel.append(LABEL_ELEMENT_SEPARATOR);
       
      }
      i=0;
      headrel.append(SPINE_ELEMENT_SEPARATOR);
      for (String name : nodeLabelSymbolTables.keySet()) {
        if (tmp.getParent().hasLabel(symbolTables.getSymbolTable(name))) {
          phrase.append(tmp.getParent().getLabelSymbol(symbolTables.getSymbolTable(name)));
        } else {
          if (tmp.getParent().isRoot()) {
            String deprelDefaultRootLabel = graph.getDefaultRootEdgeLabelSymbol(symbolTables.getSymbolTable(PHRASE));
            if (deprelDefaultRootLabel != null) {
              phrase.append(deprelDefaultRootLabel);
            } else {
              phrase.append(EMPTY_LABEL);
            }
          } else {
            phrase.append(EMPTY_LABEL);
          }
        }
        i++;
        if (i < nodeLabelSymbolTables.size()) {
          phrase.append(LABEL_ELEMENT_SEPARATOR);
        }
      }
      phrase.append(SPINE_ELEMENT_SEPARATOR);
      tmp = tmp.getParent();
    }
    if (phrase.length() == 0) {
      headrel.append(EMPTY_SPINE);
      phrase.append(EMPTY_SPINE);
    } else {
      headrel.setLength(headrel.length()-1);
      phrase.setLength(phrase.length()-1);
    }
    e.addLabel(symbolTables.getSymbolTable(HEADREL), headrel.toString());
    e.addLabel(symbolTables.getSymbolTable(PHRASE), phrase.toString());
    int a = 0;
    tmp = (PhraseStructureNode)e.getSource();
    while (top.getParent() != null && tmp.getParent() != null && tmp.getParent() != top.getParent()) {
      a++;
      tmp = tmp.getParent();
    }
    e.addLabel(symbolTables.getSymbolTable(ATTACH), Integer.toString(a));
  }
View Full Code Here

    }
  }
 
  private void connectUnattachedSpines(MappablePhraseStructureGraph graph, DependencyNode depNode) throws MaltChainedException {
    if (!depNode.isRoot()) {
      PhraseStructureNode dependentSpine = (PhraseStructureNode)depNode;
      while (dependentSpine.getParent() != null) {
        dependentSpine = dependentSpine.getParent();
      }
      if (!dependentSpine.isRoot()) {
        updatePhraseStructureGraph(graph,depNode.getHeadEdge(),true);
      }
    }
    for (int i = 0; i < depNode.getLeftDependentCount(); i++) {
      connectUnattachedSpines(graph, depNode.getLeftDependent(i));
View Full Code Here

      connectUnattachedSpines(graph, depNode.getRightDependent(i));
    }
  }
 
  public void updatePhraseStructureGraph(MappablePhraseStructureGraph graph, Edge depEdge, boolean attachHeadSpineToRoot) throws MaltChainedException {
    PhraseStructureNode dependentSpine = (PhraseStructureNode)depEdge.getTarget();
   
    if (((PhraseStructureNode)depEdge.getTarget()).getParent() == null) {
      // Restore dependent spine
      String phraseSpineLabel = null;
      String edgeSpineLabel = null;
      int empty_label = 0;
     
      if (depEdge.hasLabel(graph.getSymbolTables().getSymbolTable(PHRASE))) {
        phraseSpineLabel = depEdge.getLabelSymbol(graph.getSymbolTables().getSymbolTable(PHRASE));
      }
      if (depEdge.hasLabel(graph.getSymbolTables().getSymbolTable(HEADREL))) {
        edgeSpineLabel = depEdge.getLabelSymbol(graph.getSymbolTables().getSymbolTable(HEADREL));
      }
      if (phraseSpineLabel != null && phraseSpineLabel.length() > 0 && phraseSpineLabel.charAt(0) != EMPTY_SPINE) {
        int ps = 0, es = 0, i = 0, j = 0, n = phraseSpineLabel.length()-1, m = edgeSpineLabel.length()-1;
        PhraseStructureNode child = (PhraseStructureNode)depEdge.getTarget();
        while (true) {
          while (i <= n && phraseSpineLabel.charAt(i) != SPINE_ELEMENT_SEPARATOR) {
            if (phraseSpineLabel.charAt(i) == QUESTIONMARK) {
              empty_label++;
            } else {
              empty_label = 0;
            }
            i++;
          }
          if (depEdge.getSource().isRoot() && i >= n) {
            dependentSpine = graph.getPhraseStructureRoot();
          } else {
            dependentSpine = graph.addNonTerminalNode(++nonTerminalCounter);
          }

          if (empty_label != 2 && ps != i) {
            dependentSpine.addLabel(graph.getSymbolTables().addSymbolTable(CAT), phraseSpineLabel.substring(ps,i));
          }

          empty_label = 0;
          if (edgeSpineLabel != null) {
            while (j <= m && edgeSpineLabel.charAt(j) != SPINE_ELEMENT_SEPARATOR) {
              if (edgeSpineLabel.charAt(j) == QUESTIONMARK) {
                empty_label++;
              } else {
                empty_label = 0;
              }
              j++;
            }
          }
          lockUpdate = true;
          Edge e = graph.addPhraseStructureEdge(dependentSpine, child);
          if (empty_label != 2 && es != j && edgeSpineLabel != null  && e != null) {
            e.addLabel(graph.getSymbolTables().addSymbolTable(EDGELABEL), edgeSpineLabel.substring(es,j));
          } else if (es == j) {
            e.addLabel(graph.getSymbolTables().addSymbolTable(EDGELABEL), EMPTY_LABEL);
          }

          lockUpdate = false;
          child = dependentSpine;
          if (i >= n) { break; }
          empty_label = 0;
          ps = i = i + 1;
          es = j = j + 1;
        }
      }

      // Recursively attach the dependent spines to target node.
      DependencyNode target = (DependencyNode)depEdge.getTarget();
      for (int i = 0; i < target.getLeftDependentCount(); i++) {
        updatePhraseStructureGraph(graph, target.getLeftDependent(i).getHeadEdge(), attachHeadSpineToRoot);
      }
      for (int i = target.getRightDependentCount()-1; i >= 0 ; i--) {
        updatePhraseStructureGraph(graph, target.getRightDependent(i).getHeadEdge(), attachHeadSpineToRoot);
      }
    } else {
      // If dependent spine already exist, then set dependentSpine to the highest nonterminal
      // of the dependent spine.
      while (dependentSpine.getParent() != null && !dependentSpine.getParent().isRoot()) {
        dependentSpine = dependentSpine.getParent();
      }
    }

   
    PhraseStructureNode headSpine = null;
    if (((PhraseStructureNode)depEdge.getSource()).getParent() != null) {
      // If head spine exist, then attach dependent spine to the head spine at the attachment level a.
      int a = 0;
      headSpine = ((PhraseStructureNode)depEdge.getSource()).getParent();
      if (depEdge.hasLabel(graph.getSymbolTables().getSymbolTable(ATTACH))) {
        try {
        a = Integer.parseInt((depEdge.getLabelSymbol(graph.getSymbolTables().getSymbolTable(ATTACH))));
        } catch (NumberFormatException e) {
          throw new MaltChainedException(e.getMessage());
        }
      }
      for (int i = 0; i < a && headSpine != null; i++) {
        headSpine = headSpine.getParent();
      }
     
      if ((headSpine == null || headSpine == dependentSpine) && attachHeadSpineToRoot) {
        headSpine = graph.getPhraseStructureRoot();
      }
View Full Code Here

TOP

Related Classes of org.maltparserx.core.syntaxgraph.node.PhraseStructureNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.