Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Exact Word Counting
Eggie
post Apr 15 2008, 07:57 PM
Post #1


Advanced Member
Group Icon

Group: Members
Posts: 131
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735



I need a script or something...
i need to count how many times a word like "love" or "car" is used in a website ot in text without having to count it one by one...
is there any script or addon for mozilla that does it?
i need answer fast...
i googled it but came up with nothing

if noone knows...can you just write a script which count the number of a word "car" in
CODE
$text=my car is the most beautiful car in the world;


This post has been edited by Eggie: Apr 15 2008, 09:54 PM
Go to the top of the page
 
+Quote Post
sparkx
post Apr 15 2008, 10:43 PM
Post #2


Sparkx
Group Icon

Group: [HOSTED]
Posts: 343
Joined: 11-October 06
From: Dana Point, CA, USA
Member No.: 16,496



I have no clue if there is a better (more secure way of doing this) but you could just run a str check. Now I am just making up this code so I have no clue it works. Basically what I am going to do is check the length of the string before and after the word is replaced. Subtract the two (gives you the number of letters total) then I check the length of the replaced word and divide that by the result above. I like using basic concepts to get the job done.

CODE
$text="my car is the most beautiful car in the world";
$word=("car");
$normal_length=strlen($text);
$replaced_length=strlen(str_ireplace($word,"",$text));
$result_length=($normal_length-$replaced_length);
$word_length=strlen($word);
$count=($result_length/$word);

Now I did this pretty much step by step (you can make it a LOT shorter). Here is my explaination:
Steps:
1) Set the text var with text you want to search
2) Set the word to search for.
3) Find length of normal string and string without the word.
4) Subtract to find the difference in characters.
5) Check the length of the word.
6) Do some more basic math to find the number of times the word is used and return it as $count.

Is this what you were looking for? Hope this helps,
Sparkx
Go to the top of the page
 
+Quote Post
Mordent
post Apr 16 2008, 09:42 AM
Post #3


Premium Member
Group Icon

Group: [HOSTED]
Posts: 223
Joined: 30-June 07
Member No.: 23,045



I like sparkx's way of doing it, but one thing you might want to do is make $word " car " (with spaces around it). This stops words like "carousel" showing up as one of the words. Admittedly, you'd have to set $word_length to strlen($word)-2 to take the spaces in to account, but that shouldn't be too bad.

One problem with this approach is that punctuation can ruin the count, as " car." wouldn't count, nor would "Car " (as in if it's at the beginning of the string). Capital letters need to be taken in to account as well. Anyone got any ideas on how to improve on this?
Go to the top of the page
 
+Quote Post
toby
post Apr 16 2008, 03:52 PM
Post #4


Premium Member
Group Icon

Group: Members
Posts: 488
Joined: 29-September 06
Member No.: 16,228



You could use regex, like "/\wcar\w/i", I think \w is the opposite of \W, which is alphanumeric, then whatever your languages version of regml(0) is.
Go to the top of the page
 
+Quote Post
sparkx
post Apr 16 2008, 08:02 PM
Post #5


Sparkx
Group Icon

Group: [HOSTED]
Posts: 343
Joined: 11-October 06
From: Dana Point, CA, USA
Member No.: 16,496



Mordent I like your addition (I can't believe I would have over looked that). Anyway if you do it that way you would need to set $text=" ".$text." "; so that if car is used at the beginning or end it would also show up. Capital letters are taken into account however. Notice that I used str_ireplace() instead of str_replace() which makes it case in-sensitive so basically when you replace car it will also replace Car, CAr and any cap/lowercase combo.
If you use Toby's way I think the opposite of /W is [^a-zA-Z] but either way works.

Thanks,
Sparkx
Go to the top of the page
 
+Quote Post
Mordent
post Apr 17 2008, 07:56 PM
Post #6


Premium Member
Group Icon

Group: [HOSTED]
Posts: 223
Joined: 30-June 07
Member No.: 23,045



There's still the problem of not catching words that either start the string (and therefore don't have a space at the front) or end a sentence (and so have a "." instead of a space) or have some other piece of punctuation either before or after them. Given that we don't have any problems with capitalisation (I'll admit that I didn't notice you'd used str_ireplace() ), any thoughts on how to fix this other problem? I'm thinking using regular expressions to check for any punctuation or a space (both before and after), but I haven't used enough of them to know if this method would work. Thoughts?
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. World's Longest Single Word Domain Name!(76)
  2. Any MS Word Plugin For Writing Movie Scripts ?(2)
  3. World's Smallest Website(71)
  4. Abiword(8)
  5. Assign Keyword To Firefox Bookmarks(3)
  6. Cube Domain: A Word About Indians & Their English(8)
  7. A Word About VB6(4)
  8. MS Word Registration Code Recovery(0)
  9. VB.NET: How To Edit & -90degree Rotate Text In Ms Word File ?(3)
  10. Google Soon To Step Into Online Word Processing(21)
  11. Listing Posts In Word Press(2)
  12. A Word About The "FREE HOSTING" System(14)
  13. Some Fun With Algebra(12)
  14. Can't See/print Out Right Border In Word(3)
  15. Word Document Corrupted?(13)
  1. Difference Between OpenOffice Write And MS Word(7)
  2. Microsoft Word Security Issue(7)
  3. OpenOffice Writer Vs Microsoft Word(12)
  4. Just A Word(1)
  5. How To Limit The Number Of Pages In Ms Word?(6)
  6. How To Download Mircosoft Word And Powerpoint?(16)
  7. Counting Down To A Time(7)
  8. What Is A Stronger Word Than Obsessed?(0)
  9. Microsoft Word 2007(11)
  10. Wordpress Tutorials(0)


 



- Lo-Fi Version Time is now: 6th September 2008 - 07:13 AM