3int Similarity_Table::find_id_path(
Path path){
4 if(path_id.find(path) == path_id.end()){
5 path_id[path] = paths.size();
7 vector<pair<int,double>> empty_vec;
8 similarity_graph.push_back(empty_vec);
13void Similarity_Table::read_comparation(ifstream &table_file){
14 string string_path1, string_path2;
16 table_file >> string_path1 >> string_path2 >> similarity;
18 int id1 = find_id_path(
Path(string_path1));
19 int id2 = find_id_path(
Path(string_path2));
25 similarity_graph[id1].push_back(make_pair(id2,similarity));
26 similarity_graph[id2].push_back(make_pair(id1,similarity));
27 similarity_table[make_pair(id1,id2)] = similarity;
30void Similarity_Table::read_file_table(ifstream &table_file){
31 int number_comparations;
32 table_file >> number_comparations;
33 for(
int i = 0; i < number_comparations; i++){
34 read_comparation(table_file);
38void Similarity_Table::init_similarity_table(){
39 std::ifstream table_file;
40 table_file.open(SIMILARITY_TABLE_FILE_NAME);
43 read_file_table(table_file);
49 similarity_threshold= _similarity_threshold;
50 init_similarity_table();
54 similarity_threshold = DEFAULT_SIMILARITY;
55 init_similarity_table();
59 similarity_threshold = new_similarity_threshold;
63 int id1 = find_id_path(path1);
64 int id2 = find_id_path(path2);
67 return MAXIMUM_SIMILARITY;
72 pair<int,int> aux = make_pair(id1,id2);
73 if(similarity_table.find(aux) != similarity_table.end()){
74 return similarity_table[aux];
76 return MINIMUM_SIMILARITY;
79bool Similarity_Table::is_above_threshold(
double similarity){
80 return similarity_threshold <= similarity + EPS_ERROR_MARGIN;
85 return is_above_threshold(similarity);
93 int id = find_id_path(reference);
95 for(
auto [
id,similarity] : similarity_graph[
id]){
96 if(is_above_threshold(similarity)){
97 ret.push_back(paths[
id]);
104 vector<tuple<double,Path,Path>> similar_path_pairs;
105 for(
auto [ ids, similarity] : similarity_table){
106 Path path1 = paths[ids.first];
107 Path path2 = paths[ids.second];
109 similar_path_pairs.push_back({similarity,path1,path2});
112 sort(similar_path_pairs.rbegin(),similar_path_pairs.rend());
113 return similar_path_pairs;
118 vector<pair<Path,Path>>
ret;
119 for(
auto [similarity,path1,path2] : similar_path_pairs){
120 ret.push_back({path1,path2});
125vector<tuple<int,Path,Path>> Similarity_Table::sort_pairs_by_line_number(vector<pair<Path,Path>> similar_path_pairs){
126 vector<tuple<int,Path,Path>> similar_path_pairs_with_number_of_lines;
127 for(
auto [path1,path2] : similar_path_pairs){
129 tuple<int,Path,Path> aux = {function.number_of_lines(),path1,path2};
130 similar_path_pairs_with_number_of_lines.push_back(aux);
133 similar_path_pairs_with_number_of_lines.begin(),
134 similar_path_pairs_with_number_of_lines.end(),
135 [&](tuple<int,Path,Path> pair1, tuple<int,Path,Path> pair2){
136 int number_lines1 = get<0>(pair1);
137 int number_lines2 = get<0>(pair2);
138 return number_lines1 > number_lines2;
141 return similar_path_pairs_with_number_of_lines;
147 vector<tuple<int,Path,Path>> similar_path_pairs_with_number_of_lines =
148 sort_pairs_by_line_number(similar_path_pairs);
150 vector<pair<Path,Path>>
ret;
151 for(
auto [line_number,path1,path2] : similar_path_pairs_with_number_of_lines){
152 ret.push_back({path1,path2});
Represents a code function with its content and metadata.
Path manipulation class for tool-specific directory structure.
vector< pair< Path, Path > > get_all_similar_path_pairs_sorted_by_line_number()
Gets all similar path pairs, sorted by line count.
void update_similarity(double new_similarity_threshold)
Updates similarity threshold.
vector< Path > get_path_list()
Gets list of all known paths.
vector< Path > get_similar_path_to_the_reference(Path reference)
Gets paths similar to reference path.
double is_similar(Path path1, Path path2)
Checks if two paths are similar.
vector< tuple< double, Path, Path > > get_all_path_pairs_and_similarity_sorted_by_similarity()
Gets all similar path pairs with scores, sorted.
Similarity_Table()
Constructs with default similarity threshold.
double get_similarity(Path path1, Path path2)
Gets similarity between two paths.
vector< pair< Path, Path > > get_all_similar_path_pairs_sorted_by_similarity()
Gets all similar path pairs, sorted by similarity.
void ensure_file_is_open(std::ifstream &file, string file_name)
Ensures that a file stream is successfully opened.
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Similarity relationships storage and analysis.