Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8159989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:06:10+00:00 2026-06-06T18:06:10+00:00

I am trying to find keywords from a text file, when the keywords are

  • 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";

        }



        ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:06:12+00:00Added an answer on June 6, 2026 at 6:06 pm

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

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

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find a way to remove blank pages from a document I wrote
Trying to find a good name for a method swapping each coordinate X and
Trying to find a way to send a POST HTTPS request from Python to
I am trying to track a set of keywords from Twitter by using the
I'm trying to extract specific fields from my file. Basically, output fields only containing
I'm trying to find certain keywords in a string with python. The string is
I am trying to create a function which will extract meta keywords from a
I'm trying to define attributes from an array like so: [a, b, c].each do
I am trying to loop through some 50-odd files in a directory. Each file
I'm trying to load data from an xml file, load them in arrays and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.