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 348311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:21:10+00:00 2026-05-12T11:21:10+00:00

I have been set a challenge to create an indexer that takes all words

  • 0

I have been set a challenge to create an indexer that takes all words 4 characters or more, and stores them in a database along with how many times the word was used.

I have to run this indexer on 4,000 txt files. Currently, it takes about 12-15 minutes – and I’m wondering if anyone has a suggestion for speeding things up?

Currently I’m placing the words in an array as follows:

// ==============================================================
// === Create an index of all the words in the document
// ==============================================================
function index(){
    $this->index = Array();
    $this->index_frequency = Array();

    $this->original_file = str_replace("\r", " ", $this->original_file);
    $this->index = explode(" ", $this->original_file);

    // Build new frequency array
    foreach($this->index as $key=>$value){
        // remove everything except letters
        $value = clean_string($value);

        if($value == '' || strlen($value) < MIN_CHARS){
            continue;
        }

        if(array_key_exists($value, $this->index_frequency)){
            $this->index_frequency[$value] = $this->index_frequency[$value] + 1;
        } else{
            $this->index_frequency[$value] = 1;
        }
    }
    return $this->index_frequency;
}

I think the biggest bottleneck at the moment is the script to store the words in the database. It needs to add the document to the essays table and then if the word exists in the table just append essayid(frequency of the word) to the field, if the word doesnt exist, then add it…

// ==============================================================
// === Store the word frequencies in the db
// ==============================================================
private function store(){
    $index = $this->index();

    mysql_query("INSERT INTO essays (checksum, title, total_words) VALUES ('{$this->checksum}', '{$this->original_filename}', '{$this->get_total_words()}')") or die(mysql_error());

    $essay_id = mysql_insert_id();

    foreach($this->index_frequency as $key=>$value){

        $check_word = mysql_result(mysql_query("SELECT COUNT(word) FROM `index` WHERE word = '$key' LIMIT 1"), 0);

        $eid_frequency = $essay_id . "(" . $value . ")";

        if($check_word == 0){
            $save = mysql_query("INSERT INTO `index` (word, essays) VALUES ('$key', '$eid_frequency')");
        } else {
            $eid_frequency = "," . $eid_frequency;
            $save = mysql_query("UPDATE `index` SET essays = CONCAT(essays, '$eid_frequency') WHERE word = '$key' LIMIT 1");
        }
    }
}
  • 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-05-12T11:21:10+00:00Added an answer on May 12, 2026 at 11:21 am

    You might consider profiling your app to know exactly where are your bottlenecks. This might give you a better understanding of what can be improved.

    Regarding DB optimisation: check if you have an index on word column, then try lowering the number of times you access DB. INSERT ... ON DUPLICATE KEY UPDATE ..., maybe?

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

Sidebar

Related Questions

I have several tables in my database that have read-only fields that get set
I am getting the error of not all named parameters have been set .
Is it possible to get read access to favorited sites (that have been set
I have been set a project from school to design snake in VB.Net but
trying to display data only after variables have been set. $(document).ready(function () { function
I have been trying to set up Eclipse and the Arduino libraries. I have
I have been trying to set the scroll of my page to a certain
I have been trying to set AlarmManager for Specific date, but it is not
I have been asked to set up some authentication for some content on our
I have been trying to set the value of a hidden input by using

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.