0

I am trying to find keywords from a text file, when the keywords are found I provide a score of 1 for each match. The program below counts the score efficiently but doesn't sort the score from higher to lower. Please provide me a solution for this problem.

Here is the code:

           <?php
              $lines = file("Abstract.txt");
              $Result = array();
              $index = 0;
              $search_term1= "Tamoxifen";
              $search_term2="Doxorubicin";
              $search_term3="Synergistic";
              $search_term4="MCF-7";
                $search_term5="Inhibition";


        $keywords = array($search_term1, $search_term2, $search_term3);
        $replace_keyword = array("<b>".$search_term1."</b>", "<b>".$search_term2."</b>", "<b>".$search_term3."</b>");

        foreach ($lines as $line_num => $line) 
        {
            if($line_num > 1)
                {
                $arr = explode("\t", $line); //Reads tab separated file
                $Pubmed = trim((string)$arr[0]); //first column is Pubmed
                $title = trim((string)$arr[1]); //second column is title
                $abstract = trim((string)$arr[2]); //third column is abstract
                $score = substr_count(strtoupper($title), strtoupper($search_term1)) + substr_count(strtoupper($abstract), strtoupper($search_term1)); //counts occurrence of 1st string in title & abstract
                $score += (substr_count(strtoupper($title), strtoupper($search_term2)) + substr_count(strtoupper($abstract), strtoupper($search_term2))); //counts occurrence of 2nd string in title & abstract
                $score += (substr_count(strtoupper($title),strtoupper($search_term3)) + substr_count(strtoupper($abstract), strtoupper($search_term3))); //counts occurrence of 3rd string in title & abstract


                //store the result as well as data into array 
                    $Result[$index]["Pubmed"] = $Pubmed;
                    $Result[$index]["<BR>score"] = $score; 
                    $Result[$index]["<BR>title"] = str_ireplace($keywords, $replace_keyword, $title);
                    $Result[$index]["<BR>abstract"] = str_ireplace($keywords, $replace_keyword, $abstract);
                    $index++;
                }
        }


            //sort the array by score
            $sorter=array();
            $ret=array();
            reset($Result);
            $key = "score";
            foreach ($Result as $ii => $va) {
            $sorter[$ii]=$va[$key];
        }
            arsort($sorter);
            foreach ($sorter as $ii => $va) {
            $ret[$ii]=$Result[$ii];
        }
            $Result=$ret;

            foreach($Result as $instance)
        {
            $keys = array_keys($instance);
                foreach($keys as $key)
                {
                    if(!strcmp($key,"abstract")) 
                    echo "\n<b>".$key."</b> : ".$instance[$key]."\n";
                else
                echo "<b>".$key."</b> : ".$instance[$key]."\n";
        }
                echo "\n\n_________________________________________________________________________________________________________________________________________________________________________________________________\n\n";

        }



        ?>
3
  • 9
    please provide a minimalistic code which is just enough to reproduce the problem. Commented Jun 26, 2012 at 20:33
  • 1
    What does the result look like and what do you want it to look like? Commented Jun 26, 2012 at 20:34
  • @jeroen: I have around 400 articles each has Pubmed id, title and Abstracts. I am trying to search for keywords in the articles and calculate a score for each match. The program below calculates the score but doesn't sort it. The result should be like $Pubmed <BR> $Score <BR> $Title <BR> $Abstracts, now this will be for all the 400 articles I just want the articles with higher score to be at the top followed by lower scores. Commented Jun 26, 2012 at 20:41

1 Answer 1

4

The bad row is: $key = "score";

as you save the score in: $Result[$index]["&lt;BR>score"] = $score;

it should be: $key = "<BR>score";

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.