Esempio n. 1
0
 @Override
 protected Node[] createNodes(Object key) {
   Node node;
   if (key == keyLayout) node = new LayoutNode((RADVisualContainer) container);
   else {
     node = new RADComponentNode((RADComponent) key);
     node.getChildren().getNodes(); // enforce subnodes creation
   }
   return new Node[] {node};
 }
 public <T> void parallelRecursive(
     final Executor exec, List<Node<T>> nodes, final Collection<T> results) {
   for (final Node<T> n : nodes) {
     exec.execute(
         new Runnable() {
           public void run() {
             results.add(n.compute());
           }
         });
     parallelRecursive(exec, n.getChildren(), results);
   }
 }
  /** Collapse node - reassign parent child relationships as needed */
  public void redo() {
    Node parent = node.getParent();

    if (nodeToCollapse == null) {
      parent.setChildrenChanged(true);
      parent.removeChild(node);
      return;
    }

    if (nodeToCollapse == parent) {
      // Node otherChild = parent.getChildAtIndex(1 - parent.indexOfChild(node));
      parentIndex = oldGrandParent.indexOfChild(parent);
      oldGrandParent.removeChild(parent);
      int tempIndex = parentIndex;
      Iterator it = parent.getChildren().iterator();
      while (it.hasNext()) {
        Node ch = (Node) it.next();
        if (ch != node) {
          oldGrandParent.addToChildren(tempIndex++, ch);
        }
      }
      oldGrandParent.setChildrenChanged(true);
    } else {
      // nodeToCollapse is the other child
      parent.removeChild(node);
      parent.removeChild(nodeToCollapse);

      Iterator it = nodeToCollapse.getChildren().iterator();
      while (it.hasNext()) {
        Node ch = (Node) it.next();
        parent.addToChildren(ch);
      }
      parent.setChildrenChanged(true);
      parent.setChildCountOnServer(parent.getChildren().size());
    }
    NodeGraveyard.getGraveyard().addNode(nodeToCollapse);
  }
  private void buildNameToNodeListTable(Node curNode) throws IOException {
    String fullName = curNode.getName();
    String parent = curNode.getParent();
    String separator = System.getProperty("file.separator");

    if (parent != null) {
      if (!fullName.startsWith(parent)) {
        throw new RuntimeException(
            "Internal error: parent of file name \""
                + fullName
                + "\" does not match file name \""
                + parent
                + "\"");
      }

      int len = parent.length();
      if (!parent.endsWith(separator)) {
        len += separator.length();
      }

      String fileName = fullName.substring(len);

      if (fileName == null) {
        throw new RuntimeException("Internal error: file name was empty");
      }

      List nodeList = (List) nameToNodeListTable.get(fileName);
      if (nodeList == null) {
        nodeList = new Vector();
        nameToNodeListTable.put(fileName, nodeList);
      }

      nodeList.add(curNode);
    } else {
      if (curNode != rootNode) {
        throw new RuntimeException(
            "Internal error: parent of file + \"" + fullName + "\"" + " was null");
      }
    }

    if (curNode.isDirectory()) {
      Iterator iter = curNode.getChildren();
      if (iter != null) {
        while (iter.hasNext()) {
          buildNameToNodeListTable((Node) iter.next());
        }
      }
    }
  }
Esempio n. 5
0
 public void printSiblings() {
   if (parent == null) System.out.println("Siblings: " + parent);
   else {
     System.out.print("Siblings: ");
     ArrayList<Node> siblings = parent.getChildren();
     if (siblings.isEmpty()) System.out.print("null");
     else {
       for (int i = 0; i < siblings.size(); ++i) {
         Node child = siblings.get(i);
         if (!child.getName().equals(name)) System.out.print(child.getName() + "\t");
       }
     }
     System.out.println();
   }
 }
Esempio n. 6
0
    boolean insert(Node p, List<Node> Qi, int level) {
      Node parent = null;
      List<Node> nQi = new ArrayList<Node>();
      for (Node q : Qi) {
        if (dist(p, q) <= layerSize[level]) {
          nQi.add(q);
          parent = q;
        }
      }

      if (parent == null) // separation holds
      return true;

      for (Node q : Qi)
        for (Node ch : q.getChildren(level)) if (dist(p, ch) <= layerSize[level]) nQi.add(ch);

      if (insert(p, nQi, level + 1)) parent.addChild(p, level);

      return false;
    }
Esempio n. 7
0
    void findNearest(Node p, List<Node> Qi, int level) {
      for (; !Qi.isEmpty(); level++) {
        List<Node> Q = new ArrayList<>();
        for (Node q : Qi) {
          Q.add(q);
          for (Node ch : q.getChildren(level)) {
            if (ch != p) {
              double dist = dist(p, ch);
              if (bestDist > dist) {
                bestDist = dist;
                bestNode = ch;
              }
            }
            Q.add(ch);
          }
        }

        Qi = new ArrayList<Node>();
        for (Node q : Q)
          if (q.maxChildLevel > level && dist(p, q) <= bestDist + layerSize[level]) Qi.add(q);
      }
    }
 public <T> void sequentialRecursive(List<Node<T>> nodes, Collection<T> results) {
   for (Node<T> n : nodes) {
     results.add(n.compute());
     sequentialRecursive(n.getChildren(), results);
   }
 }
Esempio n. 9
0
  /**
   * parse sentence and generate .trees file
   *
   * @param en
   * @param align
   * @param out
   */
  public static void parse(String en, String align, String out, boolean verbose) {

    // use alignments?
    boolean use_alignments = true;
    if (align.startsWith("no_align")) {
      use_alignments = false;
      System.err.println("Not using alignments.");
    } else {
      System.err.println("Using alignments from " + align);
    }

    // setup stanfordparser
    String grammar = "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz";
    String[] options = {"-outputFormat", "wordsAndTags, typedDependencies"};
    LexicalizedParser lp = LexicalizedParser.loadModel(grammar, options);
    TreebankLanguagePack tlp = lp.getOp().langpack();
    java.util.function.Predicate<java.lang.String> punctuationFilter = x -> true;

    GrammaticalStructureFactory gsf =
        new edu.stanford.nlp.trees.EnglishGrammaticalStructureFactory(punctuationFilter);

    // read document
    Iterable<List<? extends HasWord>> sentences;
    Reader r = new Reader(en);
    String line = null;
    List<List<? extends HasWord>> tmp = new ArrayList<List<? extends HasWord>>();
    while ((line = r.getNext()) != null) {
      Tokenizer<? extends HasWord> token =
          tlp.getTokenizerFactory().getTokenizer(new StringReader(line));
      List<? extends HasWord> sentence = token.tokenize();
      tmp.add(sentence);
    }
    sentences = tmp;

    // set up alignment file reader
    Reader alignment = new Reader();
    if (use_alignments) {
      alignment = new Reader(align);
    }

    // set up tree file writer
    Writer treeWriter = new Writer(out);

    // parse
    long start = System.currentTimeMillis();
    // System.err.print("Parsing sentences ");
    int sentID = 0;
    for (List<? extends HasWord> sentence : sentences) {
      Tree t = new Tree();
      // t.setSentID(++sentID);
      System.err.println("parse Sentence :" + sentence + "...");
      // System.err.print(".");
      System.err.println("-----------------------------------------------------------------------");
      edu.stanford.nlp.trees.Tree parse = lp.parse(sentence);
      // parse.pennPrint();

      // List for root node and lexical nodes
      List<Node> loneNodes = new LinkedList<Node>();
      List<Node> governingNodes = new LinkedList<Node>();

      // ROOT node
      Node root = new Node(true, true);
      root.setTag("ROOT");
      t.setRoot(root);
      loneNodes.add(root);
      governingNodes.add(root);

      // tagging

      int counter = 0;
      String surface = "";
      String tag = "";

      for (TaggedWord tw : parse.taggedYield()) {
        Node n = new Node();
        Node governingNode = new Node();
        n.setNodeID(++counter);
        surface = tw.value();
        tag = tw.tag();
        if (surface.startsWith("-LRB-")) {
          surface = "(";
        } else if (surface.startsWith("-RRB-")) {
          surface = ")";
          // } else if (surface.startsWith("-LSB-")){
          //    surface = "[";
          // } else if (surface.startsWith("-RSB-")){
          //    surface = "]";
          // } else if (surface.startsWith("-LCB-")){
          //    surface = "{";
          // } else if (surface.startsWith("-RCB-")){
          //    surface = "}";
        } else if (surface.startsWith("''")) {
          surface = "\"";
        }
        tag = tag.replaceAll("#", "-NUM-");
        surface = surface.replaceAll("&", "-AMP-");
        surface = surface.replaceAll("#", "-NUM-");
        surface = surface.replaceAll(">", "-GRE-");
        surface = surface.replaceAll("=", "-EQU-");
        n.setInitialLexicalIndex(counter);
        governingNode.setInitialLexicalIndex(counter);
        n.setSurface(surface);
        // System.out.print("("+tw.value()+" : ");
        n.setTag(tag);
        governingNode.setTag("_" + tag);
        governingNode.setLabel("_gov");
        // System.out.print(tw.tag()+")");
        loneNodes.add(n);
        governingNodes.add(governingNode);
        governingNode.setChild(n);
      }

      // System.out.println("");

      // t.setSentLength(t.getNodes().size() - 1);
      // List<Node> loneNodes = new LinkedList<Node>();
      Node[] nodes = new Node[2000];
      // labeling
      int depIndex;
      int govIndex;
      String[] depInfo;
      String[] govInfo;
      GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
      List<TypedDependency> tdl = gs.typedDependencies(false);
      // List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
      for (TypedDependency td : tdl) {
        depIndex = td.dep().index();
        govIndex = td.gov().index();
        // System.out.println("Index1:"+depIndex);
        // System.out.println("Index2:"+govIndex);
        // if (nodes[depIndex] == null){
        //	System.out.println("Making node!");
        //	nodes[depIndex] = new Node();
        // }
        // if (nodes[govIndex] == null){
        //	System.out.println("Making node!");
        //	nodes[govIndex] = new Node();
        // }
        Node dep = loneNodes.get((depIndex));
        Node gov = governingNodes.get((govIndex));
        Node depcopy = governingNodes.get((depIndex));
        Node govcopy = loneNodes.get((govIndex));
        dep.setLabel(td.reln().toString());
        depcopy.setLabel(td.reln().toString());
        govcopy.setLabel("head");
        // System.out.println(td.toString());
        govInfo = td.gov().toString().split("/");
        depInfo = td.dep().toString().split("/");
        // System.out.println(td.gov().toString());
        // System.out.println(td.dep().toString());
        // dep.setSurface(depInfo[0]);
        // dep.setTag(depInfo[1]);
        gov.setChild(governingNodes.get(depIndex));
        governingNodes.get(depIndex).setParent(gov);
        // gov.setChild(dep);
        dep.setParent(governingNodes.get(depIndex));
      }
      // t.setRoot(nodes[0]);

      // Collapse tree to remove unneeded governing nodes:

      Node gov;
      Node dep;
      Node parent;
      List<Node> children;

      for (int i = 1; i < governingNodes.size(); i++) { // start with index 1 to skip root
        gov = governingNodes.get(i);
        dep = loneNodes.get(i);
        if (gov.getChildren().size() <= 1) {
          int k = 0;
          parent = gov.getParent();
          children = parent.getChildren();

          for (Node n : children) {
            if (n == gov) {
              gov.getParent().replaceChild(k, dep);
              dep.setParent(gov.getParent());
            }
            k++;
          }
        }
      }
      // Mark head nodes with appropriate label:
      int k = 0;
      for (Node n : loneNodes) {
        if (k != 0) {
          if (n.getLabel() == n.getParent().getLabel()) {
            n.setLabel("head");
          }
        } else {
          n.setLabel("null");
        }
        k++;
      }
      // Sort lexical children of each governing node in lexical order

      for (Node n : governingNodes) {
        n.sortChildrenByInitialIndex();
      }

      // combine with alignment
      if (use_alignments) {
        t.initialize(alignment.readNextAlign());
      } else {
        t.initializeUnaligned();
      }

      // write tree to file
      treeWriter.write(t);

      // print tree to console

      System.out.println(t.toSentence());
      if (verbose) {
        System.err.println(t.toString());
        // t.recursivePrint();
      }
      System.err.println("#######################################################################");
    }
    long stop = System.currentTimeMillis();
    System.err.println("...done! [" + (stop - start) / 1000 + " sec].");

    treeWriter.close();
  }