473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Keyword array

I'm looking to make a keyword array.

Let's say I have the following array:

$words = array
("foo","lorem","foo","ipsum","bar","foo","dolor"," sit","foo","bar","amet","
lorem");

I'd like to create a new array with the top X words, ordered by occurrence
In the above example, the new array would be:
$keywords = array("foo","bar","lorem");

Any help is appreciated.

--
Karl Groves
www.karlcore.com
Aug 15 '06 #1
6 1858
Rik
Karl Groves wrote:
I'm looking to make a keyword array.

Let's say I have the following array:

$words = array
("foo","lorem","foo","ipsum","bar","foo","dolor"," sit","foo","bar","amet","
lorem");

I'd like to create a new array with the top X words, ordered by
occurrence In the above example, the new array would be:
$keywords = array("foo","bar","lorem");

I assum the top X words, ordered _descending_?

$number = 10;//or another if you wish
$top = array_slice(array_keys(rsort(array_count_values($w ords))),0,$number);

If PHP5 or higher this will be better:
$top =
array_keys(array_slice(rsort(array_count_values($w ords)),0,$number,true));

Grtz,
--
Rik Wasmus
Aug 15 '06 #2
"Rik" <lu************@hotmail.comwrote in
news:eb**********@netlx020.civ.utwente.nl:
Karl Groves wrote:
>I'm looking to make a keyword array.

Let's say I have the following array:

$words = array
("foo","lorem","foo","ipsum","bar","foo","dolor"," sit","foo","bar","am
e
t","
>lorem");

I'd like to create a new array with the top X words, ordered by
occurrence In the above example, the new array would be:
$keywords = array("foo","bar","lorem");


I assum the top X words, ordered _descending_?

$number = 10;//or another if you wish
$top =
array_slice(array_keys(rsort(array_count_values($w ords))),0,$number);

If PHP5 or higher this will be better:
$top =
array_keys(array_slice(rsort(array_count_values($w ords)),0,
$number,true
));

Grtz,
Thanks for the response.

Here's what I have:

$words = array("foo", "lorem", "ipsum", "foo", "dolor", "sit", "bar",
"amet", "four", "score", "foo", "and", "seven", "bar", "years", "ago",
"foo", "our", "lipsum", "forefathers", "foo");

$number = 3;

$top = array_keys(array_slice(rsort(array_count_values($w ords)),0,
$number,true));

print("<pre>");
var_dump($top);
print("</pre>");
Returns the following error:

Warning: array_keys() [function.array-keys]: The first argument should
be an array in /path/to/file.php on line 7

--
Karl Groves
www.karlcore.com
Aug 15 '06 #3
Rik
Karl Groves wrote:
"Rik" <lu************@hotmail.comwrote in
news:eb**********@netlx020.civ.utwente.nl:
>Karl Groves wrote:
>>I'm looking to make a keyword array.

Let's say I have the following array:

$words = array
("foo","lorem","foo","ipsum","bar","foo","dolor", "sit","foo","bar","am
e t","
>>lorem");

I'd like to create a new array with the top X words, ordered by
occurrence In the above example, the new array would be:
$keywords = array("foo","bar","lorem");


I assum the top X words, ordered _descending_?

$number = 10;//or another if you wish
$top =
array_slice(array_keys(rsort(array_count_values($ words))),0,$number);

If PHP5 or higher this will be better:
$top =
array_keys(array_slice(rsort(array_count_values($ words)),0,
$number,true ));

Grtz,

Thanks for the response.

Here's what I have:

$words = array("foo", "lorem", "ipsum", "foo", "dolor", "sit", "bar",
"amet", "four", "score", "foo", "and", "seven", "bar", "years", "ago",
"foo", "our", "lipsum", "forefathers", "foo");

$number = 3;

$top = array_keys(array_slice(rsort(array_count_values($w ords)),0,
$number,true));

print("<pre>");
var_dump($top);
print("</pre>");
Returns the following error:

Warning: array_keys() [function.array-keys]: The first argument should
be an array in /path/to/file.php on line 7
Je m'excuse. rsort offcourse returns a bool.

$occurence = array_count_values($words);
rsort($occurence);
$top = array_slice(array_keys($occurence),0,$number);

Suits me right for trying to make an illegible oneliner.

Grtz,
--
Rik Wasmus
Aug 15 '06 #4
"Rik" <lu************@hotmail.comwrote in news:dc00a$44e22551$8259c69c
$3****@news1.tudelft.nl:
Karl Groves wrote:
>"Rik" <lu************@hotmail.comwrote in
news:eb**********@netlx020.civ.utwente.nl:
>>Karl Groves wrote:
I'm looking to make a keyword array.

Let's say I have the following array:

$words = array

("foo","lorem","foo","ipsum","bar","foo","dolor"," sit","foo","bar","am
>>e t","
lorem");

I'd like to create a new array with the top X words, ordered by
occurrence In the above example, the new array would be:
$keywords = array("foo","bar","lorem");
I assum the top X words, ordered _descending_?

$number = 10;//or another if you wish
$top =
array_slice(array_keys(rsort(array_count_values( $words))),0,
$number);
>>>
If PHP5 or higher this will be better:
$top =
array_keys(array_slice(rsort(array_count_values( $words)),0,
$number,true ));

Grtz,

Thanks for the response.

Here's what I have:

$words = array("foo", "lorem", "ipsum", "foo", "dolor", "sit", "bar",
"amet", "four", "score", "foo", "and", "seven", "bar", "years",
"ago",
>"foo", "our", "lipsum", "forefathers", "foo");

$number = 3;

$top = array_keys(array_slice(rsort(array_count_values($w ords)),0,
$number,true));

print("<pre>");
var_dump($top);
print("</pre>");
Returns the following error:

Warning: array_keys() [function.array-keys]: The first argument
should
>be an array in /path/to/file.php on line 7

Je m'excuse. rsort offcourse returns a bool.

$occurence = array_count_values($words);
rsort($occurence);
$top = array_slice(array_keys($occurence),0,$number);

Suits me right for trying to make an illegible oneliner.

Thanks, though this is now only giving the keys, not the words
themselves

array(3) {
[0]=>
int(0)
[1]=>
int(1)
[2]=>
int(2)
}

--
Karl Groves
www.karlcore.com
Aug 15 '06 #5
Rik
Karl Groves wrote:
"Rik" <lu************@hotmail.comwrote in
news:dc00a$44e22551$8259c69c $3****@news1.tudelft.nl:
>Karl Groves wrote:
>>>
$top = array_keys(array_slice(rsort(array_count_values($w ords)),0,
$number,true));

print("<pre>");
var_dump($top);
print("</pre>");
Returns the following error:

Warning: array_keys() [function.array-keys]: The first argument
should be an array in /path/to/file.php on line 7

Je m'excuse. rsort offcourse returns a bool.

$occurence = array_count_values($words);
rsort($occurence);
$top = array_slice(array_keys($occurence),0,$number);

Suits me right for trying to make an illegible oneliner.


Thanks, though this is now only giving the keys, not the words
themselves

array(3) {
[0]=>
int(0)
[1]=>
int(1)
[2]=>
int(2)
}
Pfff, never a good idea to try to post while talking to your mother on the
phone (oh yeah, which disease has this family member got now...). You have
my complete and undivided attention now: occording to the manual rsort()
deletes the keys we so desperately want. arsort() is the solution.

Here we go, and I've finally tested the code before posting :-).
<?php
$number = 4;
$words = array("foo", "lorem", "ipsum", "foo", "dolor", "sit", "bar",
"amet", "four", "score", "foo", "and", "seven", "bar", "years", "ago",
"foo", "our", "lipsum", "forefathers", "foo");

$occurence = array_count_values($words);
arsort($occurence);
$top = array_slice(array_keys($occurence),0,$number);
print_r($top);
?>
(
[0] =foo
[1] =bar
[2] =years
[3] =seven
)

Sorry for the mess I posted earlier.

Grtz,
--
Rik Wasmus
Aug 15 '06 #6
"Rik" <lu************@hotmail.comwrote in
news:d0**************************@news1.tudelft.nl :

)

Sorry for the mess I posted earlier.
Thank you so much for the help. Everything works as intended now.

--
Karl Groves
www.karlcore.com
Aug 15 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Auleciems | last post by:
Hi: I'm having trouble using an Object which is created based on the following: Public CarDetail () as Car Where the CLASS "Car" is defined as: Public Class Car
7
by: tweak | last post by:
Can someone give me a short example as how to best use this keyword in your code? This is my understanding: by definition restrict sounds like it is suppose to restrict access to memory...
9
by: Jackie | last post by:
Hi everyone, Does anyone know when "register" declarations should be used and when "register" must not be used? If possible please give examples for both cases. Thanks
2
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this...
2
by: coz | last post by:
I created a wrapper class for a dll written in C. Will the following code prevent the "ENTIRE ARRAY" from being moved, not just the first array element? The Wrapper class will be instantiated in...
54
by: Sahil Malik [MVP] | last post by:
What the heck - I can't find it. A bit shocked to see it missing though. So "Does VB.NET have the yield keyword, or any equivalent of it" ? -- - Sahil Malik Upcoming ADO.NET 2.0 book -...
2
by: csperler | last post by:
Hi There - The code that follows does not work. I'm trying to dynamically invoke a method that contains a params-attributed parameter. However, the array of objects that are passed into the...
4
by: David | last post by:
Is there a way to find which element in an object array the keyword 'this' is acting upon? For example: function doMe(){ var aTags = document.getElementById("list").getElementsByTagName("a");...
33
by: Snis Pilbor | last post by:
With the "as if" rule in play, doesn't that effectively render the "register" keyword completely useless? Example: I make a silly compiler which creates code that goes out of its way to take a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.