Jump to content

Recommended Posts

What i need to do: collect two pieces of text from an html form and produce a list of the words they have in common, and how many times each common word appears in each text.

E.G.
Word  |Times in Tx1  | Times in Tx2
Hello   |5                 | 3

At the moment I've got three main arrays, one containing each of the texts and one containing the shared words. I've got a big chunk of code but the results it produces are...well, let's say they have erroneous tendancies.

Heres some of my code so you can see what I'm trying to do (hopefully it's clear enough to not need too much background, or a full listing). This is the first thing I've ever done in php, and the first thing I've had to program in any language in a long time, so you'll have to let me know if I'm making any fundamental or stupid mistakes!

[code]
//generating the array of shared words

$shared = array();
$kk=0;
for ($ii =0; $ii<$maxwordcount;$ii++)
{
for ($jj =0; $jj<$maxwordcount;$jj++)
{
if ($arrayofwords[$ii] == $arrayofwords2[$jj])
{
if (in_array($arrayofwords[$ii],$shared))
{
//$counter[$kk-1]++;
             }
else
{
$temp1 = $arrayofwords[$ii];
$shared[$kk]=$temp1;
$kk++;
  }
}
}
}
[/code]

And here, I'm trying to count how many times each shared word appears in text1 (but it doesn't really work):
[code]
$count1 = array();
$words1 = array();

for ($ab=0;$ab<count($shared);$ab++)
{
if (in_array($shared[$ab],$arrayofwords))
{
if (in_array($shared[$ab],$shared)) //if it's already in the shared array...
  {
  for ($xy=0;$xy<count(shared)+1;$xy++)
  {
    if ($shared[$ab]==$shared[$xy]) //find it and increment the counter
    {
    $count1[$ab] = ($count1[$ab] +1);
    $words1[$ab]=$shared[$ab];
    }
  }
  }
  else{
$count1[$ab] = ($count1[$ab] +1);
$words1[$ab]=$shared[$ab];}
}
}
[/code]

Any suggestions on an easier way to achieve this, or help on where I'm going logically wrong would be much appreciated! 
try[code]<?php
$w[1]="one two none one one xx prpr one none";
$w[2]="one two one none none xxxx one none none";
$word_array[1] = explode(' ',$w[1]);
$word_array[2] = explode(' ',$w[2]);
$shared = array_intersect($word_array[1], $word_array[2]);
for ($i=1; $i<3; $i++) {
foreach ($word_array[$i] as $a) {
if (in_array($a, $shared)) $out[$a][$i]++;
}
}
echo '
<table border="2">
<tr>
<td>Word</td>
<td>count in Tx1</td>
<td>count in Tx2</td>
</tr>';
foreach ($out as $w => $b) echo "
<tr>
<td>$w</td>
<td>$b[1]</td>
<td>$b[2]</td>
</tr>";
echo '
</table>';
?>[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.