summaryrefslogtreecommitdiffstats
path: root/linguistics/src/main/java/com/yahoo/language/process/Encoder.java
blob: 27f73d15e544c46c81d7286c1e3e00fdc6e677fb (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
48
49
50
51
52
53
54
55
56
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.language.process;

import com.yahoo.language.Language;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;

import java.util.List;

/**
 * An encoder converts a text string to a tensor or list of tokens
 *
 * @author bratseth
 */
public interface Encoder {

    /** An instance of this which throws IllegalStateException if attempted used */
    Encoder throwsOnUse = new FailingEncoder();

    /**
     * Encodes text into tokens in a list of ids.
     *
     * @param text the text to encode
     * @param language the language of the text, or UNKNOWN to use language independent encoding
     * @return the text encoded to a list of segment ids
     * @throws IllegalArgumentException if the language is not supported by this encoder
     */
    List<Integer> encode(String text, Language language);

    /**
     * Encodes text into tokens in a tensor.
     * The information contained in the encoding may depend on the tensor type.
     *
     * @param text the text to encode
     * @param language the language of the text, or UNKNOWN to use language independent encoding
     * @param tensorType the type of the ttensor to be returned
     * @return the tex encoded into a tensor of the supplied type
     * @throws IllegalArgumentException if the language or tensor type is not supported by this encoder
     */
    Tensor encode(String text, Language language, TensorType tensorType);

    class FailingEncoder implements Encoder {

        @Override
        public List<Integer> encode(String text, Language language) {
            throw new IllegalStateException("No encoder has been configured");
        }

        @Override
        public Tensor encode(String text, Language language, TensorType tensorType) {
            throw new IllegalStateException("No encoder has been configured");
        }

    }

}