aboutsummaryrefslogtreecommitdiff
path: root/icing/scoring/advanced_scoring/scoring-visitor.h
blob: 9d9c1052d8808054d8c185ed703dd43d49315cdd (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
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (C) 2022 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_SCORING_ADVANCED_SCORING_SCORING_VISITOR_H_
#define ICING_SCORING_ADVANCED_SCORING_SCORING_VISITOR_H_

#include "icing/text_classifier/lib3/utils/base/status.h"
#include "icing/text_classifier/lib3/utils/base/statusor.h"
#include "icing/join/join-children-fetcher.h"
#include "icing/legacy/core/icing-string-util.h"
#include "icing/proto/scoring.pb.h"
#include "icing/query/advanced_query_parser/abstract-syntax-tree.h"
#include "icing/scoring/advanced_scoring/score-expression.h"
#include "icing/scoring/bm25f-calculator.h"
#include "icing/store/document-store.h"

namespace icing {
namespace lib {

class ScoringVisitor : public AbstractSyntaxTreeVisitor {
 public:
  explicit ScoringVisitor(double default_score,
                          const DocumentStore* document_store,
                          const SchemaStore* schema_store,
                          SectionWeights* section_weights,
                          Bm25fCalculator* bm25f_calculator,
                          const JoinChildrenFetcher* join_children_fetcher)
      : default_score_(default_score),
        document_store_(*document_store),
        schema_store_(*schema_store),
        section_weights_(*section_weights),
        bm25f_calculator_(*bm25f_calculator),
        join_children_fetcher_(join_children_fetcher) {}

  void VisitFunctionName(const FunctionNameNode* node) override;
  void VisitString(const StringNode* node) override;
  void VisitText(const TextNode* node) override;
  void VisitMember(const MemberNode* node) override;

  void VisitFunction(const FunctionNode* node) override {
    return VisitFunctionHelper(node, /*is_member_function=*/false);
  }

  void VisitUnaryOperator(const UnaryOperatorNode* node) override;
  void VisitNaryOperator(const NaryOperatorNode* node) override;

  // RETURNS:
  //   - An ScoreExpression instance able to evaluate the expression on success.
  //   - INVALID_ARGUMENT if the AST does not conform to supported expressions,
  //   such as type errors.
  //   - INTERNAL if there are inconsistencies.
  libtextclassifier3::StatusOr<std::unique_ptr<ScoreExpression>>
  Expression() && {
    if (has_pending_error()) {
      return pending_error_;
    }
    if (stack.size() != 1) {
      return absl_ports::InternalError(IcingStringUtil::StringPrintf(
          "Expect to get only one result from "
          "ScoringVisitor, but got %zu. There must be inconsistencies.",
          stack.size()));
    }
    return std::move(stack[0]);
  }

 private:
  // Visit function node. If is_member_function is true, a ThisExpression will
  // be added as the first function argument.
  void VisitFunctionHelper(const FunctionNode* node, bool is_member_function);

  bool has_pending_error() const { return !pending_error_.ok(); }

  std::unique_ptr<ScoreExpression> pop_stack() {
    std::unique_ptr<ScoreExpression> result = std::move(stack.back());
    stack.pop_back();
    return result;
  }

  double default_score_;
  const DocumentStore& document_store_;
  const SchemaStore& schema_store_;
  SectionWeights& section_weights_;
  Bm25fCalculator& bm25f_calculator_;
  // A non-null join_children_fetcher_ indicates scoring in a join.
  const JoinChildrenFetcher* join_children_fetcher_;  // Does not own.

  libtextclassifier3::Status pending_error_;
  std::vector<std::unique_ptr<ScoreExpression>> stack;
};

}  // namespace lib
}  // namespace icing

#endif  // ICING_SCORING_ADVANCED_SCORING_SCORING_VISITOR_H_