Return to Snippet

Revision: 16830
at December 2, 2009 10:09 by kurokikaze


Updated Code
function stringTokenize($sBuffer, $sSplit) {
		$iCount = 0;

		if(strlen($sBuffer) == 0)
			return;

		$sToken = strtok($sBuffer, $sSplit);
		$aTokens[$iCount] = $sToken;

		while ($sToken !== false) {
			$sToken = strtok($sSplit);
			if(strlen($sToken) > 0) {
				$iCount++;
				$aTokens[$iCount] = $sToken;
			}
		}

		return $aTokens;
	}

	function lcount($string) {
		return array($string => 1);
	}

	function rcount($w1, $w2) {
		if (!is_array($w1)) {
			$w1 = array();
		}
		foreach ($w2 as $word => $count) {
			$w1[$word] += $count;
		}
		return $w1;
	}

	$text = 'Matthew Boulton (1728�1809) was an English manufacturer and the partner of engineer James Watt. In the final quarter of the 18th century, the partnership installed hundreds of Boulton & Watt steam engines, which were a great advance on the state of the art, making possible the mechanisation of factories and mills. He became associated with James Watt when Watt\'s business partner, John Roebuck, was unable to pay a debt to Boulton, who accepted Roebuck\'s share of Watt\'s patent as settlement. He then successfully lobbied Parliament to extend Watt\'s patent for an additional seventeen years, enabling the firm to market Watt\'s steam engine. Boulton applied modern techniques to the minting of coins, striking millions of pieces for Britain and other countries, and supplying the Royal Mint with up-to-date equipment. Boulton was a key member of the Lunar Society, a group of Birmingham-area men prominent in the arts, sciences, and theology. Members included Boulton, Watt, Erasmus Darwin, Josiah Wedgwood, and Joseph Priestley. Members of the Society have been given credit for developing concepts and techniques in science, agriculture, manufacturing, mining, and transportation that laid the groundwork for the Industrial Revolution and for later discoveries, including the theory of evolution.';

	$result = array_reduce(array_map('lcount', array_map('strtolower', stringTokenize($text, ' ,-.:;?()'))), 'rcount');

	$words = array_flip(array(
		'the',
		'of',
		'and',
		'to',
		'for',
		'a',
		'was',
		'in',
		'an',
		'He',
		'but'
	));

	$result = array_diff_key($result, $words);

	arsort($result);
	var_dump($result);

Revision: 16829
at December 2, 2009 10:08 by kurokikaze


Updated Code
function stringTokenize($sBuffer, $sSplit) {
		$iCount = 0;

		if(strlen($sBuffer) == 0)
			return;

		$sToken = strtok($sBuffer, $sSplit);
		$aTokens[$iCount] = $sToken;

		while ($sToken !== false) {
			$sToken = strtok($sSplit);
			if(strlen($sToken) > 0) {
				$iCount++;
				$aTokens[$iCount] = $sToken;
			}
		}

		return $aTokens;
	}

	function lcount($string) {
		return array($string => 1);
	}

	function rcount($w1, $w2) {
		if (!is_array($w1)) {
			$w1 = array();
		}
		foreach ($w2 as $word => $count) {
			$w1[$word] += $count;
		}
		return $w1;
	}

	$text = 'Matthew Boulton (1728�1809) was an English manufacturer and the partner of engineer James Watt. In the final quarter of the 18th century, the partnership installed hundreds of Boulton & Watt steam engines, which were a great advance on the state of the art, making possible the mechanisation of factories and mills. He became associated with James Watt when Watt\'s business partner, John Roebuck, was unable to pay a debt to Boulton, who accepted Roebuck\'s share of Watt\'s patent as settlement. He then successfully lobbied Parliament to extend Watt\'s patent for an additional seventeen years, enabling the firm to market Watt\'s steam engine. Boulton applied modern techniques to the minting of coins, striking millions of pieces for Britain and other countries, and supplying the Royal Mint with up-to-date equipment. Boulton was a key member of the Lunar Society, a group of Birmingham-area men prominent in the arts, sciences, and theology. Members included Boulton, Watt, Erasmus Darwin, Josiah Wedgwood, and Joseph Priestley. Members of the Society have been given credit for developing concepts and techniques in science, agriculture, manufacturing, mining, and transportation that laid the groundwork for the Industrial Revolution and for later discoveries, including the theory of evolution.';

	$result = array_reduce(array_map('lcount', array_map('strtolower', stringTokenize($text, ' ,-.:;?()'))), 'rcount');

	$words = array_flip(array(
		'the',
		'of',
		'and',
		'to',
		'for',
		'a',
		'was',
		'in',
		'an',
		'He',
		'but'
	));

	$result = array_diff_key($result, $words);

	arsort($result);
	var_dump($result);

Revision: 16828
at August 17, 2009 13:11 by kurokikaze


Initial Code
function stringTokenize($sBuffer, $sSplit) {
		$iCount = 0;

		if(strlen($sBuffer) == 0)
			return;

		$sToken = strtok($sBuffer, $sSplit);
		$aTokens[$iCount] = $sToken;

		while ($sToken !== false) {
			$sToken = strtok($sSplit);
			if(strlen($sToken) > 0) {
				$iCount++;
				$aTokens[$iCount] = $sToken;
			}
		}

		return $aTokens;
	}

	function lcount($string) {
		return array($string => 1);
	}

	function rcount($w1, $w2) {
		if (!is_array($w1)) {
			$w1 = array();
		}
		foreach ($w2 as $word => $count) {
			$w1[$word] += $count;
		}
		return $w1;
	}

	$text = 'Matthew Boulton (1728–1809) was an English manufacturer and the partner of engineer James Watt. In the final quarter of the 18th century, the partnership installed hundreds of Boulton & Watt steam engines, which were a great advance on the state of the art, making possible the mechanisation of factories and mills. He became associated with James Watt when Watt\'s business partner, John Roebuck, was unable to pay a debt to Boulton, who accepted Roebuck\'s share of Watt\'s patent as settlement. He then successfully lobbied Parliament to extend Watt\'s patent for an additional seventeen years, enabling the firm to market Watt\'s steam engine. Boulton applied modern techniques to the minting of coins, striking millions of pieces for Britain and other countries, and supplying the Royal Mint with up-to-date equipment. Boulton was a key member of the Lunar Society, a group of Birmingham-area men prominent in the arts, sciences, and theology. Members included Boulton, Watt, Erasmus Darwin, Josiah Wedgwood, and Joseph Priestley. Members of the Society have been given credit for developing concepts and techniques in science, agriculture, manufacturing, mining, and transportation that laid the groundwork for the Industrial Revolution and for later discoveries, including the theory of evolution.';

	$result = array_reduce(array_map('lcount', array_map('strtolower', stringTokenize($text, ' ,-.:;?()'))), 'rcount');

	$words = array_flip(array(
		'the',
		'of',
		'and',
		'to',
		'for',
		'a',
		'was',
		'in',
		'an',
		'He',
		'but'
	));

	$result = array_diff_key($result, $words);

	arsort($result);
	var_dump($result);

Initial URL
http://wp.me/pfjwm-1U

Initial Description
$words is array of excluded words.

Initial Title
Count instances of words in text

Initial Tags


Initial Language
PHP