Can a function return two values?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Re: Can a function return two values?
Chopping sentences by a word into 'subsentences'.
TheText to be parsed:
I am a Scottish man who lives in Bulgaria who eats cabbages. I see dead people who are freakin' everywhere! Who codes in Java? Do not ask for whom the bell tolls. It tolls for thee.
Note spaces around 'who'.
			
			
									
									
						TheText to be parsed:
I am a Scottish man who lives in Bulgaria who eats cabbages. I see dead people who are freakin' everywhere! Who codes in Java? Do not ask for whom the bell tolls. It tolls for thee.
Code: Select all
put splitSentences(TheText, " who ") into theSplitTextArrayCode: Select all
function splitSentences pText, splitWord
   repeat with s = 1 to the number of sentences of ptext
      get sentence s of ptext
      split it by splitWord
      put it into splitText[s]
   end repeat
    return splitText
end splitSentences- 
				richmond62
- Livecode Opensource Backer 
- Posts: 10202
- Joined: Fri Feb 19, 2010 10:17 am
Re: Can a function return two values?
BUT . . .
does the splitWord include the splitWord in the derived 'subsentences?
BECAUSE if one uses itemDelimiter the itemDelimiter gets lost.
			
			
									
									
						does the splitWord include the splitWord in the derived 'subsentences?
BECAUSE if one uses itemDelimiter the itemDelimiter gets lost.
Re: Can a function return two values?
True. That makes wordOffset more straightforward. It simply identifies a word in the text string, and that value can be either the end or the beginning of each half.BECAUSE if one uses itemDelimiter the itemDelimiter gets lost.
Craig
Re: Can a function return two values?
It's also true that if you already know the split word, it's pretty simple to (although you'd need to be a bit more sophisticated with a returned array, but hardly taxing)
			
			
									
									
						Code: Select all
return splitWord && splitTextRe: Can a function return two values?
Building a bit on Jim's suggestion, here's a more robust function.
I cheated a bit an included relative pronouns in the subordinating conjunctions list.
Still will have some problems with constructs like "no matter who..." and "no matter what..."
and will split "as long as" twice (a second time for the "as") but it's another step along the way.
			
			
									
									I cheated a bit an included relative pronouns in the subordinating conjunctions list.
Code: Select all
constant kSubordinatingConjunctions = "after,although,as,as if, as long as,as much as,as soon as,as though,because,before,even if,even though,how,if,in as much as,in case,in order that,in order to,in so far as,insofar as,just as,lest,no matter,now that,once,provided,provided that,rather than,since,so that,than,that,though,till,unless,until,when,whenever,where,whereas,wherever,whether,which,whichever,while,who,whoever,whoever,whomever,whosever,why"
function splitSentences pText
   local tSplitText, tText
   
   set the itemdelimiter to comma
   replace "(" with space in pText
   replace ")" with space in pText
   repeat with s = 1 to the number of sentences of pText
      put sentence s of ptext into tText
      repeat for each item tSplitWord in kSubordinatingConjunctions
         if space&tSplitWord&space is in tText then
            replace space&tSplitWord&space with cr&tSplitWord&space in tText
         end if
      end repeat
      split tText by cr
      put tText into tSplitText[s]
   end repeat
   breakpoint
   return tSplitText
end splitSentencesand will split "as long as" twice (a second time for the "as") but it's another step along the way.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
						PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Can a function return two values?
A revision does a better job of things.
Still will trip over some simple things like "there it is as you like it" and can't differentiate between subordinating conjunctions and adverbial clauses, but I'm not sure how much that can be automated away.
			
			
									
									Still will trip over some simple things like "there it is as you like it" and can't differentiate between subordinating conjunctions and adverbial clauses, but I'm not sure how much that can be automated away.
Code: Select all
constant kSubordinatingConjunctions = "why,whosever,whomever,whoever,whoever,who,while,whichever,which,whether,wherever,whereas,where,whenever,when,until,unless,till,though,that,than,so that,since,rather than,provided that,provided,once,now that,no matter,lest,just as,insofar as,in so far as,in order to,in order that,in case,in as much as,if,how,even though,even if,before,because,as though,as soon as,as much as,as long as,as if,as,although,after"
function splitSentences pText
   local tSplitText, tText
   
   set the itemdelimiter to comma
   replace "(" with space in pText
   replace ")" with space in pText
   replace comma with space in pText
   repeat with s = 1 to the number of sentences of pText
      put sentence s of ptext into tText
      repeat for each item tSplitWord in kSubordinatingConjunctions
         if space&tSplitWord&space is in tText then
            replace space&tSplitWord&space with cr&tSplitWord&numtochar(3) in tText
         end if
      end repeat
      replace numtochar(3) with space in tText
      split tText by cr
      put tText into tSplitText[s]
   end repeat
   return tSplitText
end splitSentencesPowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
						PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
- 
				richmond62
- Livecode Opensource Backer 
- Posts: 10202
- Joined: Fri Feb 19, 2010 10:17 am
Re: Can a function return two values?
- -can't differentiate between subordinating conjunctions and adverbial clauses
https://youtu.be/sLeG7gxIJx4
SURELY the point of the exercise is to SPLIT a sequence of words in half somewhere and NOT get into
grammatical nit-picking.

Re: Can a function return two values?
MarkW wrote: 
			
			
									
									
						Nice!A revision does a better job of things.
