// required by TreeSelectionListener interface
  public void valueChanged(TreeSelectionEvent e) {

    // get selected node
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();

    if (node == null) return;

    // get node data object
    TermNode nodeInfo = (TermNode) node.getUserObject();
    // load children only for leaf nodes and those that have
    // not been marked as processed.
    if (node.isLeaf() && node.getAllowsChildren()) {
      System.out.println("will load children for: " + nodeInfo);
      // load children. if no children, set allowsChildren to false
      if (!parent.loadChildren(node, nodeInfo.getTermId())) {
        node.setAllowsChildren(false);
      }
    }
    // loadmetadata
    System.out.println("will load metadata for: " + nodeInfo.getTermId());
    // call method on parent container
    parent.loadMetaData(nodeInfo.getTermId());
  }
  /** Compute the tf-idf vector for each document using Sahami-Heilman's similarity Algorithm */
  public String getNormalizedAugment() {
    // Build the TFIDF term vectors for the relevant documents
    ArrayList<TermVector> docVectors = new ArrayList<TermVector>();
    for (ResultNode rn : result.getRelevantResultNodes()) {
      TermVector tv = new TermVector(rn, invIdx); // term vector for a document
      docVectors.add(tv);
    }

    // Original algorithm truncates each vector to 50 highest scoring terms
    // Our vectors are short, no need to truncate

    // Find the Centroid of the L2 normalized term vectors
    // Centroid is a vector of all distinct terms in the relevant documents
    // The term weight is the sum of the normalized weight of the term in all relevant docs
    TermVector centroid = new TermVector();
    for (TermVector tv : docVectors) {
      for (TermNode tn : tv.getTerms()) {
        String term = tn.getTerm();
        double weight = tn.getNormalizedWeight();
        centroid.addTerm(term, weight);
      }
    }

    // Normalize the Centroid
    centroid.l2normalize();

    // Sort descending order by normalized weight
    ArrayList<TermNode> termList = centroid.getTerms();
    Collections.sort(termList);

    // quick way to index all the previously used search terms
    // check the new found terms against previous query to make sure there are no duplicates
    HashSet<String> lastQueryWords = new HashSet<String>(Arrays.asList(lastQuery.split(" ")));

    // augment with up to 2 top tf-idf score terms
    double weightDiffThreshold = 0.2; // weight is less than 20% difference
    String augment = null;
    double augmentWeight = 0;
    for (int i = 0; i < termList.size(); i++) {
      TermNode t = termList.get(i);
      String term = t.getTerm();
      double weight = t.getWeight();

      // do not re-query terms that are in the previous query
      if (lastQueryWords.contains(term)) {
        System.out.println("term " + term + " (" + weight + ") is in previous query. Skipping.");
        continue;
      }
      // System.out.println("DEBUG: " + term + " weight=" + t.getWeight());

      // if the top 2 terms are close together in the score
      // use both terms in the next search
      if ((augment != null) && (weight > 0)) {
        // tie break results that are close
        double weightDiff = (augmentWeight - weight) / augmentWeight;
        if (weightDiff < weightDiffThreshold) {
          augment += " " + term;
        }
        break;
      } else {
        augment = term;
        augmentWeight = weight;
      }
    }
    return augment;
  }
Beispiel #3
0
  /**
   * Add support for right clicking verticies to expand them.
   *
   * @param e
   */
  @Override
  public void mousePressed(MouseEvent e) {
    super.mousePressed(e);
    GUI2.getInstance().getGraphPanel().highlightedNodes.clear();
    if (e.getButton() != MouseEvent.BUTTON3) {
      popup.setVisible(false);
    }
    if (e.getButton() == MouseEvent.BUTTON3) {
      final VisualizationViewer<V, E> vv = (VisualizationViewer<V, E>) e.getSource();
      GraphElementAccessor<V, E> pickSupport = vv.getPickSupport();
      Layout<V, E> layout = vv.getGraphLayout();
      Point2D loc = e.getPoint();
      V vertex = pickSupport.getVertex(layout, loc.getX(), loc.getY());
      final TermNode node = (TermNode) vertex;
      if (vertex == null) {
        popup.setVisible(false);
        return;
      } else {
        popup.setVisible(false);
        popup.removeAll();

        int dncsvis = node.getDownCablesetVisibleCount();
        int upcsvis = node.getUpCablesetVisibleCount();
        System.out.println("UpCSVis: " + upcsvis + " DnCSVis " + dncsvis);

        if (upcsvis < node.getInEdges().size()) {
          popup.add(
              new AbstractAction(
                  "Show All In Edges (" + (node.getInEdges().size() - upcsvis) + " edges)") {

                public void actionPerformed(ActionEvent e) {
                  GUI2.getInstance().getGraph().showInEdges(node);
                  vv.repaint();
                  // JungGraphPanel.instance.showUpCableset(node);
                }
              });

          popup.add(
              new AbstractAction("Show In Edges By Relation") {

                public void actionPerformed(ActionEvent e) {
                  CaseframeBasedShowHideDialog cd =
                      new CaseframeBasedShowHideDialog(
                          GUI2.getInstance(),
                          new ArrayList<String>(
                              GUI2.getInstance().getGraph().getInHiddenFSymbols(node)));

                  cd.setHelpText("   Select the relations you        wish to show in the graph.");
                  cd.setVisible(true);

                  for (String fsym : cd.getResult()) {
                    GUI2.getInstance().getGraph().showInEdges(node, fsym);
                  }

                  vv.repaint();
                }
              });

          /*JMenu submenu = new JMenu("Show In Relations");
          final HashMap<Caseframe, ArrayList<Edge>> hm = JungGraphPanel.instance.getHiddenUpCablesetCfs(node);
          for(final Caseframe cf : hm.keySet()){
          submenu.add(new AbstractAction(cf.toString()) {
          public void actionPerformed(ActionEvent e) {
          for(Edge je : hm.get(cf))
          JungGraphPanel.instance.showNode(je.from);
          }
          });
          }
          popup.add(submenu);*/
        }
        if (dncsvis < node.getOutEdges().size()) {
          popup.add(
              new AbstractAction(
                  "Show All Out Edges (" + (node.getOutEdges().size() - dncsvis) + " edges)") {

                public void actionPerformed(ActionEvent e) {
                  GUI2.getInstance().getGraph().showOutEdges(node);
                  vv.repaint();
                }
              });
        }

        if (upcsvis > 0) {
          popup.add(
              new AbstractAction("Hide All In Edges (" + upcsvis + " edges)") {

                public void actionPerformed(ActionEvent e) {
                  GUI2.getInstance().getGraph().hideInEdges(node);
                  vv.repaint();
                }
              });

          popup.add(
              new AbstractAction("Hide In Edges By Relation") {

                public void actionPerformed(ActionEvent e) {
                  CaseframeBasedShowHideDialog cd =
                      new CaseframeBasedShowHideDialog(
                          GUI2.getInstance(),
                          new ArrayList<String>(
                              GUI2.getInstance().getGraph().getInShownFSymbols(node)));

                  cd.setHelpText("   Select the relations you       wish to hide from the graph.");
                  cd.setVisible(true);

                  for (String fsym : cd.getResult()) {
                    GUI2.getInstance().getGraph().hideInEdges(node, fsym);
                  }

                  vv.repaint();
                }
              });
        }
        if (dncsvis > 0 && !node.getTerm().isMolecular()) {
          popup.add(
              new AbstractAction("Hide All Out Edges (" + dncsvis + " edges)") {

                public void actionPerformed(ActionEvent e) {
                  GUI2.getInstance().getGraph().hideOutEdges(node);
                  // JungGraphPanel.instance.hideDownCableset(node);
                  vv.repaint();
                }
              });
        }

        popup.add(
            new AbstractAction("Hide Node") {

              public void actionPerformed(ActionEvent e) {
                GUI2.getInstance().getGraph().hideVertex(node);
                vv.repaint();
                // JungGraphPanel.instance.hideNode(node);
              }
            });
        if (!node.getTerm().isAsserted()) {
          popup.add(
              new AbstractAction("Assert") {

                public void actionPerformed(ActionEvent e) {
                  FnInterop.addToContext(node.getTerm(), Context.getCurrentContext());
                  GUI2.getInstance().getGraphPanel().getVV().repaint();
                }
              });
        }
        if (node.getTerm().isAsserted()) {
          popup.add(
              new AbstractAction("Unassert") {

                public void actionPerformed(ActionEvent e) {
                  FnInterop.unassertTerm(node.getTerm());
                  GUI2.getInstance().getGraphPanel().getVV().repaint();
                }
              });
        }

        popup.show(vv, e.getX(), e.getY());
      }
    }
  }