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

  • Home
  • SEARCH
  • 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 7411983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:28:14+00:00 2026-05-29T06:28:14+00:00

I have a phrase counter function as part of a class that I would

  • 0

I have a phrase counter function as part of a class that I would like to adapt to run very large data sets

<?php
private static function do_phrase_count($words, $multidim, $count, $pcount){
        if($multidim === false){
            $words = array($words);
        }

        $tally = array();
        $arraycount = 0;
        foreach($words as $wordgroup){
             $max = count($wordgrounp) - $pcount;
             for($x = 0; $x < $max; $x++){
                $cutoff = $x + $pcount;
                $spacekey = false;
                $phrase = '';
                $z = 0;
                for($y = $x; $y < $cutoff; $y++){
                    if($spacekey) $phrase .= ' ';
                    else $spacekey = true;
                    $phrase .= $wordgroup[$y + $z];
                    $z++;
                }
                if(isset($tally[$phrase])){
                     $tally[$phrase]++;
                     $arraycount++;
                 }
                else $tally[$phrase] = 1;
                if($arraycount > 99999){
                    arsort($tally);
                    $tally = array_slice($tally, 0, 50000);
                    $arraycount = 49999;
                }
            }
        }
        arsort($tally);
        $out = array_slice($tally, 0, $count);
        return $out;
}
  • $words is an array of words to check
  • $multidim is a boolean showing if the array is cascading or flat
  • $count is the number of elements to be returned
  • $pcount is the number of words in a phrase

With every iteration, array_key_exists gets slower, so at a certain point I need to decrease the size of the tally array.

I was considering using a limit (100K) to stop the script from adding new array elements to $tally, or even using a percentage of total words, but after I stop adding new elements to the array I lose the ability to track trends that may pop up. (If I’m analysing data from a whole year, by the time I got to June, I wouldn’t be able to see “summer time” as a trend).

Anyone have a solution as to how to limit my tally array to keep the script zinging without losing the ability to track trends?

UPDATE: I changed the script per your suggestions. Thank you for helping. I also figured out a solution to cut down on the size of the array.

  • 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-29T06:28:14+00:00Added an answer on May 29, 2026 at 6:28 am
    if(isset($tally[$phrase])) 
       $tally[$phrase]++;
    else 
       $tally[$phrase] = 1;
    

    It should be faster than array_key_exists

    ps: test sample

    function genRandomString($length) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
        $string = '';    
        for ($p = 0; $p < $length; $p++) {
            $string .= $characters[mt_rand(0, strlen($characters)-1)];
        }
        return $string;
    }
    function microtime_float()
    {
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }
    $len = 1000000; $str = genRandomString($len);
    $tally = array();
    $stamp1 = microtime_float();
    for($i=0; $i<$len; $i++)
    {
        if(array_key_exists($str[$i], $tally)) 
            $tally[$str[$i]]++;
        else 
            $tally[$str[$i]] = 1;
    }    
    echo microtime_float() - $stamp1 . '<br />';
    $tally = array(); $stamp1 = microtime_float();
    for($i = 0; $i<$len; $i++)
    {
        if(isset($tally[$str[$i]])) 
           $tally[$str[$i]]++;
        else 
           $tally[$str[$i]] = 1;
    }    
    echo microtime_float() - $stamp1 . '<br />';
    

    Result:

    0.80751395225525
    0.44111108779907
    

    Of course, the amount of keys is limited here.

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

Sidebar

Related Questions

I have a ModelForm field that is based on the following Model: class Phrase(models.Model):
I have a phrase in English that looks like this: I have {0} dogs
I have a javascript that displays a generated text into a div: document.getElementById('phrase').innerHTML =
i have a phrase like this Computer, Eddie is gone to the market. I
I have some data stored in SQL Server that contains apostrophes. Within the SQL
I have the following SQL query: SELECT Phrases.* FROM Phrases WHERE (((Phrases.phrase) Like *ing
What does the regular in the phrase regular expression mean? I have heard that
First off, before I ask, i would like to point out that this question
I have a search query that takes a phrase typed in by the user
I have a query that tokenizes a phrase on spaces and needs to look

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.