summaryrefslogtreecommitdiffstats
path: root/linguistics/src/test/java/com/yahoo/language/opennlp/OpenNlpDetectorTestCase.java
blob: aaa6b2a64840ca9d1539c702c4cb28334823a2d3 (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
57
58
59
60
61
62
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.language.opennlp;

import com.yahoo.language.Language;
import com.yahoo.language.detect.Detector;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * @author jonmv
 */
public class OpenNlpDetectorTestCase {

    @Test
    public void testDetection() {
        Detector detector = new OpenNlpDetector();

        assertLanguage(Language.UNKNOWN,
                       "",
                       detector);

        assertLanguage(Language.UNKNOWN,
                       "Hello!",
                       detector);

        // from https://en.wikipedia.org/wiki/Yahoo
        assertLanguage(Language.ENGLISH,
                       "Yahoo became a public company via an initial public offering in April 1996 and its stock price rose 600% within two years.",
                       detector);

        // from https://de.wikipedia.org/wiki/Yahoo
        assertLanguage(Language.GERMAN,
                       "1996 ging Yahoo mit 46 Angestellten an die Börse. 2009 arbeiteten insgesamt rund 13.500 Mitarbeiter für Yahoo.",
                       detector);

        // from https://fr.wikipedia.org/wiki/Yahoo
        assertLanguage(Language.FRENCH,
                       "À l'origine, Yahoo! était uniquement un annuaire Web.",
                       detector);

        // Test fallback to SimpleDetector
        assertLanguage(Language.CHINESE_TRADITIONAL, // CHINESE_SIMPLIFIED input
                       "\u6211\u80FD\u541E\u4E0B\u73BB\u7483\u800C\u4E0D\u4F24\u8EAB\u4F53\u3002",
                       detector);

        // from https://ru.wikipedia.org/wiki/%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D1%8F
        assertLanguage(Language.RUSSIAN,
                       "7 февраля 2000 года Yahoo.com подвергся DDoS атаке и на несколько часов приостановил работу.",
                       detector);

        // https://he.wikipedia.org/wiki/Yahoo!
        assertLanguage(Language.HEBREW,
                       "אתר יאהו! הוא אחד מאתרי האינטרנט הפופולריים ביותר בעולם, עם מעל 500 מיליון כניסות בכל יום",
                       detector);
    }

    private void assertLanguage(Language language, String input, Detector detector) {
        assertEquals(language, detector.detect(input, null).getLanguage());
    }

}