//*******************************************************************
// Welcome to CompileJava!
// If you experience any issues, please contact us ('More Info')  -->
// Also, sorry that the "Paste" feature no longer works! GitHub broke
// this (so we'll switch to a new provider): https://blog.github.com\
// /2018-02-18-deprecation-notice-removing-anonymous-gist-creation/
//*******************************************************************

package macampcorp.macamp.utils;

import java.lang.Math; // headers MUST be above the first class
import java.text.Normalizer;
import java.util.ArrayList;


// one class needs to have a main() method
public class Levenshtein
{
  
  	public static String normalize(String input)
    {
      	String normalized = Normalizer.normalize(input, Normalizer.Form.NFD);
		String accentRemoved = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
      	
      return accentRemoved.toLowerCase();
      
    }
                                                
    private static int minimum(int a, int b, int c) {                            
        return Math.min(Math.min(a, b), c);                                      
    }                                                                            
                                                                                 
    private static int computeLevenshteinDistance(CharSequence lhs, CharSequence rhs) {      
      	System.out.println(lhs+" "+rhs);
        int[][] distance = new int[lhs.length() + 1][rhs.length() + 1];        
                                                                                 
        for (int i = 0; i <= lhs.length(); i++)                                 
            distance[i][0] = i;                                                  
        for (int j = 1; j <= rhs.length(); j++)                                 
            distance[0][j] = j;                                                  
                                                                                 
        for (int i = 1; i <= lhs.length(); i++)                                 
            for (int j = 1; j <= rhs.length(); j++)                             
                distance[i][j] = minimum(                                        
                        distance[i - 1][j] + 1,                                  
                        distance[i][j - 1] + 1,                                  
                        distance[i - 1][j - 1] + ((lhs.charAt(i - 1) == rhs.charAt(j - 1)) ? 0 : 1));
                                                                                 
        return distance[lhs.length()][rhs.length()];                           
    }                                                                            

  public static int compute(String ref, String test)
  {
    return computeLevenshteinDistance(normalize(ref), normalize(test));
  }

  public static boolean test(String ref, String test)
  {
    int s = (ref.length()/3);
    if(s==0) s++;
    return compute(ref, test)<s;
  }
  
  public static boolean test(String ref, ArrayList<String> tests)
  {
    for(int i=0; i<tests.size(); i++)
      if(test(ref, tests.get(i))) return true;
     return false;
  }
  
  public static boolean test(String ref, String[] tests)
  {
    for(int i=0; i<tests.length; i++)
      if(test(ref, tests[i])) return true;
     return false;
  }
 
}

