From 29ae7ad7e5f47a7020278173c1aa6d3b54021e81 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 1 Apr 2011 20:51:38 +0200 Subject: [PATCH] Weigh phrases by number of occurences. --- .../java/net/pterodactylus/sone/web/SearchPage.java | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index b10162e..2b854f3 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -191,21 +191,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); -- 2.7.4