Skip to content

Commit

Permalink
optimized levenshtein algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelo committed Jun 14, 2022
1 parent b883bf0 commit 4b32053
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 40 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog (unofficial)

## [1.2.3] - 2022-06-14
- Optimized LevenshteinDistance algorithm.

## [1.2.2] - 2022-06-07
- Fixed When press build button will detect have any data source exist, if not will return and alert.

Expand Down
Binary file modified Documentation/images/desc_img_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 5 additions & 39 deletions Editor/BundleBuildMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,17 @@ private BuildBundleInfo _GetBuildBundleInfoByAssetPath(string assetPath)
private BuildBundleInfo _GetBuildBundleInfoByBundleNameWithSimilarAssetPath(string bundleName, string tAssetPath)
{
var BuildBundleInfoArray = this._GetBuildBundleInfoArrayByBundleName(bundleName);
Dictionary<int, BuildBundleInfo> samples = new Dictionary<int, BuildBundleInfo>();
Dictionary<decimal, BuildBundleInfo> samples = new Dictionary<decimal, BuildBundleInfo>();
foreach (var buildBundleInfo in BuildBundleInfoArray)
{
samples.Add(LevenshteinDistance(buildBundleInfo.assetPath, tAssetPath), buildBundleInfo);
decimal pct = LevenshteinDistance.LevenshteinDistanceDecimal(buildBundleInfo.assetPath, tAssetPath);
if (!samples.ContainsKey(pct)) samples.Add(pct, buildBundleInfo);
}

if (samples.Count > 0)
{
int max = samples.Keys.Max();
decimal max = samples.Keys.Max();
Debug.Log($"<color=#FFE733>BundleName: {bundleName},</color> <color=#FF9933>Most Similar ({(max * 100).ToString("f2")}%) => Replace</color> <color=#33D6FF>old AssetPath: {samples[max].assetPath}</color> <color=#FF9933>to</color> <color=#A4FF33>new AssetPath:{tAssetPath}</color>");
return samples[max];
}

Expand Down Expand Up @@ -348,42 +350,6 @@ public BundleBuildMap[] GetCustomBuildMaps()
return this.customBuildMaps.ToArray();
}

/// <summary>
/// String similarity (levenshtein distance algorithm)
/// </summary>
/// <param name="s"></param>
/// <param name="t"></param>
/// <returns></returns>
internal static int LevenshteinDistance(string s, string t)
{
int n = s.Length;
int m = t.Length;
int[,] d = new int[n + 1, m + 1];
if (n == 0)
{
return m;
}
if (m == 0)
{
return n;
}
for (int i = 0; i <= n; d[i, 0] = i++)
;
for (int j = 0; j <= m; d[0, j] = j++)
;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
d[i, j] = Math.Min(
Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
d[i - 1, j - 1] + cost);
}
}
return d[n, m];
}

#region Implement ABDataSource interfaces
public string[] GetAssetPathsFromAssetBundle(string assetBundleName)
{
Expand Down
88 changes: 88 additions & 0 deletions Editor/LevenshteinDistance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;

public static class LevenshteinDistance
{
/// <summary>
/// Get smaller num
/// </summary>
/// <param name="first"></param>
/// <param name="second"></param>
/// <param name="third"></param>
/// <returns></returns>
private static int _LowerOfThree(int first, int second, int third)
{
int min = Math.Min(first, second);
return Math.Min(min, third);
}

/// <summary>
/// Caculate similar string return int [Levenshtein distance algorithm]
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public static int LevenshteinDistanceInt(string str1, string str2)
{
int[,] Matrix;
int n = str1.Length;
int m = str2.Length;

int temp, i, j;
char ch1;
char ch2;
if (n == 0)
{
return m;
}
if (m == 0)
{

return n;
}
Matrix = new int[n + 1, m + 1];

for (i = 0; i <= n; i++)
{
// init first row
Matrix[i, 0] = i;
}

for (j = 0; j <= m; j++)
{
// init first column
Matrix[0, j] = j;
}

for (i = 1; i <= n; i++)
{
ch1 = str1[i - 1];
for (j = 1; j <= m; j++)
{
ch2 = str2[j - 1];
if (ch1.Equals(ch2))
{
temp = 0;
}
else
{
temp = 1;
}
Matrix[i, j] = _LowerOfThree(Matrix[i - 1, j] + 1, Matrix[i, j - 1] + 1, Matrix[i - 1, j - 1] + temp);
}
}

return Matrix[n, m];
}

/// <summary>
/// Caculate similar string return decimal (Percent) [Levenshtein distance algorithm]
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public static decimal LevenshteinDistanceDecimal(string str1, string str2)
{
int val = LevenshteinDistanceInt(str1, str2);
return 1 - (decimal)val / Math.Max(str1.Length, str2.Length);
}
}
11 changes: 11 additions & 0 deletions Editor/LevenshteinDistance.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"name": "com.unity.assetbundlebrowser.plus",
"displayName": "Asset Bundle Browser Plus",
"version": "1.2.2",
"version": "1.2.3",
"unity": "2018.1",
"description": "The Asset Bundle Browser tool enables the user to view and edit the configuration of asset bundles for their Unity project. It will block editing that would create invalid bundles, and inform you of any issues with existing bundles. It also provides basic build functionality.\n\nUse this tool as an alternative to selecting assets and setting their asset bundle manually in the inspector. It can be dropped into any Unity project with a version of 5.6 or greater. It will create a new menu item in Window > AssetBundle Browser. The bundle configuration, build functionality, and built-bundle inspection are split into three tabs within the new window.",
"keywords": ["asset", "bundle", "bundles", "assetbundles"],
Expand Down

0 comments on commit 4b32053

Please sign in to comment.