summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/yql/ResegmentingTestCase.java
blob: 8c4d8e0fe8414ead75f0eaf15032e579be4d53a0 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.yql;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.yahoo.search.query.parser.Parsable;
import com.yahoo.search.query.parser.ParserEnvironment;

/**
 * Check rules for resegmenting words in YQL+ when segmenter is deemed
 * incompatible. The class under testing is {@link YqlParser}.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class ResegmentingTestCase {
    private YqlParser parser;

    @Before
    public void setUp() throws Exception {
        ParserEnvironment env = new ParserEnvironment();
        parser = new YqlParser(env);
    }

    @After
    public void tearDown() throws Exception {
        parser = null;
    }

    @Test
    public final void testWord() {
        assertEquals(
                "title:'a b'",
                parser.parse(
                        new Parsable()
                                .setQuery("select * from sources * where [{\"segmenter\": {\"version\": \"18.47.39\", \"backend\": \"nonexistant\"}}] (title contains \"a b\");"))
                        .toString());
    }

    @Test
    public final void testPhraseSegment() {
        assertEquals(
                "title:'c d'",
                parser.parse(
                        new Parsable()
                                .setQuery("select * from sources * where"
                                        + " [{\"segmenter\": {\"version\": \"18.47.39\", \"backend\": \"nonexistant\"}}]"
                                        + " (title contains ([{\"origin\": {\"offset\": 0, \"length\":3, \"original\": \"c d\"}}]"
                                        + " phrase(\"a\",  \"b\")));"))
                        .toString());
    }

    @Test
    public final void testPhraseInEquiv() {
        assertEquals(
                "EQUIV title:a title:'c d'",
                parser.parse(
                        new Parsable()
                                .setQuery("select * from sources * where"
                                        + " [{\"segmenter\": {\"version\": \"18.47.39\", \"backend\": \"nonexistant\"}}]"
                                        + " (title contains"
                                        + " equiv(\"a\","
                                        + " ([{\"origin\": {\"offset\": 0, \"length\":3, \"original\": \"c d\"}}]\"b\")"
                                        + ")"
                                        + ");"))
                        .toString());
    }

    @Test
    public final void testPhraseSegmentToAndSegment() {
        assertEquals(
                "SAND title:c title:d",
                parser.parse(
                        new Parsable()
                                .setQuery("select * from sources * where"
                                        + " [{\"segmenter\": {\"version\": \"18.47.39\", \"backend\": \"nonexistant\"}}]"
                                        + " (title contains ([{\"origin\": {\"offset\": 0, \"length\":3, \"original\": \"c d\"}, \"andSegmenting\": true}]"
                                        + " phrase(\"a\",  \"b\")));"))
                        .toString());
    }

    @Test
    public final void testPhraseSegmentInPhrase() {
        assertEquals(
                "title:\"a 'c d'\"",
                parser.parse(
                        new Parsable()
                                .setQuery("select * from sources * where [{\"segmenter\": {\"version\": \"18.47.39\", \"backend\": \"nonexistant\"}}]"
                                        + " (title contains phrase(\"a\","
                                        + " ([{\"origin\": {\"offset\": 0, \"length\":3, \"original\": \"c d\"}}]"
                                        + "  phrase(\"e\", \"f\"))));"))
                        .toString());
    }

    @Test
    public final void testWordNoImplicitTransforms() {
        assertEquals(
                "title:a b",
                parser.parse(
                        new Parsable()
                                .setQuery("select * from sources * where [{\"segmenter\": {\"version\": \"18.47.39\", \"backend\": \"nonexistant\"}}] (title contains ([{\"implicitTransforms\": false}]\"a b\"));"))
                        .toString());
    }

    @Test
    public final void testPhraseSegmentNoImplicitTransforms() {
        assertEquals(
                "title:'a b'",
                parser.parse(
                        new Parsable()
                                .setQuery("select * from sources * where"
                                        + " [{\"segmenter\": {\"version\": \"18.47.39\", \"backend\": \"nonexistant\"}}]"
                                        + " (title contains ([{\"origin\": {\"offset\": 0, \"length\":3, \"original\": \"c d\"}, \"implicitTransforms\": false}]"
                                        + " phrase(\"a\",  \"b\")));"))
                        .toString());
    }

    @Test
    public final void testPhraseSegmentToAndSegmentNoImplicitTransforms() {
        assertEquals(
                "SAND title:a title:b",
                parser.parse(
                        new Parsable()
                                .setQuery("select * from sources * where"
                                        + " [{\"segmenter\": {\"version\": \"18.47.39\", \"backend\": \"nonexistant\"}}]"
                                        + " (title contains ([{\"origin\": {\"offset\": 0, \"length\":3, \"original\": \"c d\"}, \"andSegmenting\": true, \"implicitTransforms\": false}]"
                                        + " phrase(\"a\",  \"b\")));"))
                        .toString());
    }

    @Test
    public final void testPhraseSegmentInPhraseNoImplicitTransforms() {
        assertEquals(
                "title:\"a 'e f'\"",
                parser.parse(
                        new Parsable()
                                .setQuery("select * from sources * where [{\"segmenter\": {\"version\": \"18.47.39\", \"backend\": \"nonexistant\"}}]"
                                        + " (title contains phrase(\"a\","
                                        + " ([{\"origin\": {\"offset\": 0, \"length\":3, \"original\": \"c d\"}, \"implicitTransforms\": false}]"
                                        + "  phrase(\"e\", \"f\"))));"))
                        .toString());
    }

}