-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improvement][chat] Add threshold judgment to field replacement (#1850)
- Loading branch information
Showing
9 changed files
with
132 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
common/src/main/java/com/tencent/supersonic/common/jsqlparser/EditDistanceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.tencent.supersonic.common.jsqlparser; | ||
|
||
public class EditDistanceUtils { | ||
|
||
public static double getSimilarity(String word1, String word2) { | ||
return 1 - (double) editDistance(word1, word2) / Math.max(word2.length(), word1.length()); | ||
} | ||
|
||
public static int editDistance(String word1, String word2) { | ||
final int m = word1.length(); | ||
final int n = word2.length(); | ||
int[][] dp = new int[m + 1][n + 1]; | ||
for (int j = 0; j <= n; ++j) { | ||
dp[0][j] = j; | ||
} | ||
for (int i = 0; i <= m; ++i) { | ||
dp[i][0] = i; | ||
} | ||
|
||
for (int i = 1; i <= m; ++i) { | ||
char ci = word1.charAt(i - 1); | ||
for (int j = 1; j <= n; ++j) { | ||
char cj = word2.charAt(j - 1); | ||
if (ci == cj) { | ||
dp[i][j] = dp[i - 1][j - 1]; | ||
} else if (i > 1 && j > 1 && ci == word2.charAt(j - 2) | ||
&& cj == word1.charAt(i - 2)) { | ||
dp[i][j] = 1 + Math.min(dp[i - 2][j - 2], Math.min(dp[i][j - 1], dp[i - 1][j])); | ||
} else { | ||
dp[i][j] = Math.min(dp[i - 1][j - 1] + 1, | ||
Math.min(dp[i][j - 1] + 1, dp[i - 1][j] + 1)); | ||
} | ||
} | ||
} | ||
return dp[m][n]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.