projects
/
Sone.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Order by name as default, store sort options in template differently.
[Sone.git]
/
src
/
main
/
java
/
net
/
pterodactylus
/
sone
/
web
/
SearchPage.java
diff --git
a/src/main/java/net/pterodactylus/sone/web/SearchPage.java
b/src/main/java/net/pterodactylus/sone/web/SearchPage.java
index
1d1aa36
..
6f5e001
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/SearchPage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/SearchPage.java
@@
-1,5
+1,5
@@
/*
/*
- * Sone -
Options
Page.java - Copyright © 2010 David Roden
+ * Sone -
Search
Page.java - Copyright © 2010 David Roden
*
* 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
*
* 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
@@
-24,17
+24,20
@@
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Profile.Field;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Profile.Field;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.util.collection.
Convert
er;
-import net.pterodactylus.util.collection.
Convert
ers;
+import net.pterodactylus.util.collection.
Mapp
er;
+import net.pterodactylus.util.collection.
Mapp
ers;
import net.pterodactylus.util.collection.Pagination;
import net.pterodactylus.util.filter.Filter;
import net.pterodactylus.util.filter.Filters;
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;
import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
@@
-49,6
+52,9
@@
import net.pterodactylus.util.text.TextException;
*/
public class SearchPage extends SoneTemplatePage {
*/
public class SearchPage extends SoneTemplatePage {
+ /** The logger. */
+ private static final Logger logger = Logging.getLogger(SearchPage.class);
+
/**
* Creates a new search page.
*
/**
* Creates a new search page.
*
@@
-99,8
+105,8
@@
public class SearchPage extends SoneTemplatePage {
Collections.sort(sortedPostHits, Hit.DESCENDING_COMPARATOR);
/* extract Sones and posts. */
Collections.sort(sortedPostHits, Hit.DESCENDING_COMPARATOR);
/* extract Sones and posts. */
- List<Sone> resultSones =
Converters.convertList(sortedSoneHits, new HitConvert
er<Sone>());
- List<Post> resultPosts =
Converters.convertList(sortedPostHits, new HitConvert
er<Post>());
+ List<Sone> resultSones =
Mappers.mappedList(sortedSoneHits, new HitMapp
er<Sone>());
+ List<Post> resultPosts =
Mappers.mappedList(sortedPostHits, new HitMapp
er<Post>());
/* pagination. */
Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0));
/* pagination. */
Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0));
@@
-137,7
+143,7
@@
public class SearchPage extends SoneTemplatePage {
Set<Hit<T>> hits = new HashSet<Hit<T>>();
for (T object : objects) {
String objectString = stringGenerator.generateString(object);
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;
hits.add(new Hit<T>(object, score));
}
return hits;
@@
-185,9
+191,10
@@
public class SearchPage extends SoneTemplatePage {
* The expression to search
* @return The score of the expression
*/
* 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) {
+ logger.log(Level.FINEST, "Calculating Score for “%s”…", expression);
+ double optionalHits = 0;
+ double requiredHits = 0;
int forbiddenHits = 0;
int requiredPhrases = 0;
for (Phrase phrase : phrases) {
int forbiddenHits = 0;
int requiredPhrases = 0;
for (Phrase phrase : phrases) {
@@
-197,22
+204,26
@@
public class SearchPage extends SoneTemplatePage {
}
int matches = 0;
int index = 0;
}
int matches = 0;
int index = 0;
+ double score = 0;
while (index < expression.length()) {
int position = expression.toLowerCase().indexOf(phraseString, index);
if (position == -1) {
break;
}
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();
index = position + phraseString.length();
+ logger.log(Level.FINEST, "Got hit at position %d.", position);
++matches;
}
++matches;
}
+ logger.log(Level.FINEST, "Score: %f", score);
if (matches == 0) {
continue;
}
if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
if (matches == 0) {
continue;
}
if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
- requiredHits +=
matches
;
+ requiredHits +=
score
;
}
if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
}
if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
- optionalHits +=
matches
;
+ optionalHits +=
score
;
}
if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
forbiddenHits += matches;
}
if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
forbiddenHits += matches;
@@
-418,7
+429,7
@@
public class SearchPage extends SoneTemplatePage {
@Override
public int compare(Hit<?> leftHit, Hit<?> rightHit) {
@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
);
}
};
}
};
@@
-427,7
+438,7
@@
public class SearchPage extends SoneTemplatePage {
private final T object;
/** The score of the object. */
private final T object;
/** The score of the object. */
- private final
int
score;
+ private final
double
score;
/**
* Creates a new hit.
/**
* Creates a new hit.
@@
-437,7
+448,7
@@
public class SearchPage extends SoneTemplatePage {
* @param score
* The score of the object
*/
* @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;
}
this.object = object;
this.score = score;
}
@@
-456,7
+467,7
@@
public class SearchPage extends SoneTemplatePage {
*
* @return The score of the object
*/
*
* @return The score of the object
*/
- public
int
getScore() {
+ public
double
getScore() {
return score;
}
return score;
}
@@
-469,13
+480,13
@@
public class SearchPage extends SoneTemplatePage {
* The type of the object to extract
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
* The type of the object to extract
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
- public static class Hit
Converter<T> implements Convert
er<Hit<T>, T> {
+ public static class Hit
Mapper<T> implements Mapp
er<Hit<T>, T> {
/**
* {@inheritDoc}
*/
@Override
/**
* {@inheritDoc}
*/
@Override
- public T
convert
(Hit<T> input) {
+ public T
map
(Hit<T> input) {
return input.getObject();
}
return input.getObject();
}