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-2013 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-2013 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-2013 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(
  219.             "/[^\.\n\،\؛\,\;](.+?)[\.\n\،\؛\,\;]/u"
  220.             $str
  221.             $sentences
  222.         );
  223.         $_sentences $sentences[0];
  224.  
  225.         if ($mode == 'rate'{
  226.             $str            preg_replace("/\s{2,}/u"' '$str);
  227.             $totalChars     mb_strlen($str);
  228.             $totalSentences count($_sentences);
  229.  
  230.             $maxChars round($int $totalChars 100);
  231.             $int      round($int $totalSentences 100);
  232.         else {
  233.             $maxChars 99999;
  234.         }
  235.         
  236.         $summary '';
  237.  
  238.         $str           strip_tags($str);
  239.         $normalizedStr $this->doNormalize($str);
  240.         $cleanedStr    $this->cleanCommon($normalizedStr);
  241.         $stemStr       $this->draftStem($cleanedStr);
  242.         
  243.         preg_match_all(
  244.             "/[^\.\n\،\؛\,\;](.+?)[\.\n\،\؛\,\;]/u"
  245.             $stemStr
  246.             $sentences
  247.         );
  248.         $_stemmedSentences $sentences[0];
  249.  
  250.         $wordRanks $this->rankWords($stemStr);
  251.         
  252.         if ($keywords{
  253.             $keywords $this->doNormalize($keywords);
  254.             $keywords $this->draftStem($keywords);
  255.             $words    explode(' '$keywords);
  256.             
  257.             foreach ($words as $word{
  258.                 $wordRanks[$word1000;
  259.             }
  260.         }
  261.         
  262.         $sentencesRanks $this->rankSentences(
  263.             $_sentences
  264.             $_stemmedSentences
  265.             $wordRanks
  266.         );
  267.         
  268.         list($sentences$ranks$sentencesRanks;
  269.  
  270.         $minRank $this->minAcceptedRank($sentences$ranks$int$maxChars);
  271.  
  272.         $totalSentences count($ranks);
  273.         
  274.         for ($i 0$i $totalSentences$i++{
  275.             if ($sentencesRanks[1][$i>= $minRank{
  276.                 if ($output == 'summary'{
  277.                     $summary .= ' '.$sentencesRanks[0][$i];
  278.                 else {
  279.                     $summary .= '<span class="' $style .'">' 
  280.                                 $sentencesRanks[0][$i'</span>';
  281.                 }
  282.             else {
  283.                 if ($output == 'highlight'{
  284.                     $summary .= $sentencesRanks[0][$i];
  285.                 }
  286.             }
  287.         }
  288.         
  289.         if ($output == 'highlight'{
  290.             $summary str_replace("\n"'<br />'$summary);
  291.         }
  292.         
  293.         return $summary;
  294.     }
  295.           
  296.     /**
  297.      * Summarize input Arabic string (document content) into specific number of
  298.      * sentences in the output
  299.      *                        
  300.      * @param string  $str      Input Arabic document as a string
  301.      * @param integer $int      Number of sentences required in output summary
  302.      * @param string  $keywords List of keywords higlited by search process
  303.      *                    
  304.      * @return string Output summary requested
  305.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  306.      */
  307.     public function doSummarize($str$int$keywords)
  308.     {
  309.         $summary $this->summarize(
  310.             $str$keywords$int'number''summary'$style
  311.         );
  312.         
  313.         return $summary;
  314.     }
  315.     
  316.     /**
  317.      * Summarize percentage of the input Arabic string (document content) into output
  318.      *      
  319.      * @param string  $str      Input Arabic document as a string
  320.      * @param integer $rate     Rate of output summary sentence number as
  321.      *                           percentage of the input Arabic string
  322.      *                           (document content)
  323.      * @param string  $keywords List of keywords higlited by search process
  324.      *                    
  325.      * @return string Output summary requested
  326.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  327.      */
  328.     public function doRateSummarize($str$rate$keywords)
  329.     {
  330.         $summary $this->summarize(
  331.             $str$keywords$rate'rate''summary'$style
  332.         );
  333.         
  334.         return $summary;
  335.     }
  336.     
  337.     /**
  338.      * Highlight key sentences (summary) of the input string (document content)
  339.      * using CSS and send the result back as an output
  340.      *                             
  341.      * @param string  $str      Input Arabic document as a string
  342.      * @param integer $int      Number of key sentences required to be
  343.      *                           highlighted in the input string
  344.      *                           (document content)
  345.      * @param string  $keywords List of keywords higlited by search process
  346.      * @param string  $style    Name of the CSS class you would like to apply
  347.      *                    
  348.      * @return string Output highlighted key sentences summary (using CSS)
  349.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  350.      */
  351.     public function highlightSummary($str$int$keywords$style)
  352.     {
  353.         $summary $this->summarize(
  354.             $str$keywords$int'number''highlight'$style
  355.         );
  356.         
  357.         return $summary;
  358.     }
  359.     
  360.     /**
  361.      * Highlight key sentences (summary) as percentage of the input string
  362.      * (document content) using CSS and send the result back as an output.
  363.      *                    
  364.      * @param string  $str      Input Arabic document as a string
  365.      * @param integer $rate     Rate of highlighted key sentences summary
  366.      *                           number as percentage of the input Arabic
  367.      *                           string (document content)
  368.      * @param string  $keywords List of keywords higlited by search process
  369.      * @param string  $style    Name of the CSS class you would like to apply
  370.      *                    
  371.      * @return string Output highlighted key sentences summary (using CSS)
  372.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  373.      */
  374.     public function highlightRateSummary($str$rate$keywords$style)
  375.     {
  376.         $summary $this->summarize(
  377.             $str$keywords$rate'rate''highlight'$style
  378.         );
  379.         
  380.         return $summary;
  381.     }
  382.     
  383.     /**
  384.      * Extract keywords from a given Arabic string (document content)
  385.      *      
  386.      * @param string  $str Input Arabic document as a string
  387.      * @param integer $int Number of keywords required to be extracting
  388.      *                      from input string (document content)
  389.      *                    
  390.      * @return string List of the keywords extracting from input Arabic string
  391.      *                (document content)
  392.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  393.      */
  394.     public function getMetaKeywords($str$int)
  395.     {
  396.         $patterns     array();
  397.         $replacements array();
  398.         $metaKeywords '';
  399.         
  400.         array_push($patterns'/\.|\n|\،|\؛|\(|\[|\{|\)|\]|\}|\,|\;/u');
  401.         array_push($replacements' ');
  402.         $str preg_replace($patterns$replacements$str);
  403.         
  404.         $normalizedStr $this->doNormalize($str);
  405.         $cleanedStr    $this->cleanCommon($normalizedStr);
  406.         
  407.         $str preg_replace('/(\W)ال(\w{3,})/u''\\1\\2'$cleanedStr);
  408.         $str preg_replace('/(\W)وال(\w{3,})/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.         $str preg_replace('/(\w{3,})ها(\W)/u''\\1\\2'$str);
  415.         $str preg_replace('/(\w{3,})نا(\W)/u''\\1\\2'$str);
  416.         $str preg_replace('/(\w{3,})ني(\W)/u''\\1\\2'$str);
  417.         $str preg_replace('/(\w{3,})كم(\W)/u''\\1\\2'$str);
  418.         $str preg_replace('/(\w{3,})تم(\W)/u''\\1\\2'$str);
  419.         $str preg_replace('/(\w{3,})كن(\W)/u''\\1\\2'$str);
  420.         $str preg_replace('/(\w{3,})ات(\W)/u''\\1\\2'$str);
  421.         $str preg_replace('/(\w{3,})ين(\W)/u''\\1\\2'$str);
  422.         $str preg_replace('/(\w{3,})تن(\W)/u''\\1\\2'$str);
  423.         $str preg_replace('/(\w{3,})ون(\W)/u''\\1\\2'$str);
  424.         $str preg_replace('/(\w{3,})ان(\W)/u''\\1\\2'$str);
  425.         $str preg_replace('/(\w{3,})تا(\W)/u''\\1\\2'$str);
  426.         $str preg_replace('/(\w{3,})وا(\W)/u''\\1\\2'$str);
  427.         $str preg_replace('/(\w{3,})ة(\W)/u''\\1\\2'$str);
  428.  
  429.         $stemStr preg_replace('/(\W)\w{1,3}(\W)/u''\\2'$str);
  430.         
  431.         $wordRanks $this->rankWords($stemStr);
  432.         
  433.         arsort($wordRanksSORT_NUMERIC);
  434.         
  435.         $i 1;
  436.         foreach ($wordRanks as $key => $value{
  437.             if ($this->acceptedWord($key)) {
  438.                 $metaKeywords .= $key '، ';
  439.                 $i++;
  440.             }
  441.             if ($i $int{
  442.                 break;
  443.             }
  444.         }
  445.         
  446.         $metaKeywords mb_substr($metaKeywords0-2);
  447.         
  448.         return $metaKeywords;
  449.     }
  450.     
  451.     /**
  452.      * Normalized Arabic document
  453.      *      
  454.      * @param string $str Input Arabic document as a string
  455.      *      
  456.      * @return string Normalized Arabic document
  457.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  458.      */
  459.     protected function doNormalize($str)
  460.     {
  461.         $str str_replace($this->_normalizeAlef'ا'$str);
  462.         $str str_replace($this->_normalizeDiacritics''$str);
  463.         $str strtr(
  464.             $str
  465.             'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  466.             'abcdefghijklmnopqrstuvwxyz'
  467.         );
  468.  
  469.         return $str;
  470.     }
  471.     
  472.     /**
  473.      * Extracting common Arabic words (roughly)
  474.      * from input Arabic string (document content)
  475.      *                        
  476.      * @param string $str Input normalized Arabic document as a string
  477.      *      
  478.      * @return string Arabic document as a string free of common words (roughly)
  479.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  480.      */
  481.     public function cleanCommon($str)
  482.     {
  483.         $str str_replace($this->_commonWords' '$str);
  484.         
  485.         return $str;
  486.     }
  487.     
  488.     /**
  489.      * Remove less significant Arabic letter from given string (document content).
  490.      * Please note that output will not be human readable.
  491.      *                      
  492.      * @param string $str Input Arabic document as a string
  493.      *      
  494.      * @return string Output string after removing less significant Arabic letter
  495.      *                 (not human readable output)
  496.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  497.      */
  498.     protected function draftStem($str)
  499.     {
  500.         $str str_replace($this->_commonChars''$str);
  501.         return $str;
  502.     }
  503.     
  504.     /**
  505.      * Ranks words in a given Arabic string (document content). That rank refers
  506.      * to the frequency of that word appears in that given document.
  507.      *                      
  508.      * @param string $str Input Arabic document as a string
  509.      *      
  510.      * @return hash Associated array where document words referred by index and
  511.      *               those words ranks referred by values of those array items.
  512.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  513.      */
  514.     protected function rankWords($str)
  515.     {
  516.         $wordsRanks array();
  517.         
  518.         $str   str_replace($this->_separators' '$str);
  519.         $words preg_split("/[\s,]+/u"$str);
  520.         
  521.         foreach ($words as $word{
  522.             if (isset($wordsRanks[$word])) {
  523.                 $wordsRanks[$word]++;
  524.             else {
  525.                 $wordsRanks[$word1;
  526.             }
  527.         }
  528.  
  529.         foreach ($wordsRanks as $wordRank => $total{
  530.             if (mb_substr($wordRank01== 'و'{
  531.                 $subWordRank mb_substr($wordRank1mb_strlen($wordRank1);
  532.                 if (isset($wordsRanks[$subWordRank])) {
  533.                     unset($wordsRanks[$wordRank]);
  534.                     $wordsRanks[$subWordRank+= $total;
  535.                 }
  536.             }
  537.         }
  538.  
  539.         return $wordsRanks;
  540.     }
  541.     
  542.     /**
  543.      * Ranks sentences in a given Arabic string (document content).
  544.      *      
  545.      * @param array $sentences        Sentences of the input Arabic document
  546.      *                                 as an array
  547.      * @param array $stemmedSentences Stemmed sentences of the input Arabic
  548.      *                                 document as an array
  549.      * @param array $arr              Words ranks array (word as an index and
  550.      *                                 value refer to the word frequency)
  551.      *                         
  552.      * @return array Two dimension array, first item is an array of document
  553.      *                sentences, second item is an array of ranks of document
  554.      *                sentences.
  555.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  556.      */
  557.     protected function rankSentences($sentences$stemmedSentences$arr)
  558.     {
  559.         $sentenceArr array();
  560.         $rankArr     array();
  561.         
  562.         $max count($sentences);
  563.         
  564.         for ($i 0$i $max$i++{
  565.             $sentence $sentences[$i];
  566.  
  567.             $w     0;
  568.             $first mb_substr($sentence01);
  569.             $last  mb_substr($sentence-11);
  570.                     
  571.             if ($first == "\n"{
  572.                 $w += 3;
  573.             elseif (in_array($first$this->_separators)) {
  574.                 $w += 2;
  575.             else {
  576.                 $w += 1;
  577.             }
  578.                     
  579.             if ($last == "\n"{
  580.                 $w += 3;
  581.             elseif (in_array($last$this->_separators)) {
  582.                 $w += 2;
  583.             else {
  584.                 $w += 1;
  585.             }
  586.  
  587.             foreach ($this->_importantWords as $word{
  588.                 if ($word != ''{
  589.                     $w += mb_substr_count($sentence$word);
  590.                 }
  591.             }
  592.             
  593.             $sentence mb_substr(mb_substr($sentence0-1)1);
  594.             if (!in_array($first$this->_separators)) {
  595.                 $sentence $first $sentence;
  596.             }
  597.             
  598.             $stemStr $stemmedSentences[$i];
  599.             $stemStr mb_substr($stemStr0-1);
  600.             
  601.             $words preg_split("/[\s,]+/u"$stemStr);
  602.             
  603.             $totalWords count($words);
  604.             if ($totalWords 4{
  605.                 $totalWordsRank 0;
  606.                 
  607.                 foreach ($words as $word{
  608.                     if (isset($arr[$word])) {
  609.                         $totalWordsRank += $arr[$word];
  610.                     }
  611.                 }
  612.                 
  613.                 $wordsRank     $totalWordsRank $totalWords;
  614.                 $sentenceRanks $w $wordsRank;
  615.                 
  616.                 array_push($sentenceArr$sentence $last);
  617.                 array_push($rankArr$sentenceRanks);
  618.             }
  619.         }
  620.         
  621.         $sentencesRanks array($sentenceArr$rankArr);
  622.         
  623.         return $sentencesRanks;
  624.     }
  625.     
  626.     /**
  627.      * Calculate minimum rank for sentences which will be including in the summary
  628.      *      
  629.      * @param array   $str Document sentences
  630.      * @param array   $arr Sentences ranks
  631.      * @param integer $int Number of sentences you need to include in your summary
  632.      * @param integer $max Maximum number of characters accepted in your summary
  633.      *      
  634.      * @return integer Minimum accepted sentence rank (sentences with rank more
  635.      *                  than this will be listed in the document summary)
  636.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  637.      */
  638.     protected function minAcceptedRank($str$arr$int$max)
  639.     {
  640.         $len array();
  641.         
  642.         foreach ($str as $line{
  643.             $len[mb_strlen($line);
  644.         }
  645.  
  646.         rsort($arrSORT_NUMERIC);
  647.  
  648.         $totalChars 0;
  649.         
  650.         for ($i=0$i<=$int$i++{
  651.  
  652.             if (!isset($arr[$i])) {
  653.                 $minRank 0;
  654.                 break;
  655.             }
  656.  
  657.             $totalChars += $len[$i];
  658.  
  659.             if ($totalChars >= $max{
  660.                 $minRank $arr[$i];
  661.                 break;
  662.             }
  663.  
  664.             $minRank $arr[$i];
  665.         }
  666.  
  667.         return $minRank;
  668.     }
  669.     
  670.     /**
  671.      * Check some conditions to know if a given string is a formal valid word or not
  672.      *      
  673.      * @param string $word String to be checked if it is a valid word or not
  674.      *      
  675.      * @return boolean True if passed string is accepted as a valid word else
  676.      *                  it will return False
  677.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  678.      */
  679.     protected function acceptedWord($word)
  680.     {
  681.         $accept true;
  682.         
  683.         if (mb_strlen($word3{
  684.             $accept false;
  685.         }
  686.         
  687.         return $accept;
  688.     }
  689. }

Documentation generated on Mon, 14 Jan 2013 17:48:37 +0100 by phpDocumentor 1.4.0