All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.mallet.grmm.inference.gbp.RegionGraph Maven / Gradle / Ivy

Go to download

MALLET is a Java-based package for statistical natural language processing, document classification, clustering, topic modeling, information extraction, and other machine learning applications to text.

The newest version!
/* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept.
   This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
   http://www.cs.umass.edu/~mccallum/mallet
   This software is provided under the terms of the Common Public License,
   version 1.0, as published by http://www.opensource.org.  For further
   information, see the file `LICENSE' included with this distribution. */
package cc.mallet.grmm.inference.gbp;

import gnu.trove.THashSet;

import java.util.*;

import cc.mallet.grmm.types.Factor;
import cc.mallet.grmm.types.VarSet;
import cc.mallet.grmm.types.Variable;


/**
 * Created: May 27, 2005
 *
 * @author  ");
      buf.append (edge.to);
    }
    buf.append ("\n");
    return buf.toString ();
  }

  public boolean contains (Region region)
  {
    return regions.contains (region);
  }

  /** Returns the region in this graph whose factor list contains only
   *    a given potential.
   * @param ptl
   * @param doCreate If true, an appropriate region will be created and added
   * to graph if none is found.
   * @return A region, or null if no region found and doCreate false.
   */
  public Region findRegion (Factor ptl, boolean doCreate)
  {
    Set allVars = ptl.varSet ();
    for (Iterator it = regions.iterator (); it.hasNext ();) {
      Region region = (Region) it.next ();
      if (region.vars.size() == allVars.size() && region.vars.containsAll (allVars))
        return region;
    }

    if (doCreate) {
      Region region = new Region (ptl);
      addRegion (region);
      return region;
    } else {
      return null;
    }
  }

  /** Returns the region in this graph whose variable list contains only
   *    a given variable.
   * @param var
   * @param doCreate If true, an appropriate region will be created and added
   * to graph if none is found.
   * @return A region, or null if no region found and doCreate false.
   */
  public Region findRegion (Variable var, boolean doCreate)
  {
    for (Iterator it = regions.iterator (); it.hasNext ();) {
      Region region = (Region) it.next ();
      if ((region.vars.size() == 1) && (region.vars.contains (var))) {
        return region;
      }
    }


    if (doCreate) {
      Region region = new Region (var);
      addRegion (region);
      return region;
    } else {
      return null;
    }
  }

  /** Finds the smallest region containing a given variable.
   *   This might return a region that contains many extraneous variables.
   * @param variable
   * @return
   */
  public Region findContainingRegion (Variable variable)
  {
    Region ret = null;
    for (Iterator it = regions.iterator (); it.hasNext ();) {
      Region region = (Region) it.next ();
      if (region.vars.contains (variable)) {
        if (ret == null || region.vars.size() < ret.vars.size ())
          ret = region;
      }
    }
    return ret;
  }

  /** Finds the smallest region containing all the variables in a given set.
   *   This might return a region that contains many extraneous variables.
   * @param varSet
   * @return
   */
  public Region findContainingRegion (VarSet varSet)
  {
    Region ret = null;
    for (Iterator it = regions.iterator (); it.hasNext ();) {
      Region region = (Region) it.next ();
      if (region.vars.containsAll (varSet)) {
        if (ret == null || region.vars.size() < ret.vars.size ())
          ret = region;
      }
    }
    return ret;
  }

  public int numEdges ()
  {
    return edges.size ();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy