PlotSquared/PlotSquared/src/main/java/com/intellectualcrafters/plot/StringComparsion.java

75 lines
2.0 KiB
Java
Raw Normal View History

2014-11-01 15:59:35 +01:00
package com.intellectualcrafters.plot;
import java.util.ArrayList;
import java.util.Collections;
/**
* String comparsion library
2014-11-05 04:42:08 +01:00
*
2014-11-01 15:59:35 +01:00
* @author Citymonstret
*/
public class StringComparsion {
private String bestMatch;
2014-11-06 22:04:41 +01:00
private double match = 0;
private Object bestMatchObject;
2014-11-01 15:59:35 +01:00
2014-11-05 04:42:08 +01:00
public StringComparsion(final String input, final Object[] objects) {
2014-11-01 15:59:35 +01:00
double c = 0;
2014-11-05 04:42:08 +01:00
for (final Object o : objects) {
if ((c = compare(input, o.toString())) > this.match) {
this.match = c;
this.bestMatch = o.toString();
this.bestMatchObject = o;
2014-11-01 15:59:35 +01:00
}
}
}
public Object getMatchObject() {
return this.bestMatchObject;
}
2014-11-01 15:59:35 +01:00
public String getBestMatch() {
return this.bestMatch;
}
public Object[] getBestMatchAdvanced() {
2014-11-05 04:42:08 +01:00
return new Object[] { this.match, this.bestMatch };
2014-11-01 15:59:35 +01:00
}
2014-11-05 04:42:08 +01:00
public static double compare(final String s1, final String s2) {
final ArrayList p1 = wLetterPair(s1.toUpperCase()), p2 = wLetterPair(s2.toUpperCase());
int intersection = 0;
final int union = p1.size() + p2.size();
for (final Object aP1 : p1) {
for (final Object aP2 : p2) {
if (aP1.equals(aP2)) {
2014-11-01 15:59:35 +01:00
intersection++;
p2.remove(aP2);
break;
}
}
}
return (2.0 * intersection) / union;
}
2014-11-05 04:42:08 +01:00
public static ArrayList wLetterPair(final String s) {
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;
}
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;
}
}