Posted on

Italian plurals

A Relevanssi user asked about Italian plurals on the Relevanssi support forums. The best answer is to use the Relevanssi Snowball Stemmer; it supports Italian and can handle more than plurals.

You can add a function to handle just the plurals, though. Add this to your site:

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 );

This function checks each word, and if it ends with “ghe” or “che”, the ending is changed to “ga” or “ca”, respectively. Then if the word is over three letters long, a final “a”, “e”, “i”, or “o” is stripped. Now plural and singular forms are considered the same in the search.

Remember to rebuild the index after adding this function!

3 comments Italian plurals

  1. Hello,
    thank you for the function, but it is not that simple: for example
    “Anche” has two meanings
    1) “also”,
    2) “hip”, “Anca” singular, whose plural is “Anche”.
    So if you search for “anca” you will receive results that do not contain that word, but the very common particle “Anche”
    Mauro

    1. Yes, that’s a common problem with all of these stupid methods that do not understand language. Relevanssi of course does not understand anything about meanings, but neither do most search engines. The stemmer isn’t any better in this.

      That’s the price you pay: these methods increase the recall (number of results found) at the cost of precision (you also get more false positives). It’s up to you what you think is the more important direction.

Leave a Reply to Mauro Cancel reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *