+ Added formattable benchmark for returns and adjusted to vaguely fuzzy-search

This commit is contained in:
Matthew Cech
2019-05-05 18:55:05 -07:00
parent 3a8306f1e6
commit 99667330df
10 changed files with 248 additions and 30 deletions
+5 -3
View File
@@ -1,8 +1,10 @@
package core.benchmark;
public abstract class BenchmarkCommand {
public BenchmarkCommand() { }
public abstract class BenchmarkCommand
{
public BenchmarkCommand()
{ }
// OVERRIDE ME
public abstract String OnRun(BenchmarkManager manager, BenchmarkInput input);
public abstract BenchmarkFormattable OnRun(BenchmarkManager manager, BenchmarkInput input);
}
@@ -0,0 +1,40 @@
package core.benchmark;
import dataStructures.KittyEmbed;
import dataStructures.Response;
// Only one isn't null!
public class BenchmarkFormattable
{
public final KittyEmbed resEmbed;
public final String resString;
// Default constructor is hidden and disabled.
// If somehow it is called, defaults to an empty string and no embed.
@SuppressWarnings("unused")
private BenchmarkFormattable()
{
this.resEmbed = null;
this.resString = "";
}
public BenchmarkFormattable(String res)
{
this.resEmbed = null;
this.resString = res;
}
public BenchmarkFormattable(KittyEmbed embed)
{
this.resEmbed = embed;
this.resString = null;
}
public void Call(Response res)
{
if(resEmbed == null)
res.Call(resString);
else
res.CallEmbed(resEmbed);
}
}
+2 -2
View File
@@ -22,7 +22,7 @@ public class BenchmarkFramework
}
// Runs a command if possible.
public String Run(String args)
public BenchmarkFormattable Run(String args)
{
BenchmarkInput input = new BenchmarkInput(args);
return ExecuteCommand(input.key, input);
@@ -39,7 +39,7 @@ public class BenchmarkFramework
}
// Executes a command with the specified name, and provides it with some extra input data.
private String ExecuteCommand(String name, BenchmarkInput input)
private BenchmarkFormattable ExecuteCommand(String name, BenchmarkInput input)
{
BenchmarkCommand command = benchmarkCommand.get(name.toLowerCase());
+74
View File
@@ -2,8 +2,10 @@ package core.benchmark;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import dataStructures.Pair;
import utils.FileUtils;
import utils.directoryMonitor.DirectoryMonitor;
import utils.directoryMonitor.MonitoredFile;
@@ -67,6 +69,78 @@ public class BenchmarkManager
}
}
// Re-evaluates and re-orders the input list based on the Levenshtein distance of the
// original search and the contents of the provided list of benchmark entries.
public List<BenchmarkEntry> EvaluateLevenshteinDistance(List<BenchmarkEntry> entries, String search)
{
// Populate cost list with heuristic results
List<Pair<BenchmarkEntry, Integer>> cost = new ArrayList<Pair<BenchmarkEntry, Integer>>();
int bestSoFar = Integer.MAX_VALUE;
for(int i = 0; i < entries.size(); ++i)
{
BenchmarkEntry entry = entries.get(i);
String entryString = entry.brand + " " + entry.model;
int heuristic = LevenshteinHeuristic(entryString, search, bestSoFar);
if(heuristic < bestSoFar)
bestSoFar = heuristic;
cost.add(new Pair<BenchmarkEntry, Integer>(entry, heuristic));
}
// Sort list
Collections.sort(cost, (i1, i2) -> i1.Second.compareTo(i2.Second));
// Build new output list
List<BenchmarkEntry> sortedEntries = new ArrayList<BenchmarkEntry>();
for(int i = 0; i < cost.size(); ++i)
sortedEntries.add(cost.get(i).First);
return sortedEntries;
}
// Finds the minimum integer in an array of ints and returns it.
private int min(int[] arr)
{
int min = Integer.MAX_VALUE;
for(int i = 0; i < arr.length; ++i)
{
if(arr[i] < min)
min = arr[i];
}
return min;
}
// Returns cost based on distance of characters from string. This is a kinda sloppy way
// to do it, but because I keep tabs on the best result so far, it could be much worse.
// Still chucks a lot - a non-recursive result w/ memoization would be best but this will do.
private int LevenshteinHeuristic(String str1, String str2, int bestSoFar)
{
int cost;
if(str1.length() <= 0)
return str2.length();
if(str2.length() <= 0)
return str1.length();
if(str1.charAt(0) == str2.charAt(0))
cost = 0;
else
cost = 1;
int distance = Math.abs(str1.length() - str2.length());
if(distance > bestSoFar)
return distance;
int s1 = LevenshteinHeuristic(str1.substring(1), str2, bestSoFar) + 1;
int s2 = LevenshteinHeuristic(str1, str2.substring(1), bestSoFar) + 1;
int s3 = LevenshteinHeuristic(str1.substring(1), str2.substring(1), bestSoFar) + cost;
return min(new int[] {s1, s2, s3 });
}
// Search for a substring in the model name
public List<BenchmarkEntry> FindModel(String modelSubstr)
{