working function for calculating local similarity