Paragraphs and sentences

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
mtecedor
Posts: 68
Joined: Fri Sep 04, 2009 6:40 pm

Paragraphs and sentences

Post by mtecedor » Tue May 25, 2010 6:56 pm

Hi All,

I am stuck in the first stepts of something that should be fairly easy but I cannot find the appropriate way to do it.

I have a textfield containing five paragraphs. I need the application to go over that textfield and copy the first sentence of each paragraph in five empty textfields.

My first problem is that I cannot even identify the paragraphs. This is what I wrote

Code: Select all

on OpenCard
put the text of field "Text2" of card "text" into tText
set the itemdel to cr
put the number of items in tText into tPar
If I understood correctly, cr is the return key, so the content of tPar should be five. Instead, when I check the content of tPar I got 26, which is the number of sentences in the text.

So, my question is, how do I get the number of paragraphs? And if setting the itemdel to cr is giving me the number of sentences, could I use that information to select only the first sentence of each paragraph?

Maybe there is an easier way to do it and I am just making it more complicated that it is :shock:

Thanks

Marta

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Paragraphs and sentences

Post by dunbarx » Tue May 25, 2010 8:04 pm

You meant, I guess, that 26 was the number of lines. The number of sentences would be separated by periods, no?

The question is, how do you delimit paragraphs? If you have an empty line separating them, as in:

Now is the time
for all good men.

To come to the
aid of their country.

then you have a hook to parsing paragraphs. It would be that two adjacent returns exist in a row. With this, you can take your text, put it into a variable, replace "return & return" with some little used char (how about ASCII 244?), set the itemdelimiter to that char, and then get the first sentence of each item. The first sentence would be char 1 to the offset of the period that ends the sentence.

This assumes you have no periods in the text of the first sentence, of course. These things are never easy.

If you do not separate paragraphs that way, tell us how you do.

Craig Newman
Last edited by dunbarx on Tue May 25, 2010 8:17 pm, edited 1 time in total.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Paragraphs and sentences

Post by bn » Tue May 25, 2010 8:16 pm

Marta,
I don't know why your script does not work, seems allright.
In Rev everything in a paragraph (something ending with a return) is a line. So you could say

Code: Select all

   put field "Text2" of card "text" into tText
   -- the number of paragraphs
   put the number of lines of tText into tCountLines -- here is 
   set the itemdelimiter to "." 
   -- get the first sentences, this assumes they end with a period
   repeat with i = 1 to tCountLines 
      put item 1 of line i of tText & "." & return after tCollectFirstSentences
   end repeat
   delete last char of tCollectFirstSentences  -- a return
   put tCountLines & cr & tCollectFirstSentences
this puts the number of paragraphs/lines and the first sentence of each line into the message box.
See how it goes. If it still does not work and still gives you 26 lines, then there is something wrong with your text.
Regards
Bernd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Paragraphs and sentences

Post by dunbarx » Tue May 25, 2010 8:19 pm

Bernd.

The number of lines is not the same as the number of paragraphs. In my example:

Now is the time
for all good men.

To come to the
aid of their country.

There are five lines, but only two paragraphs. I think this is what the issue is.

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Paragraphs and sentences

Post by bn » Tue May 25, 2010 8:38 pm

Craig,
I thought of the way texteditors treat a paragraph (everthing between 2 returns, everything before first return and everything after last return (except when empty)) and the lines only as apparent lines, since they wrap around at the right border automatically. If this assumption is not the case of course it gets more complicated, as you pointed out.
regards
Bernd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Paragraphs and sentences

Post by dunbarx » Tue May 25, 2010 9:57 pm

Bernd.

I'm with you. In the usual case a return does delimit a paragraph, and an additional return generally follows for form's sake.

But he pointed out that he had a line count (I think), that is, a whole lot more returns than you would expect, or need. I don't know if he uses returns in some way that simple text entry does not require. So I just latched onto the one distinguishing characteristic that we could isolate, two returns in a row.

Craig

mtecedor
Posts: 68
Joined: Fri Sep 04, 2009 6:40 pm

Re: Paragraphs and sentences

Post by mtecedor » Tue May 25, 2010 11:04 pm

Hi Bernd, Craig,

Thanks a lot for your help. I finally figure out (or that's what I think) what's going on. My paragraphs are delimited by return, but I was thinking in a "normal" way of understanding lines, and it seems that is not the case in revolution. 26 are the number of sentences in the textfield.

In any case, that is even better for me, because I just select what item I want to put into everyf field.

Thanks a lot for your help :)

Marta

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Paragraphs and sentences

Post by dunbarx » Tue May 25, 2010 11:38 pm

Marta.

Not exactly sure what you really have here. "Sentences" are not a concept native to Revolution.

But if you are back in control, well and good.

Craig

mtecedor
Posts: 68
Joined: Fri Sep 04, 2009 6:40 pm

Re: Paragraphs and sentences

Post by mtecedor » Wed May 26, 2010 2:02 pm

Craig,

I thought that it was giving me the number of lines because I set the itemdel. If that number is not the number of sentences, then I don't understand what it is. Because definitely is not the number of lines (I have around 100) neither the number of paragraphs (the text only have 5 + two lines delimited by cr). Now I am worry that it works with my current text but not when I change it.

Marta

Klaus
Posts: 14193
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Paragraphs and sentences

Post by Klaus » Wed May 26, 2010 2:17 pm

Hi Marta,

if you write a loooooong sentence without any RETURN into a (new) field with its "dontwrap" property set to false then
it LOOKS like many lines in that field, although this is only ONE line for Rev. "lines" are separated by CR/RETURN
(Remember that Rev does not know what a "sentence" is!)

Then do this:
...
put the num of lines of fld "one looooong text without CR"
-> 1
...
put the num of lines of the formattedtext of fld "one looooong text without CR"
-> the number of the visible lines

Maybe this helps you understand whats going on there?
But maybe I just don't understand what your problem is :-D


Best from germany

Klaus

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Paragraphs and sentences

Post by bn » Wed May 26, 2010 2:19 pm

Marta,
Rev refers to what is called a paragraph in a word processor to a line. So the number of lines of a text is the number of paragraphs.

In a field you have wrapping of text. If a text is longer than the width of the field the text wraps around. This has no effect on the number of lines, since lines are ended by a return. Automatic wrapping of text does not add a return.

A sentence is usually delimited by a period, a question mark or an exclamation mark. Rev does not know what a sentence is. You have to script that.
In the example I have set the itemdelimiter to "." . Than I take item 1 of a line as sentence 1.

That is what I understood what you wanted to do.

If your text has sentences that end with a question mark or an exclamation mark one would have to test for those also to get the first sentence.

If you apply these rules to your script there is no reason why it should not work with any text you have in your field.

I hope this clarifies this a bit.

If you like you can upload a simple example stack with your field/text and it would be easier to comment on the type of problem you describe. You have to zip the stack before you can upload it. You upload it with the "Upload attachment" option in the reply form. Don't forget to "add the file"

regards
Bernd

Post Reply