summaryrefslogtreecommitdiffstats
path: root/linguistics/src/main/java/com/yahoo/language/opennlp/OpenNlpLinguistics.java
blob: 0837b25c1514aa63ed0e93f5edd1a290b79b7e4a (plain) (blame)
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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.language.opennlp;

import com.google.inject.Inject;
import com.yahoo.language.detect.Detector;
import com.yahoo.language.process.Tokenizer;
import com.yahoo.language.simple.SimpleDetector;
import com.yahoo.language.simple.SimpleLinguistics;
import java.util.logging.Logger;
import java.util.logging.Level;

/**
 * Returns a linguistics implementation based on OpenNlp,
 * and (optionally, default on) Optimaize for language detection.
 */
public class OpenNlpLinguistics extends SimpleLinguistics {

    private static final Logger log = Logger.getLogger(OpenNlpLinguistics.class.getName());
    private final Detector detector;

    public OpenNlpLinguistics() {
        this(true);
    }

    @Inject
    public OpenNlpLinguistics(OpennlpLinguisticsConfig config) {
        this(config.detector().enableOptimaize());
    }

    public OpenNlpLinguistics(boolean enableOptimaize) {
        this(enableOptimaize ? new OptimaizeDetector() : new SimpleDetector());
        log.log(Level.FINE, "using "+(enableOptimaize ? "Optimaize" : "Simple")+" detector");
    }

    private OpenNlpLinguistics(Detector detector) {
        this.detector = detector;
    }

    @Override
    public Tokenizer getTokenizer() {
        return new OpenNlpTokenizer(getNormalizer(), getTransformer());
    }

    @Override
    public Detector getDetector() { return detector; }

}