ge plugin: fix fuzzy search highlighting

This commit is contained in:
Dennis
2020-04-16 21:52:36 +02:00
committed by Adam
parent 9a9f40b862
commit 059b28989e
2 changed files with 51 additions and 4 deletions

View File

@@ -28,6 +28,7 @@
package net.runelite.client.plugins.grandexchange;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.primitives.Shorts;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
@@ -182,7 +183,8 @@ public class GrandExchangePlugin extends Plugin
/**
* Logic from {@link org.apache.commons.text.similarity.FuzzyScore}
*/
private static List<Integer> findFuzzyIndices(String term, String query)
@VisibleForTesting
static List<Integer> findFuzzyIndices(String term, String query)
{
List<Integer> indices = new ArrayList<>();
@@ -201,7 +203,9 @@ public class GrandExchangePlugin extends Plugin
{
final char queryChar = queryLowerCase.charAt(queryIndex);
for (; termIndex < termLowerCase.length(); termIndex++)
boolean termCharacterMatchFound = false;
for (; termIndex < termLowerCase.length()
&& !termCharacterMatchFound; termIndex++)
{
final char termChar = termLowerCase.charAt(termIndex);
@@ -211,7 +215,7 @@ public class GrandExchangePlugin extends Plugin
// we can leave the nested loop. Every character in the
// query can match at most one character in the term.
break;
termCharacterMatchFound = true;
}
}
}
@@ -548,10 +552,11 @@ public class GrandExchangePlugin extends Plugin
StringBuilder newItemName = new StringBuilder(itemName);
for (int index : indices)
{
if (wasFuzzySearch && (itemName.charAt(index) == ' ' || itemName.charAt(index) == '-'))
if (itemName.charAt(index) == ' ' || itemName.charAt(index) == '-')
{
continue;
}
newItemName.insert(index + 1, "</u>");
newItemName.insert(index, underlineTag);
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2020, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.grandexchange;
import java.util.Arrays;
import java.util.List;
import static net.runelite.client.plugins.grandexchange.GrandExchangePlugin.findFuzzyIndices;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class GrandExchangePluginTest
{
@Test
public void testFindFuzzyIndices()
{
List<Integer> fuzzyIndices = findFuzzyIndices("Ancestral robe bottom", "obby");
// r<u>ob</u>e <u>b</u>ottom
assertEquals(Arrays.asList(11, 12, 15), fuzzyIndices);
}
}