crodas\TextRank\Summary::getSummary PHP Method

getSummary() public method

public getSummary ( $text )
    public function getSummary($text)
    {
        $sentences = $this->config->trigger('get_sentences', $text);
        $candidates = [];
        $x = microtime(true);
        foreach ($sentences as $id => $t) {
            try {
                $words = $this->config->trigger('get_words', $t);
                $words = $this->config->trigger('filter_keywords', $words);
                $words = $this->config->trigger('normalize_keywords', $words);
                $words = array_filter($words, function ($word) {
                    return !ctype_punct($word);
                });
                $candidates[$id] = $words;
            } catch (\Exception $e) {
            }
        }
        $pr = new SummaryPageRank();
        $sorted = $pr->sort($candidates);
        $keys = array_slice($sorted, 0, ceil(count($sorted) * 0.05), true);
        $txt = "";
        foreach (array_keys($keys) as $key) {
            $txt .= $sentences[$key] . "\n";
        }
        return $txt;
    }

Usage Example

コード例 #1
0
 /**
  * Extract relevant summary from text
  *
  * @param string|null $text
  * @param int|null    $limit
  * @param bool|true   $withoutStopWords
  *
  * @return string
  */
 public function summary($text = null, $limit = null, $withoutStopWords = true)
 {
     $config = new Config();
     if ($withoutStopWords) {
         $config->addListener(new Stopword());
     }
     $analyzer = new Summary($config);
     try {
         $summary = $analyzer->getSummary($this->cleanup($text));
         if ($summary && is_integer($limit)) {
             $summary = mb_strimwidth($summary, 0, $limit, "...");
         }
     } catch (\RuntimeException $e) {
         $summary = null;
     }
     return $summary;
 }
Summary