aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/test/string_field_builder.cpp
blob: d81572d89133ce5bde92e360dab2127cc0af1c56 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "string_field_builder.h"
#include "doc_builder.h"
#include <vespa/document/annotation/annotation.h>
#include <vespa/document/annotation/span.h>
#include <vespa/document/annotation/spanlist.h>
#include <vespa/document/annotation/spantree.h>
#include <vespa/document/fieldvalue/stringfieldvalue.h>
#include <vespa/searchlib/util/linguisticsannotation.h>
#include <vespa/fastlib/text/unicodeutil.h>
#include <vespa/vespalib/text/utf8.h>

#include <cassert>

using document::Annotation;
using document::AnnotationType;
using document::FixedTypeRepo;
using document::StringFieldValue;
using document::Span;
using document::SpanList;
using document::SpanNode;
using document::SpanTree;
using vespalib::Utf8Reader;
using vespalib::Utf8Writer;
using search::linguistics::SPANTREE_NAME;

namespace search::test {

StringFieldBuilder::StringFieldBuilder(const DocBuilder& doc_builder)
    : _value(),
      _span_start(0u),
      _span_list(nullptr),
      _span_tree(),
      _last_span(nullptr),
      _repo(doc_builder.get_repo(), doc_builder.get_document_type())
{
}

StringFieldBuilder::~StringFieldBuilder() = default;

void
StringFieldBuilder::start_annotate()
{
    auto span_list_up = std::make_unique<SpanList>();
    _span_list = span_list_up.get();
    _span_tree = std::make_unique<SpanTree>(SPANTREE_NAME, std::move(span_list_up));
}

void
StringFieldBuilder::add_span()
{
    assert(_value.size() > _span_start);
    const SpanNode &span = _span_list->add(std::make_unique<Span>(_span_start, _value.size() - _span_start));
    _last_span = &span;
    _span_start = _value.size();
}

StringFieldBuilder&
StringFieldBuilder::token(const vespalib::string& val, bool is_word)
{
    if (val.empty()) {
        return *this;
    }
    if (!_span_tree) {
        start_annotate();
    }
    _span_start = _value.size();
    _value.append(val);
    add_span();
    if (is_word) {
        _span_tree->annotate(*_last_span, *AnnotationType::TERM);
    }
    return *this;
}

StringFieldBuilder&
StringFieldBuilder::alt_word(const vespalib::string& val)
{
    assert(_last_span != nullptr);
    _span_tree->annotate(*_last_span,
                         Annotation(*AnnotationType::TERM,
                                    std::make_unique<StringFieldValue>(val)));
    return *this;
}

StringFieldBuilder&
StringFieldBuilder::tokenize(const vespalib::string& val)
{
    Utf8Reader reader(val);
    vespalib::string token_buffer;
    Utf8Writer writer(token_buffer);
    uint32_t c = 0u;
    bool old_word = false;

    while (reader.hasMore()) {
        c = reader.getChar();
        bool new_word = Fast_UnicodeUtil::IsWordChar(c);
        if (old_word != new_word) {
            if (!token_buffer.empty()) {
                token(token_buffer, old_word);
                token_buffer.clear();
            }
            old_word = new_word;
        }
        writer.putChar(c);
    }
    if (!token_buffer.empty()) {
        token(token_buffer, old_word);
    }
    return *this;
}


document::StringFieldValue
StringFieldBuilder::build()
{
    StringFieldValue value(_value);
    // Also drop all spans no annotation for now
    if (_span_tree && _span_tree->numAnnotations() > 0u) {
        StringFieldValue::SpanTrees trees;
        trees.emplace_back(std::move(_span_tree));
        value.setSpanTrees(trees, _repo);
    } else {
        _span_tree.reset();
    }
    _span_list = nullptr;
    _last_span = nullptr;
    _span_start = 0u;
    _value.clear();
    return value;
}

}