/** * Process one instance of a spelling replacement and modify the return * data structure with the details of what was done. * * @param string $term The actually term we're replacing * @param string $targetTerm The term above, or the token it is inside * @param bool $inToken Flag for whether the token or term is used * @param array $details The spelling suggestions * @param array $returnArray Return data structure so far * @param Params $params Params helper object * * @return array $returnArray modified */ protected function doSingleReplace($term, $targetTerm, $inToken, $details, $returnArray, Params $params) { $returnArray[$targetTerm]['freq'] = $details['freq']; foreach ($details['suggestions'] as $word => $freq) { // If the suggested word is part of a token if ($inToken) { // We need to make sure we replace the whole token $replacement = str_replace($term, $word, $targetTerm); } else { $replacement = $word; } // Do we need to show the whole, modified query? if ($this->phrase) { $label = $params->getDisplayQueryWithReplacedTerm($targetTerm, $replacement); } else { $label = $replacement; } // Basic spelling suggestion data $returnArray[$targetTerm]['suggestions'][$label] = ['freq' => $freq, 'new_term' => $replacement]; // Only generate expansions if enabled in config if ($this->expand) { // Parentheses differ for shingles if (strstr($targetTerm, " ") !== false) { $replacement = "(({$targetTerm}) OR ({$replacement}))"; } else { $replacement = "({$targetTerm} OR {$replacement})"; } $returnArray[$targetTerm]['suggestions'][$label]['expand_term'] = $replacement; } } return $returnArray; }