['cherche', 'recherche', 'cherchons', 'recherchons', 'besoin', 'possible', 'svp', 'merci'], // The English list of search_api_solr is short (no « i », « am », « my »…). 'en' => ['i', 'am', "i'm", 'we', 'my', 'me', 'looking', 'look', 'searching', 'search', 'need', 'want', 'possible', 'something', 'please', 'thanks'], ]; /** * Leading elisions of French, removed before the stop word check. */ private const ELISION = '/^(?:[cdjlmnst]|qu)[\'’]/u'; /** * Stop words by language, loaded once. * * @var array> */ private static array $stopWords = []; /** * Returns the words of a phrase that carry meaning, in order. * * @return string[] * Lowercased words; empty when the phrase has only stop words. */ public static function words(string $phrase, string $langcode): array { $skip = self::stopWords($langcode); $words = []; // Solr and Search API syntax between words is a separator (« bois:verre », // « bois/métal »); hyphens, apostrophes, and . , in numbers stay. $phrase = preg_replace('#[:;/\\\\^&|()\[\]{}<>"«»~*?!=+\#@$]+#u', ' ', $phrase) ?? $phrase; foreach (preg_split('/\s+/u', mb_strtolower($phrase), -1, PREG_SPLIT_NO_EMPTY) as $token) { // Edge punctuation, and leading + - ! that Search API reads as operators // (« -bois » excluded wood and returned every other node, H3). $token = trim($token, ",;:!?.()[]{}\"«»'’<>"); $token = ltrim($token, '+-!'); $word = preg_replace(self::ELISION, '', $token) ?? $token; // No letter or digit (« || », « & »): breaks the payload boost of Solr. if (!preg_match('/[\p{L}\p{N}]/u', $word) || isset($skip[$word]) || isset($skip[$token])) { continue; } $words[] = $word; } return $words; } /** * Stop words and request words of a language. * * @return array * Words as keys. */ private static function stopWords(string $langcode): array { if (!isset(self::$stopWords[$langcode])) { $list = self::REQUEST_WORDS[$langcode] ?? []; $storage = \Drupal::entityTypeManager()->getStorage('solr_field_type'); foreach (["text_{$langcode}_7_0_0", "text_{$langcode}_6_0_0"] as $id) { /** @var \Drupal\search_api_solr\SolrFieldTypeInterface|null $type */ if ($type = $storage->load($id)) { $text = $type->getTextFiles()['stopwords'] ?? ''; foreach (preg_split('/\R/u', $text) as $line) { $line = trim(preg_replace('/#.*/u', '', $line) ?? ''); if ($line !== '') { $list[] = mb_strtolower($line); } } break; } } self::$stopWords[$langcode] = array_fill_keys($list, TRUE); } return self::$stopWords[$langcode]; } }