aboutsummaryrefslogtreecommitdiffstats
path: root/searchsummary/src/tests/docsummary/annotation_converter/annotation_converter_test.cpp
blob: 0a05e0783824d5b13f80199b465fb631b9ebef59 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#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/datatype/annotationtype.h>
#include <vespa/document/fieldvalue/stringfieldvalue.h>
#include <vespa/document/repo/configbuilder.h>
#include <vespa/document/repo/fixedtyperepo.h>
#include <vespa/juniper/juniper_separators.h>
#include <vespa/searchlib/util/linguisticsannotation.h>
#include <vespa/searchsummary/docsummary/annotation_converter.h>
#include <vespa/searchsummary/docsummary/i_juniper_converter.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/stllike/asciistream.h>

using document::Annotation;
using document::AnnotationType;
using document::DocumentType;
using document::DocumentTypeRepo;
using document::Span;
using document::SpanList;
using document::SpanTree;
using document::StringFieldValue;
using search::docsummary::AnnotationConverter;
using search::docsummary::IJuniperConverter;
using search::linguistics::SPANTREE_NAME;
using vespalib::Slime;
using vespalib::slime::SlimeInserter;

namespace {

DocumenttypesConfig
get_document_types_config()
{
    using namespace document::config_builder;
    DocumenttypesConfigBuilderHelper builder;
    builder.document(42, "indexingdocument",
                     Struct("indexingdocument.header"),
                     Struct("indexingdocument.body"));
    return builder.config();
}

class MockJuniperConverter : public IJuniperConverter
{
    vespalib::string _result;
public:
    void convert(vespalib::stringref input, vespalib::slime::Inserter&) override {
        _result = input;
    }
    const vespalib::string& get_result() const noexcept { return _result; }
};

}

class AnnotationConverterTest : public testing::Test
{
protected:
    std::shared_ptr<const DocumentTypeRepo> _repo;
    const DocumentType*                     _document_type;
    document::FixedTypeRepo                 _fixed_repo;

    AnnotationConverterTest();
    ~AnnotationConverterTest() override;
    void set_span_tree(StringFieldValue& value, std::unique_ptr<SpanTree> tree);
    StringFieldValue make_annotated_string();
    StringFieldValue make_annotated_chinese_string();
    vespalib::string make_exp_il_annotated_string();
    vespalib::string make_exp_il_annotated_chinese_string();
    void expect_annotated(const vespalib::string& exp, const StringFieldValue& fv);
};

AnnotationConverterTest::AnnotationConverterTest()
    : testing::Test(),
      _repo(std::make_unique<DocumentTypeRepo>(get_document_types_config())),
      _document_type(_repo->getDocumentType("indexingdocument")),
      _fixed_repo(*_repo, *_document_type)
{
}

AnnotationConverterTest::~AnnotationConverterTest() = default;

void
AnnotationConverterTest::set_span_tree(StringFieldValue & value, std::unique_ptr<SpanTree> tree)
{
    StringFieldValue::SpanTrees trees;
    trees.push_back(std::move(tree));
    value.setSpanTrees(trees, _fixed_repo);
}

StringFieldValue
AnnotationConverterTest::make_annotated_string()
{
    auto span_list_up = std::make_unique<SpanList>();
    auto span_list = span_list_up.get();
    auto tree = std::make_unique<SpanTree>(SPANTREE_NAME, std::move(span_list_up));
    tree->annotate(span_list->add(std::make_unique<Span>(0, 3)), *AnnotationType::TERM);
    tree->annotate(span_list->add(std::make_unique<Span>(4, 3)),
                   Annotation(*AnnotationType::TERM, std::make_unique<StringFieldValue>("baz")));
    StringFieldValue value("foo bar");
    set_span_tree(value, std::move(tree));
    return value;
}

StringFieldValue
AnnotationConverterTest::make_annotated_chinese_string()
{
    auto span_list_up = std::make_unique<SpanList>();
    auto span_list = span_list_up.get();
    auto tree = std::make_unique<SpanTree>(SPANTREE_NAME, std::move(span_list_up));
    // These chinese characters each use 3 bytes in their UTF8 encoding.
    tree->annotate(span_list->add(std::make_unique<Span>(0, 15)), *AnnotationType::TERM);
    tree->annotate(span_list->add(std::make_unique<Span>(15, 9)), *AnnotationType::TERM);
    StringFieldValue value("我就是那个大灰狼");
    set_span_tree(value, std::move(tree));
    return value;
}

vespalib::string
AnnotationConverterTest::make_exp_il_annotated_string()
{
    using namespace juniper::separators;
    vespalib::asciistream exp;
    exp << "foo" << unit_separator_string <<
        " " << unit_separator_string << interlinear_annotation_anchor_string <<
        "bar" << interlinear_annotation_separator_string <<
        "baz" << interlinear_annotation_terminator_string << unit_separator_string;
    return exp.str();
}

vespalib::string
AnnotationConverterTest::make_exp_il_annotated_chinese_string()
{
    using namespace juniper::separators;
    vespalib::asciistream exp;
    exp << "我就是那个" << unit_separator_string <<
        "大灰狼" << unit_separator_string;
    return exp.str();
}

void
AnnotationConverterTest::expect_annotated(const vespalib::string& exp, const StringFieldValue& fv)
{
    MockJuniperConverter juniper_converter;
    AnnotationConverter annotation_converter(juniper_converter);
    Slime slime;
    SlimeInserter inserter(slime);
    annotation_converter.convert(fv, inserter);
    EXPECT_EQ(exp, juniper_converter.get_result());
}


TEST_F(AnnotationConverterTest, convert_plain_string)
{
    using namespace juniper::separators;
    vespalib::string exp("Foo Bar Baz");
    StringFieldValue plain_string("Foo Bar Baz");
    expect_annotated(exp + unit_separator_string, plain_string);
}

TEST_F(AnnotationConverterTest, convert_annotated_string)
{
    auto exp = make_exp_il_annotated_string();
    auto annotated_string = make_annotated_string();
    expect_annotated(exp, annotated_string);
}

TEST_F(AnnotationConverterTest, convert_annotated_chinese_string)
{
        auto exp = make_exp_il_annotated_chinese_string();
        auto annotated_chinese_string = make_annotated_chinese_string();
        expect_annotated(exp, annotated_chinese_string);
}

GTEST_MAIN_RUN_ALL_TESTS()