18 std::vector<std::string> pass_through_args_) {
19 base_path = base_path_;
20 similarity = similarity_;
21 pass_through_args = std::move(pass_through_args_);
24 std::cerr <<
"SIMILARITY SHOULD BE GREATER OR EQUAL 0 TO USE DUPLICATION FINDER BY LLM EMBEDDINGS";
28std::string EmbeddingMethod::build_command(
const fs::path& source_dir)
const {
29 std::string command =
"python3 -W ignore ";
31 command +=
"/embedding/llm_detection.py";
32 command +=
" --source-dir ";
33 command += source_dir.string();
34 command +=
" --min-similarity ";
35 command += std::to_string(similarity);
37 for (
const auto& arg : pass_through_args) {
50bool EmbeddingMethod::parse_line(
const std::string& line,
DuplicationEntry& entry)
const {
52 if (tokens.size() != 3) {
57 double sim = std::stod(tokens[2]);
58 entry = {sim, tokens[0], tokens[1]};
59 }
catch (
const std::exception&) {
65std::vector<DuplicationEntry> EmbeddingMethod::read_results(FILE* pipe)
const {
66 std::vector<DuplicationEntry> pairs;
67 std::array<char, 4096> buffer{};
70 while (fgets(buffer.data(), buffer.size(), pipe) !=
nullptr) {
71 leftover += buffer.data();
74 while ((newline_pos = leftover.find(
'\n')) != std::string::npos) {
75 std::string line = leftover.substr(0, newline_pos);
76 leftover.erase(0, newline_pos + 1);
79 if (parse_line(line, entry) && std::get<0>(entry) >= similarity) {
80 pairs.push_back(entry);
85 sort(pairs.rbegin(), pairs.rend());
89void EmbeddingMethod::save_duplications(std::vector<DuplicationEntry>& file_duplication_pairs) {
92 auto fout = std::ofstream(output_file_path.string());
94 fout << file_duplication_pairs.size() <<
'\n';
95 for (
const auto& [similarity, path1, path2] : file_duplication_pairs) {
96 fout << path1 <<
' ' << path2 <<
' ';
97 fout << std::fixed << std::setprecision(2) << similarity <<
'\n';
110 fs::path relative(fd.
path);
111 std::string filename = fd.
function_name + relative.extension().string();
112 fs::path path = base / relative / filename;
119 std::string command = build_command(source_dir);
121 FILE* pipe = popen(command.c_str(),
"r");
123 std::cerr <<
"Error executing the LLM detection script.\n";
127 std::vector<DuplicationEntry> file_duplication_pairs = read_results(pipe);
133 save_duplications(file_duplication_pairs);
static Config & config()
Gets the singleton configuration instance.
void on_function(const FunctionData &fd) override
EmbeddingMethod(const fs::path &base_path_, double similarity_, std::vector< std::string > pass_through_args_={})
Constructs the LLM detector.
std::string function_name
Name of the function.
std::shared_ptr< T > get_feature() const
fs::path build_similarity_path() const
Builds similarity file path.
Configuration management interface.
LLM-embedding-based code duplication detection.
Provides access to function storage locations.
std::tuple< double, std::string, std::string > DuplicationEntry
tuple format of the detected duplicates (similarity_percentage, path1, path2)
std::vector< std::string > split_string(const std::string &s, char delimiter)
Splits a string by a delimiter into tokens.
void write_file(const fs::path &path, const std::string &content)
Writes content to a file at specified path.
fs::path source_feature_path
std::filesystem::path third_party_dir
Directory containing third-party dependencies.
Defines utility functions used across all files.