File size: 3,423 Bytes
393badf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package SentenceSplitting;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;

import opennlp.tools.dictionary.Dictionary;
import opennlp.tools.ml.EventTrainer;
import opennlp.tools.sentdetect.SentenceDetectorFactory;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.sentdetect.SentenceSampleStream;
import opennlp.tools.util.InputStreamFactory;
import opennlp.tools.util.MarkableFileInputStreamFactory;
import opennlp.tools.util.PlainTextByLineStream;
import opennlp.tools.util.TrainingParameters;
import opennlp.tools.util.model.ModelType;

/*
 * This class is used to create the sentence splitter model for Spanish clinical cases using the Apache OpenNLP API.
 * Input:
 * - Training file, all documents' sentences splitted, one sentence per line, everything in a single file.
 * - Path where the final model will be printed.
 * - Name of the created model's file.
 * - File with a list of abbreviations, one abbreviation per line.
 * We recommend using the abbreviations' file in order to get a better model.
 * Output:
 * - Sentence splitting model.
 */

public class CreateModelSS {
	
	public static void main (String args[]) throws IOException
	{
		String trainFile = args[0];
		String outModel = args[1];
		String modelName = args[2];
		String abbrFile = args[3];
		
		// directory to save the model file that is to be generated, create this directory in prior
		File destDir = new File(outModel);
		
		// Load dictionary of abbreviations.
		Dictionary abbrDictionary = makeAbbrDictionary(abbrFile);
		
		// Load train set.
        InputStreamFactory in = new MarkableFileInputStreamFactory(new File(trainFile));
 
        // parameters used by machine learning algorithm, Maxent, to train its weights
        TrainingParameters mlParams = new TrainingParameters();
        mlParams.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(4000));
        mlParams.put(TrainingParameters.CUTOFF_PARAM, Integer.toString(3)); 
        mlParams.put(TrainingParameters.TRAINER_TYPE_PARAM, EventTrainer.EVENT_VALUE);
        mlParams.put(TrainingParameters.ALGORITHM_PARAM, ModelType.MAXENT.name());
        
        // Train the model.
        SentenceDetectorFactory sentenceDetectorFactory = SentenceDetectorFactory.create(null, "es", true, abbrDictionary, ".?!".toCharArray());
        SentenceModel sentdetectModel = SentenceDetectorME.train(
                "es",
                new SentenceSampleStream(new PlainTextByLineStream(in, StandardCharsets.UTF_8)),
                sentenceDetectorFactory,
                mlParams);
  //              mlParams.defaultParams());
 
     // Print out the model into a file.
        File outFile = new File(destDir,modelName); 
        FileOutputStream outFileStream = new FileOutputStream(outFile); 
        sentdetectModel.serialize(outFileStream);		
	}
	
	/*
	 * This method reads the abbreviation list file and loads the complete list into a dictionary.
	 */
	public static Dictionary makeAbbrDictionary(String abbrFile) throws IOException
	{
		Dictionary dictionary = new Dictionary();
		
		Reader reader = new BufferedReader(new FileReader(abbrFile));		
		dictionary = Dictionary.parseOneEntryPerLine(reader);
		
		return dictionary;
	}
}