Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## calculation

i have big problem, would anyone pls kindly help.

i have to read these quotes:
quotes[0] = "to be or not to be that is the question"

quotes[1] = "love looks not with the eyes but with the mind"

quotes[2] = "love all trust a few do wrong to none"

then I have to calculate the following:

1) df = number of quotes containing the term (e.g. df["to"] = 2 from quotes[0] and quotes[2])

2) pairs of QID and tf
QID = quote ID
tf = frequency of a term within a quote
(e.g. For "to" , {QID = 0, tf = 2}, {QID = 2, tf = 1})

(but if it is "a", it will only show {QID = 2, tf = 1})

and print in the format as follow:
/*** word<tab>df<tab>QID<tab>tf ***/
a<tab>1<tab>2<tab>1
to<tab>2<tab>0<tab>2<tab>2<tab>1

how can I do that in PHP? please kindly help. thank you.

• If this is also part of some homework assignment that you also posted about here:

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

then once again you need to attempt to write the code and then ask specific questions.

• hello, this is my code...

<?
// word to search for....
\$word = 'to';
// using your example array \$quotes

\$quotes=file("quote.txt");
\$num=count(\$quotes);
\$remove_punc = array('.', ',');

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

\$keep = array();

for(\$i = 0; \$i < sizeof(\$cleaned_quotes); \$i++)
{
\$check = explode(' ', \$cleaned_quotes[\$i]);
\$count = 0;

for(\$x = 0; \$x < sizeof(\$check); \$x++)
{
(\$check[\$x] == \$word) ? \$count++ : '';
}

if (\$count > '0')
{
\$keep[] = array('' . \$count . '', '' . \$i . '');

}
}

if (!empty(\$keep))
{
\$hold = '';
\$total = sizeof(\$keep);

for(\$z = 0; \$z < sizeof(\$keep); \$z++)
{
\$tab = (\$z != '0') ? "\t" : "";
\$hold .= \$tab . \$keep[\$z][1] . "\t" . \$keep[\$z][0];
}

echo \$word . "\t" . \$total . "\t" . \$hold;

}
else
{
echo 'Sorry no matches found!';
}
?>

but then the problem is
i want the program read the file "quote.txt" which contains few quotes. then it will count all the words that appears. but I can only do it manually which I have defines \$word = 'to'...
after that I have to retype the code into \$word = 'a"...etc..

is there a way to do that and print it accordingly as follow

a 1 3 1
all 1 3 1
be 1 1 2
but 1 2 1
do 1 3 1
eyes 1 2 1
few 1 3 1
is 1 1 1
looks 1 2 1
love 2 2 1 3 1
...etc.