<commit_before><?php

namespace Boom\Page;

use Boom\Page\Page as Page;

use \Kohana as Kohana;
use \TextStatistics as TextStatistics;

class ReadabilityScore
{
    /**
	 *
	 * @var Page
	 */
    protected $page;

    public function __construct(Page $page)
    {
        $this->page = $page;
        $this->_loadDependencies();
    }

    protected function _getPageText()
    {
        $chunks = \ORM::factory('Chunk_Text')
            ->where('is_block', '=', true)
            ->where('page_vid', '=', $this->page->getCurrentVersion()->id)
            ->find_all();

        $text = "";
        foreach ($chunks as $chunk) {
            $text .= $chunk->text;
        }

        return $text;
    }

    public function getSmogScore()
    {
        $text = $this->_getPageText();

        if (strlen($text) > 100) {
            $stats = new TextStatistics();

            return $stats->smog_index($text);
        }
    }

    protected function _loadDependencies()
    {
        if ( ! class_exists('TextStatistics')) {
            require Kohana::find_file('vendor/text-statistics', 'TextStatistics');
        }
    }
}
<commit_msg>Updated Page\ReadabilityScore so that whole toolbar doesn't fail if TextStatistics isn't found<commit_after><?php

namespace Boom\Page;

use Boom\Page\Page;
use Kohana;
use DaveChild\TextStatistics as TS;

class ReadabilityScore
{
    /**
	 *
	 * @var Page
	 */
    protected $page;

    public function __construct(Page $page)
    {
        $this->page = $page;
    }

    protected function _getPageText()
    {
        $chunks = \ORM::factory('Chunk_Text')
            ->where('is_block', '=', true)
            ->where('page_vid', '=', $this->page->getCurrentVersion()->id)
            ->find_all();

        $text = "";
        foreach ($chunks as $chunk) {
            $text .= $chunk->text;
        }

        return $text;
    }

    public function getSmogScore()
    {
        if (class_exists('TextStatistics')) {
            $text = $this->_getPageText();

            if (strlen($text) > 100) {
                $stats = new TextStatistics();

                return $stats->smog_index($text);
            }
        }

        return 0;
    }
}

{'fraction_non_alphanumeric': 0.144874715261959, 'fraction_numerical': 0.0031890660592255125, 'mean_word_length': 1.955585464333782, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '395726', 'n_tokens_mistral': 687, 'n_tokens_neox': 652, 'n_words': 138}