all groups > flash actionscript > february 2007 >
You're in the

flash actionscript

group:

Word count function


Word count function golem2
2/19/2007 11:59:56 PM
flash actionscript:
Hi ,
Anyone has a good word count function? One that will ignore long white spaces and newlines?

Many thanks

Re: Word count function kglad
2/20/2007 12:16:38 AM
add numbers to s if you want to count numbers as words and adjust for
hyphenated words if you want to count them as two words:



s = "abcdefghijklmnopqrstuvwxyz";
alphaA = s.split("");
function wordCountF(s) {
var tempS = s;
tempS.toLowerCase();
var tempA = tempS.split("
").join(",").split("\n").join(",").split("\r").join(",").split(",");
wordCount = 0;
for (var i = 0; i<tempA.length; i++) {
for (var j = 0; j<alphaA.length; j++) {
if (tempA[i].indexOf(alphaA[j])>-1) {
wordCount++;
break;
}
}
}
return wordCount;
}
Re: Word count function kglad
2/20/2007 12:57:13 AM
Re: Word count function golem2
2/25/2007 11:30:05 PM
Hi kglad

The function work good, but it seems to count words like : "You?ll" "that?s" "He?s"
as two words....

Re: Word count function kglad
2/25/2007 11:37:04 PM
Re: Word count function golem2
2/26/2007 2:09:47 AM
.....mm
you are right it is some other charachter that sneeks in when copy and paste
from word to flash.
the way I use the function is I read a string from a textfield adn count it.
works of for input text. but when I paste some text from word to dynamic
textfield
It looks ok but add some extras to the counting. I do the folowing to see
these extra bits:
copy from word to flash, then copy from flash back to word... and I get some
extra chars looks like that:


from word:

We just moved here over the summer,? said Hoppy.
?What?s your dad like?? Gerald asked.
?He?s great. We go surfing together when he gets home from work.?


then paste in flash.
copy and paste back in word, I get:


We just moved here over the summer,? said Hoppy.
?What?s your dad like?? Gerald asked.
?He?s great. We go surfing together when he gets home from work.?

any idea, why?
Re: Word count function golem2
2/26/2007 3:05:20 AM
well, chackei it again and it is the function,I think it counts wrong some
quatation marks.

the folloing text in word counts 152, the function counts 159.

I don't fully understand the function to try to debug it.

-----------------
Hoppy and Gerald went back to the garage to get their bikes. Hoppy?s mom was
in the garden again.
?I?ll ride back to Gerald?s house with him,? Hoppy said.
?You?ll need to get those glasses fixed better, Gerald,? said Mom. She wrapped
a blue bandage around them, too.
?I will. Bye and thanks again,? Gerald said, as the boys got on their bikes.
?Where to?? asked Hoppy.
?Up by the park,? replied Gerald.
As they rode along, Gerald asked, ?Why do you hang around with those kids??
?I only met them today,? said Hoppy. ?I thought they were cool. But I was
wrong.?
?Wow, that?s a relief. I really thought they were your friends,? said Gerald.
?I don?t know anyone yet, except Mom and Dad. We just moved here over the
summer,? said Hoppy.
?What?s your dad like?? Gerald asked.
?He?s great. We go surfing together when he gets home from work.?

Re: Word count function kglad
2/26/2007 5:38:46 AM
Re: Word count function golem2
2/26/2007 8:51:16 PM
well, that is strange. The text have 152 words. you count 145 I get 159 .....
anyhow, here is the solution that produces the correct counting:

- add all caps to s on first line...

cheers,
Golem2

s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
alphaA = s.split ("");
function wordCountF (s) {
var tempS = s;
tempS.toLowerCase ();
var tempA = tempS.split (" ").join (",").split ("\n").join (",").split
("\r").join (",").split (",");
wordCount = 0;
for (var i = 0; i < tempA.length; i++) {
for (var j = 0; j < alphaA.length; j++) {
if (tempA[i].indexOf (alphaA[j]) > -1) {
wordCount++;
break;
}
}
}
return wordCount;
}
str = t1.text;
wordCountF (str);
Re: Word count function kglad
2/26/2007 9:01:43 PM
the correction should be:



s = "abcdefghijklmnopqrstuvwxyz";
alphaA = s.split("");
function wordCountF(s) {
tempS = s.toLowerCase();
var tempA = tempS.split("
").join(",").split("\n").join(",").split("\r").join(",").split(",");
wordCount = 0;
for (var i = 0; i<tempA.length; i++) {
for (var j = 0; j<alphaA.length; j++) {
if (tempA[i].indexOf(alphaA[j])>-1) {
wordCount++;
break;
}
}
}
return wordCount;
}
Re: Word count function golem2
2/26/2007 9:23:25 PM
Re: Word count function kglad
2/26/2007 10:05:19 PM
AddThis Social Bookmark Button