cvs: pear /Text_Statistics/Text Statistics.php /Text_Statistics/tests Statistics02.phpt
| From: | George Schlossnagle | Date: | Wed, 05 Feb 2003 04:15:23 +0000 |
| Subject: | cvs: pear /Text_Statistics/Text Statistics.php /Text_Statistics/tests Statistics02.phpt | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-8717@lists.php.net to get a copy of this message | ||
gschlossnagle Tue Feb 4 23:15:23 2003 EDT
Added files:
/pear/Text_Statistics/tests Statistics02.phpt
Modified files:
/pear/Text_Statistics/Text Statistics.php
Log:
Slightly modified version of Jesus M. Castagnetto's patch to
add Paragraph and character frequency counts. Unit test to
test the paragraph counting code.
Index: pear/Text_Statistics/Text/Statistics.php
diff -u pear/Text_Statistics/Text/Statistics.php:1.2 pear/Text_Statistics/Text/Statistics.php:1.3
--- pear/Text_Statistics/Text/Statistics.php:1.2 Sun Feb 2 05:07:40 2003
+++ pear/Text_Statistics/Text/Statistics.php Tue Feb 4 23:15:23 2003
@@ -16,7 +16,7 @@
// | Author: George Schlossnagle <george@omniti.com> |
// +----------------------------------------------------------------------+
//
-// $Id: Statistics.php,v 1.2 2003/02/02 10:07:40 arnaud Exp $
+// $Id: Statistics.php,v 1.3 2003/02/05 04:15:23 gschlossnagle Exp $
/**
* Text_Statistics calculates some basic readability metrics on a
@@ -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,14 +127,53 @@
}
/**
+ * 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()
{
+ // char frequencies
+ $this->_charFreq = count_chars($this->text);
$lines = explode("\n", $this->text);
+ // Set ourselves as 'out of text block to start
+ $intext = 0;
foreach( $lines as $line ) {
+ // A paragraph is when we enter a text line
+ // after a line that was all whitespace
+ if(preg_match("/\S/", $line)) {
+ if($intext == 0) {
+ $this->_numParas++;
+ $intext = 1;
+ }
+ }
+ else {
+ $intext = 0;
+ }
$this->_analyze_line($line);
}
$this->flesch = 206.835 -
@@ -165,4 +206,4 @@
$this->numSentences += count($matches[0]);
}
}
-?>
\ No newline at end of file
+?>
Index: pear/Text_Statistics/tests/Statistics02.phpt
+++ pear/Text_Statistics/tests/Statistics02.phpt
<?php
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt.
|
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: George Schlossnagle <george@omniti.com> |
// +----------------------------------------------------------------------+
//
// $Id: Statistics02.phpt,v 1.1 2003/02/05 04:15:22 gschlossnagle Exp $
require_once "PHPUnit.php";
require_once "Text/Statistics.php";
class ParagraphTestCase extends PHPUnit_TestCase {
var $sample;
var $sampleAbbr;
var $object;
function setUp()
{
$this->sample = "
This is a test of the multi paragraph support.
We'll try to be a bit clever.
Hopefully clever enough.
";
$this->object = new Text_Statistics($this->sample);
}
function TextTestCase($name)
{
$this->PHPUnit_TestCase($name);
}
function testNumParagraphs()
{
$this->assertEquals(3, $this->object->getNumParagraphs());
}
}
$suite = new PHPUnit_TestSuite('ParagraphTestCase');
$result = PHPUnit::run($suite);
echo $result->toString();
?>