File size: 2,735 Bytes
6f3ebfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package org.maltparser.concurrent;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.maltparser.core.exception.MaltChainedException;
import org.maltparser.core.lw.helper.Utils;
import org.maltparser.core.options.OptionManager;

/**
* The purpose of ConcurrentMaltParserService is to provide an interface to MaltParser that makes it easier to parse sentences from 
* other programs. ConcurrentMaltParserService is hopefully thread-safe and can be used in a multi threaded environment. This class 
* replace the old interface org.maltparser.MaltParserService when you want to load a MaltParser model and then use it 
* to parse sentences in both a single threaded or multi threaded application. 
* 
* How to use ConcurrentMaltParserService, please see the examples provided in the directory 'examples/apiexamples/srcex'
* 
* @author Johan Hall
*/
public final class ConcurrentMaltParserService {
	private static int optionContainerCounter = 0;

    
	private static synchronized int getNextOptionContainerCounter() {
		return optionContainerCounter++;
	}
	
	private static synchronized void loadOptions() throws MaltChainedException {
    	if (!OptionManager.instance().hasOptions()) {
    		OptionManager.instance().loadOptionDescriptionFile();
    		OptionManager.instance().generateMaps();
    	}
	}
	
    /**
     * Initialize a MaltParser model from a MaltParser model file (.mco)
     * 
     * @param mcoFile File object containing the path to a valid MaltParser model file. Usually the file extension is ".mco" 
     * @return MaltParser model
     * @throws MaltChainedException
     * @throws MalformedURLException
     */
    public static ConcurrentMaltParserModel initializeParserModel(File mcoFile) throws MaltChainedException, MalformedURLException {
        return initializeParserModel(mcoFile.toURI().toURL());
    }
    
    /**
     * Initialize a MaltParser model from a MaltParser model file (.mco)
     * 
     * @param mcoURL URL to a valid MaltParser model file. Usually the file extension is ".mco"  
     * @return a concurrent MaltParser model
     * @throws MaltChainedException
     */
    public static ConcurrentMaltParserModel initializeParserModel(URL mcoURL) throws MaltChainedException {
    	loadOptions();
    	int optionContainer = getNextOptionContainerCounter();
    	String parserModelName = Utils.getInternalParserModelName(mcoURL);
		OptionManager.instance().parseCommandLine("-m parse", optionContainer);
        OptionManager.instance().loadOptions(optionContainer, Utils.getInputStreamReaderFromConfigFileEntry(mcoURL, parserModelName, "savedoptions.sop", "UTF-8"));
        return new ConcurrentMaltParserModel(optionContainer, mcoURL);
    }
}