Please contact (802) 224 - 6525 for price quotes and programming support.
Follow this link for a resume of php and Mysql Programing experience
Digg This!

0 Members and 1 Guest are viewing this topic. « previous next »

Pages: [1] 2 Go Down Print
Author Topic: Word Counting With Exceptions.  (Read 7454 times)
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« on: September 28, 2007, 01:14:02 AM »

Hi:

I have javascript code to count words in a Textarea. How is it possible to have it NOT COUNT specific words, or look for specific words and subtract them from the count. Here's the code that I'm using.


Code:

<script language="JavaScript">
function cnt(w,x){
var y=w.value;
var r = 0;

var c = 0;




a=y.replace(/\s/g,' ');
a=a.split(' ');
for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
x.value=r;
}

// working form


</script>
</head>

<body>
<p><CENTER>

<form name="myform">
<textarea rows="15" name="w" cols="45" onkeyup="cnt(this,document.myform.c)">
</textarea>

Word Count: <input type="text" name="c" value="0" size="5" onkeyup="cnt(document.myform.w,this)" />
</form>
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #1 on: September 28, 2007, 01:15:21 AM »

Try this (it has problems, tho):


Code:
<script language="JavaScript">
function ExceptThis(a,z){
var r;
out = " blah "; // replace this
add = ""; // with this
temp = "" + a; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add +
temp.substring((pos + out.length), temp.length));
}
r = temp - 2;
z.value=r;
}


function cnt(w,x){
var y=w.value;
var r = 0;
var c = 0;
a=y.replace(/\s/g,' ');
a=a.split(' ');
for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
ExceptThis(r,x);

}

// working form


</script>
</head>

<body>
<p><CENTER>

<form name="myform">
<textarea rows="15" name="w" cols="45" onkeyup="cnt(this,document.myform.c)">
</textarea>

Word Count: <input type="text" name="c" value="0" size="5" onclick="cnt(document.myform.w,this)" />
</form>
Perhaps someone can help.
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #2 on: September 28, 2007, 01:16:04 AM »

Actually, that doesn't work. I'll keep trying.
Sorry.
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #3 on: September 28, 2007, 01:17:01 AM »

OK. now. Try this (this is my last try, then I'm gonna let the professionals help).


Code:
<script language="JavaScript">
function cnt(w,x){
var y=w.value;
var r = 0;
var c = 0;
var p = " blah"
var u = ""

a=y.replace(/\s/g,' ');
wugga = a.replace(p,' ');
while (wugga.indexOf(p)>-1) {
pos= wugga.indexOf(p);
wugga = "" + (wugga.substring(0, pos) + u +
wugga.substring((pos + p.length), wugga.length));
}
a = wugga;
a=a.split(' ');
for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
x.value=r;
}

// working form


</script>
</head>

<body>
<p><CENTER>

<form name="myform">
<textarea rows="15" name="w" cols="45" onkeyup="cnt(this,document.myform.c)">
</textarea>

Word Count: <input type="text" name="c" value="0" size="5" onclick="cnt(document.myform.w,this)" />
</form>
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #4 on: September 28, 2007, 01:17:37 AM »

Hi:

Thanks for the effort, but I couldn't figure out which words it was not supposed to count.
Maybe an explaination could help.
I'll like to have some words that it will ignore or subtract from the final count.

Any other workable ideas welcome.
Thanks.
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #5 on: September 28, 2007, 01:18:34 AM »

This is a utility to capitalize titles and headings on significant words.
The rules are simple- the first word is always capitalized.
After the first word, do not capitalize common articles and modifiers- like a, an, is, the and so on.

If you can capitalize particular words, you can count them. And vice versa.



Code:
String.prototype.recapp= function(){
   var str= this.toLowerCase(), cnt;
   var Rx= /\b([a-z]+('s)?)\b/g;
   var rxX= /^(a|an|and|are|as|at|by|but|can|for|from|if|in(to)?|is|it(\'?s)?|of|on|so|the|to|with(in|out)?)$/;
   str= str.replace(Rx,function(w){
      if(cnt && rxX && rxX.test(w)) return w;
      cnt= true;
      return w.charAt(0).toUpperCase()+w.substring(1);
   });
   return str;
}//sample output:

This is a Utility to Capitalize Titles and Headings on Significant Words
The Rules are Simple- the First Word is Always Capitalized
After the First Word, Do Not Capitalize Common Articles and Modifiers- Like a, an, is, the and so on
If You can Capitalize Particular Words, You can Count Them

And Vice Versa
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #6 on: September 28, 2007, 01:19:36 AM »

Code:
 
<script type="text/javascript">

function count() {
var arr = ["A", "a", "and", "at", "In", "is", "of", "not", "the","to" ];
var el = document.getElementById('textareaid');
var s = el.innerHTML.split(' ');
var w = 0;
for(var i = 0; i< s.length; i++ ) {
for(var n = 0; n<arr.length; n++ ) {
if(s == arr[n]) { w++; }
}
}
alert(i); // 101
alert(w); // 22
alert(i-w); // 79
}
</script>
</head>
<body>
<input type="button" value="count word" onclick="count()">

<textarea id="textareaid" cols="20" rows="15">
A Private Conversation
Last week I went to the theatre. I had a very good seat. The play was very interesting. I did not enjoy it. A young man and a young woman were sitting behind me. They were talking loudly. I got very angry. I could not hear the actors. I turned round. I looked at the man and the woman angrily. They did not pay any attention. In the end, I could not bear it. I turned round again.
"I can't hear a word!" I said angrily.
"It's none of your business," the young man said rudely. " This is private conversation!"
</textarea>
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #7 on: September 28, 2007, 01:21:16 AM »

Hi Ayşe:

Thanks.

Must be brain fade here. 2 problems.

Can u rewrite (i-w); so that I can use / show the value "q" in a field as in

<input type="text" name="q" size="5">

"q" is var with the value of (i-w);


Also, the value of var i defaults to 1 so i is really i-1

Thanks. This was really on target.
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #8 on: September 28, 2007, 01:22:24 AM »

  <script type="text/javascript">

function count() {
var arr = ["A", "a", "and", "at", "In", "is", "of", "not", "the","to" ];
var el = document.getElementById('textareaid');
var s = el.innerHTML.split(' ');
var w = 0;
var cnt = 0;
if(el.innerHTML.match(' ')) {
for(var i = 0; i < s.length; i++ ) { cnt++;
for(var n = 0; n < arr.length; n++ ) {
if(s == arr[n]) { w++; }
}
}
}
var txt = document.getElementById('simora');
txt.value = w;
alert(cnt); // 103
alert(w); // 22
alert(cnt-w); // 81
}
</script>
</head>
<body>
<input type="button" value="count word" onclick="count()">

<textarea id="textareaid" cols="20" rows="15">A Private Conversation. Last week I went to the theatre. I had a very good seat. The play was very interesting. I did not enjoy it. A young man and a young woman were sitting behind me. They were talking loudly. I got very angry. I could not hear the actors. I turned round. I looked at the man and the woman angrily. They did not pay any attention. In the end, I could not bear it. I turned round again. "I can't hear a word!" I said angrily. "It's none of your business," the young man said rudely. "This is private conversation!"</textarea>

<input type="text" id="simora" name="q" size="5" readonly>
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #9 on: September 28, 2007, 01:23:13 AM »

Code:
 
<script type="text/javascript">

function count() {
var arr = ["A", "a", "and", "at", "In", "is", "of", "not", "the","to" ];
var el = document.getElementById('textareaid');
var s = el.value.split(' ');
var w = 0;
var cnt = 0;
// if(el.value.match(' ')) {
for(var i = 0; i < s.length; i++ ) { cnt++;
for(var n = 0; n < arr.length; n++ ) {
if(s == arr[n]) { w++; }
}
}
// }
var txt = document.getElementById('simora');
txt.value = cnt - w;
//alert(cnt);
// alert(w);
// alert(cnt-w);
}
</script>
</head>
<body>

Write your message in textarea then click button:

<textarea id="textareaid" cols="20" rows="15"></textarea>
<input type="button" value="count word" onclick="count()">
<input type="text" id="simora" name="q" size="5" readonly>

Code:
 
<script type="text/javascript">

String.prototype.count = function (myid) {
var arr = ["A", "a", "and", "at", "In", "is", "of", "not", "the","to" ];
var s = this.split(' ');
var w = 0;
var cnt = 0;
for(var i = 0; i < s.length; i++ ) { cnt++;
for(var n = 0; n < arr.length; n++ ) {
if(s == arr[n]) { w++; }
}
}
var txt = document.getElementById(myid);
txt.value = cnt - w;
}
</script>
</head>
<body>

Write your message in textarea:

<textarea id="textareaid" cols="20" rows="15" onkeyup="this.value.count('simora')"></textarea>
<input type="text" id="simora" name="q" size="5" readonly>
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #10 on: September 28, 2007, 01:23:44 AM »

Hi Thanks;

I did make one little change.
To show what was left over after the exceptions were taken out of the total

var txt = document.getElementById('simora');
txt.value = (cnt-w);

Perfect.

Thanks a million.
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #11 on: September 28, 2007, 01:24:17 AM »

What an excellent script.
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #12 on: September 28, 2007, 01:26:02 AM »

Hi there,

I am a relative CSS & JavaScript novice and I have a particular problem that is beyond my level of knowledge so I thought I'd tap the collective wisdom of this forum.

I would like to know if there is a means of writing JavaScript that will dynamically display the styles which are currently applied to any DOM element on a web page (i.e., those applied by ID, those applied by class, etc.) as the user moves their mouse over them.

Ideally the styles would be displayed in a tooltip style pop-up layer which would remain displayed until the user mouses off the element. But how exactly the styles are displayed is not critical. The idea is that the web page itself would act as a sort of live stylesheet reference, dynamically revealing to the user which styles are being applied to which elements, allowing a stylesheet author to know which styles they need to customize to affect a given element.

I would greatly appreciate any help or suggestions you may have on how this can be achieved in a way that will work consistently in most popular modern browsers.

Many thanks in advance.
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #13 on: September 28, 2007, 01:26:35 AM »

Web Developer toolbar, and others, already does this.
__________________
At least 98% of internet users' DNA is identical to that of chimpanzees
Logged
superwebdesigner
Full Member
***

Karma: 0
Offline Offline

Posts: 194


View Profile
« Reply #14 on: September 28, 2007, 01:27:20 AM »

Thanks for the reply. Yes I am aware of the web developer tool bar and other similar tools and plug-ins that provide this kind of functionality.

But we want to provide a quick and easy visualization of the styles in use on a page to our customers without the need for the download of a third-party tool or application.
Logged
Pages: [1] 2 Go Up Print 
« previous next »
Jump to:  

Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Christmas2006 design by Bloc
Page created in 0.257 seconds with 30 queries.