Page 1 of 1
Referencing the Character Immediately After a Certain Word
Posted: Thu Mar 20, 2014 7:09 am
by theotherbassist
I'm copying words one by one (through a script that sends them in order) from a field called "insertField" to another field called "worddisplay." I want to be able to reference the character directly after word x (the currently displayed word in "worddisplay") to see if it's a 'return' (CR) and, if so, run a script that delays the display of the next word slightly. So, really--it's quite simple.
I just need to send a command if the character following word x is a CR. Can I find the next character with reference only to the word--or do I need to incorporate some kind of reference to other chunks like the line?
Re: Referencing the Character Immediately After a Certain Wo
Posted: Thu Mar 20, 2014 9:22 am
by SparkOut
My personal and subjective idea would be to loop " repeat for each line tLine in field "insertField" and within that loop to "repeat for each word tWord in tLine". Then you don't need to bother with the test for cr at all.
Re: Referencing the Character Immediately After a Certain Wo
Posted: Thu Mar 20, 2014 9:54 am
by bn
Hi TheOtherBassist,
If you want to know if there is a return after one word I would would take the index word and the following word and test if there is a CR in between. That would be the trigger for you to display. That works fine except for the last word, this is where I cheat and just append a dummy word.
Code: Select all
on mouseUp
put field 1 into tData
put the number of words of tData into tNoOfWords
put space & "xxx" after tData -- in case last word of field ends with a return
repeat with i = 1 to tNoOfWords
put word i to i + 1 of tData into tTwoWords
if tTwoWords contains cr then
put word 1 of tTwoWords into field 2
else
-- put word 1 of tTwoWords into field 2
end if
end repeat
end mouseUp
Mind you that a word in LIvecode is not what you expect from the usual meaning of word. In Livecode a word is delimited by space tab or return. So " word. " would count as word but has the period appended. Since you have control over the source of text you can control for that.
Kind regards
Bernd
Re: Referencing the Character Immediately After a Certain Wo
Posted: Thu Mar 20, 2014 2:00 pm
by dunbarx
Bernd raises an important point in the way LC deals with words. This should be carefully considered in all your dealings with the language.
Think about this handler, which uses the native line break to locate CR's in your text, obviating the need to test the chars themselves. Similar to the idea Sparkout mentioned. With a bit of text in a field 1, put this in a button somewhere:
Code: Select all
on mouseUp
get fld 1
repeat with y = 1 to the number of lines in it
put the number of words in line y of it into line y of temp
end repeat
put line 1 of temp into returnList
repeat with y = 2 to the number of lines in temp
put line y of temp + line y-1 of returnList into line y of returnList
end repeat
repeat with y = 1 to the number of words in it
if y is among the lines of returnList then
put "delay"
else
put "normal"
end if
end repeat
end mouseUp
Step through this and see if it does what you want. Then adapt it to your needs.
Craig Newman
Re: Referencing the Character Immediately After a Certain Wo
Posted: Thu Mar 20, 2014 3:51 pm
by Thierry
Here is 2 other ways to do it.
Umm, this one is in fact what sparkout was saying:
Code: Select all
on mouseUp
local lastWordinAline
repeat for each line thisLine in fld 1
put the last word of thisLine into lastWordinAline
repeat for each word aWord in word 1 to -2 of thisLine
put aWord && "normal"
end repeat
put lastWordinAline && "delay"
end repeat
end mouseup
and this one will give some headacke to some of you
Code: Select all
on mouseup
put fld 1 into theText
if the last char of theText is not "\n" then put return after theText
repeat while matchChunk( theText,"(\w+)\.?(\n)?", w1,w2, n1,n2)
put char w1 to w2 of theText into aWord
if n1 is empty then put aWord && "normal"
else put aWord && "delay"
delete char 1 to w2 of theText
end repeat
end mouseup
Regards,
Thierry
Re: Referencing the Character Immediately After a Certain Wo
Posted: Thu Mar 20, 2014 8:23 pm
by theotherbassist
Bernd, that seems like a good approach. I'm aware of the way words are conceptualized in Livecode. It plays right into what I need, because I'm trying to maintain punctuation. I'm just making a reader that shoots text at you one word at a time. I have proper pauses in for all punctuation because it's easy to run a pause script based on the last character of the current word. But when there is a paragraph break in the original text field, I need a pause too. So I don't want it to pause based on the last word of a line, but instead based on whether it is the last word of the paragraph.
Re: Referencing the Character Immediately After a Certain Wo
Posted: Thu Mar 20, 2014 8:37 pm
by bn
Hi Theo,
if I may address you in this way lacking a proper first name.
May I point out that all the other solutions do basically the same and are certainly faster than what I proposed. I just thought is to be easier to figure if I stick to words.
So I don't want it to pause based on the last word of a line, but instead based on whether it is the last word of the paragraph.
A line in Livecode is what in other systems is called a paragraph, they are the same. A line in Livecode is everything between two returns. So if e.g Simon repeats for each line he actually says repeat for each paragraph. Same for the others. You would just have to catch for empty lines.
In my approach it does not matter if there is just a return or 5 returns between the two words. But you have to catch for the last word.
Glad it helped and if you get more into Livecode the way to go in analysing text (chunking) would probably be "repeat for each line" etc. It is orders of magnitude faster than what I proposed.
Kind regards
Bernd
Re: Referencing the Character Immediately After a Certain Wo
Posted: Fri Mar 21, 2014 4:22 am
by jameshale
As the others have mentioned using the line chunk is the simplest solution here.
Often when I code a loop for something I discover a variation in the input material that I need to contend with.
The first thought is usually to just test for it within the loop but this is not always the most efficient.
In your case your presentation handler needs no change at all and adding a test within it for <return>s would be a complication.
However as "words" are contained within "lines" and it is the "words" you are presenting your current handler simply needs to be "contained" within the test for lines.
One way would be to put it into a repeat loop with its own pause.
Code: Select all
--input_text is a variable containing your source material.
Repeat with line_index = 1 to the number of lines in input_text
..you_handler_for_presenting_words
--add pause for end of 'paragraph'
wait x ticks
end repeat
James
Re: Referencing the Character Immediately After a Certain Wo
Posted: Fri Mar 21, 2014 4:41 am
by Simon
OK, who can find the discussion of this type of app on the Use List?
It wasn't that long ago but I guess all the keywords I remembered are incorrect. I was sure "wpm" showed up.
There was a couple of stacks posted.
Simon
Re: Referencing the Character Immediately After a Certain Wo
Posted: Fri Mar 21, 2014 5:57 am
by theotherbassist
Thanks for the replies, everyone. If anybody knows the location of the threads Simon was speaking of, that would be helpful. It's a pretty simple idea that I'm sure a lot of others have done, but it's cool to be able to write my own version. I find it frustrating that livecode is limited to importing text from txt, rtf, csv, or the clipboard though. I know there is an app out there that will do a docx, but it just isn't the same as native support.
Re: Referencing the Character Immediately After a Certain Wo
Posted: Fri Mar 21, 2014 6:17 am
by Simon
Hi theotherbassist,
I remember that it started with a link demonstrating the action.
If you know the company name you can search on that.
Simon
Re: Referencing the Character Immediately After a Certain Wo
Posted: Fri Mar 21, 2014 9:01 am
by bn
Hi Simon, theotherbassist,
topic was brought up by Thierry Douez on the use-list
http://runtime-revolution.278305.n4.nab ... 76554.html
See the thread for more info
Kind regards
Bernd
Re: Referencing the Character Immediately After a Certain Wo
Posted: Sat Mar 22, 2014 6:36 am
by Simon
Great memory again Bernd!
This is the second time you were able remember more about a topic than me.
Simon