Re: Patch for PEAR::Text_Statistics

From: Date: Wed, 05 Feb 2003 03:03:11 +0000
Subject: Re: Patch for PEAR::Text_Statistics
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-12977@lists.php.net to get a copy of this message
Looks good. Couple things: 1) There are unit tests in the tests dir. If you want to add some testing code, you might want to do it there. In fact since you are adding some new functionality, additional tests would be in order. 2) The paragraph counting can be folded into the lines. If a $line matches /^\s*$/ you can increment the paragraph count. This seems a bit cleaner. 3) I don't know what the general PEAR style guides are for noting authorship of particular blocks inside the code. I personally think it detracts from code readability. I would prefer you just add yourself to the author block at the top of the file or what have you. I bow to concensus. I'll modify your patch when I get a sec, or you can just go ahead and commit the changes with the above mods yourself. George On Tuesday, February 4, 2003, at 08:42 PM, Jesus M. Castagnetto wrote:
Made some quick additions to the Text/Statitistics.php file. Here is the "diff -u" output (some code for testing after that): --- Statistics.php-orig Tue Feb 4 17:00:38 2003 +++ Statistics.php Tue Feb 4 17:28:19 2003 @@ -106,10 +106,12 @@
      * @var    number
      * @access protected
      */
-    var $_abbreviations = array('/Mr\./'   => 'Misterr',
+    var $_abbreviations = array('/Mr\./'   => 'Mister',
                                 '/Mrs\./i' => 'Misses', // Phonetic
                                 '/etc\./i' => 'etcetera',
                                 '/Dr\./i'  => 'Doctor',
+                                '/Jr\./i' => 'Junior',
+                                '/Sr\./i' => 'Senior',
                                );
     /**
@@ -125,12 +127,45 @@
     }
     /**
+     * Returns the character frequencies.
+     *
+     * @return array of frequencies, where the index is the ASCII byte char
value
+     * @access public
+     * @author Jesus M. Castagnetto <jmcastagnetto@php.net>
+     */
+    function getCharFreq() {
+        return $this->_charFreq;
+    }
+
+    /**
+     * Returns the number of paragaphs.
+     * Paragraphs are defined as chunks of text separated by
+     * and empty line.
+     *
+     * @return long
+     * @access public
+     * @author Jesus M. Castagnetto <jmcastagnetto@php.net>
+     */
+    function getNumParagraphs() {
+        return $this->_numParas;
+    }
+
+    /**
      * Compute statistics for the document object.
      *
      * @access protected
      */
     function _analyze()
     {
+        // JMC changes
+        // char frequencies
+        $this->_charFreq = count_chars($this->text);
+        // paragraphs are defined as chunks of text separated by
+        // and empty line
+        $para_re = '/\n\s*\n/m';
+        $this->_paras = preg_split($para_re, $this->text);
+        $this->_numParas = count($this->_paras);
+        // end of JMC changes
         $lines = explode("\n", $this->text);
         foreach( $lines as $line ) {
             $this->_analyze_line($line);
@@ -165,4 +200,4 @@
         $this->numSentences += count($matches[0]);
     }
} -?> \ No newline at end of file +?> [jmc_test.php] <?php require_once 'Text/Statistics.php'; $text = "This is a simple text. With some paragraphs. This is a simple text. With some paragraphs. This is a simple text. With some paragraphs. This is a simple text. With some paragraphs. This is a simple text. With some paragraphs. This is a simple text. With some paragraphs. This is a simple text. With some paragraphs. This is a simple text. With some paragraphs. This is a simple text. With some paragraphs. This is a simple text. With some paragraphs. This is a simple text. With some paragraphs. This is a simple text. With some paragraphs. This is a simple text. With some paragraphs."; $stats = new Text_Statistics($text); print_r($stats); ?> ===== --- Jesus M. Castagnetto (jcastagnetto@yahoo.com) Research: http://metallo.scripps.edu/ Personal: http://www.castagnetto.org/ __________________________________________________ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com -- PEAR Development Mailing List (http://pear.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.pear.dev (#12977) next »