aboutsummaryrefslogtreecommitdiff
path: root/icing/util/tokenized-document.h
blob: 7cc34e39b2c677cc102b9785784c077c0157a162 (plain)
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
// Copyright (C) 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ICING_STORE_TOKENIZED_DOCUMENT_H_
#define ICING_STORE_TOKENIZED_DOCUMENT_H_

#include <cstdint>
#include <string>
#include <vector>

#include "icing/text_classifier/lib3/utils/base/statusor.h"
#include "icing/proto/document.pb.h"
#include "icing/schema/joinable-property.h"
#include "icing/schema/schema-store.h"
#include "icing/schema/section.h"
#include "icing/tokenization/language-segmenter.h"

namespace icing {
namespace lib {

struct TokenizedSection {
  SectionMetadata metadata;
  std::vector<std::string_view> token_sequence;

  TokenizedSection(SectionMetadata&& metadata_in,
                   std::vector<std::string_view>&& token_sequence_in)
      : metadata(std::move(metadata_in)),
        token_sequence(std::move(token_sequence_in)) {}
};

class TokenizedDocument {
 public:
  static libtextclassifier3::StatusOr<TokenizedDocument> Create(
      const SchemaStore* schema_store,
      const LanguageSegmenter* language_segmenter, DocumentProto document);

  const DocumentProto& document() const { return document_; }

  int32_t num_string_tokens() const {
    int32_t num_string_tokens = 0;
    for (const TokenizedSection& section : tokenized_string_sections_) {
      num_string_tokens += section.token_sequence.size();
    }
    return num_string_tokens;
  }

  const std::vector<TokenizedSection>& tokenized_string_sections() const {
    return tokenized_string_sections_;
  }

  const std::vector<Section<int64_t>>& integer_sections() const {
    return integer_sections_;
  }

  const std::vector<JoinableProperty<std::string_view>>&
  qualified_id_join_properties() const {
    return joinable_property_group_.qualified_id_properties;
  }

 private:
  // Use TokenizedDocument::Create() to instantiate.
  explicit TokenizedDocument(
      DocumentProto&& document,
      std::vector<TokenizedSection>&& tokenized_string_sections,
      std::vector<Section<int64_t>>&& integer_sections,
      JoinablePropertyGroup&& joinable_property_group)
      : document_(std::move(document)),
        tokenized_string_sections_(std::move(tokenized_string_sections)),
        integer_sections_(std::move(integer_sections)),
        joinable_property_group_(std::move(joinable_property_group)) {}

  DocumentProto document_;
  std::vector<TokenizedSection> tokenized_string_sections_;
  std::vector<Section<int64_t>> integer_sections_;
  JoinablePropertyGroup joinable_property_group_;
};

}  // namespace lib
}  // namespace icing

#endif  // ICING_STORE_TOKENIZED_DOCUMENT_H_