Java List First Item orderByFirstOccurenceInText(String listToOrder[], String textToParse, boolean fuzzy)

Here you can find the source of orderByFirstOccurenceInText(String listToOrder[], String textToParse, boolean fuzzy)

Description

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

License

Open Source License

Parameter

Parameter Description
listToOrder e.g. FORMULA_1_DRIVERS = {"Lewis Hamilton","Heikki Kovalainen","Felipe Massa"}
textToParse e.g. "Felipe Masse is on fire. His is ahead of Lewis Hamilton"

Return

{"Felipe Massa","Lewis Hamilton"}

Declaration

@SuppressWarnings({ "UnusedDeclaration" })
public static List<String> orderByFirstOccurenceInText(String listToOrder[], String textToParse,
        boolean fuzzy) 

Method Source Code

//package com.java2s;
/*//from w  ww.  j a  v a 2s .c  o 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 occurence in the given text.
     *
     * @param listToOrder e.g. FORMULA_1_DRIVERS = {"Lewis Hamilton","Heikki Kovalainen","Felipe Massa"}
     * @param textToParse e.g. "Felipe Masse is on fire. His is ahead of Lewis Hamilton"
     * @param fuzzy:      tokenize Strings and ordery by their occurence e.g. "Lewis Hamilton" --> {"Lewis","Hamilton"}
     * @return {"Felipe Massa","Lewis Hamilton"}
     */
    @SuppressWarnings({ "UnusedDeclaration" })
    public static List<String> orderByFirstOccurenceInText(String listToOrder[], String textToParse,
            boolean fuzzy) {

        List<String> sortedList = new ArrayList<String>();
        HashMap<Integer, String> h = new HashMap<Integer, String>();

        if (listToOrder != null && !textToParse.equals("")) {
            for (String s : listToOrder) {
                if (textToParse.indexOf(s) > 0) {
                    h.put(textToParse.indexOf(s), s);
                }
            }

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

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

        if (fuzzy) {
            sortedList.addAll(orderByFuzzyFirstOccurenceInText(listToOrder, textToParse));
        }
        return sortedList;
    }

    /**
     * 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. mergeLists(List first, List second)
  3. mergePreserveOrder(List first, List second)
  4. moveItemKindInFirst(final List list)
  5. nullSafeAppend(List first, List second)
  6. orderByFuzzyFirstOccurenceInText(String listToOrder[], String textToParse)
  7. permutationsWithFirstElement(List listings)
  8. prepend(String first, List list)
  9. putFirstLast(List list)