خرید بک لینک

Vote count: 0

waing, long code, method names and variables in dutch, translated the Idea

What am I trying to create?

I'm trying to create a search algorithm for a website, the input can contain every single character. When only 1 company matches the input, only 1 company needs to be shown on the next page, for security and inside-information there's a maximum of 9 search results.

Summary of how it should work

  1. Is the input more than 3 characters after eachother?
  2. Retrieve all information from all companies (pasted together in DAO with ==%= delimiter
  3. Remove first part of the string (before delimiter) and convert into int ID (to prevent Array)
  4. Check if there are multiple words in the input --> if yes, split into array of words
  5. Check per word if the dataString contains this word
  6. If the word doesn't match and it contains a special character, delete the first one and retu
  7. repeat 5-6
  8. If the word doesn't contain a special character, delete the last character and retu
  9. repeat 5-8 --> min of 3 letters length
  10. Store every found word in a ZoekResultaat (SearchResult) Object --> into array
  11. Define the indexCijfer (used to define ranking) based on if the word is in the dataString in first 3 tries
  12. If a searchResult already is in the array which came from the same dataString, combine them (.addMatchendeText("string"))
  13. Convert ArrayList of Objects to LinkedHashMap and pass to jsp page

Where does it go wrong?

Theres a term that every company has, when searching for this term, the search results are perfect, 9 pieces of them.

But when searching with a input which only 1 or 2 companies contain, it only searches the first dataString and immediately tries to convertToHashMap while only 1 dataString was searched.

Code

I'll post everything from my Servlet, I expect the problem to be in the doPost and not in the other methods, but I might be wrong, I can't find it.

public class ZoekServlet extends HttpServlet {

RequestDispatcher rd = null;
HttpServletRequest request = null;
HttpServletResponse response = null;
String input = "";
String woord = "";
String data;
ArrayList<ZoekResultaat> alleResultaten;

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    input = req.getParameter("input").toLowerCase();
    request = req;
    response = resp;

    if (input == null || input.equals("")) {
        req.setAttribute("errorMessage", "Vul een geldige zoekterm in");
        forward("zoekResultaten.jsp");
    } else {
        // minimal of 3 following characters REGEX
        Patte p = Patte.compile(".*[A-Za-z0-9]{3}.*");
        Matcher m = p.matcher(input);

        if (m.find()) {
            // Retrieve all data (==%= delimited)
            List<String> alleInfo = BedrijfDAO.getDatabase().getDataBedrijven();

            alleResultaten = new ArrayList<ZoekResultaat>();

            for (String info : alleInfo) {
                int indexCijfer = 0;
                // make searchMachine Case Insensitive
                data = info.toLowerCase();
                String idString = "";
                int id = -1;
                // first part of info (dataString) is the id so convert that
                Patte patte = Patte.compile(".*?==%=(.*?)");
                Matcher matcher = patte.matcher(info);
                if (matcher.find()) {
                    idString = matcher.group(0).substring(0, matcher.group(0).length() - 4);
                    id = Integer.parseInt(idString);
                }

                // if input exists out of words
                if (input.contains(" ")) {
                    String[] woorden = input.split(" ");
                    for (String wrd : woorden) {
                        int count = 0;
                        woord = wrd;
                        while (woord.length() > 3) {
                            count++;
                            if (searchSingleWordData()) {
                                ZoekResultaat zoekResultaat = new ZoekResultaat(id, woord, indexCijfer);
                                if (isInArray(zoekResultaat)) {
                                    zoekResultaat = getUitArray(zoekResultaat.getBedrijfId());
                                    zoekResultaat.setIndexCijfer(zoekResultaat.getIndexCijfer() + 20);
                                    zoekResultaat.addMatchendeText(woord);
                                    alleResultaten.add(zoekResultaat);
                                    System.out.println("--------------------cIvar--------------added with double text");
                                    break;
                                } else {
                                    if (count < 3) {
                                        indexCijfer += 20;
                                    } else {
                                        indexCijfer += 10;
                                    }
                                    alleResultaten.add(new ZoekResultaat(id, woord, indexCijfer));
                                    System.out.println("--------------------cIvar--------------added");

                                    break;
                                }
                            }
                        }
                    }
                } else {
                    int count = 0;
                    // als de datastring precies de input bevat
                    while (input.length() > 3) {
                        if (searchData()) {
                            ZoekResultaat zoekResultaat = new ZoekResultaat(id, woord, indexCijfer);
                            if (isInArray(zoekResultaat)) {
                                zoekResultaat = getUitArray(zoekResultaat.getBedrijfId());
                                zoekResultaat.setIndexCijfer(zoekResultaat.getIndexCijfer() + 20);
                                zoekResultaat.addMatchendeText(input);
                                alleResultaten.add(zoekResultaat);
                                System.out.println("--------------------cIvar--------------added with double input");
                                break;
                            } else {
                                if (count < 3) {
                                    indexCijfer += 50;
                                } else {
                                    indexCijfer += 15;
                                }

                                alleResultaten.add(new ZoekResultaat(id, input, indexCijfer));
                                System.out.println("-----cIvar----------------------Added#2");

                                break;
                            }
                        }
                    }
                }
            }

            // convert array to LinkedHashMap
            LinkedHashMap<Integer, ZoekResultaat> resultaten = convertToHashMap(alleResultaten);
            // pass on to jsp page
            req.setAttribute("resultaten", resultaten);
            forward("zoekResultaten.jsp");

        } else {
            req.setAttribute("errorMessage", "De zoekterm moet minimaal bestaan uit 3 letters en/of cijfers");
            forward("zoekPagina.jsp");
        }

    }
}


public boolean searchData() {
    System.out.println("----------cIvar---------------------searchData called");
    System.out.println("----------cIvar---------------------Input: "+ input);
    System.out.println("----------cIvar---------------------Woord: "+ woord);
    System.out.println("----------cIvar---------------------Data: "+data);
    if (data.contains(input)) {
        retu true;
    } else {
        // Does it contain a special character?
        Patte p = Patte.compile("[^A-Za-z0-9\s]");
        Matcher m = p.matcher(input);
        if (m.find()) {
            // remove first special character
            input = input.replaceFirst("[^A-Za-z0-9\s]", "");

        }
        // remove last character
        else {
            input = input.substring(0, input.length() - 1);
        }
        retu false;

    }
}
public void forward(String path) throws ServletException, IOException {
    rd = request.getRequestDispatcher(path);
    rd.forward(request, response);
}

public LinkedHashMap<Integer, ZoekResultaat> convertToHashMap(ArrayList<ZoekResultaat> array2) {
    System.out.println("----------cIvar---------------------convertToHashMap called");
    LinkedHashMap<Integer, ZoekResultaat> lhashmap = new LinkedHashMap<Integer, ZoekResultaat>();
    //loop maximaal 9x
    for (int i = 1; i < 10; i++) {
        //highest indexCijfer in the array: array
        int hoogsteCijfer = 0;
        ZoekResultaat hoogsteObject = null;
        //if alleResultaten is nog empty
        if (!alleResultaten.isEmpty() && alleResultaten.size() >= 1) {
            //copy to another array (previously to prevent ConcurrentModificationException
            ArrayList<ZoekResultaat> array = new ArrayList<ZoekResultaat>(alleResultaten);
            //per zoekResultaat
            for (ZoekResultaat zoek : array) {
                //if 'zoek' has a higher indexCijfer
                if (zoek.getIndexCijfer() > hoogsteCijfer) {
                    hoogsteCijfer = zoek.getIndexCijfer();
                    hoogsteObject = zoek;
                }
            }
            //add to results (results = lhashmap)
            lhashmap.put(i, hoogsteObject);
            //remove from alleResultaten to prevent 10 the same results
            System.out.println("-----------------------cIvar----------hO: "+hoogsteObject);
            System.out.println("-----------------------cIvar----------hS: "+hoogsteCijfer);

            alleResultaten.remove(hoogsteObject);
        } else {

            System.out.println("-----------------------cIvar----------ELSE: ");
            break;
        }
    }
    retu lhashmap;
}

public boolean isInArray(ZoekResultaat zoek) {
    System.out.println("----------cIvar---------------------isInArray called");
    for (ZoekResultaat z : alleResultaten) {
        if (z.getBedrijfId() == zoek.getBedrijfId()) {
            retu true;
        }
    }
    retu false;
}

public ZoekResultaat getUitArray(int bedrijfId) {
    System.out.println("----------cIvar---------------------getUitArray called");
    for (ZoekResultaat z : alleResultaten) {
        if (z.getBedrijfId() == bedrijfId) {
            alleResultaten.remove(z);
            retu z;
        }
    }
    retu null;
}

public boolean searchSingleWordData() {
    System.out.println("----------cIvar---------------------searchSingleWord called");
    if (data.contains(woord)) {
        retu true;
    } else {
        // Contains special character?
        Patte p = Patte.compile("[^A-Za-z0-9\s]");
        Matcher m = p.matcher(woord);
        if (m.find()) {
            // remove first special character
            woord = woord.replaceFirst("[^A-Za-z0-9\s]", "");

        }
        // remove last character
        else {
            woord = woord.substring(0, woord.length() - 1);
        }
        retu false;

    }
}

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: سه شنبه 7 ارديبهشت 1395 ساعت: 3:28

صفحه بندی