Skip to content

Commit

Permalink
Update rag analyzer (#2232)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

* Add `RAGAnalyzer::GetBestTokens` function
 It is now only for test in Debug mode
 It differs with dfs in some cases
* Fix some existing bugs

Issue link:#2159

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)
- [x] Refactoring
- [x] Test cases
  • Loading branch information
yangzq50 authored Nov 15, 2024
1 parent 549a758 commit ba0ceb8
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 106 deletions.
30 changes: 18 additions & 12 deletions src/common/analyzer/darts_trie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,28 @@ void DartsTrie::Load(const String &file_name) { darts_->open(file_name.c_str());

void DartsTrie::Save(const String &file_name) { darts_->save(file_name.c_str()); }

bool DartsTrie::HasKeysWithPrefix(const String &key) {
std::size_t key_pos = 0;
DartsCore::value_type result = 0;
std::size_t id = 0;
for (std::size_t i = 0; i < key.length(); ++i) {
result = darts_->traverse(key.c_str(), id, key_pos, i + 1);
if (result == -2)
return false;
// string literal "" is null-terminated
constexpr std::string_view empty_null_terminated_sv = "";

bool DartsTrie::HasKeysWithPrefix(std::string_view key) const {
if (key.empty()) [[unlikely]] {
key = empty_null_terminated_sv;
}
std::size_t id = 0;
std::size_t key_pos = 0;
const auto result = darts_->traverse(key.data(), id, key_pos, key.size());
return result != -2;
}

int DartsTrie::Get(const String &key) {
DartsCore::value_type value;
darts_->exactMatchSearch(key.c_str(), value);
return value;
int DartsTrie::Traverse(const char *key, std::size_t &node_pos, std::size_t &key_pos, const std::size_t length) const {
return darts_->traverse(key, node_pos, key_pos, length);
}

int DartsTrie::Get(std::string_view key) const {
if (key.empty()) [[unlikely]] {
key = empty_null_terminated_sv;
}
return darts_->exactMatchSearch<DartsCore::value_type>(key.data(), key.size());
}

} // namespace infinity
6 changes: 4 additions & 2 deletions src/common/analyzer/darts_trie.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public:

void Save(const String &file_name);

bool HasKeysWithPrefix(const String &key);
bool HasKeysWithPrefix(std::string_view key) const;

int Get(const String &key);
int Traverse(const char *key, SizeT &node_pos, SizeT &key_pos, SizeT length) const;

int Get(std::string_view key) const;
};

} // namespace infinity
Loading

0 comments on commit ba0ceb8

Please sign in to comment.