Search in sources :

Example 1 with Alternative

use of com.yahoo.prelude.query.WordAlternativesItem.Alternative in project vespa by vespa-engine.

the class StemmingSearcher method singleWordSegment.

private TaggableItem singleWordSegment(BlockItem current, StemList segment, Index index, Substring substring) {
    String indexName = current.getIndexName();
    if (index.getLiteralBoost() || index.getStemMode() == StemMode.ALL) {
        // Yes, this will create a new WordAlternativesItem even if stemmed
        // and original form are identical. This is to decrease complexity
        // in accent removal and lowercasing.
        List<Alternative> terms = new ArrayList<>(segment.size() + 1);
        terms.add(new Alternative(current.stringValue(), 1.0d));
        for (String term : segment) {
            terms.add(new Alternative(term, 0.7d));
        }
        WordAlternativesItem alternatives = new WordAlternativesItem(indexName, current.isFromQuery(), substring, terms);
        return alternatives;
    } else {
        WordItem first = singleStemSegment((Item) current, segment.get(0), indexName, substring);
        return first;
    }
}
Also used : Alternative(com.yahoo.prelude.query.WordAlternativesItem.Alternative)

Example 2 with Alternative

use of com.yahoo.prelude.query.WordAlternativesItem.Alternative in project vespa by vespa-engine.

the class WordAlternativesItemTestCase method testWordAlternativesItem.

@Test
public final void testWordAlternativesItem() {
    List<Alternative> terms = new ArrayList<>();
    List<Alternative> expected;
    terms.add(new Alternative("1", 1.0));
    terms.add(new Alternative("2", 1.0));
    terms.add(new Alternative("3", 1.0));
    terms.add(new Alternative("4", 1.0));
    expected = new ArrayList<>(terms);
    terms.add(new Alternative("1", .1));
    terms.add(new Alternative("2", .2));
    terms.add(new Alternative("3", .3));
    terms.add(new Alternative("4", .4));
    WordAlternativesItem w = new WordAlternativesItem("", true, null, terms);
    assertEquals(expected, w.getAlternatives());
}
Also used : Alternative(com.yahoo.prelude.query.WordAlternativesItem.Alternative) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with Alternative

use of com.yahoo.prelude.query.WordAlternativesItem.Alternative in project vespa by vespa-engine.

the class LowercasingTestCase method testLowercasingWordAlternatives.

@Test
public void testLowercasingWordAlternatives() {
    execution = new Execution(new Chain<Searcher>(new VespaLowercasingSearcher(new LowercasingConfig(new LowercasingConfig.Builder().transform_weighted_sets(false)))), Execution.Context.createContextStub(settings));
    Query q = new Query();
    WordAlternativesItem root;
    List<WordAlternativesItem.Alternative> terms = new ArrayList<>();
    terms.add(new Alternative("ABC", 1.0));
    terms.add(new Alternative("def", 1.0));
    root = new WordAlternativesItem(BAMSE, true, null, terms);
    q.getModel().getQueryTree().setRoot(root);
    Result r = execution.search(q);
    root = (WordAlternativesItem) r.getQuery().getModel().getQueryTree().getRoot();
    assertEquals(3, root.getAlternatives().size());
    assertEquals("ABC", root.getAlternatives().get(0).word);
    assertEquals(1.0d, root.getAlternatives().get(0).exactness, 1e-15d);
    assertEquals("abc", root.getAlternatives().get(1).word);
    assertEquals(.7d, root.getAlternatives().get(1).exactness, 1e-15d);
    assertEquals("def", root.getAlternatives().get(2).word);
    assertEquals(1.0d, root.getAlternatives().get(2).exactness, 1e-15d);
}
Also used : Chain(com.yahoo.component.chain.Chain) Execution(com.yahoo.search.searchchain.Execution) Alternative(com.yahoo.prelude.query.WordAlternativesItem.Alternative) Query(com.yahoo.search.Query) ArrayList(java.util.ArrayList) WordAlternativesItem(com.yahoo.prelude.query.WordAlternativesItem) Result(com.yahoo.search.Result) Test(org.junit.Test)

Example 4 with Alternative

use of com.yahoo.prelude.query.WordAlternativesItem.Alternative in project vespa by vespa-engine.

the class WordAlternativesItemTestCase method testSetAlternatives.

@Test
public final void testSetAlternatives() {
    List<Alternative> terms = new ArrayList<>();
    terms.add(new Alternative("1", 1.0));
    terms.add(new Alternative("2", 1.0));
    WordAlternativesItem w = new WordAlternativesItem("", true, null, terms);
    terms.add(new Alternative("1", 1.5));
    terms.add(new Alternative("2", 0.5));
    w.setAlternatives(terms);
    assertTrue("Could not overwrite alternative", w.getAlternatives().stream().anyMatch((a) -> a.word.equals("1") && a.exactness == 1.5));
    assertTrue("Old alternative unexpectedly removed", w.getAlternatives().stream().anyMatch((a) -> a.word.equals("2") && a.exactness == 1.0));
    assertEquals(2, w.getAlternatives().size());
    terms.add(new Alternative("3", 0.5));
    w.setAlternatives(terms);
    assertTrue("Could not add new term", w.getAlternatives().stream().anyMatch((a) -> a.word.equals("3") && a.exactness == 0.5));
}
Also used : Alternative(com.yahoo.prelude.query.WordAlternativesItem.Alternative) List(java.util.List) Test(org.junit.Test) Assert(org.junit.Assert) ArrayList(java.util.ArrayList) Alternative(com.yahoo.prelude.query.WordAlternativesItem.Alternative) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with Alternative

use of com.yahoo.prelude.query.WordAlternativesItem.Alternative in project vespa by vespa-engine.

the class WordAlternativesItemTestCase method testAddTerm.

@Test
public final void testAddTerm() {
    List<Alternative> terms = new ArrayList<>();
    terms.add(new Alternative("1", 1.0));
    terms.add(new Alternative("2", 1.0));
    WordAlternativesItem w = new WordAlternativesItem("", true, null, terms);
    w.addTerm("1", 0.1);
    assertEquals(terms, w.getAlternatives());
    w.addTerm("1", 2.0);
    assertTrue("Could not add new alternative", w.getAlternatives().stream().anyMatch((a) -> a.word.equals("1") && a.exactness == 2.0));
    assertEquals(2, w.getAlternatives().size());
    w.addTerm("3", 0.5);
    assertTrue("Could not add new term", w.getAlternatives().stream().anyMatch((a) -> a.word.equals("3") && a.exactness == 0.5));
}
Also used : Alternative(com.yahoo.prelude.query.WordAlternativesItem.Alternative) List(java.util.List) Test(org.junit.Test) Assert(org.junit.Assert) ArrayList(java.util.ArrayList) Alternative(com.yahoo.prelude.query.WordAlternativesItem.Alternative) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Alternative (com.yahoo.prelude.query.WordAlternativesItem.Alternative)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 List (java.util.List)2 Assert (org.junit.Assert)2 Chain (com.yahoo.component.chain.Chain)1 WordAlternativesItem (com.yahoo.prelude.query.WordAlternativesItem)1 Query (com.yahoo.search.Query)1 Result (com.yahoo.search.Result)1 Execution (com.yahoo.search.searchchain.Execution)1