You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jo...@apache.org on 2011/01/25 16:09:05 UTC
svn commit: r1063312 -
/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/
Author: joern
Date: Tue Jan 25 15:09:04 2011
New Revision: 1063312
URL: http://svn.apache.org/viewvc?rev=1063312&view=rev
Log:
OPENLP-24 Formated code to comply with conventions
Modified:
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/BinToAscii.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ContextGenerator.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/Counter.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/DataStream.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/DomainToModelMap.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/Evalable.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GIS.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISModel.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISTrainer.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/IntegerPool.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelDomain.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelReplacementManager.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelSetter.java
incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/PlainTextByLineDataStream.java
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/BinToAscii.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/BinToAscii.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/BinToAscii.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/BinToAscii.java Tue Jan 25 15:09:04 2011
@@ -34,22 +34,20 @@ import java.util.zip.GZIPOutputStream;
*/
public class BinToAscii {
- public static void main(String[] args) throws IOException {
- PrintWriter out =
- new PrintWriter(new OutputStreamWriter(
- new GZIPOutputStream(
- new FileOutputStream(args[1]))));
- DataInputStream in =
- new DataInputStream(new GZIPInputStream(
- new FileInputStream(args[0])));
+ public static void main(String[] args) throws IOException {
+ PrintWriter out = new PrintWriter(new OutputStreamWriter(
+ new GZIPOutputStream(new FileOutputStream(args[1]))));
+ DataInputStream in = new DataInputStream(new GZIPInputStream(
+ new FileInputStream(args[0])));
- double d;
- try {
- while(true)
- out.println(in.readDouble());
- } catch (Exception E) {}
- out.close();
- in.close();
- }
+ double d;
+ try {
+ while (true)
+ out.println(in.readDouble());
+ } catch (Exception E) {
+ }
+ out.close();
+ in.close();
+ }
}
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ContextGenerator.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ContextGenerator.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ContextGenerator.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ContextGenerator.java Tue Jan 25 15:09:04 2011
@@ -24,10 +24,9 @@ package opennlp.maxent;
*/
public interface ContextGenerator {
- /**
- * Builds up the list of contextual predicates given an Object.
- */
- public String[] getContext(Object o);
-
-}
+ /**
+ * Builds up the list of contextual predicates given an Object.
+ */
+ public String[] getContext(Object o);
+}
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/Counter.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/Counter.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/Counter.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/Counter.java Tue Jan 25 15:09:04 2011
@@ -24,9 +24,17 @@ package opennlp.maxent;
* incrementation.
*/
public class Counter {
- private int counter = 1;
- public void increment() { counter++; }
- public int intValue() { return counter; }
- public boolean passesCutoff(int c) { return counter >= c; }
+ private int counter = 1;
+ public void increment() {
+ counter++;
+ }
+
+ public int intValue() {
+ return counter;
+ }
+
+ public boolean passesCutoff(int c) {
+ return counter >= c;
+ }
}
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/DataStream.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/DataStream.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/DataStream.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/DataStream.java Tue Jan 25 15:09:04 2011
@@ -27,19 +27,18 @@ package opennlp.maxent;
*/
public interface DataStream {
- /**
- * Returns the next slice of data held in this DataStream.
- *
- * @return the Object representing the data which is next in this
- * DataStream
- */
- public Object nextToken ();
+ /**
+ * Returns the next slice of data held in this DataStream.
+ *
+ * @return the Object representing the data which is next in this DataStream
+ */
+ public Object nextToken();
- /**
- * Test whether there are any Events remaining in this EventStream.
- *
- * @return true if this DataStream has more data tokens
- */
- public boolean hasNext ();
+ /**
+ * Test whether there are any Events remaining in this EventStream.
+ *
+ * @return true if this DataStream has more data tokens
+ */
+ public boolean hasNext();
}
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/DomainToModelMap.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/DomainToModelMap.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/DomainToModelMap.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/DomainToModelMap.java Tue Jan 25 15:09:04 2011
@@ -36,54 +36,54 @@ import opennlp.model.MaxentModel;
*/
public class DomainToModelMap {
- // the underlying object which stores the mapping
- private Map map = Collections.synchronizedMap(new HashMap());
+ // the underlying object which stores the mapping
+ private Map map = Collections.synchronizedMap(new HashMap());
- /**
- * Sets the model for the given domain.
- *
- * @param domain The ModelDomain object which keys to the model.
- * @param model The MaxentModel trained for the domain.
- */
- public void setModelForDomain (ModelDomain domain, MaxentModel model) {
- map.put(domain, model);
+ /**
+ * Sets the model for the given domain.
+ *
+ * @param domain
+ * The ModelDomain object which keys to the model.
+ * @param model
+ * The MaxentModel trained for the domain.
+ */
+ public void setModelForDomain(ModelDomain domain, MaxentModel model) {
+ map.put(domain, model);
+ }
+
+ /**
+ * Get the model mapped to by the given ModelDomain key.
+ *
+ * @param domain
+ * The ModelDomain object which keys to the desired model.
+ * @return The MaxentModel corresponding to the given domain.
+ */
+ public MaxentModel getModel(ModelDomain domain) {
+ if (map.containsKey(domain)) {
+ return (MaxentModel) map.get(domain);
+ } else {
+ throw new NoSuchElementException("No model has been created for "
+ + "domain: " + domain);
}
+ }
-
- /**
- * Get the model mapped to by the given ModelDomain key.
- *
- * @param domain The ModelDomain object which keys to the desired model.
- * @return The MaxentModel corresponding to the given domain.
- */
- public MaxentModel getModel (ModelDomain domain) {
- if (map.containsKey(domain)) {
- return (MaxentModel)map.get(domain);
- } else {
- throw new NoSuchElementException("No model has been created for "+
- "domain: " + domain);
- }
- }
-
-
- /**
- * Removes the mapping for this ModelDomain key from this map if present.
- *
- * @param domain The ModelDomain key whose mapping is to be removed from
- * the map.
- */
- public void removeDomain (ModelDomain domain) {
- map.remove(domain);
- }
-
-
- /**
- * A set view of the ModelDomain keys contained in this map.
- *
- * @return a set view of the ModelDomain keys contained in this map
- */
- public Set keySet () {
- return map.keySet();
- }
+ /**
+ * Removes the mapping for this ModelDomain key from this map if present.
+ *
+ * @param domain
+ * The ModelDomain key whose mapping is to be removed from the map.
+ */
+ public void removeDomain(ModelDomain domain) {
+ map.remove(domain);
+ }
+
+ /**
+ * A set view of the ModelDomain keys contained in this map.
+ *
+ * @return a set view of the ModelDomain keys contained in this map
+ */
+ public Set keySet() {
+ return map.keySet();
+ }
}
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/Evalable.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/Evalable.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/Evalable.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/Evalable.java Tue Jan 25 15:09:04 2011
@@ -30,38 +30,41 @@ import opennlp.model.MaxentModel;
*/
public interface Evalable {
- /**
- * The outcome that should be considered a negative result. This is used
- * for computing recall. In the case of binary decisions, this would be
- * the false one.
- *
- * @return the events that this EventCollector has gathered
- */
- public String getNegativeOutcome();
+ /**
+ * The outcome that should be considered a negative result. This is used for
+ * computing recall. In the case of binary decisions, this would be the false
+ * one.
+ *
+ * @return the events that this EventCollector has gathered
+ */
+ public String getNegativeOutcome();
- /**
- * Returns the EventCollector that is used to collect all relevant
- * information from the data file. This is used for to test the
- * predictions of the model. Note that if some of your features are the
- * oucomes of previous events, this method will give you results assuming
- * 100% performance on the previous events. If you don't like this, use
- * the localEval method.
- *
- * @param r A reader containing the data for the event collector
- * @return an EventCollector
- */
- public EventCollector getEventCollector(Reader r);
-
- /**
- * If the -l option is selected for evaluation, this method will be
- * called rather than TrainEval's evaluation method. This is good if
- * your features includes the outcomes of previous events.
- *
- * @param model the maxent model to evaluate
- * @param r Reader containing the data to process
- * @param e The original Evalable. Probably not relevant.
- * @param verbose a request to print more specific processing information
- */
- public void localEval(MaxentModel model, Reader r,
- Evalable e, boolean verbose);
+ /**
+ * Returns the EventCollector that is used to collect all relevant information
+ * from the data file. This is used for to test the predictions of the model.
+ * Note that if some of your features are the oucomes of previous events, this
+ * method will give you results assuming 100% performance on the previous
+ * events. If you don't like this, use the localEval method.
+ *
+ * @param r
+ * A reader containing the data for the event collector
+ * @return an EventCollector
+ */
+ public EventCollector getEventCollector(Reader r);
+
+ /**
+ * If the -l option is selected for evaluation, this method will be called
+ * rather than TrainEval's evaluation method. This is good if your features
+ * includes the outcomes of previous events.
+ *
+ * @param model
+ * the maxent model to evaluate
+ * @param r
+ * Reader containing the data to process
+ * @param e
+ * The original Evalable. Probably not relevant.
+ * @param verbose
+ * a request to print more specific processing information
+ */
+ public void localEval(MaxentModel model, Reader r, Evalable e, boolean verbose);
}
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GIS.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GIS.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GIS.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GIS.java Tue Jan 25 15:09:04 2011
@@ -30,166 +30,204 @@ import opennlp.model.Prior;
* GISModels.
*/
public class GIS {
- /**
- * Set this to false if you don't want messages about the progress of
- * model training displayed. Alternately, you can use the overloaded
- * version of trainModel() to conditionally enable progress messages.
- */
- public static boolean PRINT_MESSAGES = true;
-
- /** If we are using smoothing, this is used as the "number" of
- * times we want the trainer to imagine that it saw a feature that it
- * actually didn't see. Defaulted to 0.1.
- */
- public static double SMOOTHING_OBSERVATION = 0.1;
-
- /**
- * Train a model using the GIS algorithm, assuming 100 iterations and no
- * cutoff.
- *
- * @param eventStream The EventStream holding the data on which this model
- * will be trained.
- * @return The newly trained model, which can be used immediately or saved
- * to disk using an opennlp.maxent.io.GISModelWriter object.
- */
- public static GISModel trainModel(EventStream eventStream) throws IOException {
- return trainModel(eventStream, 100, 0, false, PRINT_MESSAGES);
- }
-
- /**
- * Train a model using the GIS algorithm, assuming 100 iterations and no
- * cutoff.
- *
- * @param eventStream The EventStream holding the data on which this model
- * will be trained.
- * @param smoothing Defines whether the created trainer will use smoothing
- * while training the model.
- * @return The newly trained model, which can be used immediately or saved
- * to disk using an opennlp.maxent.io.GISModelWriter object.
- */
- public static GISModel trainModel(EventStream eventStream, boolean smoothing) throws IOException {
- return trainModel(eventStream, 100, 0, smoothing,PRINT_MESSAGES);
- }
-
- /**
- * Train a model using the GIS algorithm.
- *
- * @param eventStream The EventStream holding the data on which this model
- * will be trained.
- * @param iterations The number of GIS iterations to perform.
- * @param cutoff The number of times a feature must be seen in order
- * to be relevant for training.
- * @return The newly trained model, which can be used immediately or saved
- * to disk using an opennlp.maxent.io.GISModelWriter object.
- */
- public static GISModel trainModel(EventStream eventStream,
- int iterations,
- int cutoff) throws IOException {
- return trainModel(eventStream, iterations, cutoff, false,PRINT_MESSAGES);
- }
-
- /**
- * Train a model using the GIS algorithm.
- * @param eventStream The EventStream holding the data on which this model
- * will be trained.
- * @param iterations The number of GIS iterations to perform.
- * @param cutoff The number of times a feature must be seen in order
- * to be relevant for training.
- * @param smoothing Defines whether the created trainer will use smoothing
- * while training the model.
- * @param printMessagesWhileTraining Determines whether training status messages are written to STDOUT.
- * @return The newly trained model, which can be used immediately or saved
- * to disk using an opennlp.maxent.io.GISModelWriter object.
- */
- public static GISModel trainModel(EventStream eventStream,
- int iterations,
- int cutoff,
- boolean smoothing,boolean printMessagesWhileTraining) throws IOException {
- GISTrainer trainer = new GISTrainer(printMessagesWhileTraining);
- trainer.setSmoothing(smoothing);
- trainer.setSmoothingObservation(SMOOTHING_OBSERVATION);
- return trainer.trainModel(eventStream, iterations, cutoff);
- }
-
- /**
- * Train a model using the GIS algorithm.
- * @param eventStream The EventStream holding the data on which this model
- * will be trained.
- * @param iterations The number of GIS iterations to perform.
- * @param cutoff The number of times a feature must be seen in order
- * to be relevant for training.
- * @param sigma The standard deviation for the gaussian smoother.
- * @return The newly trained model, which can be used immediately or saved
- * to disk using an opennlp.maxent.io.GISModelWriter object.
- */
- public static GISModel trainModel(EventStream eventStream,
- int iterations,
- int cutoff,
- double sigma) throws IOException {
- GISTrainer trainer = new GISTrainer(PRINT_MESSAGES);
- if (sigma > 0)
- trainer.setGaussianSigma(sigma);
- return trainer.trainModel(eventStream, iterations, cutoff);
- }
+ /**
+ * Set this to false if you don't want messages about the progress of model
+ * training displayed. Alternately, you can use the overloaded version of
+ * trainModel() to conditionally enable progress messages.
+ */
+ public static boolean PRINT_MESSAGES = true;
+
+ /**
+ * If we are using smoothing, this is used as the "number" of times we want
+ * the trainer to imagine that it saw a feature that it actually didn't see.
+ * Defaulted to 0.1.
+ */
+ public static double SMOOTHING_OBSERVATION = 0.1;
+
+ /**
+ * Train a model using the GIS algorithm, assuming 100 iterations and no
+ * cutoff.
+ *
+ * @param eventStream
+ * The EventStream holding the data on which this model will be
+ * trained.
+ * @return The newly trained model, which can be used immediately or saved to
+ * disk using an opennlp.maxent.io.GISModelWriter object.
+ */
+ public static GISModel trainModel(EventStream eventStream) throws IOException {
+ return trainModel(eventStream, 100, 0, false, PRINT_MESSAGES);
+ }
- /**
- * Train a model using the GIS algorithm.
- * @param iterations The number of GIS iterations to perform.
- * @param indexer The object which will be used for event compilation.
- * @param smoothing Defines whether the created trainer will use smoothing while training the model.
- * @return The newly trained model, which can be used immediately or saved
- * to disk using an opennlp.maxent.io.GISModelWriter object.
- */
- public static GISModel trainModel(int iterations, DataIndexer indexer, boolean smoothing) {
- return trainModel(iterations,indexer,true,smoothing,null,0);
- }
-
- /**
- * Train a model using the GIS algorithm.
- * @param iterations The number of GIS iterations to perform.
- * @param indexer The object which will be used for event compilation.
- * @return The newly trained model, which can be used immediately or saved
- * to disk using an opennlp.maxent.io.GISModelWriter object.
- */
- public static GISModel trainModel(int iterations, DataIndexer indexer) {
- return trainModel(iterations,indexer,true,false,null,0);
- }
-
- /**
- * Train a model using the GIS algorithm with the specified number of iterations, data indexer, and prior.
- * @param iterations The number of GIS iterations to perform.
- * @param indexer The object which will be used for event compilation.
- * @param modelPrior The prior distribution for the model.
- * @return The newly trained model, which can be used immediately or saved
- * to disk using an opennlp.maxent.io.GISModelWriter object.
- */
- public static GISModel trainModel(int iterations, DataIndexer indexer, Prior modelPrior, int cutoff) {
- return trainModel(iterations,indexer,true,false,modelPrior,cutoff);
- }
+ /**
+ * Train a model using the GIS algorithm, assuming 100 iterations and no
+ * cutoff.
+ *
+ * @param eventStream
+ * The EventStream holding the data on which this model will be
+ * trained.
+ * @param smoothing
+ * Defines whether the created trainer will use smoothing while
+ * training the model.
+ * @return The newly trained model, which can be used immediately or saved to
+ * disk using an opennlp.maxent.io.GISModelWriter object.
+ */
+ public static GISModel trainModel(EventStream eventStream, boolean smoothing)
+ throws IOException {
+ return trainModel(eventStream, 100, 0, smoothing, PRINT_MESSAGES);
+ }
-
- /**
- * Train a model using the GIS algorithm.
- * @param iterations The number of GIS iterations to perform.
- * @param indexer The object which will be used for event compilation.
- * @param printMessagesWhileTraining Determines whether training status messages are written to STDOUT.
- * @param smoothing Defines whether the created trainer will use smoothing while training the model.
- * @param modelPrior The prior distribution for the model.
- * @param cutoff The number of times a predicate must occur to be used in a model.
- * @return The newly trained model, which can be used immediately or saved
- * to disk using an opennlp.maxent.io.GISModelWriter object.
- */
- public static GISModel trainModel(int iterations, DataIndexer indexer, boolean printMessagesWhileTraining, boolean smoothing, Prior modelPrior, int cutoff) {
- GISTrainer trainer = new GISTrainer(printMessagesWhileTraining);
- trainer.setSmoothing(smoothing);
- trainer.setSmoothingObservation(SMOOTHING_OBSERVATION);
- if (modelPrior != null) {
- return trainer.trainModel(iterations, indexer, modelPrior,cutoff);
- }
- else {
- return trainer.trainModel(iterations, indexer,cutoff);
- }
- }
+ /**
+ * Train a model using the GIS algorithm.
+ *
+ * @param eventStream
+ * The EventStream holding the data on which this model will be
+ * trained.
+ * @param iterations
+ * The number of GIS iterations to perform.
+ * @param cutoff
+ * The number of times a feature must be seen in order to be relevant
+ * for training.
+ * @return The newly trained model, which can be used immediately or saved to
+ * disk using an opennlp.maxent.io.GISModelWriter object.
+ */
+ public static GISModel trainModel(EventStream eventStream, int iterations,
+ int cutoff) throws IOException {
+ return trainModel(eventStream, iterations, cutoff, false, PRINT_MESSAGES);
+ }
+
+ /**
+ * Train a model using the GIS algorithm.
+ *
+ * @param eventStream
+ * The EventStream holding the data on which this model will be
+ * trained.
+ * @param iterations
+ * The number of GIS iterations to perform.
+ * @param cutoff
+ * The number of times a feature must be seen in order to be relevant
+ * for training.
+ * @param smoothing
+ * Defines whether the created trainer will use smoothing while
+ * training the model.
+ * @param printMessagesWhileTraining
+ * Determines whether training status messages are written to STDOUT.
+ * @return The newly trained model, which can be used immediately or saved to
+ * disk using an opennlp.maxent.io.GISModelWriter object.
+ */
+ public static GISModel trainModel(EventStream eventStream, int iterations,
+ int cutoff, boolean smoothing, boolean printMessagesWhileTraining)
+ throws IOException {
+ GISTrainer trainer = new GISTrainer(printMessagesWhileTraining);
+ trainer.setSmoothing(smoothing);
+ trainer.setSmoothingObservation(SMOOTHING_OBSERVATION);
+ return trainer.trainModel(eventStream, iterations, cutoff);
+ }
+
+ /**
+ * Train a model using the GIS algorithm.
+ *
+ * @param eventStream
+ * The EventStream holding the data on which this model will be
+ * trained.
+ * @param iterations
+ * The number of GIS iterations to perform.
+ * @param cutoff
+ * The number of times a feature must be seen in order to be relevant
+ * for training.
+ * @param sigma
+ * The standard deviation for the gaussian smoother.
+ * @return The newly trained model, which can be used immediately or saved to
+ * disk using an opennlp.maxent.io.GISModelWriter object.
+ */
+ public static GISModel trainModel(EventStream eventStream, int iterations,
+ int cutoff, double sigma) throws IOException {
+ GISTrainer trainer = new GISTrainer(PRINT_MESSAGES);
+ if (sigma > 0)
+ trainer.setGaussianSigma(sigma);
+ return trainer.trainModel(eventStream, iterations, cutoff);
+ }
+
+ /**
+ * Train a model using the GIS algorithm.
+ *
+ * @param iterations
+ * The number of GIS iterations to perform.
+ * @param indexer
+ * The object which will be used for event compilation.
+ * @param smoothing
+ * Defines whether the created trainer will use smoothing while
+ * training the model.
+ * @return The newly trained model, which can be used immediately or saved to
+ * disk using an opennlp.maxent.io.GISModelWriter object.
+ */
+ public static GISModel trainModel(int iterations, DataIndexer indexer,
+ boolean smoothing) {
+ return trainModel(iterations, indexer, true, smoothing, null, 0);
+ }
+
+ /**
+ * Train a model using the GIS algorithm.
+ *
+ * @param iterations
+ * The number of GIS iterations to perform.
+ * @param indexer
+ * The object which will be used for event compilation.
+ * @return The newly trained model, which can be used immediately or saved to
+ * disk using an opennlp.maxent.io.GISModelWriter object.
+ */
+ public static GISModel trainModel(int iterations, DataIndexer indexer) {
+ return trainModel(iterations, indexer, true, false, null, 0);
+ }
+
+ /**
+ * Train a model using the GIS algorithm with the specified number of
+ * iterations, data indexer, and prior.
+ *
+ * @param iterations
+ * The number of GIS iterations to perform.
+ * @param indexer
+ * The object which will be used for event compilation.
+ * @param modelPrior
+ * The prior distribution for the model.
+ * @return The newly trained model, which can be used immediately or saved to
+ * disk using an opennlp.maxent.io.GISModelWriter object.
+ */
+ public static GISModel trainModel(int iterations, DataIndexer indexer,
+ Prior modelPrior, int cutoff) {
+ return trainModel(iterations, indexer, true, false, modelPrior, cutoff);
+ }
+
+ /**
+ * Train a model using the GIS algorithm.
+ *
+ * @param iterations
+ * The number of GIS iterations to perform.
+ * @param indexer
+ * The object which will be used for event compilation.
+ * @param printMessagesWhileTraining
+ * Determines whether training status messages are written to STDOUT.
+ * @param smoothing
+ * Defines whether the created trainer will use smoothing while
+ * training the model.
+ * @param modelPrior
+ * The prior distribution for the model.
+ * @param cutoff
+ * The number of times a predicate must occur to be used in a model.
+ * @return The newly trained model, which can be used immediately or saved to
+ * disk using an opennlp.maxent.io.GISModelWriter object.
+ */
+ public static GISModel trainModel(int iterations, DataIndexer indexer,
+ boolean printMessagesWhileTraining, boolean smoothing, Prior modelPrior,
+ int cutoff) {
+ GISTrainer trainer = new GISTrainer(printMessagesWhileTraining);
+ trainer.setSmoothing(smoothing);
+ trainer.setSmoothingObservation(SMOOTHING_OBSERVATION);
+ if (modelPrior != null) {
+ return trainer.trainModel(iterations, indexer, modelPrior, cutoff);
+ } else {
+ return trainer.trainModel(iterations, indexer, cutoff);
+ }
+ }
}
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISModel.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISModel.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISModel.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISModel.java Tue Jan 25 15:09:04 2011
@@ -35,169 +35,201 @@ import opennlp.model.UniformPrior;
* Iterative Scaling procedure (implemented in GIS.java).
*/
public final class GISModel extends AbstractModel {
- /**
- * Creates a new model with the specified parameters, outcome names, and predicate/feature labels.
- * @param params The parameters of the model.
- * @param predLabels The names of the predicates used in this model.
- * @param outcomeNames The names of the outcomes this model predicts.
- * @param correctionConstant The maximum number of active features which occur in an event.
- * @param correctionParam The parameter associated with the correction feature.
- */
- public GISModel (Context[] params, String[] predLabels, String[] outcomeNames, int correctionConstant, double correctionParam) {
- this(params,predLabels,outcomeNames,correctionConstant,correctionParam, new UniformPrior());
- }
-
- /**
- * Creates a new model with the specified parameters, outcome names, and predicate/feature labels.
- * @param params The parameters of the model.
- * @param predLabels The names of the predicates used in this model.
- * @param outcomeNames The names of the outcomes this model predicts.
- * @param correctionConstant The maximum number of active features which occur in an event.
- * @param correctionParam The parameter associated with the correction feature.
- * @param prior The prior to be used with this model.
- */
- public GISModel (Context[] params, String[] predLabels, String[] outcomeNames, int correctionConstant,double correctionParam, Prior prior) {
- super(params,predLabels,outcomeNames,correctionConstant,correctionParam);
- this.prior = prior;
- prior.setLabels(outcomeNames, predLabels);
- modelType = ModelType.Maxent;
- }
-
- /**
- * Use this model to evaluate a context and return an array of the
- * likelihood of each outcome given that context.
- *
- * @param context The names of the predicates which have been observed at
- * the present decision point.
- * @return The normalized probabilities for the outcomes given the
- * context. The indexes of the double[] are the outcome
- * ids, and the actual string representation of the
- * outcomes can be obtained from the method
- * getOutcome(int i).
- */
- public final double[] eval(String[] context) {
- return(eval(context,new double[evalParams.getNumOutcomes()]));
- }
-
- public final double[] eval(String[] context, float[] values) {
- return(eval(context,values,new double[evalParams.getNumOutcomes()]));
- }
-
- public final double[] eval(String[] context, double[] outsums) {
- return eval(context,null,outsums);
- }
-
- /**
- * Use this model to evaluate a context and return an array of the
- * likelihood of each outcome given that context.
- *
- * @param context The names of the predicates which have been observed at
- * the present decision point.
- * @param outsums This is where the distribution is stored.
- * @return The normalized probabilities for the outcomes given the
- * context. The indexes of the double[] are the outcome
- * ids, and the actual string representation of the
- * outcomes can be obtained from the method
- * getOutcome(int i).
- */
- public final double[] eval(String[] context, float[] values, double[] outsums) {
- int[] scontexts = new int[context.length];
- for (int i=0; i<context.length; i++) {
- Integer ci = pmap.get(context[i]);
- scontexts[i] = ci == null ? -1 : ci;
- }
- prior.logPrior(outsums, scontexts,values);
- return GISModel.eval(scontexts,values,outsums,evalParams);
- }
-
-
- /**
- * Use this model to evaluate a context and return an array of the
- * likelihood of each outcome given the specified context and the specified parameters.
- * @param context The integer values of the predicates which have been observed at
- * the present decision point.
- * @param prior The prior distribution for the specified context.
- * @param model The set of parametes used in this computation.
- * @return The normalized probabilities for the outcomes given the
- * context. The indexes of the double[] are the outcome
- * ids, and the actual string representation of the
- * outcomes can be obtained from the method
- * getOutcome(int i).
- */
- public static double[] eval(int[] context, double[] prior, EvalParameters model) {
- return eval(context,null,prior,model);
- }
-
- /**
- * Use this model to evaluate a context and return an array of the
- * likelihood of each outcome given the specified context and the specified parameters.
- * @param context The integer values of the predicates which have been observed at
- * the present decision point.
- * @param values The values for each of the parameters.
- * @param prior The prior distribution for the specified context.
- * @param model The set of parametes used in this computation.
- * @return The normalized probabilities for the outcomes given the
- * context. The indexes of the double[] are the outcome
- * ids, and the actual string representation of the
- * outcomes can be obtained from the method
- * getOutcome(int i).
- */
- public static double[] eval(int[] context, float[] values, double[] prior, EvalParameters model) {
- Context[] params = model.getParams();
- int numfeats[] = new int[model.getNumOutcomes()];
- int[] activeOutcomes;
- double[] activeParameters;
- double value = 1;
- for (int ci = 0; ci < context.length; ci++) {
- if (context[ci] >= 0) {
- Context predParams = params[context[ci]];
- activeOutcomes = predParams.getOutcomes();
- activeParameters = predParams.getParameters();
- if (values != null) {
- value = values[ci];
- }
- for (int ai = 0; ai < activeOutcomes.length; ai++) {
- int oid = activeOutcomes[ai];
- numfeats[oid]++;
- prior[oid] += activeParameters[ai] * value;
- }
- }
- }
-
- double normal = 0.0;
- for (int oid = 0; oid < model.getNumOutcomes(); oid++) {
- if (model.getCorrectionParam() != 0) {
- prior[oid] = Math.exp(prior[oid]*model.getConstantInverse()+((1.0 - ((double) numfeats[oid] / model.getCorrectionConstant())) * model.getCorrectionParam()));
+
+ /**
+ * Creates a new model with the specified parameters, outcome names, and
+ * predicate/feature labels.
+ *
+ * @param params
+ * The parameters of the model.
+ * @param predLabels
+ * The names of the predicates used in this model.
+ * @param outcomeNames
+ * The names of the outcomes this model predicts.
+ * @param correctionConstant
+ * The maximum number of active features which occur in an event.
+ * @param correctionParam
+ * The parameter associated with the correction feature.
+ */
+ public GISModel(Context[] params, String[] predLabels, String[] outcomeNames,
+ int correctionConstant, double correctionParam) {
+ this(params, predLabels, outcomeNames, correctionConstant, correctionParam,
+ new UniformPrior());
+ }
+
+ /**
+ * Creates a new model with the specified parameters, outcome names, and
+ * predicate/feature labels.
+ *
+ * @param params
+ * The parameters of the model.
+ * @param predLabels
+ * The names of the predicates used in this model.
+ * @param outcomeNames
+ * The names of the outcomes this model predicts.
+ * @param correctionConstant
+ * The maximum number of active features which occur in an event.
+ * @param correctionParam
+ * The parameter associated with the correction feature.
+ * @param prior
+ * The prior to be used with this model.
+ */
+ public GISModel(Context[] params, String[] predLabels, String[] outcomeNames,
+ int correctionConstant, double correctionParam, Prior prior) {
+ super(params, predLabels, outcomeNames, correctionConstant, correctionParam);
+ this.prior = prior;
+ prior.setLabels(outcomeNames, predLabels);
+ modelType = ModelType.Maxent;
+ }
+
+ /**
+ * Use this model to evaluate a context and return an array of the likelihood
+ * of each outcome given that context.
+ *
+ * @param context
+ * The names of the predicates which have been observed at the
+ * present decision point.
+ * @return The normalized probabilities for the outcomes given the context.
+ * The indexes of the double[] are the outcome ids, and the actual
+ * string representation of the outcomes can be obtained from the
+ * method getOutcome(int i).
+ */
+ public final double[] eval(String[] context) {
+ return (eval(context, new double[evalParams.getNumOutcomes()]));
+ }
+
+ public final double[] eval(String[] context, float[] values) {
+ return (eval(context, values, new double[evalParams.getNumOutcomes()]));
+ }
+
+ public final double[] eval(String[] context, double[] outsums) {
+ return eval(context, null, outsums);
+ }
+
+ /**
+ * Use this model to evaluate a context and return an array of the likelihood
+ * of each outcome given that context.
+ *
+ * @param context
+ * The names of the predicates which have been observed at the
+ * present decision point.
+ * @param outsums
+ * This is where the distribution is stored.
+ * @return The normalized probabilities for the outcomes given the context.
+ * The indexes of the double[] are the outcome ids, and the actual
+ * string representation of the outcomes can be obtained from the
+ * method getOutcome(int i).
+ */
+ public final double[] eval(String[] context, float[] values, double[] outsums) {
+ int[] scontexts = new int[context.length];
+ for (int i = 0; i < context.length; i++) {
+ Integer ci = pmap.get(context[i]);
+ scontexts[i] = ci == null ? -1 : ci;
+ }
+ prior.logPrior(outsums, scontexts, values);
+ return GISModel.eval(scontexts, values, outsums, evalParams);
+ }
+
+
+ /**
+ * Use this model to evaluate a context and return an array of the likelihood
+ * of each outcome given the specified context and the specified parameters.
+ *
+ * @param context
+ * The integer values of the predicates which have been observed at
+ * the present decision point.
+ * @param prior
+ * The prior distribution for the specified context.
+ * @param model
+ * The set of parametes used in this computation.
+ * @return The normalized probabilities for the outcomes given the context.
+ * The indexes of the double[] are the outcome ids, and the actual
+ * string representation of the outcomes can be obtained from the
+ * method getOutcome(int i).
+ */
+ public static double[] eval(int[] context, double[] prior,
+ EvalParameters model) {
+ return eval(context, null, prior, model);
+ }
+
+ /**
+ * Use this model to evaluate a context and return an array of the likelihood
+ * of each outcome given the specified context and the specified parameters.
+ *
+ * @param context
+ * The integer values of the predicates which have been observed at
+ * the present decision point.
+ * @param values
+ * The values for each of the parameters.
+ * @param prior
+ * The prior distribution for the specified context.
+ * @param model
+ * The set of parametes used in this computation.
+ * @return The normalized probabilities for the outcomes given the context.
+ * The indexes of the double[] are the outcome ids, and the actual
+ * string representation of the outcomes can be obtained from the
+ * method getOutcome(int i).
+ */
+ public static double[] eval(int[] context, float[] values, double[] prior,
+ EvalParameters model) {
+ Context[] params = model.getParams();
+ int numfeats[] = new int[model.getNumOutcomes()];
+ int[] activeOutcomes;
+ double[] activeParameters;
+ double value = 1;
+ for (int ci = 0; ci < context.length; ci++) {
+ if (context[ci] >= 0) {
+ Context predParams = params[context[ci]];
+ activeOutcomes = predParams.getOutcomes();
+ activeParameters = predParams.getParameters();
+ if (values != null) {
+ value = values[ci];
}
- else {
- prior[oid] = Math.exp(prior[oid]*model.getConstantInverse());
+ for (int ai = 0; ai < activeOutcomes.length; ai++) {
+ int oid = activeOutcomes[ai];
+ numfeats[oid]++;
+ prior[oid] += activeParameters[ai] * value;
}
- normal += prior[oid];
}
+ }
- for (int oid = 0; oid < model.getNumOutcomes(); oid++) {
- prior[oid] /= normal;
+ double normal = 0.0;
+ for (int oid = 0; oid < model.getNumOutcomes(); oid++) {
+ if (model.getCorrectionParam() != 0) {
+ prior[oid] = Math
+ .exp(prior[oid]
+ * model.getConstantInverse()
+ + ((1.0 - ((double) numfeats[oid] / model
+ .getCorrectionConstant())) * model.getCorrectionParam()));
+ } else {
+ prior[oid] = Math.exp(prior[oid] * model.getConstantInverse());
}
- return prior;
+ normal += prior[oid];
}
-
-
- public static void main(String[] args) throws java.io.IOException {
- if (args.length == 0) {
- System.err.println("Usage: GISModel modelname < contexts");
- System.exit(1);
- }
- AbstractModel m = new opennlp.maxent.io.SuffixSensitiveGISModelReader(new File(args[0])).getModel();
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- DecimalFormat df = new java.text.DecimalFormat(".###");
- for (String line = in.readLine(); line != null; line = in.readLine()) {
- String[] context = line.split(" ");
- double[] dist = m.eval(context);
- for (int oi=0;oi<dist.length;oi++) {
- System.out.print("["+m.getOutcome(oi)+" "+df.format(dist[oi])+"] ");
- }
- System.out.println();
+ for (int oid = 0; oid < model.getNumOutcomes(); oid++) {
+ prior[oid] /= normal;
+ }
+ return prior;
+ }
+
+ public static void main(String[] args) throws java.io.IOException {
+ if (args.length == 0) {
+ System.err.println("Usage: GISModel modelname < contexts");
+ System.exit(1);
+ }
+ AbstractModel m = new opennlp.maxent.io.SuffixSensitiveGISModelReader(
+ new File(args[0])).getModel();
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ DecimalFormat df = new java.text.DecimalFormat(".###");
+ for (String line = in.readLine(); line != null; line = in.readLine()) {
+ String[] context = line.split(" ");
+ double[] dist = m.eval(context);
+ for (int oi = 0; oi < dist.length; oi++) {
+ System.out.print("[" + m.getOutcome(oi) + " " + df.format(dist[oi])
+ + "] ");
}
+ System.out.println();
}
+ }
}
\ No newline at end of file
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISTrainer.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISTrainer.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISTrainer.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISTrainer.java Tue Jan 25 15:09:04 2011
@@ -53,14 +53,18 @@ class GISTrainer {
* Specifies whether unseen context/outcome pairs should be estimated as occur very infrequently.
*/
private boolean useSimpleSmoothing = false;
+
/**
* Specifies whether a slack parameter should be used in the model.
*/
private boolean useSlackParameter = false;
- /** Specified whether parameter updates should prefer a distribution of parameters which
+
+ /**
+ * Specified whether parameter updates should prefer a distribution of parameters which
* is gaussian.
*/
private boolean useGaussianSmoothing = false;
+
private double sigma = 2.0;
// If we are using smoothing, this is used as the "number" of
@@ -70,72 +74,109 @@ class GISTrainer {
private boolean printMessages = false;
- /** Number of unique events which occured in the event set. */
- private int numUniqueEvents;
- /** Number of predicates. */
- private int numPreds;
- /** Number of outcomes. */
- private int numOutcomes;
+ /**
+ * Number of unique events which occured in the event set.
+ */
+ private int numUniqueEvents;
+
+ /**
+ * Number of predicates.
+ */
+ private int numPreds;
+
+ /**
+ * Number of outcomes.
+ */
+ private int numOutcomes;
- /** Records the array of predicates seen in each event. */
+ /**
+ * Records the array of predicates seen in each event.
+ */
private int[][] contexts;
- /** The value associated with each context. If null then context values are assumes to be 1. */
+ /**
+ * The value associated with each context. If null then context values are assumes to be 1.
+ */
private float[][] values;
- /** List of outcomes for each event i, in context[i]. */
+ /**
+ * List of outcomes for each event i, in context[i].
+ */
private int[] outcomeList;
- /** Records the num of times an event has been seen for each event i, in context[i]. */
+ /**
+ * Records the num of times an event has been seen for each event i, in context[i].
+ */
private int[] numTimesEventsSeen;
- /** The number of times a predicate occured in the training data. */
+ /**
+ * The number of times a predicate occured in the training data.
+ */
private int[] predicateCounts;
private int cutoff;
- /** Stores the String names of the outcomes. The GIS only tracks outcomes
- as ints, and so this array is needed to save the model to disk and
- thereby allow users to know what the outcome was in human
- understandable terms. */
+ /**
+ * Stores the String names of the outcomes. The GIS only tracks outcomes as
+ * ints, and so this array is needed to save the model to disk and thereby
+ * allow users to know what the outcome was in human understandable terms.
+ */
private String[] outcomeLabels;
- /** Stores the String names of the predicates. The GIS only tracks
- predicates as ints, and so this array is needed to save the model to
- disk and thereby allow users to know what the outcome was in human
- understandable terms. */
+ /**
+ * Stores the String names of the predicates. The GIS only tracks predicates
+ * as ints, and so this array is needed to save the model to disk and thereby
+ * allow users to know what the outcome was in human understandable terms.
+ */
private String[] predLabels;
- /** Stores the observed expected values of the features based on training data. */
+ /**
+ * Stores the observed expected values of the features based on training data.
+ */
private MutableContext[] observedExpects;
- /** Stores the estimated parameter value of each predicate during iteration */
+ /**
+ * Stores the estimated parameter value of each predicate during iteration
+ */
private MutableContext[] params;
- /** Stores the expected values of the features based on the current models */
+ /**
+ * Stores the expected values of the features based on the current models
+ */
private MutableContext[] modelExpects;
-
- /** This is the prior distribution that the model uses for training. */
- private Prior prior;
+ /**
+ * This is the prior distribution that the model uses for training.
+ */
+ private Prior prior;
- /** Observed expectation of correction feature. */
+ /**
+ * Observed expectation of correction feature.
+ */
private double cfObservedExpect;
- /** A global variable for the models expected value of the correction feature. */
+
+ /**
+ * A global variable for the models expected value of the correction feature.
+ */
private double CFMOD;
private final double NEAR_ZERO = 0.01;
private final double LLThreshold = 0.0001;
- /** Stores the number of features that get fired per event. */
+ /**
+ * Stores the number of features that get fired per event.
+ */
int[] numfeats;
- /** Initial probability for all outcomes. */
+ /**
+ * Initial probability for all outcomes.
+ */
EvalParameters evalParams;
+
/**
- * Creates a new <code>GISTrainer</code> instance which does
- * not print progress messages about training to STDOUT.
- *
+ * Creates a new <code>GISTrainer</code> instance which does not print
+ * progress messages about training to STDOUT.
+ *
*/
GISTrainer() {
super();
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/IntegerPool.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/IntegerPool.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/IntegerPool.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/IntegerPool.java Tue Jan 25 15:09:04 2011
@@ -25,35 +25,36 @@ package opennlp.maxent;
* non-sparse range. Use this class for operations in which a large
* number of Integer wrapper objects will be created.
*/
-
public class IntegerPool {
private Integer[] _table;
-
- /**
- * Creates an IntegerPool with 0..size Integer objects.
- *
- * @param size the size of the pool.
- */
- public IntegerPool (int size) {
- _table = new Integer[size];
- for (int i = 0; i < size; i++) {
- _table[i] = new Integer(i);
- } // end of for (int i = 0; i < size; i++)
- }
- /**
- * Returns the shared Integer wrapper for <tt>value</tt> if it is
- * inside the range managed by this pool. if <tt>value</tt> is
- * outside the range, a new Integer instance is returned.
- *
- * @param value an <code>int</code> value
- * @return an <code>Integer</code> value
- */
- public Integer get(int value) {
- if (value < _table.length && value >= 0) {
- return _table[value];
- } else {
- return new Integer(value);
- }
+ /**
+ * Creates an IntegerPool with 0..size Integer objects.
+ *
+ * @param size
+ * the size of the pool.
+ */
+ public IntegerPool(int size) {
+ _table = new Integer[size];
+ for (int i = 0; i < size; i++) {
+ _table[i] = new Integer(i);
+ } // end of for (int i = 0; i < size; i++)
+ }
+
+ /**
+ * Returns the shared Integer wrapper for <tt>value</tt> if it is inside the
+ * range managed by this pool. if <tt>value</tt> is outside the range, a new
+ * Integer instance is returned.
+ *
+ * @param value
+ * an <code>int</code> value
+ * @return an <code>Integer</code> value
+ */
+ public Integer get(int value) {
+ if (value < _table.length && value >= 0) {
+ return _table[value];
+ } else {
+ return new Integer(value);
}
-}// IntegerPool
+ }
+}
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelDomain.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelDomain.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelDomain.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelDomain.java Tue Jan 25 15:09:04 2011
@@ -29,11 +29,10 @@ package opennlp.maxent;
*/
public interface ModelDomain {
- /**
- * Get the name of this domain.
- *
- * @return The name of this domain.
- */
- public String getName ();
-
+ /**
+ * Get the name of this domain.
+ *
+ * @return The name of this domain.
+ */
+ public String getName();
}
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelReplacementManager.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelReplacementManager.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelReplacementManager.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelReplacementManager.java Tue Jan 25 15:09:04 2011
@@ -81,50 +81,55 @@ import opennlp.model.MaxentModel;
* swap to finish. These requests will then be serviced by the new model.
*/
public class ModelReplacementManager {
- private ModelSetter setter;
-
- private int users = 0;
- private boolean replacementCanProceed = true;
- private Thread replacementThread = null;
+ private ModelSetter setter;
- public ModelReplacementManager (ModelSetter ms) {
- setter = ms;
- }
+ private int users = 0;
+ private boolean replacementCanProceed = true;
+ private Thread replacementThread = null;
- /**
- * Inform the manager that a thread is using the model. If a replacement
- * is underway, the thread is forced to join the replacement thread and thus
- * wait until it is finished to begin using the model.
- */
- public void startUsingModel () {
- if (replacementThread != null) {
- try { replacementThread.join(); }
- catch (InterruptedException e) {}
- }
- replacementCanProceed = false;
- users++;
- }
+ public ModelReplacementManager(ModelSetter ms) {
+ setter = ms;
+ }
- /**
- * Inform the manager that a thread is done using the model, and thus is
- * not dependending on it being unchanged.
- */
- public void finishUsingModel () {
- users--;
- if (users<=0) replacementCanProceed = true;
+ /**
+ * Inform the manager that a thread is using the model. If a replacement is
+ * underway, the thread is forced to join the replacement thread and thus wait
+ * until it is finished to begin using the model.
+ */
+ public void startUsingModel() {
+ if (replacementThread != null) {
+ try {
+ replacementThread.join();
+ } catch (InterruptedException e) {
+ }
}
+ replacementCanProceed = false;
+ users++;
+ }
- /**
- * Replace the old model with a new one, forcing the replacement to wait
- * until all threads using the old model have finished using it.
- *
- * @param model The new model which is being swapped in.
- */
- public synchronized void replaceModel (MaxentModel model) {
- replacementThread = Thread.currentThread();
- while (!replacementCanProceed) Thread.yield();
- setter.setModel(model);
- replacementThread = null;
- }
+ /**
+ * Inform the manager that a thread is done using the model, and thus is not
+ * dependending on it being unchanged.
+ */
+ public void finishUsingModel() {
+ users--;
+ if (users <= 0)
+ replacementCanProceed = true;
+ }
+
+ /**
+ * Replace the old model with a new one, forcing the replacement to wait until
+ * all threads using the old model have finished using it.
+ *
+ * @param model
+ * The new model which is being swapped in.
+ */
+ public synchronized void replaceModel(MaxentModel model) {
+ replacementThread = Thread.currentThread();
+ while (!replacementCanProceed)
+ Thread.yield();
+ setter.setModel(model);
+ replacementThread = null;
+ }
}
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelSetter.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelSetter.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelSetter.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/ModelSetter.java Tue Jan 25 15:09:04 2011
@@ -47,10 +47,11 @@ import opennlp.model.MaxentModel;
*/
public interface ModelSetter {
- /**
- * Assign a new MaxentModel value to a MaxentModel variable.
- *
- * @param m The new model.
- */
- public void setModel (MaxentModel m);
+ /**
+ * Assign a new MaxentModel value to a MaxentModel variable.
+ *
+ * @param m
+ * The new model.
+ */
+ public void setModel(MaxentModel m);
}
Modified: incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/PlainTextByLineDataStream.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/PlainTextByLineDataStream.java?rev=1063312&r1=1063311&r2=1063312&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/PlainTextByLineDataStream.java (original)
+++ incubator/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/PlainTextByLineDataStream.java Tue Jan 25 15:09:04 2011
@@ -29,33 +29,30 @@ import java.io.Reader;
* many Maxent applications need in order to create EventStreams.
*/
public class PlainTextByLineDataStream implements DataStream {
- BufferedReader dataReader;
- String next;
-
- public PlainTextByLineDataStream (Reader dataSource) {
- dataReader = new BufferedReader(dataSource);
- try {
- next = dataReader.readLine();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public Object nextToken () {
- String current = next;
- try {
- next = dataReader.readLine();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return current;
+ BufferedReader dataReader;
+ String next;
+
+ public PlainTextByLineDataStream(Reader dataSource) {
+ dataReader = new BufferedReader(dataSource);
+ try {
+ next = dataReader.readLine();
+ } catch (IOException e) {
+ e.printStackTrace();
}
+ }
- public boolean hasNext () {
- return next != null;
+ public Object nextToken() {
+ String current = next;
+ try {
+ next = dataReader.readLine();
+ } catch (Exception e) {
+ e.printStackTrace();
}
-
+ return current;
+ }
+
+ public boolean hasNext() {
+ return next != null;
+ }
}