How to make this simple script work?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
Timothy
Posts: 78
Joined: Tue Apr 11, 2006 7:38 pm

How to make this simple script work?

Post by Timothy » Sun Jul 22, 2007 2:17 am

Field "wordList" contains a list of the 100 most commonly used words in english, one word per line.

Field "scene" contains text I want to summarize.

Field "test" is the destination field.

This script works, except it replaces strings in field "scene." I want to replace words in field "scene." Many commonly used English words are also little parts of bigger words.

I tried the replaceText command instead. It's commented out. I'm not sure if that's what replaceText is supposed to do. (As I've complained before, the documentation is way too terse for novices like me!)

If replaceText is the right command, maybe I just got the syntax wrong.

Code: Select all

on mouseUp
  Ask "Enter the intensity of summary you want (an integer from 10 to 99)"
  put it into intensity
  put field "scene" into whataVar
  repeat with i = 1 to intensity
    if the shiftKey is down then
      exit repeat
    end if
    put line i of card field "wordlist" into tNum
    replace tnum with empty in whatavar
    --replaceText(whataVar,tnum,empty) 
  end repeat
  put whataVar into field "test" 
end mouseUp

Please advise.

Thanks in advance,


Tim

xApple
Posts: 113
Joined: Wed Nov 29, 2006 10:21 pm
Location: Geneva

Post by xApple » Sun Jul 22, 2007 3:00 am

Code: Select all

on mouseUp 
  Ask "Enter the intensity of summary you want (an integer from 10 to 99)" 
  put it into intensity 
  put field "scene" into whataVar 
  repeat with i = 1 to intensity 
    if the shiftKey is down then exit repeat 
    put line i of card field "wordlist" into tNum
    repeat with w = 1 to the number of words of whataVar
       if word w of whataVar is tNum then delete word w of whataVar 
    end repeat
  end repeat 
  put whataVar into field "test" 
end mouseUp
should do it. Written on the spot and untested.
Cheers.

Timothy
Posts: 78
Joined: Tue Apr 11, 2006 7:38 pm

Post by Timothy » Sun Jul 22, 2007 9:01 pm

Thank you, xApple.

That works, but it's hopelessly slow. Field "scene" contains 2000 words. Ultimately, this script is intended to summarize hundreds of fields containing from a few hundred to a few thousand words each. My computer isn't exactly a screamer, but this script might be too slow even for a faster machine.

That's why I was trying to use the "replace" command. It seems quite fast.
Setting lockmessages and lockscreen to true speeds it up some, but not enhough.

Is there another, faster way to do what I want to do?

Thanks,


Tim

P.S.

For onlookers, there might be a problem with

Code: Select all

 repeat with w = 1 to the number of words of whataVar
       if word w of whataVar is tNum then delete word w of whataVar 
deleting word w of whatavar changes the number of words in whataVar

There are easy ways to fix this problem

I think the way I usually deal with that is

repeat with w = the number of words in whatavar down to 1

The P.S. is also written on the spot and untested

Timothy
Posts: 78
Joined: Tue Apr 11, 2006 7:38 pm

Post by Timothy » Sun Jul 22, 2007 9:13 pm

Followup to my previous message...

A bit more testing reveals another small problem with the suggested script.

Word w of whatavar sometimes includes a punctuation mark. E.g., "I," without the quotation marks.

I could figure out slow and clumsy ways to exclude the punctuation marks. What about a fast and elegant way?

Thanks again,


Tim

Timothy
Posts: 78
Joined: Tue Apr 11, 2006 7:38 pm

It works fast and correctly now

Post by Timothy » Mon Jul 23, 2007 2:01 am

I guess I fixed it myself. This script seems to work correctly. It takes three or four seconds on my 500 mhz G3 laptop when there are about 2000 words in field "scene"

I added "I'm" "It's" and "I'll" to the front of the list of commonly used English words. Maybe contractions aren't normally included, though they seem common.

Deleting the 25 most common English words, plus those extra three reduces field "scene" to 1300 words. Deleting the 99 most common words, plus the three, reduces field "scene" to 930 words.

FWIW, here's the successful script.

Code: Select all

on mouseUp
  Ask "Enter the intensity of summary you want (an integer from 10 to 99)"
  put it into intensity
  set lockmessages to true
  set lockscreen to true
  put field "scene" into whataVar
  repeat with i = 1 to intensity
    if the shiftKey is down then exit repeat
    put line i of card field "wordlist" after wordListVar
  end repeat
  repeat with w = the number of words in whataVar down to 1
    if the shiftKey is down then exit repeat
    if the charToNum of last char of word w of whataVar < 44 then delete last char of word w of whataVar
    if word w of whataVar is in wordListVar then
      delete word w of whataVar
    end if
    set cursor to busy
  end repeat
  put whataVar into field "test" 
end mouseUp
Using "is in" sped things up considerably. So did putting words to delete into a variable.

Tim

xApple
Posts: 113
Joined: Wed Nov 29, 2006 10:21 pm
Location: Geneva

Post by xApple » Mon Jul 23, 2007 3:47 pm

Seams like you don't even need our help anymore : )
Yeah, forgetting that deleting a word changes the number of words in the field was stupid ^^

Personnaly I don't see much to speed it up more now...

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5145
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Mon Jul 23, 2007 10:00 pm

Hi Tim,

Are you looking for something like this?

Code: Select all

on mouseUp
  ask "Enter the intensity of summary you want (an integer from 10 to 99)"
  if it is an integer and it > 9 and it < 99 then
    put summarize(field "scene",line 1 to it of fld "Wordlist") into fld "test"
  else if the result is not cancel then
    beep
  end if
end mouseUp

function summarize theData,theWordlist
  -- change unwanted chars into spaces
  put replaceText(theData,"[" & numToChar(2) & "-" & numToChar(48) & "]"," ") into theData
  -- make word list space delimited only
  put word 1 to -1 of theWordlist into theWordlist
  replace cr with space in theWordlist
  -- prepare regex
  replace space with " | " in theWordlist
  put replaceText(space & theData & space,space & theWordlist & space,space) into theData
  return word 1 to -1 of theData
end summarize
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply

Return to “Talking LiveCode”