Java List First Item orderByFuzzyFirstOccurenceInText(String listToOrder[], String textToParse)

Here you can find the source of orderByFuzzyFirstOccurenceInText(String listToOrder[], String textToParse)

Description

This function sorts the Strings in the given list by their first occurrence in the given text.

License

Open Source License

Declaration

public static List<String> orderByFuzzyFirstOccurenceInText(String listToOrder[], String textToParse) 

Method Source Code

//package com.java2s;
/*/* w  w  w .jav a2s  . co m*/
 * Copyright 2010 Research Studios Austria Forschungsgesellschaft mBH
 *
 * This file is part of easyrec.
 *
 * easyrec is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * easyrec is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with easyrec.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    /**
     * This function sorts the Strings in the given list by their first
     * occurrence in the given text. The Strings in the list are tokenized
     * e.g. "red bull" is split in "red" and "bull" those tokens are
     * matched by their first occurrence in the text.
     */
    public static List<String> orderByFuzzyFirstOccurenceInText(String listToOrder[], String textToParse) {
        List<String> sortedList = new ArrayList<String>();
        HashMap<Integer, String> h = new HashMap<Integer, String>();

        if (listToOrder != null && !textToParse.equals("")) {
            for (String stringTokens : listToOrder) {

                String[] tokens = stringTokens.split(" ");
                for (String token : tokens) {

                    if (textToParse.indexOf(token) > 0 && token.length() > 3) {

                        h.put(textToParse.indexOf(token), token);
                    }
                }
            }

            List<Integer> keys = new ArrayList<Integer>(h.keySet());
            Collections.sort(keys);

            for (Integer k : keys) {
                sortedList.add(h.get(k));
            }
        }
        return sortedList;
    }
}

Related

  1. mergeLists(List first, List second)
  2. mergePreserveOrder(List first, List second)
  3. moveItemKindInFirst(final List list)
  4. nullSafeAppend(List first, List second)
  5. orderByFirstOccurenceInText(String listToOrder[], String textToParse, boolean fuzzy)
  6. permutationsWithFirstElement(List listings)
  7. prepend(String first, List list)
  8. putFirstLast(List list)
  9. removeFirst(final List list)