2014-11-08 20:27:09 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// PlotSquared - A plot manager and world generator for the Bukkit API /
|
|
|
|
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
|
|
|
|
// /
|
|
|
|
// This program is free software; you can redistribute it and/or modify /
|
|
|
|
// it under the terms of the GNU General Public License as published by /
|
|
|
|
// the Free Software Foundation; either version 3 of the License, or /
|
|
|
|
// (at your option) any later version. /
|
|
|
|
// /
|
|
|
|
// This program is distributed in the hope that it will be useful, /
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
|
|
|
|
// GNU General Public License for more details. /
|
|
|
|
// /
|
|
|
|
// You should have received a copy of the GNU General Public License /
|
|
|
|
// along with this program; if not, write to the Free Software Foundation, /
|
|
|
|
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
|
|
|
|
// /
|
|
|
|
// You can contact us via: support@intellectualsites.com /
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-11-16 10:48:18 +01:00
|
|
|
package com.intellectualcrafters.plot.util;
|
2014-11-01 15:59:35 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2015-07-27 06:30:50 +02:00
|
|
|
import java.util.Collection;
|
2014-11-01 15:59:35 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
/**
|
2014-11-16 10:48:18 +01:00
|
|
|
* String comparison library
|
2014-11-05 04:42:08 +01:00
|
|
|
*
|
2014-11-01 15:59:35 +01:00
|
|
|
* @author Citymonstret
|
2015-07-13 19:48:22 +02:00
|
|
|
* @author Empire92
|
2014-11-01 15:59:35 +01:00
|
|
|
*/
|
2015-02-20 07:34:19 +01:00
|
|
|
@SuppressWarnings("unused")
|
2015-06-29 21:30:36 +02:00
|
|
|
public class StringComparison<T> {
|
2015-07-13 19:48:22 +02:00
|
|
|
|
2015-06-29 21:30:36 +02:00
|
|
|
private T bestMatch;
|
2015-05-02 15:58:52 +02:00
|
|
|
private double match = Integer.MAX_VALUE;
|
2015-06-29 21:30:36 +02:00
|
|
|
private T bestMatchObject;
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-16 10:48:18 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2014-12-18 03:15:11 +01:00
|
|
|
* @param input Input Base Value
|
|
|
|
* @param objects Objects to compare
|
2014-11-16 10:48:18 +01:00
|
|
|
*/
|
2015-06-29 21:30:36 +02:00
|
|
|
public StringComparison(String input, final T[] objects) {
|
|
|
|
init(input, objects);
|
|
|
|
}
|
|
|
|
|
2015-07-27 06:30:50 +02:00
|
|
|
public StringComparison(String input, final Collection<T> objects) {
|
|
|
|
init(input, (T[]) objects.toArray());
|
|
|
|
}
|
|
|
|
|
2015-06-29 21:30:36 +02:00
|
|
|
/**
|
|
|
|
* You should call init(...) when you are ready to get a String comparison value
|
|
|
|
*/
|
|
|
|
public StringComparison() {}
|
|
|
|
|
|
|
|
public void init(String input, final T[] objects) {
|
2015-05-02 15:58:52 +02:00
|
|
|
int c;
|
2015-06-29 21:30:36 +02:00
|
|
|
this.bestMatch = objects[0];
|
2014-11-10 11:54:36 +01:00
|
|
|
this.bestMatchObject = objects[0];
|
2015-05-02 15:58:52 +02:00
|
|
|
input = input.toLowerCase();
|
2015-06-29 21:30:36 +02:00
|
|
|
for (final T o : objects) {
|
2015-05-02 15:58:52 +02:00
|
|
|
if ((c = compare(input, o.toString().toLowerCase())) < this.match) {
|
2014-11-05 04:42:08 +01:00
|
|
|
this.match = c;
|
2015-06-29 21:30:36 +02:00
|
|
|
this.bestMatch = o;
|
2014-11-06 09:37:46 +01:00
|
|
|
this.bestMatchObject = o;
|
2014-11-01 15:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-16 10:48:18 +01:00
|
|
|
/**
|
|
|
|
* Compare two strings
|
|
|
|
*
|
2014-12-18 03:15:11 +01:00
|
|
|
* @param s1 String Base
|
|
|
|
* @param s2 Object
|
|
|
|
*
|
2014-11-16 10:48:18 +01:00
|
|
|
* @return match
|
|
|
|
*/
|
2015-05-02 15:58:52 +02:00
|
|
|
public static int compare(final String s1, final String s2) {
|
2015-07-26 20:38:08 +02:00
|
|
|
int distance = StringMan.getLevenshteinDistance(s1, s2);
|
2015-05-02 16:20:04 +02:00
|
|
|
if (s2.contains(s1)) {
|
|
|
|
distance -= (Math.min(s1.length(), s2.length()));
|
|
|
|
}
|
|
|
|
if (s2.startsWith(s1)) {
|
2015-05-02 15:58:52 +02:00
|
|
|
distance -= 4;
|
2014-11-01 15:59:35 +01:00
|
|
|
}
|
2015-05-02 15:58:52 +02:00
|
|
|
return distance;
|
2014-11-01 15:59:35 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-16 10:48:18 +01:00
|
|
|
/**
|
|
|
|
* Create an ArrayList containing pairs of letters
|
|
|
|
*
|
2014-12-18 03:15:11 +01:00
|
|
|
* @param s string to split
|
|
|
|
*
|
2014-11-16 10:48:18 +01:00
|
|
|
* @return ArrayList
|
|
|
|
*/
|
2015-06-29 21:30:36 +02:00
|
|
|
public static ArrayList<String> wLetterPair(final String s) {
|
2014-11-05 04:42:08 +01:00
|
|
|
final ArrayList<String> aPairs = new ArrayList<>();
|
|
|
|
final String[] wo = s.split("\\s");
|
|
|
|
for (final String aWo : wo) {
|
|
|
|
final String[] po = sLetterPair(aWo);
|
2014-11-01 15:59:35 +01:00
|
|
|
Collections.addAll(aPairs, po);
|
|
|
|
}
|
|
|
|
return aPairs;
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-16 10:48:18 +01:00
|
|
|
/**
|
|
|
|
* Get an array containing letter pairs
|
|
|
|
*
|
2014-12-18 03:15:11 +01:00
|
|
|
* @param s string to split
|
|
|
|
*
|
2014-11-16 10:48:18 +01:00
|
|
|
* @return Array
|
|
|
|
*/
|
2014-11-05 04:42:08 +01:00
|
|
|
public static String[] sLetterPair(final String s) {
|
|
|
|
final int numPair = s.length() - 1;
|
|
|
|
final String[] p = new String[numPair];
|
|
|
|
for (int i = 0; i < numPair; i++) {
|
2014-11-01 15:59:35 +01:00
|
|
|
p[i] = s.substring(i, i + 2);
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
2014-11-01 15:59:35 +01:00
|
|
|
return p;
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-16 10:48:18 +01:00
|
|
|
/**
|
|
|
|
* Get the object
|
|
|
|
*
|
|
|
|
* @return match object
|
|
|
|
*/
|
2015-06-29 21:30:36 +02:00
|
|
|
public T getMatchObject() {
|
2014-11-16 10:48:18 +01:00
|
|
|
return this.bestMatchObject;
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-16 10:48:18 +01:00
|
|
|
/**
|
|
|
|
* Get the best match value
|
|
|
|
*
|
|
|
|
* @return match value
|
|
|
|
*/
|
|
|
|
public String getBestMatch() {
|
2015-06-29 21:30:36 +02:00
|
|
|
return this.bestMatch.toString();
|
2014-11-16 10:48:18 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-16 10:48:18 +01:00
|
|
|
/**
|
|
|
|
* Will return both the match number, and the actual match string
|
|
|
|
*
|
|
|
|
* @return object[] containing: double, String
|
|
|
|
*/
|
2015-06-29 21:30:36 +02:00
|
|
|
public ComparisonResult getBestMatchAdvanced() {
|
|
|
|
return new ComparisonResult(this.match, this.bestMatch);
|
|
|
|
}
|
2015-07-13 19:48:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The comparison result
|
|
|
|
*/
|
2015-06-29 21:30:36 +02:00
|
|
|
public class ComparisonResult {
|
2015-07-13 19:48:22 +02:00
|
|
|
|
|
|
|
public final T best;
|
2015-06-29 21:30:36 +02:00
|
|
|
public final double match;
|
2015-07-13 19:48:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The constructor
|
|
|
|
* @param match Match value
|
|
|
|
* @param best Best Match
|
|
|
|
*/
|
|
|
|
public ComparisonResult(final double match, final T best) {
|
2015-06-29 21:30:36 +02:00
|
|
|
this.match = match;
|
|
|
|
this.best = best;
|
|
|
|
}
|
2014-11-16 10:48:18 +01:00
|
|
|
}
|
2014-11-01 15:59:35 +01:00
|
|
|
}
|