import net.pterodactylus.util.collection.Pagination;
import net.pterodactylus.util.filter.Filter;
import net.pterodactylus.util.filter.Filters;
+import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
Set<Hit<T>> hits = new HashSet<Hit<T>>();
for (T object : objects) {
String objectString = stringGenerator.generateString(object);
- int score = calculateScore(phrases, objectString);
+ double score = calculateScore(phrases, objectString);
hits.add(new Hit<T>(object, score));
}
return hits;
* The expression to search
* @return The score of the expression
*/
- private int calculateScore(List<Phrase> phrases, String expression) {
- int optionalHits = 0;
- int requiredHits = 0;
+ private double calculateScore(List<Phrase> phrases, String expression) {
+ double optionalHits = 0;
+ double requiredHits = 0;
int forbiddenHits = 0;
int requiredPhrases = 0;
for (Phrase phrase : phrases) {
}
int matches = 0;
int index = 0;
+ double score = 0;
while (index < expression.length()) {
int position = expression.toLowerCase().indexOf(phraseString, index);
if (position == -1) {
break;
}
+ score += Math.pow(1 - position / (double) expression.length(), 2);
index = position + phraseString.length();
++matches;
}
continue;
}
if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
- requiredHits += matches;
+ requiredHits += score;
}
if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
- optionalHits += matches;
+ optionalHits += score;
}
if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
forbiddenHits += matches;
@Override
public int compare(Hit<?> leftHit, Hit<?> rightHit) {
- return rightHit.getScore() - leftHit.getScore();
+ return (rightHit.getScore() < leftHit.getScore()) ? -1 : ((rightHit.getScore() > leftHit.getScore()) ? 1 : 0);
}
};
private final T object;
/** The score of the object. */
- private final int score;
+ private final double score;
/**
* Creates a new hit.
* @param score
* The score of the object
*/
- public Hit(T object, int score) {
+ public Hit(T object, double score) {
this.object = object;
this.score = score;
}
*
* @return The score of the object
*/
- public int getScore() {
+ public double getScore() {
return score;
}