From 388ac15fcf8b45332b412812218f9091654307a1 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 28 Nov 2022 22:10:09 +0100 Subject: [PATCH] solved Task 3 --- src/ch/zhaw/ads/FuzzySearchServer.java | 36 +++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/ch/zhaw/ads/FuzzySearchServer.java b/src/ch/zhaw/ads/FuzzySearchServer.java index fa7dfa6..44547f5 100644 --- a/src/ch/zhaw/ads/FuzzySearchServer.java +++ b/src/ch/zhaw/ads/FuzzySearchServer.java @@ -2,7 +2,6 @@ package ch.zhaw.ads; import java.util.ArrayList; import java.util.HashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -15,11 +14,26 @@ public class FuzzySearchServer implements CommandExecutor { // each name only once (i.e. no doublettes allowed public static void loadNames(String nameString) { // TODO implement + String[] names = nameString.split("\n"); + for (String nameLine : names) { + String name = nameLine.split(";")[0]; + if(! FuzzySearchServer.names.contains(name)){ + FuzzySearchServer.names.add(name); + } + } } // add a single trigram to 'trigrams' index public static void addToTrigrams(int nameIdx, String trig) { // TODO implement + List actualList = trigrams.get(trig); + if(actualList == null){ + actualList = new ArrayList<>(); + actualList.add(nameIdx); + trigrams.put(trig, actualList); + } else { + actualList.add(nameIdx); + } } // works better for flipped and short names if " " added and lowercase @@ -31,6 +45,11 @@ public class FuzzySearchServer implements CommandExecutor { public static List trigramForName(String name) { name = nomalize(name); // TODO implement + List trigrams = new ArrayList<>(); + for(int i = 0; i < name.length() - 2; i++){ + trigrams.add(name.substring(i, i + 3)); + } + return trigrams; } public static void constructTrigramIndex(List names) { @@ -52,9 +71,20 @@ public class FuzzySearchServer implements CommandExecutor { // if no trigram/name matches at all then return -1 public static int findIdx(String name) { counts.clear(); - int maxIdx = -1; + final int[] maxIdx = {-1}; // TODO implement - return maxIdx; + List trigrams = trigramForName(name); + trigrams.forEach(trigram -> { + if(FuzzySearchServer.trigrams.get(trigram) != null){ + FuzzySearchServer.trigrams.get(trigram).forEach(nameIndex -> { + incCount(nameIndex); + if (maxIdx[0] == -1 || counts.get(nameIndex) > counts.get(maxIdx[0])) { + maxIdx[0] = nameIndex; + } + }); + } + }); + return maxIdx[0]; } // finde Namen gebe "" zurück wenn gefundener Name nicht grösser als verlangter score ist. public static String find(String searchName, int scoreRequired) {