CoolFace
Apppublic

GIZ/Development-Project-Synergy-Finder

sourceHugging Facemitupdated 4mo agoView on Hugging Face
2likes
single_project_matching.py47 linesDownload Raw Back to functions
1import numpy as np
2from scipy.sparse import csr_matrix
3
4"""
5Function to find similar project for the single project matching
6
7Single Project Matching empowers you to choose an individual project using 
8either the project IATI ID or title, and then unveils the top x projects within a filter (filtered_df) that 
9bear the closest resemblance to your selected one (p_index).
10"""
11
12def find_similar(p_index, similarity_matrix, filtered_df, top_x):
13    """
14    p_index: index of selected project
15    similarity_matrix: matrix with similarities of all projects
16    filtered_df: df with filter applied
17    top_x: top x project which should be displayed
18    """
19
20    # convert npz sparse matrix into csr matrix
21    if not isinstance(similarity_matrix, csr_matrix):
22        similarity_matrix = csr_matrix(similarity_matrix)
23    
24    # filter out just projects from filtered_df
25    filtered_indices =  filtered_df.index.tolist()
26    filtered_column_sim_matrix = similarity_matrix[:, filtered_indices]
27
28    # create a mapping from new position to original indices
29    index_position_mapping = {position: index for position, index in enumerate(filtered_indices)}
30
31    # select just the row of th similarity matrix of the selected project index
32    project_row = filtered_column_sim_matrix.getrow(p_index).toarray().ravel()
33
34    # find top_x indices with the highest similarity scores in the row
35    sorted_indices = np.argsort(project_row)[-top_x:][::-1]
36    top_indices = [index_position_mapping[i] for i in sorted_indices]
37    top_values = project_row[sorted_indices]
38
39    # create result df with all top_x similar projects
40    result_df = filtered_df.loc[top_indices]
41    result_df['similarity'] = top_values
42
43    # filter out rows with similarity score less than 30
44    result_df = result_df[result_df['similarity'] > 0]
45
46    return result_df
47