Class BeCPGQueryHelper

java.lang.Object
fr.becpg.repo.helper.BeCPGQueryHelper

public class BeCPGQueryHelper extends Object

BeCPGQueryHelper class.

Version:
$Id: $Id
Author:
matthieu
  • Field Details

    • logger

      private static final org.apache.commons.logging.Log logger
      Constant logger
    • SUFFIX_ALL

      public static final String SUFFIX_ALL
      Constant SUFFIX_ALL="*"
      See Also:
    • SUFFIX_SPACE

      private static final String SUFFIX_SPACE
      Constant SUFFIX_SPACE=" "
      See Also:
    • SUFFIX_DOUBLE_QUOTE

      private static final String SUFFIX_DOUBLE_QUOTE
      Constant SUFFIX_DOUBLE_QUOTE="\""
      See Also:
    • SUFFIX_SIMPLE_QUOTE

      private static final String SUFFIX_SIMPLE_QUOTE
      Constant SUFFIX_SIMPLE_QUOTE="'"
      See Also:
    • NON_WORD_CHARACTERS

      private static final Pattern NON_WORD_CHARACTERS
      Anything but a letter or a digit separates two words of a search.
    • COMBINING_MARKS

      private static final Pattern COMBINING_MARKS
      Combining marks left by the Unicode decomposition of an accented letter.
    • HAN_CHARACTER

      private static final Pattern HAN_CHARACTER
      A Han ideograph is a word on its own, Chinese and Japanese being written without spaces.
    • SPACES

      private static final Pattern SPACES
      Spaces between the words of a separated text.
    • BLANKS

      private static final Pattern BLANKS
      Blanks typed between two expressions of a query, the no-break space included.
    • LIGATURES

      private static final Map<String,String> LIGATURES
      French ligatures, which Unicode decomposition does not split.
    • SURROUNDING_QUERY_SYNTAX

      private static final Pattern SURROUNDING_QUERY_SYNTAX
      Quotes around a typed query and its closing wildcard, which are search syntax, not part of the searched label.
    • RANK_EXACT

      private static final int RANK_EXACT
      Relevance ranks of a suggestion label, best first.
      See Also:
    • RANK_PREFIX

      private static final int RANK_PREFIX
      See Also:
    • RANK_WORD_PREFIX

      private static final int RANK_WORD_PREFIX
      See Also:
    • RANK_OTHER

      private static final int RANK_OTHER
      See Also:
  • Constructor Details

    • BeCPGQueryHelper

      private BeCPGQueryHelper()

      Constructor for BeCPGQueryHelper.

  • Method Details

    • isQueryMatch

      public static boolean isQueryMatch(String query, String entityName)
      Tells whether a name matches a free-text query. Every word of the query must start a distinct word of the name, whatever the order, the case, the accents or the ligatures. Punctuation separates words, and no word is an operator: "AND", "OR" or "NOT" are searched as words. Words joined by punctuation without a blank, such as "Rinvalid input: '&D'", "1=1" or "brief:marketing", form an expression: they must follow each other in the name, in the same order, the last one being possibly incomplete. A query made of punctuation only is searched as a plain substring.
      Parameters:
      query - the text typed by the user; blank or "*" matches everything
      entityName - the name to test
      Returns:
      true when the name matches the query
    • extractSearchExpressions

      private static List<List<SearchWord>> extractSearchExpressions(String query)
      Splits a query on its blanks into expressions, each expression holding the words its punctuation separates. A Han ideograph stays an expression of its own, and an expression made of punctuation only is dropped.
      Parameters:
      query - the typed query
      Returns:
      the expressions, possibly empty
    • extractSearchWords

      private static List<SearchWord> extractSearchWords(String text)
      Splits a text into the words the search compares: lower-cased, without accents nor ligatures, and cut on any character that is neither a letter nor a digit. No word is dropped, so that every typed word counts.
      Parameters:
      text - the text to split
      Returns:
      the words, possibly empty
    • separateHanCharacters

      private static String separateHanCharacters(String foldedText)
      Surrounds every Han ideograph with spaces, so that it becomes a word of its own.
      Parameters:
      foldedText - the folded text
      Returns:
      the text with its ideographs set apart
    • splitFoldedWords

      private static List<SearchWord> splitFoldedWords(String foldedText)
      Cuts a folded text on any character that is neither a letter nor a digit.
      Parameters:
      foldedText - the folded text
      Returns:
      the words, possibly empty
    • startsDistinctExpressions

      private static boolean startsDistinctExpressions(List<List<SearchWord>> queryExpressions, List<SearchWord> nameWords)
      Checks that each query expression is found on its own name words, so that a word typed twice must be found twice. Longer expressions pick their name words first, to keep a short prefix from taking the only word a longer one fits.
      Parameters:
      queryExpressions - the analysed query expressions
      nameWords - the analysed name words
      Returns:
      true when every query expression found its name words
    • countTypedCharacters

      private static int countTypedCharacters(List<SearchWord> expression)
      Counts the characters of the words of an expression, as typed.
      Parameters:
      expression - the expression
      Returns:
      its number of characters, punctuation excluded
    • findExpressionStart

      private static int findExpressionStart(List<SearchWord> expression, List<SearchWord> nameWords, boolean[] takenNameWords)
      Finds the first run of free name words matching an expression: every word but the last is the same word, the last one is started by the typed word.
      Parameters:
      expression - the typed expression
      nameWords - the name words
      takenNameWords - the name words already matched by another expression
      Returns:
      the index of the first name word of the run, or -1 when there is none
    • isExpressionAt

      private static boolean isExpressionAt(List<SearchWord> expression, List<SearchWord> nameWords, boolean[] takenNameWords, int start)
      Tells whether an expression matches the free name words starting at the given index.
      Parameters:
      expression - the typed expression
      nameWords - the name words
      takenNameWords - the name words already matched by another expression
      start - the index of the first name word
      Returns:
      true when every word of the expression matches its name word
    • foldText

      private static String foldText(String text)
      Lower-cases a text and strips its accents and ligatures, for a plain substring comparison.
      Parameters:
      text - the text to fold
      Returns:
      the folded text
    • unfoldLigatures

      private static String unfoldLigatures(String text)
      Writes the French ligatures as two letters, which Unicode decomposition leaves untouched.
      Parameters:
      text - the text to unfold
      Returns:
      the text with œ, Œ, æ and Æ spelled out
    • prepareQueryForSorting

      public static String prepareQueryForSorting(String query)

      prepareQueryForSorting.

      Parameters:
      query - a String object
      Returns:
      a String object
    • prepareQuery

      public static String prepareQuery(String query)

      prepareQuery.

      Parameters:
      query - a String object.
      Returns:
      a String object.
    • isAllQuery

      public static boolean isAllQuery(String query)

      isAllQuery.

      Parameters:
      query - a String object.
      Returns:
      a boolean.
    • sortByRelevance

      public static <T> List<T> sortByRelevance(String query, List<T> items, Function<T,String> labelOf)
      Sorts suggestions by relevance to the typed query: the label equal to the query first, then the labels starting with it, then the labels having a word starting with it, then the others. Case, accents and ligatures are ignored. The sort is stable, so suggestions of the same rank keep the order of the search engine.
      Type Parameters:
      T - the suggestion type
      Parameters:
      query - the text typed by the user
      items - the suggestions, in the order of the search engine; left untouched
      labelOf - reads the label of a suggestion, may return null
      Returns:
      the sorted suggestions, or items when there is nothing to sort
    • foldQuery

      private static String foldQuery(String query)
      Folds a typed query for a label comparison, without its surrounding quotes nor its closing wildcard.
      Parameters:
      query - the typed query, may be null
      Returns:
      the folded query, empty when nothing is left to compare
    • rankLabel

      private static int rankLabel(String foldedQuery, String label)
      Ranks a label against a folded query.
      Parameters:
      foldedQuery - the folded query, not empty
      label - the label, may be null
      Returns:
      the relevance rank, lower is better
    • hasWordStartingWith

      private static boolean hasWordStartingWith(String foldedLabel, String foldedQuery)
      Tells whether the query starts a word of the label other than the first one, a word starting after any character that is neither a letter nor a digit.
      Parameters:
      foldedLabel - the folded label
      foldedQuery - the folded query
      Returns:
      true when a word of the label starts with the query