Partial matching GAE search API

后端 未结 6 853
日久生厌
日久生厌 2020-11-28 09:35

Using the GAE search API is it possible to search for a partial match?

I\'m trying to create autocomplete functionality where the term would be a partial word. eg.

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 09:58

    Jumping in very late here.

    But here is my well documented function that does tokenizing. The docstring should help you understand it well and use it. Good luck!!!

    def tokenize(string_to_tokenize, token_min_length=2):
      """Tokenizes a given string.
    
      Note: If a word in the string to tokenize is less then
      the minimum length of the token, then the word is added to the list
      of tokens and skipped from further processing.
      Avoids duplicate tokens by using a set to save the tokens.
      Example usage:
        tokens = tokenize('pack my box', 3)
    
      Args:
        string_to_tokenize: str, the string we need to tokenize.
        Example: 'pack my box'.
        min_length: int, the minimum length we want for a token.
        Example: 3.
    
      Returns:
        set, containng the tokenized strings. Example: set(['box', 'pac', 'my',
        'pack'])
      """
      tokens = set()
      token_min_length = token_min_length or 1
      for word in string_to_tokenize.split(' '):
        if len(word) <= token_min_length:
          tokens.add(word)
        else:
          for i in range(token_min_length, len(word) + 1):
            tokens.add(word[:i])
      return tokens
    

提交回复
热议问题