I18N_Arabic
[ class tree: I18N_Arabic ] [ index: I18N_Arabic ] [ all elements ]

Source for file AutoSummarize.php

Documentation is available at AutoSummarize.php

  1. <?php
  2. /**
  3.  * ----------------------------------------------------------------------
  4.  *  
  5.  * Copyright (c) 2006-2012 Khaled Al-Sham'aa.
  6.  *  
  7.  * http://www.ar-php.org
  8.  *  
  9.  * PHP Version 5
  10.  *  
  11.  * ----------------------------------------------------------------------
  12.  *  
  13.  * LICENSE
  14.  *
  15.  * This program is open source product; you can redistribute it and/or
  16.  * modify it under the terms of the GNU Lesser General Public License (LGPL)
  17.  * as published by the Free Software Foundation; either version 3
  18.  * of the License, or (at your option) any later version.
  19.  *  
  20.  * This program is distributed in the hope that it will be useful,
  21.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  * GNU Lesser General Public License for more details.
  24.  *  
  25.  * You should have received a copy of the GNU Lesser General Public License
  26.  * along with this program.  If not, see <http://www.gnu.org/licenses/lgpl.txt>.
  27.  *  
  28.  * ----------------------------------------------------------------------
  29.  *  
  30.  * Class Name: Arabic Auto Summarize Class
  31.  *  
  32.  * Filename: AutoSummarize.php
  33.  *  
  34.  * Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose: Automatic keyphrase extraction to provide a quick mini-summary
  37.  *          for a long Arabic document.
  38.  *           
  39.  * ----------------------------------------------------------------------
  40.  *  
  41.  * Arabic Auto Summarize
  42.  *
  43.  * This class identifies the key points in an Arabic document for you to share with
  44.  * others or quickly scan. The class determines key points by analyzing an Arabic
  45.  * document and assigning a score to each sentence. Sentences that contain words
  46.  * used frequently in the document are given a higher score. You can then choose a
  47.  * percentage of the highest-scoring sentences to display in the summary.
  48.  * "ArAutoSummarize" class works best on well-structured documents such as reports,
  49.  * articles, and scientific papers.
  50.  * 
  51.  * "ArAutoSummarize" class cuts wordy copy to the bone by counting words and ranking
  52.  * sentences. First, "ArAutoSummarize" class identifies the most common words in the
  53.  * document and assigns a "score" to each word--the more frequently a word is used,
  54.  * the higher the score.
  55.  * 
  56.  * Then, it "averages" each sentence by adding the scores of its words and dividing
  57.  * the sum by the number of words in the sentence--the higher the average, the
  58.  * higher the rank of the sentence. "ArAutoSummarize" class can summarize texts to
  59.  * specific number of sentences or percentage of the original copy.
  60.  * 
  61.  * We use statistical approach, with some attention apparently paid to:
  62.  * 
  63.  * - Location: leading sentences of paragraph, title, introduction, and conclusion.
  64.  * - Fixed phrases: in-text summaries.
  65.  * - Frequencies of words, phrases, proper names
  66.  * - Contextual material: query, title, headline, initial paragraph
  67.  * 
  68.  * The motivation for this class is the range of applications for key phrases:
  69.  * 
  70.  * - Mini-summary: Automatic key phrase extraction can provide a quick mini-summary
  71.  *   for a long document. For example, it could be a feature in a web sites; just
  72.  *   click the summarize button when browsing a long web page.
  73.  * 
  74.  * - Highlights: It can highlight key phrases in a long document, to facilitate
  75.  *   skimming the document.
  76.  * 
  77.  * - Author Assistance: Automatic key phrase extraction can help an author or editor
  78.  *   who wants to supply a list of key phrases for a document. For example, the
  79.  *   administrator of a web site might want to have a key phrase list at the top of
  80.  *   each web page. The automatically extracted phrases can be a starting point for
  81.  *   further manual refinement by the author or editor.
  82.  * 
  83.  * - Text Compression: On a device with limited display capacity or limited
  84.  *   bandwidth, key phrases can be a substitute for the full text. For example, an
  85.  *   email message could be reduced to a set of key phrases for display on a pager;
  86.  *   a web page could be reduced for display on a portable wireless web browser.
  87.  * 
  88.  * This list is not intended to be exhaustive, and there may be some overlap in
  89.  * the items.
  90.  *
  91.  * Example:
  92.  * <code>
  93.  * include('./I18N/Arabic.php');
  94.  * $obj = new I18N_Arabic('AutoSummarize');
  95.  * 
  96.  * $file = 'Examples/Articles/Ajax.txt';
  97.  * $r = 20;
  98.  * 
  99.  * // get contents of a file into a string
  100.  * $fhandle = fopen($file, "r");
  101.  * $c = fread($fhandle, filesize($file));
  102.  * fclose($fhandle);
  103.  * 
  104.  * $k = $obj->getMetaKeywords($c, $r);
  105.  * echo '<b><font color=#FFFF00>';
  106.  * echo 'Keywords:</font></b>';
  107.  * echo '<p dir="rtl" align="justify">';
  108.  * echo $k . '</p>';
  109.  * 
  110.  * $s = $obj->doRateSummarize($c, $r);
  111.  * echo '<b><font color=#FFFF00>';
  112.  * echo 'Summary:</font></b>';
  113.  * echo '<p dir="rtl" align="justify">';
  114.  * echo $s . '</p>';
  115.  * 
  116.  * echo '<b><font color=#FFFF00>';
  117.  * echo 'Full Text:</font></b>';
  118.  * echo '<p><a class=ar_link target=_blank ';
  119.  * echo 'href='.$file.'>Source File</a></p>';
  120.  * </code>
  121.  *             
  122.  * @category  I18N
  123.  * @package   I18N_Arabic
  124.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  125.  * @copyright 2006-2012 Khaled Al-Sham'aa
  126.  *    
  127.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  128.  * @link      http://www.ar-php.org
  129.  */
  130.  
  131. // New in PHP V5.3: Namespaces
  132. // namespace I18N\Arabic;
  133. // 
  134. // $obj = new I18N\Arabic\AutoSummarize();
  135. // 
  136. // use I18N\Arabic;
  137. // $obj = new Arabic\AutoSummarize();
  138. //
  139. // use I18N\Arabic\AutoSummarize as AutoSummarize;
  140. // $obj = new AutoSummarize();
  141.  
  142.  
  143. /**
  144.  * This PHP class do automatic keyphrase extraction to provide a quick
  145.  * mini-summary for a long Arabic document
  146.  *  
  147.  * @category  I18N
  148.  * @package   I18N_Arabic
  149.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  150.  * @copyright 2006-2012 Khaled Al-Sham'aa
  151.  *    
  152.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  153.  * @link      http://www.ar-php.org
  154.  */ 
  155. {
  156.     private $_normalizeAlef       array('أ','إ','آ');
  157.     private $_normalizeDiacritics array('َ','ً','ُ','ٌ','ِ','ٍ','ْ','ّ');
  158.  
  159.     private $_commonChars array('ة','ه','ي','ن','و','ت','ل','ا','س','م',
  160.                                    'e''t''a''o''i''n''s');
  161.  
  162.     private $_separators array('.',"\n",'،','؛','(','[','{',')',']','}',',',';');
  163.  
  164.     private $_commonWords    array();
  165.     private $_importantWords array();
  166.  
  167.     /**
  168.      * Loads initialize values
  169.      *
  170.      * @ignore
  171.      */         
  172.     public function __construct()
  173.     {
  174.         // This common words used in cleanCommon method
  175.         $words    file(dirname(__FILE__).'/data/ar-stopwords.txt');
  176.         $en_words file(dirname(__FILE__).'/data/en-stopwords.txt');
  177.  
  178.         $words array_merge($words$en_words);
  179.         $words array_map('trim'$words);
  180.         
  181.         $this->_commonWords $words;
  182.         
  183.         // This important words used in rankSentences method
  184.         $words file(dirname(__FILE__).'/data/important-words.txt');
  185.         $words array_map('trim'$words);
  186.  
  187.         $this->_importantWords $words;
  188.     }
  189.     
  190.     /**
  191.      * Load enhanced Arabic stop words list
  192.      * 
  193.      * @return void 
  194.      */         
  195.     public function loadExtra()
  196.     {
  197.         $extra_words file(dirname(__FILE__).'/data/ar-extra-stopwords.txt');
  198.         $extra_words array_map('trim'$extra_words);
  199.  
  200.         $this->_commonWords array_merge($this->_commonWords$extra_words);
  201.     }
  202.  
  203.     /**
  204.      * Core summarize function that implement required steps in the algorithm
  205.      *                        
  206.      * @param string  $str      Input Arabic document as a string
  207.      * @param string  $keywords List of keywords higlited by search process
  208.      * @param integer $int      Sentences value (see $mode effect also)
  209.      * @param string  $mode     Mode of sentences count [number|rate]
  210.      * @param string  $output   Output mode [summary|highlight]
  211.      * @param string  $style    Name of the CSS class you would like to apply
  212.      *                    
  213.      * @return string Output summary requested
  214.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  215.      */
  216.     protected function summarize($str$keywords$int$mode$output$style=null)
  217.     {
  218.         preg_match_all("/[^\.\n\،\؛\,\;](.+?)[\.\n\،\؛\,\;]/u"$str$sentences);
  219.         $_sentences $sentences[0];
  220.  
  221.         if ($mode == 'rate'{
  222.             $str            preg_replace("/\s{2,}/u"' '$str);
  223.             $totalChars     mb_strlen($str);
  224.             $totalSentences count($_sentences);
  225.  
  226.             $maxChars round($int $totalChars 100);
  227.             $int      round($int $totalSentences 100);
  228.         else {
  229.             $maxChars 99999;
  230.         }
  231.         
  232.         $summary '';
  233.  
  234.         $str           strip_tags($str);
  235.         $normalizedStr $this->doNormalize($str);
  236.         $cleanedStr    $this->cleanCommon($normalizedStr);
  237.         $stemStr       $this->draftStem($cleanedStr);
  238.         
  239.         preg_match_all("/[^\.\n\،\؛\,\;](.+?)[\.\n\،\؛\,\;]/u"$stemStr$sentences);
  240.         $_stemmedSentences $sentences[0];
  241.  
  242.         $wordRanks $this->rankWords($stemStr);
  243.         
  244.         if ($keywords{
  245.             $keywords $this->doNormalize($keywords);
  246.             $keywords $this->draftStem($keywords);
  247.             $words    explode(' '$keywords);
  248.             
  249.             foreach ($words as $word{
  250.                 $wordRanks[$word1000;
  251.             }
  252.         }
  253.         
  254.         $sentencesRanks $this->rankSentences($_sentences
  255.                                                $_stemmedSentences
  256.                                                $wordRanks);
  257.         
  258.         list($sentences$ranks$sentencesRanks;
  259.  
  260.         $minRank $this->minAcceptedRank($sentences$ranks$int$maxChars);
  261.  
  262.         $totalSentences count($ranks);
  263.         
  264.         for ($i 0$i $totalSentences$i++{
  265.             if ($sentencesRanks[1][$i>= $minRank{
  266.                 if ($output == 'summary'{
  267.                     $summary .= ' '.$sentencesRanks[0][$i];
  268.                 else {
  269.                     $summary .= '<span class="' $style .'">' 
  270.                                 $sentencesRanks[0][$i'</span>';
  271.                 }
  272.             else {
  273.                 if ($output == 'highlight'{
  274.                     $summary .= $sentencesRanks[0][$i];
  275.                 }
  276.             }
  277.         }
  278.         
  279.         if ($output == 'highlight'{
  280.             $summary str_replace("\n"'<br />'$summary);
  281.         }
  282.         
  283.         return $summary;
  284.     }
  285.           
  286.     /**
  287.      * Summarize input Arabic string (document content) into specific number of
  288.      * sentences in the output
  289.      *                        
  290.      * @param string  $str      Input Arabic document as a string
  291.      * @param integer $int      Number of sentences required in output summary
  292.      * @param string  $keywords List of keywords higlited by search process
  293.      *                    
  294.      * @return string Output summary requested
  295.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  296.      */
  297.     public function doSummarize($str$int$keywords)
  298.     {
  299.         $summary $this->summarize($str$keywords$int
  300.                                     'number''summary'$style);
  301.         
  302.         return $summary;
  303.     }
  304.     
  305.     /**
  306.      * Summarize percentage of the input Arabic string (document content) into output
  307.      *      
  308.      * @param string  $str      Input Arabic document as a string
  309.      * @param integer $rate     Rate of output summary sentence number as
  310.      *                           percentage of the input Arabic string
  311.      *                           (document content)
  312.      * @param string  $keywords List of keywords higlited by search process
  313.      *                    
  314.      * @return string Output summary requested
  315.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  316.      */
  317.     public function doRateSummarize($str$rate$keywords)
  318.     {
  319.         $summary $this->summarize($str$keywords$rate
  320.                                     'rate''summary'$style);
  321.         
  322.         return $summary;
  323.     }
  324.     
  325.     /**
  326.      * Highlight key sentences (summary) of the input string (document content)
  327.      * using CSS and send the result back as an output
  328.      *                             
  329.      * @param string  $str      Input Arabic document as a string
  330.      * @param integer $int      Number of key sentences required to be
  331.      *                           highlighted in the input string
  332.      *                           (document content)
  333.      * @param string  $keywords List of keywords higlited by search process
  334.      * @param string  $style    Name of the CSS class you would like to apply
  335.      *                    
  336.      * @return string Output highlighted key sentences summary (using CSS)
  337.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  338.      */
  339.     public function highlightSummary($str$int$keywords$style)
  340.     {
  341.         $summary $this->summarize($str$keywords$int
  342.                                     'number''highlight'$style);
  343.         
  344.         return $summary;
  345.     }
  346.     
  347.     /**
  348.      * Highlight key sentences (summary) as percentage of the input string
  349.      * (document content) using CSS and send the result back as an output.
  350.      *                    
  351.      * @param string  $str      Input Arabic document as a string
  352.      * @param integer $rate     Rate of highlighted key sentences summary
  353.      *                           number as percentage of the input Arabic
  354.      *                           string (document content)
  355.      * @param string  $keywords List of keywords higlited by search process
  356.      * @param string  $style    Name of the CSS class you would like to apply
  357.      *                    
  358.      * @return string Output highlighted key sentences summary (using CSS)
  359.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  360.      */
  361.     public function highlightRateSummary($str$rate$keywords$style)
  362.     {
  363.         $summary $this->summarize($str$keywords$rate
  364.                                     'rate''highlight'$style);
  365.         
  366.         return $summary;
  367.     }
  368.     
  369.     /**
  370.      * Extract keywords from a given Arabic string (document content)
  371.      *      
  372.      * @param string  $str Input Arabic document as a string
  373.      * @param integer $int Number of keywords required to be extracting
  374.      *                      from input string (document content)
  375.      *                    
  376.      * @return string List of the keywords extracting from input Arabic string
  377.      *                (document content)
  378.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  379.      */
  380.     public function getMetaKeywords($str$int)
  381.     {
  382.         $patterns     array();
  383.         $replacements array();
  384.         $metaKeywords '';
  385.         
  386.         array_push($patterns'/\.|\n|\،|\؛|\(|\[|\{|\)|\]|\}|\,|\;/u');
  387.         array_push($replacements' ');
  388.         $str preg_replace($patterns$replacements$str);
  389.         
  390.         $normalizedStr $this->doNormalize($str);
  391.         $cleanedStr    $this->cleanCommon($normalizedStr);
  392.         
  393.         $str preg_replace('/(\W)ال(\w{3,})/u''\\1\\2'$cleanedStr);
  394.         $str preg_replace('/(\W)وال(\w{3,})/u''\\1\\2'$str);
  395.         $str preg_replace('/(\w{3,})هما(\W)/u''\\1\\2'$str);
  396.         $str preg_replace('/(\w{3,})كما(\W)/u''\\1\\2'$str);
  397.         $str preg_replace('/(\w{3,})تين(\W)/u''\\1\\2'$str);
  398.         $str preg_replace('/(\w{3,})هم(\W)/u''\\1\\2'$str);
  399.         $str preg_replace('/(\w{3,})هن(\W)/u''\\1\\2'$str);
  400.         $str preg_replace('/(\w{3,})ها(\W)/u''\\1\\2'$str);
  401.         $str preg_replace('/(\w{3,})نا(\W)/u''\\1\\2'$str);
  402.         $str preg_replace('/(\w{3,})ني(\W)/u''\\1\\2'$str);
  403.         $str preg_replace('/(\w{3,})كم(\W)/u''\\1\\2'$str);
  404.         $str preg_replace('/(\w{3,})تم(\W)/u''\\1\\2'$str);
  405.         $str preg_replace('/(\w{3,})كن(\W)/u''\\1\\2'$str);
  406.         $str preg_replace('/(\w{3,})ات(\W)/u''\\1\\2'$str);
  407.         $str preg_replace('/(\w{3,})ين(\W)/u''\\1\\2'$str);
  408.         $str preg_replace('/(\w{3,})تن(\W)/u''\\1\\2'$str);
  409.         $str preg_replace('/(\w{3,})ون(\W)/u''\\1\\2'$str);
  410.         $str preg_replace('/(\w{3,})ان(\W)/u''\\1\\2'$str);
  411.         $str preg_replace('/(\w{3,})تا(\W)/u''\\1\\2'$str);
  412.         $str preg_replace('/(\w{3,})وا(\W)/u''\\1\\2'$str);
  413.         $str preg_replace('/(\w{3,})ة(\W)/u''\\1\\2'$str);
  414.  
  415.         $stemStr preg_replace('/(\W)\w{1,3}(\W)/u''\\2'$str);
  416.         
  417.         $wordRanks $this->rankWords($stemStr);
  418.         
  419.         arsort($wordRanksSORT_NUMERIC);
  420.         
  421.         $i 1;
  422.         foreach ($wordRanks as $key => $value{
  423.             if ($this->acceptedWord($key)) {
  424.                 $metaKeywords .= $key '، ';
  425.                 $i++;
  426.             }
  427.             if ($i $int{
  428.                 break;
  429.             }
  430.         }
  431.         
  432.         $metaKeywords mb_substr($metaKeywords0-2);
  433.         
  434.         return $metaKeywords;
  435.     }
  436.     
  437.     /**
  438.      * Normalized Arabic document
  439.      *      
  440.      * @param string $str Input Arabic document as a string
  441.      *      
  442.      * @return string Normalized Arabic document
  443.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  444.      */
  445.     protected function doNormalize($str)
  446.     {
  447.         $str str_replace($this->_normalizeAlef'ا'$str);
  448.         $str str_replace($this->_normalizeDiacritics''$str);
  449.         $str strtr($str
  450.                      'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  451.                      'abcdefghijklmnopqrstuvwxyz');
  452.  
  453.         return $str;
  454.     }
  455.     
  456.     /**
  457.      * Extracting common Arabic words (roughly)
  458.      * from input Arabic string (document content)
  459.      *                        
  460.      * @param string $str Input normalized Arabic document as a string
  461.      *      
  462.      * @return string Arabic document as a string free of common words (roughly)
  463.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  464.      */
  465.     public function cleanCommon($str)
  466.     {
  467.         $str str_replace($this->_commonWords' '$str);
  468.         
  469.         return $str;
  470.     }
  471.     
  472.     /**
  473.      * Remove less significant Arabic letter from given string (document content).
  474.      * Please note that output will not be human readable.
  475.      *                      
  476.      * @param string $str Input Arabic document as a string
  477.      *      
  478.      * @return string Output string after removing less significant Arabic letter
  479.      *                 (not human readable output)
  480.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  481.      */
  482.     protected function draftStem($str)
  483.     {
  484.         $str str_replace($this->_commonChars''$str);
  485.         return $str;
  486.     }
  487.     
  488.     /**
  489.      * Ranks words in a given Arabic string (document content). That rank refers
  490.      * to the frequency of that word appears in that given document.
  491.      *                      
  492.      * @param string $str Input Arabic document as a string
  493.      *      
  494.      * @return hash Associated array where document words referred by index and
  495.      *               those words ranks referred by values of those array items.
  496.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  497.      */
  498.     protected function rankWords($str)
  499.     {
  500.         $wordsRanks array();
  501.         
  502.         $str   str_replace($this->_separators' '$str);
  503.         $words preg_split("/[\s,]+/u"$str);
  504.         
  505.         foreach ($words as $word{
  506.             if (isset($wordsRanks[$word])) {
  507.                 $wordsRanks[$word]++;
  508.             else {
  509.                 $wordsRanks[$word1;
  510.             }
  511.         }
  512.  
  513.         foreach ($wordsRanks as $wordRank => $total{
  514.             if (mb_substr($wordRank01== 'و'{
  515.                 $subWordRank mb_substr($wordRank1mb_strlen($wordRank1);
  516.                 if (isset($wordsRanks[$subWordRank])) {
  517.                     unset($wordsRanks[$wordRank]);
  518.                     $wordsRanks[$subWordRank+= $total;
  519.                 }
  520.             }
  521.         }
  522.  
  523.         return $wordsRanks;
  524.     }
  525.     
  526.     /**
  527.      * Ranks sentences in a given Arabic string (document content).
  528.      *      
  529.      * @param array $sentences        Sentences of the input Arabic document
  530.      *                                 as an array
  531.      * @param array $stemmedSentences Stemmed sentences of the input Arabic
  532.      *                                 document as an array
  533.      * @param array $arr              Words ranks array (word as an index and
  534.      *                                 value refer to the word frequency)
  535.      *                         
  536.      * @return array Two dimension array, first item is an array of document
  537.      *                sentences, second item is an array of ranks of document
  538.      *                sentences.
  539.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  540.      */
  541.     protected function rankSentences($sentences$stemmedSentences$arr)
  542.     {
  543.         $sentenceArr array();
  544.         $rankArr     array();
  545.         
  546.         $max count($sentences);
  547.         
  548.         for ($i 0$i $max$i++{
  549.             $sentence $sentences[$i];
  550.  
  551.             $w     0;
  552.             $first mb_substr($sentence01);
  553.             $last  mb_substr($sentence-11);
  554.                     
  555.             if ($first == "\n"{
  556.                 $w += 3;
  557.             elseif (in_array($first$this->_separators)) {
  558.                 $w += 2;
  559.             else {
  560.                 $w += 1;
  561.             }
  562.                     
  563.             if ($last == "\n"{
  564.                 $w += 3;
  565.             elseif (in_array($last$this->_separators)) {
  566.                 $w += 2;
  567.             else {
  568.                 $w += 1;
  569.             }
  570.  
  571.             foreach ($this->_importantWords as $word{
  572.                 if ($word != ''{
  573.                     $w += mb_substr_count($sentence$word);
  574.                 }
  575.             }
  576.             
  577.             $sentence mb_substr(mb_substr($sentence0-1)1);
  578.             if (!in_array($first$this->_separators)) {
  579.                 $sentence $first $sentence;
  580.             }
  581.             
  582.             $stemStr $stemmedSentences[$i];
  583.             $stemStr mb_substr($stemStr0-1);
  584.             
  585.             $words preg_split("/[\s,]+/u"$stemStr);
  586.             
  587.             $totalWords count($words);
  588.             if ($totalWords 4{
  589.                 $totalWordsRank 0;
  590.                 
  591.                 foreach ($words as $word{
  592.                     if (isset($arr[$word])) {
  593.                         $totalWordsRank += $arr[$word];
  594.                     }
  595.                 }
  596.                 
  597.                 $wordsRank     $totalWordsRank $totalWords;
  598.                 $sentenceRanks $w $wordsRank;
  599.                 
  600.                 array_push($sentenceArr$sentence $last);
  601.                 array_push($rankArr$sentenceRanks);
  602.             }
  603.         }
  604.         
  605.         $sentencesRanks array($sentenceArr$rankArr);
  606.         
  607.         return $sentencesRanks;
  608.     }
  609.     
  610.     /**
  611.      * Calculate minimum rank for sentences which will be including in the summary
  612.      *      
  613.      * @param array   $str Document sentences
  614.      * @param array   $arr Sentences ranks
  615.      * @param integer $int Number of sentences you need to include in your summary
  616.      * @param integer $max Maximum number of characters accepted in your summary
  617.      *      
  618.      * @return integer Minimum accepted sentence rank (sentences with rank more
  619.      *                  than this will be listed in the document summary)
  620.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  621.      */
  622.     protected function minAcceptedRank($str$arr$int$max)
  623.     {
  624.         $len array();
  625.         
  626.         foreach ($str as $line{
  627.             $len[mb_strlen($line);
  628.         }
  629.  
  630.         rsort($arrSORT_NUMERIC);
  631.  
  632.         $totalChars 0;
  633.         
  634.         for ($i=0$i<=$int$i++{
  635.  
  636.             if (!isset($arr[$i])) {
  637.                 $minRank 0;
  638.                 break;
  639.             }
  640.  
  641.             $totalChars += $len[$i];
  642.  
  643.             if ($totalChars >= $max{
  644.                 $minRank $arr[$i];
  645.                 break;
  646.             }
  647.  
  648.             $minRank $arr[$i];
  649.         }
  650.  
  651.         return $minRank;
  652.     }
  653.     
  654.     /**
  655.      * Check some conditions to know if a given string is a formal valid word or not
  656.      *      
  657.      * @param string $word String to be checked if it is a valid word or not
  658.      *      
  659.      * @return boolean True if passed string is accepted as a valid word else
  660.      *                  it will return False
  661.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  662.      */
  663.     protected function acceptedWord($word)
  664.     {
  665.         $accept true;
  666.         
  667.         if (mb_strlen($word3{
  668.             $accept false;
  669.         }
  670.         
  671.         return $accept;
  672.     }
  673. }

Documentation generated on Wed, 29 Aug 2012 08:32:49 +0200 by phpDocumentor 1.4.0