aboutsummaryrefslogtreecommitdiffstats
path: root/linguistics-components/src/test/java/com/yahoo/language/wordpiece/WordPieceEmbedderTest.java
blob: 13e0cbce10ddc6a5ee260f43a88cd36501611563 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.language.wordpiece;

import com.yahoo.config.FileReference;
import com.yahoo.language.tools.EmbedderTester;
import org.junit.Test;

/**
 * Tests the WordPiece embedder
 *
 * @author bratseth
 */
public class WordPieceEmbedderTest {

    private static final String vocabulary = "src/test/models/wordpiece/bert-base-uncased-vocab.txt";

    @Test
    public void testWordPieceSegmentation() {
        var tester = new EmbedderTester(new WordPieceEmbedder.Builder(vocabulary).build());
        tester.assertSegmented("what was the impact of the manhattan project",
                               "what", "was", "the", "impact", "of", "the", "manhattan", "project");
        tester.assertSegmented("overcommunication", "over", "##com", "##mun", "##ication");
    }

    @Test
    public void testWordPieceEmbedding() {
        var tester = new EmbedderTester(new WordPieceEmbedder.Builder(vocabulary).build());
        tester.assertEmbedded("what was the impact of the manhattan project",
                              "tensor(x[8])",
                              2054, 2001, 1996, 4254, 1997, 1996, 7128, 2622);
    }

    @Test
    public void testWordPieceEmbedderConfiguration() {
        var config = new WordPieceConfig.Builder().model(new WordPieceConfig.Model.Builder()
                                                                                  .language("unknown")
                                                                                  .path(new FileReference(vocabulary)))
                                             .build();
        var tester = new EmbedderTester(new WordPieceEmbedder(config));
        tester.assertSegmented("what was the impact of the manhattan project",
                               "what", "was", "the", "impact", "of", "the", "manhattan", "project");
    }

}