Page 1 of 1

Counting words

Posted: Sat Dec 26, 2009 12:21 am
by mtecedor
Hi All,

I need to count the number of words the end user selects on a given text. The end users can highlight and unhighlight words by clicking on two buttons after selecting the words, so I need to add and subtract to the counter. I have been able to count the number of words selected when the button is clicked, but I need to add/subtract multiple times, otherwise the counter always restart everytime the user click one of the two buttons.

This is the code I have right now:

on mouseUp
put the number of words of field "Text2" into tText
if the selectedText is not empty then
put number of words of selectedText into tSelected
put tText - tSelected into tNumberSelected
else
answer "Por favor, seleccciona las palabras que quieres subrayar."
pass mouseUp
end if

Any suggestion?

Thanks a lot and Merry Xmas

Re: Counting words

Posted: Sat Dec 26, 2009 12:52 am
by mtecedor
Hi again,

I think I got it. Just defining the variable as global seems to fix the problem.

More problems with counter

Posted: Sat Dec 26, 2009 1:52 am
by mtecedor
Hi again,

It seems the counter does not work the way I want. I hope someone has an idea to solve this dilemma.

The scenario:
End users have to highlight a text in yellow. If they highlight more than 50%, they get a warning message letting them know that they have highlighted too much. Thus, they should unhighlight some words.

The counter keeps track of the highlighted words. So, it must be able to add the number of words highlighted and to subtract the number of words unhighlighted.

The problem:
The counter works based on the number of words selected. it does not take into consideration whether the background color of the word is yellow. So, if in order to unhighlight the user select a chunk of text containing non highlighted words, the counter subtract more than it should.

My question:
Is there a way to script the code in such a way that it only subtract the words whose background color is yellow?

This is part of my code in the unhighlight button:

put the number of words of field "Text2" into tText
if the selectedText is not empty then
put number of words of selectedText into tSelected
subtract tSelected from tTotalSelected
else
answer "Por favor, selecciona las palabras que quieres borrar."
pass mouseUp
end if

Thanks

Marta

Re: Counting words

Posted: Sat Dec 26, 2009 2:55 am
by sturgis
Not sure if this method will work for what you're doing as it requires the field to be locked, but heres a simple method that does what you want using mouseDoubleDown to pick and choose words to colorize, and keeps track of total words and hilited words.

Code: Select all

local theTotalWords,theChosenWords
on mouseDoubleUp
   lock screen
   put the number of words of field "wordField" into theTotalWords   
   if the clickChunk is not empty then -- empty if a user clicks whitespace
      if the backColor of the clickChunk is empty then -- check to see if the word is already hilited.
         add 1 to theChosenWords -- if the word wasn't previously hilited, add one to the count
         set the backColor of the clickChunk to 128,128,128 --colorize the selected word 
      else
         subtract 1 from theChosenWords -- if the clicked word was already chosen, lower the count then remove the background color
         set the backColor of the clickChunk to empty
      end if   
      unlock screen
   end if   
   put "Total Words: " & theTotalWords && "+ Chosen Words: " & theChosenWords && "= " & theTotalWords-theChosenWords
end mouseDoubleUp

Re: Counting words

Posted: Sun Dec 27, 2009 12:33 am
by mtecedor
Hi again,

Sturgis, thanks for the idea. I modified a little bit and now it does something closer to what I want.

Now, it only subtract from the counter when some of the selected words have yellow background color. However, it still subtracts words that have no backgColor.

Is it possible to put in a variable only the words that have backColor? Is there any command that can tells apart what words have a specific features?

I was trying with "put", "find" command and similar, but it doesnt work.

Thanks

Marta

Re: Counting words

Posted: Sun Dec 27, 2009 2:40 am
by sturgis
The answer is.. it depends.

If all your background color selections follow the rules for as far as how words and/or items are designated, you can cycle through the field and check the state of each word.

If however, there are words that are partially backcolored then the returned state will be "mixed"

You can do this to check word status of a field. I seem to recall somewhere that item matching with a delimiter is slightly faster than locating word positions so did it as items instead. Assuming you can contstrain selections to whole words only elsewhere in your code, you could add a counter to this handler that increments on the condition that "not (the backcolor of item i of field "wordField" is empty)"

Also be aware that punctuation can be part of a "word" so if a word has has its backcolor set to yellow, but the trailing . is missed, checking backcolor will report mixed.

Code: Select all

on mouseUp
   set the itemDelimiter to space
   repeat with i = 1 to the number of items in field "wordField"
      put the backColor of item i of field "wordField" && item i of field "wordField" &  return after tmpVar
   end repeat
   put tmpVar
end mouseUp

Re: Counting words

Posted: Sun Dec 27, 2009 8:04 pm
by mtecedor
Hi Sturgis,

Thanks a lot for the tip. I ended up doing something similar, but it doesnt work.

This is my code:

on mouseUp
if the selectedText is not empty then
repeat with i = 1 to the number of words of selectedText
put the backColor of word i of selectedText into tColorSelected -- compilation error at line 26 (Expression: bad factor), char 38
if tColorSelected is "255,255,0" then
put the number of words of tColorSelected into tSelected
subtract tSelected from tTotalSelected
end if
end repeat
else
answer "Por favor, selecciona las palabras que quieres borrar."
pass mouseUp
end if

I used similar code somewhere else and it works perfectly. any idea why?

Marta

Re: Counting words

Posted: Sun Dec 27, 2009 8:37 pm
by sturgis
The compile error is caused because the code lacks

Code: Select all

end mouseUp
Since the block structure of the code is thrown off by the lack, the compiler gets confused and can't pinpoint the actual problem, but thats it.

Good luck with your coding!

Re: Counting words

Posted: Sun Dec 27, 2009 8:47 pm
by sturgis
Just reread this. I'm not sure, but I don't think you can use selectedText for your test. I beleive it becomes disconnected from the field itself so "selectedText" doesn't have the property "backColor"

I'm thinking you need to come at the problem from a different direction.

Re: Counting words

Posted: Sun Dec 27, 2009 10:42 pm
by bn
Hi Marta,

there are a couple of problems in the code
put the backColor of word i of selectedText into tColorSelected
if you put "put the backColor of word i of the selectedText into...
it will compile but will not work because word i of the selectedText is not word i of the field.

I took your code and made it work without really understanding what you are after but here it goes

Code: Select all

on mouseUp 
   if the selectedText is not empty then
      put the selectedChunk into tChunk
      put the number of words of char 1 to word 2 of tChunk of field 1 into tCountWordsStart
      put the number of words of char 1 to word 4 of tChunk of field 1 into tCountWordsEnd
      repeat with i = tCountWordsStart to tCountWordsEnd
         put the backColor of  word i of field 1 into tColorSelected
         if tColorSelected is "255,255,0" then
            put tCountWordsEnd - tCountWordsStart + 1 into tSelected
            subtract tSelected from tTotalSelected
         end if
      end repeat
   else
      answer "Por favor, selecciona las palabras que quieres borrar."
      pass mouseUp
   end if
end mouseUp

So it might or might not be what you want. By using the selectedChunk you know which char to char is selected. SelectedChunk has the form: "Char x to y of field 1". Then you count the number of words from the beginning of the text to the first char of the selection and to the last char of the selection. By subtracting the number of words of field 1 to the last char of the selection from the number of words of field 1 to the first char of the selection you get the count of words of the selection.
I did not find an easier way to do it, there might well be one.
regards
Bernd

Re: Counting words

Posted: Tue Dec 29, 2009 4:35 pm
by mtecedor
Sturgis, Bernd,

Thanks a lot for your ideas. It seems that nothing is really doing what I want.

Progamming the counter for not substracting the words when their backColor is not yellow seems to work. The problem is if the user selects a chunk that contains both highlighted and non-highlighted words. I cannot figure out a way to tell apart this two options when they are in the same selectedChunk.

If someone thinks about other way to approach this problem, please, let me know.

Marta

Re: Counting words

Posted: Tue Dec 29, 2009 7:08 pm
by bn
Marta,
I am sorry, I did not test my code enough. try this

Code: Select all

on mouseUp 
   if the selectedText is not empty then
      put the selectedChunk into tChunk
      put the number of words of char 1 to word 2 of tChunk of field 1 into tCountWordsStart
      put the number of words of char 1 to word 4 of tChunk of field 1 into tCountWordsEnd
      put 0 into tSelected
      put tCountWordsEnd - tCountWordsStart into tTotalSelected
      add 1 to tTotalSelected
      put " total selected: " & tTotalSelected -- delete later
      repeat with i = tCountWordsStart to tCountWordsEnd
         put the backColor of  word i of field 1 into tColorSelected
         if tColorSelected is "255,255,0" then
            add 1 to tSelected -- adds to yellow background count
            subtract  1 from tTotalSelected -- counts no of selected words down
         end if
      end repeat
      put " yellow background: " &  tSelected after msg -- delete later
   else
      answer "Por favor, selecciona las palabras que quieres borrar."
      pass mouseUp
   end if
end mouseUp
it will put the number of initially selected words and the number of words with a yellow background into the message box. You can test it then. Later you take this out. it is marked.
I hope this works better. In my testing it did.
regards
Bernd

Re: Counting words

Posted: Wed Dec 30, 2009 6:15 pm
by mtecedor
Bernd,

thanks a lot, that works!!!!

Marta

Re: Counting words

Posted: Thu Dec 15, 2022 11:05 pm
by dunbarx
Timac.

Welcome to the Forum.

This is a thread from 13 years ago.

Welcome anyway. :D

Craig

Re: Counting words

Posted: Fri Dec 16, 2022 9:13 am
by Klaus
dunbarx wrote:
Thu Dec 15, 2022 11:05 pm
...
This is a thread from 13 years ago.
Yep, that and a meaningless posting indicates a SPAMMER! :-)