Page 1 of 1

how to remove "comma, full stop" from a sentence

Posted: Mon Oct 20, 2003 1:52 am
by theclay7
hello,

i have sentences as follow:

$quote[1] = "To be or not to be, that is the question.";
$quote[2] = "Love looks not with the eyes, but with the mind.";
$quote[3] = "Love all, trust a few. Do wrong to none.";

now I would like to do the following,

1) for each quote[$i], remove all ","(comma) or "."(full stop)...does anyone know how? I have the following code but it does not work. What should I write in $patterns?

Code: Select all

for ($i=1; $i<4; $i++)
&#123;
	$quote&#1111;$i] = strtolower($quote&#1111;$i]);
	echo "$quote&#1111;$i]<BR>";
	$pieces = explode(" ", $quote&#1111;$i]);
	for ($j=0; $j< (count($pieces)); $j++)
	&#123;
		echo "$pieces&#1111;$j] ";
		$patterns = "\.";
		$replacement = "";
		print preg_replace($patterns, $replacement, $string);
		echo "$pieces&#1111;$j] <BR>";
	&#125;
&#125;
2) also, I would like to count a) number of frequecy of each term (e.g. "to" has a frequency of 2 in qoute[1]) and b) number of frequency for all three quotes (i.e. "to" has frequency = 3)

but I have no idea how? can anyone please give me an idea?

Thank you very very much.

Posted: Mon Oct 20, 2003 3:16 am
by twigletmac
For question 1, str_replace() would probably be best for this job:

Code: Select all

$quotes[] = "To be or not to be, that is the question.";
$quotes[] = "Love looks not with the eyes, but with the mind.";
$quotes[] = "Love all, trust a few. Do wrong to none."; 

$remove_punc = array('.', ',');

foreach ($quotes as $quote) {
    $quote = strtolower($quote);
    $cleaned_quotes[] = str_replace($remove_punc, '', $quote);
}

echo '<pre>';
print_r($cleaned_quotes);
echo '</pre>';
Mac

Posted: Mon Oct 20, 2003 3:34 am
by twigletmac
For question 2, here's a bit of code that works, it can probably be optimised some:

Code: Select all

$i = 0;
foreach ($cleaned_quotes as $quote) {
	$quote_pieces = explode(' ', $quote);
	$unique_words = array_unique($quote_pieces);

	foreach ($unique_words as $word) {
		$count[$i][$word] = 0;
		foreach ($quote_pieces as $quote_word) {
			if ($quote_word == $word) {
				$count[$i][$word]++;
			}
		}
	}
	$i++;
}
echo '<pre>';
print_r($count);
echo '</pre>';
Mac

Posted: Mon Oct 20, 2003 4:02 am
by theclay7
thanks you very much...however, I am not familiar with foreach...

also, how can I count "to" from all three quotes instead of one.

One more question, if all the three sentences are read from another file, for example:

quote.txt has the following;

To be or not to be, that is the question.
Love looks not with the eyes, but with the mind.
Love all, trust a few. Do wrong to none.

1) how do I read this file?
2) how do I do the same as I wanted to do before?

Thank you very much for your kind help

Posted: Mon Oct 20, 2003 4:18 am
by twigletmac
foreach is a pretty simple construct for looping through arrays, check out:
http://php.net/manual/en/control-structures.foreach.php

To read the quotes from a file, just replace (in the code I posted):

Code: Select all

$quotes[] = "To be or not to be, that is the question.";
$quotes[] = "Love looks not with the eyes, but with the mind.";
$quotes[] = "Love all, trust a few. Do wrong to none.";
with

Code: Select all

$quote_file = 'quote.txt'; // you may have to add more path information

$quotes = file($quote_file);
Do you need to be able to count the number of times a word appears from all the quotes, and also for each individual quote, or just from all the quotes.

Mac