使用求2个字符串最长公共子序列的方法来实现 git diff 算法 java 实现

GitDiffTest2.java:


import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class GitDiffTest2 {

    private static final String folder = "\\xxx\\";
    // private static final String folder = "\\xxx\\";

    private static final List lines_v1 = loadTxtFile2List(folder + "txt1.txt" );
    private static final List lines_v2 = loadTxtFile2List(folder + "txt2.txt");

    // 1 2 34567 89
    //      ||
    //      ||
    //      ||
    //    \\||//
    //     \\//
    //      \/
    // 1   34567 ab c

    public static void main(String[] args) {
        int size_v1 = lines_v1.size();
        int size_v2 = lines_v2.size();
        int[][] dp = new int[size_v1][size_v2];
        for (int index_v1 = 0; index_v1 < size_v1; index_v1++) {
            for (int index_v2 = 0; index_v2 < size_v2; index_v2++) {
                String line_v1 = lines_v1.get(index_v1);
                String line_v2 = lines_v2.get(index_v2);
                if ( line_v1.equals( line_v2 ) ) {
                    if( index_v1 == 0 || index_v2 == 0 ){
                        // ... a
                        //     a
                        dp[index_v1][index_v2] = 1;
                    }else {
                        // ... a
                        // ... a
                        dp[index_v1][index_v2] = dp[index_v1 - 1][index_v2 - 1] + 1;
                    }
                } else {
                    if( index_v1==0 || index_v2==0 ){
                        // ... a
                        //     b
                        dp[index_v1][index_v2] = 0;
                    }else {
                        // ... a     ... a    ...
                        // ... b     ...      ... b
                        dp[index_v1][index_v2] = Math.max(dp[index_v1 - 1][index_v2], dp[index_v1][index_v2 - 1]);
                    }
                }
            }
        }


        List result = new ArrayList<>();
        int index_v1 = lines_v1.size() - 1;
        int index_v2 = lines_v2.size() - 1;
        //            |
        // 1 2 34567 89
        // 1   34567 ab c
        //              |
        while (index_v1 > 0 && index_v2 > 0) {
            String line_v1 = lines_v1.get(index_v1);
            String line_v2 = lines_v2.get(index_v2);
            if ( line_v1.equals( line_v2 ) ) {
                // v1:..... a
                // v2:  ... a
                // 原封不动的输出行 a,因为 新旧版本文本中都有 a 行
                result.add( "  " + lines_v1.get( index_v1 ) );
                index_v1--;
                index_v2--;
            }else {
                // v1:..... a
                // v2:  ... b

                // v1:  ..... a
                // v2:  ... b      如果 lcs1 更长,则最长公共子串中不包含 行a,意味着 v2版本中需要删除v1版本中的行a
                int lcs1 = dp[index_v1 - 1][index_v2];

                // v1:..... a
                // v2:    ... b    如果 lcs2更长,则最长公共子串中不包含行b,意味着v2版本中需要新增 行b
                int lcs2 = dp[index_v1][index_v2-1];

                if ( lcs1 > lcs2 ) {
                    result.add( "- " + lines_v1.get( index_v1 ) );
                    index_v1--;
                } else {
                    result.add( "+ " + lines_v2.get( index_v2 ) );
                    index_v2--;
                }
            }
        }
        while (index_v2 > 0) {
            result.add("+ " + lines_v2.get(index_v2));
            index_v2--;
        }
        while (index_v1 > 0) {
            result.add("- " + lines_v1.get(index_v1));
            index_v1--;
        }
        for (int k = result.size() - 1; k >= 0; k--) {
            System.out.println(result.get(k));
        }
    }

    private static List loadTxtFile2List(String filePath) {
        BufferedReader reader = null;
        List lines = new ArrayList<>();
        try {
            reader = new BufferedReader(new FileReader(filePath));
            String line = reader.readLine();
            while (line != null) {
                lines.add( line );
                line = reader.readLine();
            }
            return lines;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

DemoClass1.java:

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.similarity.LevenshteinDistance;

@Slf4j
public class DemoClass1 {

    private static final LevenshteinDistance LEVENSHTEIN_DISTANCE = LevenshteinDistance.getDefaultInstance();

    public static String null2emptyWithTrim( String str ){
        if( str == null ){
            str = "";
        }
        str = str.trim();
        return str;
    }

    public static String requiredStringParamCheck(String param, String paramRemark) {
        param = null2emptyWithTrim( param );
        if( param.length() == 0 ){
            String msg = "操作失败,请求参数 \"" + paramRemark + "\" 为空";
            log.error( msg );
            throw new BusinessLogicException( msg );
        }
        return param;
    }

    public static double calculateSimilarity( String str1,String str2 ){
        int distance = LEVENSHTEIN_DISTANCE.apply(str1, str2);
        double similarity = 1 - (double) distance / Math.max(str1.length(), str2.length());
        System.out.println("相似度:" + similarity);
        return similarity;
    }
}
DemoClass2.java:

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.similarity.LevenshteinDistance;

@Slf4j
public class DemoClass2 {

    private static final LevenshteinDistance LEVENSHTEIN_DISTANCE = LevenshteinDistance.getDefaultInstance();
    private static final LevenshteinDistance LEVENSHTEIN_DISTANCE1 = LevenshteinDistance.getDefaultInstance();
    private static final LevenshteinDistance LEVENSHTEIN_DISTANCE2 = LevenshteinDistance.getDefaultInstance();

    public static String null2emptyWithTrim( String str ){
        // if( str == null ){
        //     str = "";
        // }
        // str = str.trim();
        return str;
    }

    public static String requiredStringParamCheck(String param, String paramRemark) {
        return null;
    }

    public static double calculateSimilarity( String str1,String str2 ){
        try {
            int distance = LEVENSHTEIN_DISTANCE.apply(str1, str2);
            double similarity = 1 - (double) distance / Math.max(str1.length(), str2.length());
            return similarity;
        }catch ( Exception e ){
            e.printStackTrace();
            return 0d;
        }
    }
}

测试输出:


  
  import com.goldwind.ipark.common.exception.BusinessLogicException;
  import lombok.extern.slf4j.Slf4j;
  import org.apache.commons.text.similarity.LevenshteinDistance;
  
  @Slf4j
+ public class DemoClass2 {
- public class DemoClass1 {
  
      private static final LevenshteinDistance LEVENSHTEIN_DISTANCE = LevenshteinDistance.getDefaultInstance();
+     private static final LevenshteinDistance LEVENSHTEIN_DISTANCE1 = LevenshteinDistance.getDefaultInstance();
+     private static final LevenshteinDistance LEVENSHTEIN_DISTANCE2 = LevenshteinDistance.getDefaultInstance();
  
      public static String null2emptyWithTrim( String str ){
+         // if( str == null ){
+         //     str = "";
+         // }
+         // str = str.trim();
-         if( str == null ){
-             str = "";
-         }
-         str = str.trim();
          return str;
      }
  
      public static String requiredStringParamCheck(String param, String paramRemark) {
+         return null;
-         param = null2emptyWithTrim( param );
-         if( param.length() == 0 ){
-             String msg = "操作失败,请求参数 \"" + paramRemark + "\" 为空";
-             log.error( msg );
-             throw new BusinessLogicException( msg );
-         }
-         return param;
      }
  
      public static double calculateSimilarity( String str1,String str2 ){
+         try {
+             int distance = LEVENSHTEIN_DISTANCE.apply(str1, str2);
+             double similarity = 1 - (double) distance / Math.max(str1.length(), str2.length());
+             return similarity;
+         }catch ( Exception e ){
+             e.printStackTrace();
+             return 0d;
+         }
-         int distance = LEVENSHTEIN_DISTANCE.apply(str1, str2);
-         double similarity = 1 - (double) distance / Math.max(str1.length(), str2.length());
-         System.out.println("相似度:" + similarity);
-         return similarity;
      }
  }

你可能感兴趣的:(动态规划,算法,最长公共子序列,动态规划,算法,java,最长公共子序列,数据结构)