Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
# Other
find/*.txt
find/data/*
multi-find/data/*
multifind/data/*
search/data/*
.vscode/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ replace
general
- [X] chunking
- [X] recursive find in directory
- [ ] parallelization
- [ ] parallelization
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions search/fuzzy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef FUZZY_H
#define FUZZY_H

#include <string>
#include <algorithm>
#include <cctype>

// Check if pattern is a fuzzy subsequence of str
inline bool isFuzzySubsequence(const std::string& pattern, const std::string& str) {
size_t j = 0;
for (size_t i = 0; i < str.size() && j < pattern.size(); ++i) {
if (tolower(str[i]) == tolower(pattern[j])) {
++j;
}
}
return j == pattern.size();
}

// Scoring: 100 down
inline int fuzzyScore(const std::string& pattern, const std::string& str) {
if (!isFuzzySubsequence(pattern, str)) return -1;

// Boost if match is closer to start or fewer gaps
int lenDiff = str.length() - pattern.length();
int score = 100 - lenDiff;
if (!str.empty() && tolower(str[0]) == tolower(pattern[0])) {
score += 10;
}
return score;
}

#endif
Binary file added search/search
Binary file not shown.
36 changes: 36 additions & 0 deletions search/search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <tuple>
#include "../helpers/fileHelper.h"
#include "fuzzy.h"

int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <fuzzy_query>\n";
return 1;
}

std::string query = argv[1];
auto files = getAllFiles(".");

std::vector<std::pair<std::string, int>> matches;

for (const auto& file : files) {
int score = fuzzyScore(query, file);
if (score >= 0) {
matches.emplace_back(file, score);
}
}

std::sort(matches.begin(), matches.end(), [](auto& a, auto& b) {
return a.second > b.second;
});

for (const auto& [file, score] : matches) {
std::cout << file << " (score: " << score << ")\n";
}

return 0;
}