|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package opennlp.tools.cmdline.sentiment; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.LinkedList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import opennlp.tools.cmdline.AbstractCrossValidatorTool; |
| 25 | +import opennlp.tools.cmdline.CmdLineUtil; |
| 26 | +import opennlp.tools.cmdline.TerminateToolException; |
| 27 | +import opennlp.tools.cmdline.params.BasicTrainingParams; |
| 28 | +import opennlp.tools.cmdline.params.CVParams; |
| 29 | +import opennlp.tools.cmdline.sentiment.SentimentCrossValidatorTool.CVToolParams; |
| 30 | +import opennlp.tools.sentiment.SentimentCrossValidator; |
| 31 | +import opennlp.tools.sentiment.SentimentEvaluationMonitor; |
| 32 | +import opennlp.tools.sentiment.SentimentFactory; |
| 33 | +import opennlp.tools.sentiment.SentimentSample; |
| 34 | +import opennlp.tools.util.eval.EvaluationMonitor; |
| 35 | +import opennlp.tools.util.model.ModelUtil; |
| 36 | + |
| 37 | +/** |
| 38 | + * Class for helping perform cross validation on the Sentiment Analysis Parser. |
| 39 | + */ |
| 40 | +public class SentimentCrossValidatorTool |
| 41 | + extends AbstractCrossValidatorTool<SentimentSample, CVToolParams> { |
| 42 | + |
| 43 | + /** |
| 44 | + * Interface for parameters |
| 45 | + */ |
| 46 | + interface CVToolParams extends BasicTrainingParams, CVParams { |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructor |
| 52 | + */ |
| 53 | + public SentimentCrossValidatorTool() { |
| 54 | + super(SentimentSample.class, CVToolParams.class); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns the short description of the tool |
| 59 | + * |
| 60 | + * @return short description |
| 61 | + */ |
| 62 | + public String getShortDescription() { |
| 63 | + return "K-fold cross validator for the learnable Sentiment Analysis Parser"; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Runs the tool |
| 68 | + * |
| 69 | + * @param format |
| 70 | + * the format to be used |
| 71 | + * @param args |
| 72 | + * the arguments |
| 73 | + */ |
| 74 | + public void run(String format, String[] args) { |
| 75 | + super.run(format, args); |
| 76 | + |
| 77 | + mlParams = CmdLineUtil.loadTrainingParameters(params.getParams(), true); |
| 78 | + if (mlParams == null) { |
| 79 | + mlParams = ModelUtil.createDefaultTrainingParameters(); |
| 80 | + } |
| 81 | + |
| 82 | + List<EvaluationMonitor<SentimentSample>> listeners = new LinkedList<>(); |
| 83 | + if (params.getMisclassified()) { |
| 84 | + listeners.add(new SentimentEvaluationErrorListener()); |
| 85 | + } |
| 86 | + SentimentDetailedFMeasureListener detailedFListener = null; |
| 87 | + SentimentFactory sentimentFactory = new SentimentFactory(); |
| 88 | + |
| 89 | + SentimentCrossValidator validator; |
| 90 | + try { |
| 91 | + validator = new SentimentCrossValidator(params.getLang(), mlParams, sentimentFactory, |
| 92 | + listeners.toArray(new SentimentEvaluationMonitor[listeners.size()])); |
| 93 | + validator.evaluate(sampleStream, params.getFolds()); |
| 94 | + } catch (IOException e) { |
| 95 | + throw new TerminateToolException(-1, |
| 96 | + "IO error while reading training data or indexing data: " |
| 97 | + + e.getMessage(), |
| 98 | + e); |
| 99 | + } finally { |
| 100 | + try { |
| 101 | + sampleStream.close(); |
| 102 | + } catch (IOException e) { |
| 103 | + // sorry that this can fail |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + System.out.println("done"); |
| 108 | + |
| 109 | + System.out.println(); |
| 110 | + |
| 111 | + if (detailedFListener == null) { |
| 112 | + System.out.println(validator.getFMeasure()); |
| 113 | + } else { |
| 114 | + System.out.println(detailedFListener.toString()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments