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