Support » Plugin: Relevanssi - A Better Search » exclude the last letter to allow the user to do a unique search for singular and

  • Hi,
    in the Italian language, the last letter of a word identifies the singular or plural. (example: MelA is singular; MelE is plural).

    I would like to know if possible, with your search plugin, to exclude the last letter to allow the user to do a unique search for singular and plural (eg “Mel” and the result is both MelA and MelE)

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Except that it’s not just the last letter; sometimes you have to consider the last two letters, i.e. “-ga” to “-ghe” and “-ca” to “-che”. But yes, you can do that. The best way to do this is to use the relevanssi_remove_punctuation filter hook.

    add_filter( 'relevanssi_remove_punctuation', function( $string ) {
      $end = mb_substr( $string, -3 );
      if ( 'ghe' === $end ) {
        $string = mb_substr( $string, 0, -3 ) . 'ga';
      }
      if ( 'che' === $end ) {
        $string = mb_substr( $string, 0, -3 ) . 'ca';
      }
      $end = mb_substr( $string, -1 );
      if ( mb_strlen( $end ) > 3 && in_array( $end, array( 'a', 'e', 'i', 'o' ) ) {
        $string = mb_substr( $string, 0, -1 );
      }
      return $string;
    }, 10, 9 );

    This first changes ‘ghe’ to ‘ga’ and ‘che’ to ‘ca’, then if the word is longer than three letters and there’s an ‘a’, ‘e’, ‘i’ or ‘o’ at the end, the final vowel is removed. So “Mela” and “Mele” become “Mel”, “Riga” and “Righe” become “Rig” and so on. This change is applied to both the search terms and the words in your index; you just need to rebuild the index after adding this function.

    With Relevanssi Premium, there’s an even better solution: you can use the Relevanssi Snowball Stemmer, which has a full Italian stemmer that can do more than just plurals.

    Thread Starter lisabaldrati

    (@lisabaldrati)

    Thanks fo your help 🙂

    Plugin Author Mikko Saari

    (@msaari)

    Sorry, actually, this does not work as expected. The relevanssi_remove_punctuation filter does not filter individual words but whole post content before it is split into words.

    So, in order for this to work, the function needs to be changed so that it looks at all the words in the string.

    Plugin Author Mikko Saari

    (@msaari)

    This version works:

    add_filter( 'relevanssi_remove_punctuation', function( $string ) {
    	$words     = explode( ' ', $string );
        $new_words = array();
    	foreach ( $words as $word ) {
    		$end = mb_substr( $word, -3 );
    		if ( 'ghe' === $end ) {
    			$word = mb_substr( $word, 0, -3 ) . 'ga';
    	    }
    	    if ( 'che' === $end ) {
    	        $word = mb_substr( $word, 0, -3 ) . 'ca';
    	    }
    		$end = mb_substr( $word, -1 );
    		if ( mb_strlen( $word ) > 3 && in_array( $end, array( 'a', 'e', 'i', 'o' ) ) ) {
    			$word = mb_substr( $word, 0, -1 );
    	    }
            $new_words[] = $word;
    	}
        return implode( ' ', $new_words );
    }, 10, 9 );
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.