X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FSearchPage.java;h=73673abed1e6ba0ce2579ff32c1091511b5f7af3;hb=9ee1da2306234248b6077946fc254d8436fb4bd1;hp=bd0354f1ff1c11ba463ddedcc9ccc3bd6c2d039f;hpb=c67605d6ee939160420d176cba9571f442c14978;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index bd0354f..73673ab 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -65,6 +65,9 @@ public class SearchPage extends SoneTemplatePage { // SONETEMPLATEPAGE METHODS // + /** + * {@inheritDoc} + */ @Override protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); @@ -76,7 +79,7 @@ public class SearchPage extends SoneTemplatePage { List phrases = parseSearchPhrases(query); Set sones = webInterface.getCore().getSones(); - Set> soneHits = getHits(sones, phrases, SoneStringGenerator.GENERATOR); + Set> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR); Set posts = new HashSet(); for (Sone sone : sones) { @@ -107,6 +110,17 @@ public class SearchPage extends SoneTemplatePage { templateContext.set("soneHits", sonePagination.getItems()); templateContext.set("postPagination", postPagination); templateContext.set("postHits", postPagination.getItems()); + + /* mark found posts and Sones as knew. */ + for (Sone sone : sonePagination.getItems()) { + webInterface.getCore().markSoneKnown(sone); + } + for (Post post : postPagination.getItems()) { + webInterface.getCore().markPostKnown(post); + for (Reply reply : webInterface.getCore().getReplies(post)) { + webInterface.getCore().markReplyKnown(reply); + } + } } // @@ -144,8 +158,8 @@ public class SearchPage extends SoneTemplatePage { * Parses the given query into search phrases. The query is split on * whitespace while allowing to group words using single or double quotes. * Isolated phrases starting with a “+” are - * {@link Phrase.Optionality#REQUIRED}, phrases with a - * “-” are {@link Phrase.Optionality#FORBIDDEN}. + * {@link Phrase.Optionality#REQUIRED}, phrases with a “-” are + * {@link Phrase.Optionality#FORBIDDEN}. * * @param query * The query to parse @@ -188,21 +202,31 @@ public class SearchPage extends SoneTemplatePage { int forbiddenHits = 0; int requiredPhrases = 0; for (Phrase phrase : phrases) { + String phraseString = phrase.getPhrase().toLowerCase(); if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) { ++requiredPhrases; } - boolean matches = expression.toLowerCase().contains(phrase.getPhrase().toLowerCase()); - if (!matches) { + int matches = 0; + int index = 0; + while (index < expression.length()) { + int position = expression.toLowerCase().indexOf(phraseString, index); + if (position == -1) { + break; + } + index = position + phraseString.length(); + ++matches; + } + if (matches == 0) { continue; } if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) { - ++requiredHits; + requiredHits += matches; } if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) { - ++optionalHits; + optionalHits += matches; } if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) { - ++forbiddenHits; + forbiddenHits += matches; } } return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2); @@ -236,8 +260,28 @@ public class SearchPage extends SoneTemplatePage { */ private static class SoneStringGenerator implements StringGenerator { - /** A static instance of the Sone string generator. */ - public static final SoneStringGenerator GENERATOR = new SoneStringGenerator(); + /** A static instance of a complete Sone string generator. */ + public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true); + + /** + * A static instance of a Sone string generator that will only use the + * name of the Sone. + */ + public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false); + + /** Whether to generate a string from all data of a Sone. */ + private final boolean complete; + + /** + * Creates a new Sone string generator. + * + * @param complete + * {@code true} to use the profile’s fields, {@code false} to + * not to use the profile‘s fields + */ + private SoneStringGenerator(boolean complete) { + this.complete = complete; + } /** * {@inheritDoc} @@ -256,8 +300,10 @@ public class SearchPage extends SoneTemplatePage { if (soneProfile.getLastName() != null) { soneString.append(' ').append(soneProfile.getLastName()); } - for (Field field : soneProfile.getFields()) { - soneString.append(' ').append(field.getValue()); + if (complete) { + for (Field field : soneProfile.getFields()) { + soneString.append(' ').append(field.getValue()); + } } return soneString.toString(); } @@ -267,8 +313,7 @@ public class SearchPage extends SoneTemplatePage { /** * Generates a {@link String} from a {@link Post}, concatenating the text of * the post, the text of all {@link Reply}s, and the name of all - * {@link Sone}s that have - * replied. + * {@link Sone}s that have replied. * * @author David ‘Bombe’ Roden */ @@ -282,10 +327,10 @@ public class SearchPage extends SoneTemplatePage { StringBuilder postString = new StringBuilder(); postString.append(post.getText()); if (post.getRecipient() != null) { - postString.append(' ').append(SoneStringGenerator.GENERATOR.generateString(post.getRecipient())); + postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient())); } for (Reply reply : webInterface.getCore().getReplies(post)) { - postString.append(' ').append(SoneStringGenerator.GENERATOR.generateString(reply.getSone())); + postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone())); postString.append(' ').append(reply.getText()); } return postString.toString(); @@ -437,6 +482,9 @@ public class SearchPage extends SoneTemplatePage { */ public static class HitConverter implements Converter, T> { + /** + * {@inheritDoc} + */ @Override public T convert(Hit input) { return input.getObject();