ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
tfidf_method.cpp
Go to the documentation of this file.
6
7#include <iostream>
8
10
11TfidfMethod::TfidfMethod(const fs::path& base_path_, double similarity_) {
12 base_path = base_path_;
13 similarity = similarity_;
14}
15
16void TfidfMethod::execute_by_feature(const fs::path& folder_path, const std::string feature_name) {
17 fs::path output_parsed = FunctionStorage{}.build_similarity_path();
18 if (!feature_name.empty())
19 fs::path output_parsed = base_path / ("output_parsed" + feature_name + ".txt");
20
21 fs::path cwd = fs::current_path();
22 fs::current_path(folder_path);
23
24 std::string command_tool = "python3 -W ignore ";
25 command_tool += Config::config().third_party_dir.string();
26 command_tool += "/tfidf/duplicate_code_detection.py";
27 command_tool += " --project-root-dir ";
28 command_tool += folder_path.string();
29 command_tool += " -d ";
30 command_tool += folder_path.string();
31
32 fm::write(SAVING_MESSAGE);
33
34 FILE* pipe = popen(command_tool.c_str(), "r");
35 if (!pipe) {
36 std::cerr << "Error executing the tool.\n";
37 return;
38 }
39
40 Parser parser(output_parsed, similarity);
41 parser.exec_from_stream(pipe);
42
43 fs::current_path(cwd);
44 pclose(pipe);
45}
46
48 fs::path base = base_path / source_feature_path;
49
50 auto source = fd.get_feature<SourceFeature>();
51 if (!source)
52 return;
53
54 fs::path relative(fd.path);
55 std::string filename = fd.function_name + relative.extension().string();
56 fs::path path = base / relative / filename;
57 Utils::write_file(path, source->code + "\n");
58}
59
61 fs::path base = base_path / source_feature_path;
62
64}
static Config & config()
Gets the singleton configuration instance.
Definition: config.cpp:68
static void write(const std::string &template_str, const std::vector< T > &data, enum Format effective=Format::AUTO, RowColorFn color_fn=nullptr, std::ostream &out=std::cout)
std::string function_name
Name of the function.
std::string path
std::shared_ptr< T > get_feature() const
fs::path build_similarity_path() const
Builds similarity file path.
Parses and transforms duplication detection tool output.
Definition: parser.hpp:68
void exec_from_stream(FILE *pipe)
Main parsing execution method using stream.
Definition: parser.cpp:88
void execute() override
void on_function(const FunctionData &fd) override
TfidfMethod(const fs::path &base_path_, double similarity_)
Constructs the duplication finder tool.
void execute_by_feature(const fs::path &path, const std::string feature_name="")
Executes the full duplication analysis pipeline.
Configuration management interface.
Provides access to function storage locations.
void write_file(const fs::path &path, const std::string &content)
Writes content to a file at specified path.
Definition: utils.cpp:19
fs::path source_feature_path
std::filesystem::path third_party_dir
Directory containing third-party dependencies.
Definition: schema.hpp:17
Main duplication detection tool interface.