protected function buildResultIndex($result)
    {
        $corpus = [];

        foreach ($this->getContentFields() as $field => $weightings) {
            $textualContent = $result->{$field};

            // This is to handle taxonomies that need to be converted from an array
            // into a string of words.
            if (is_array($textualContent)) {
                $textualContent = implode(' ', $textualContent);
            }

            $textualContent = strip_tags($textualContent);
            $textualContent = preg_replace('/[^\w\s]/', '', $textualContent);
            $textualContent = mb_strtolower($textualContent);
            $corpus[$field] = $textualContent;
        }

        $dictionary = [];
        $count = [];

        foreach ($corpus as $id => $doc) {
            $terms = explode(' ', $doc);
            $count[$id] = count($terms);

            foreach ($terms as $term) {
                if (!isset($dictionary[$term])) {
                    $dictionary[$term] = ['frequency' => 0, 'postings' => []];
                }
                if (!isset($dictionary[$term]['postings'][$id])) {
                    ++$dictionary[$term]['frequency'];
                    $dictionary[$term]['postings'][$id] = ['frequency' => 0];
                }

                ++$dictionary[$term]['postings'][$id]['frequency'];
            }
        }

        return ['count' => $count, 'dictionary' => $dictionary];
    }
protected function buildResultIndex($result)
    {
        $corpus = [];

        foreach ($this->getContentFields() as $field => $weightings) {
            $textualContent = $result->{$field};

            // This is to handle taxonomies that need to be converted from an array
            // into a string of words.
            if (is_array($textualContent)) {
                $textualContent = implode(' ', $textualContent);
            }

            $textualContent = strip_tags($textualContent);
            $textualContent = preg_replace('/[^\w\s]/', '', $textualContent);
            $textualContent = mb_strtolower($textualContent);
            $corpus[$field] = $textualContent;
        }

        $dictionary = [];
        $count = [];

        foreach ($corpus as $id => $doc) {
            $terms = explode(' ', $doc);
            $count[$id] = count($terms);

            foreach ($terms as $term) {
                if (!isset($dictionary[$term])) {
                    $dictionary[$term] = ['frequency' => 0, 'postings' => []];
                }
                if (!isset($dictionary[$term]['postings'][$id])) {
                    ++$dictionary[$term]['frequency'];
                    $dictionary[$term]['postings'][$id] = ['frequency' => 0];
                }

                ++$dictionary[$term]['postings'][$id]['frequency'];
            }
        }

        return ['count' => $count, 'dictionary' => $dictionary];
    }
protected function buildResultIndex ( $ result ) { $ corpus = [ ] ; foreach ( $ this -> getContentFields ( ) as $ field => $ weightings ) { $ textualContent = $ result -> { $ field } ; // This is to handle taxonomies that need to be converted from an array // into a string of words. if ( is_array ( $ textualContent ) ) { $ textualContent = implode ( ' ' , $ textualContent ) ; } $ textualContent = strip_tags ( $ textualContent ) ; $ textualContent = preg_replace ( '/[^\w\s]/' , '' , $ textualContent ) ; $ textualContent = mb_strtolower ( $ textualContent ) ; $ corpus [ $ field ] = $ textualContent ; } $ dictionary = [ ] ; $ count = [ ] ; foreach ( $ corpus as $ id => $ doc ) { $ terms = explode ( ' ' , $ doc ) ; $ count [ $ id ] = count ( $ terms ) ; foreach ( $ terms as $ term ) { if ( ! isset ( $ dictionary [ $ term ] ) ) { $ dictionary [ $ term ] = [ 'frequency' => 0 , 'postings' => [ ] ] ; } if ( ! isset ( $ dictionary [ $ term ] [ 'postings' ] [ $ id ] ) ) { ++ $ dictionary [ $ term ] [ 'frequency' ] ; $ dictionary [ $ term ] [ 'postings' ] [ $ id ] = [ 'frequency' => 0 ] ; } ++ $ dictionary [ $ term ] [ 'postings' ] [ $ id ] [ 'frequency' ] ; } } return [ 'count' => $ count , 'dictionary' => $ dictionary ] ; }
This is a simple version of the Vector Space Model.

@see https://en.wikipedia.org/wiki/Vector_space_model

The goal is to determine a relavance score for a corpus of values
based on both the existence of a word or words but also based on
how important the words are.

For example, when querying results against a search of 'lorem ipsum';
a result with the title 'Lorem Ipsum' should score higher
than a result with the title 'An article about robots and lorem ipsum'

The ratio of the appearance of the query words to the overall size of
the document is used to produce a better score.

@param object $result A single result to score

@return array An array consisting of a count / dictionary of words
This is a simple version of the Vector Space Model .