Skip to main contentSkip to footer

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!

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Index custom field contents
Custom fields (also known as post meta or meta fields) are a major part of WordPress the default search won’t…
WP Download Manager

…these issues. When you upload the files to the Relevanssi attachment reading server, Relevanssi uses the get_attached_file() function to get the file name, but that does not work with WP Download Manager. Thus we need to use the relevanssi_get_attached_file filter hook to provide the file name and path for Relevanssi….…add-on adds an advanced search with the [wpdb_archive_filter] shortcode. That search does not use Relevanssi by default, but it can be modified to make use of Relevanssi with this little function you can add to your site: add_filter( ‘wpdm_packages_query_params’, ‘rlv_use_relevanssi’ ); function rlv_use_relevanssi( $params ) { if ( isset( $params[‘s’]…

Related Posts:

Comment Section:

3 Comments. Leave new

  • Thank you Mikko
    You are right

    Reply
  • 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

    Reply
    • 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.

      Reply

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed