-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### What problem does this PR solve? Add init scorer, BM25 is initially added TODO: Column length reader and writer has not been implemented. More rankers are required Issue link:#421 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
- Loading branch information
Showing
14 changed files
with
311 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
module; | ||
|
||
#include <cmath> | ||
|
||
module bm25_ranker; | ||
|
||
import stl; | ||
|
||
namespace infinity { | ||
|
||
constexpr float k1 = 1.2F; | ||
constexpr float b = 0.75F; | ||
|
||
BM25Ranker::BM25Ranker(u64 total_df) : total_df_(std::max(total_df, 1UL)) {} | ||
|
||
void BM25Ranker::AddTermParam(u64 tf, u64 df, double avg_column_len, u64 column_len) { | ||
float smooth_idf = std::log(1.0F + (total_df_ - df + 0.5F) / (df + 0.5F)); | ||
float smooth_tf = (k1 + 1.0F) * tf / (tf + k1 * (1.0F - b + b * column_len / avg_column_len)); | ||
score_ += smooth_idf * smooth_tf; | ||
} | ||
} // namespace infinity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
module; | ||
|
||
export module bm25_ranker; | ||
|
||
import stl; | ||
|
||
namespace infinity { | ||
export class BM25Ranker { | ||
public: | ||
BM25Ranker(u64 total_df); | ||
~BM25Ranker() = default; | ||
|
||
void AddTermParam(u64 tf, u64 df, double avg_column_len, u64 column_len); | ||
|
||
float GetScore() { return score_; } | ||
|
||
private: | ||
float score_{0}; | ||
i64 total_df_{0}; | ||
}; | ||
} // namespace infinity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
module; | ||
|
||
export module column_length_io; | ||
|
||
import stl; | ||
import index_defines; | ||
|
||
namespace infinity { | ||
export class ColumnLengthWriter {}; | ||
|
||
export class ColumnLengthReader { | ||
public: | ||
ColumnLengthReader() = default; | ||
~ColumnLengthReader() = default; | ||
|
||
u32 GetColumnLength(u64 column_id, docid_t doc_id) { return 0; } | ||
}; | ||
} // namespace infinity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
module; | ||
|
||
module match_data; | ||
|
||
import stl; | ||
import term_doc_iterator; | ||
import index_defines; | ||
import bm25_ranker; | ||
|
||
namespace infinity { | ||
|
||
Scorer::Scorer(u64 num_of_docs) : total_df_(num_of_docs) { column_length_reader_ = MakeUnique<ColumnLengthReader>(); } | ||
|
||
u32 Scorer::GetOrSetColumnIndex(u64 column_id) { | ||
if (column_index_map_.find(column_id) == column_index_map_.end()) { | ||
column_index_map_[column_id] = column_counter_; | ||
match_data_.term_columns_.resize(column_counter_ + 1); | ||
return column_counter_++; | ||
} else | ||
return column_index_map_[column_id]; | ||
} | ||
|
||
void Scorer::InitRanker(const Map<u64, double> &weight_map) { | ||
for (auto it : weight_map) { | ||
u32 column_index = GetOrSetColumnIndex(it.first); | ||
column_weights_.resize(column_index + 1); | ||
column_weights_[column_index] = it.second; | ||
column_ids_.resize(column_index + 1); | ||
column_ids_[column_index] = it.first; | ||
} | ||
for (u32 i = 0; i < column_counter_; ++i) { | ||
avg_column_length_[i] = GetAvgColumnLength(column_ids_[i]); | ||
} | ||
} | ||
|
||
double Scorer::GetAvgColumnLength(u64 column_id) { | ||
double length = 0.0F; | ||
// TODO | ||
return length; | ||
} | ||
|
||
void Scorer::AddDocIterator(TermDocIterator *iter, u64 column_id) { | ||
u32 column_index = GetOrSetColumnIndex(column_id); | ||
iterators_.resize(column_index + 1); | ||
iterators_[column_index].push_back(iter); | ||
} | ||
|
||
float Scorer::Score(docid_t doc_id) { | ||
float score = 0.0F; | ||
for (u32 i = 0; i < column_counter_; i++) { | ||
BM25Ranker ranker(total_df_); | ||
u32 column_len = column_length_reader_->GetColumnLength(column_ids_[i], doc_id); | ||
Vector<TermDocIterator *> &column_iters = iterators_[i]; | ||
TermColumnMatchData &column_match_data = match_data_.term_columns_[i]; | ||
for (u32 j = 0; j < column_iters.size(); j++) { | ||
if (column_iters[j]->GetTermMatchData(column_match_data, doc_id)) { | ||
ranker.AddTermParam(column_match_data.tf_, column_iters[j]->GetDF(), avg_column_length_[i], column_len); | ||
} | ||
} | ||
auto s = ranker.GetScore(); | ||
score += column_weights_[i] * s; | ||
} | ||
return score; | ||
} | ||
|
||
} // namespace infinity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
module; | ||
|
||
export module match_data; | ||
|
||
import stl; | ||
import third_party; | ||
import index_defines; | ||
import column_length_io; | ||
|
||
namespace infinity { | ||
export struct TermColumnMatchData { | ||
docid_t doc_id_; | ||
tf_t tf_; | ||
docpayload_t doc_payload_; | ||
}; | ||
|
||
export struct MatchData { | ||
Vector<TermColumnMatchData> term_columns_; | ||
|
||
TermColumnMatchData *ResolveTermColumn(u32 column_sequence) { return &term_columns_[column_sequence]; } | ||
}; | ||
|
||
class TermDocIterator; | ||
export class Scorer { | ||
public: | ||
Scorer(u64 num_of_docs); | ||
|
||
~Scorer() = default; | ||
|
||
void InitRanker(const Map<u64, double> &weight_map); | ||
|
||
void AddDocIterator(TermDocIterator *iter, u64 column_id); | ||
|
||
float Score(docid_t doc_id); | ||
|
||
private: | ||
u32 GetOrSetColumnIndex(u64 column_id); | ||
|
||
double GetAvgColumnLength(u64 column_id); | ||
|
||
struct Hash { | ||
inline u64 operator()(const u64 &val) const { return val; } | ||
}; | ||
|
||
u64 total_df_; | ||
u32 column_counter_{0}; | ||
FlatHashMap<u64, u32, Hash> column_index_map_; | ||
Vector<u64> column_ids_; | ||
Vector<Vector<TermDocIterator *>> iterators_; | ||
Vector<double> column_weights_; | ||
Vector<double> avg_column_length_; | ||
UniquePtr<ColumnLengthReader> column_length_reader_; | ||
MatchData match_data_; | ||
}; | ||
|
||
} // namespace infinity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.