solved Task 3

This commit is contained in:
schrom01 2022-11-28 22:10:09 +01:00
parent 2f8f8df07c
commit 388ac15fcf
1 changed files with 33 additions and 3 deletions

View File

@ -2,7 +2,6 @@ package ch.zhaw.ads;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -15,11 +14,26 @@ public class FuzzySearchServer implements CommandExecutor {
// each name only once (i.e. no doublettes allowed // each name only once (i.e. no doublettes allowed
public static void loadNames(String nameString) { public static void loadNames(String nameString) {
// TODO implement // 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 // add a single trigram to 'trigrams' index
public static void addToTrigrams(int nameIdx, String trig) { public static void addToTrigrams(int nameIdx, String trig) {
// TODO implement // TODO implement
List<Integer> 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 // works better for flipped and short names if " " added and lowercase
@ -31,6 +45,11 @@ public class FuzzySearchServer implements CommandExecutor {
public static List<String> trigramForName(String name) { public static List<String> trigramForName(String name) {
name = nomalize(name); name = nomalize(name);
// TODO implement // TODO implement
List<String> 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<String> names) { public static void constructTrigramIndex(List<String> names) {
@ -52,9 +71,20 @@ public class FuzzySearchServer implements CommandExecutor {
// if no trigram/name matches at all then return -1 // if no trigram/name matches at all then return -1
public static int findIdx(String name) { public static int findIdx(String name) {
counts.clear(); counts.clear();
int maxIdx = -1; final int[] maxIdx = {-1};
// TODO implement // TODO implement
return maxIdx; List<String> 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. // finde Namen gebe "" zurück wenn gefundener Name nicht grösser als verlangter score ist.
public static String find(String searchName, int scoreRequired) { public static String find(String searchName, int scoreRequired) {