Function to Upper and Lower Case sentences
J. Landman Gay
jacque at hyperactivesw.com
Thu Dec 22 16:03:57 EST 2011
On 12/21/11 7:00 PM, Sivakatirswami wrote:
> well I got this far..
I tinkered with this a little bit last night because I thought a regex
solution would work, but my skills aren't good enough. So I ended up
with something similar to what you've already done.
> #I think there is a better trim leading/trailing space function I saw
> years ago...
I think you're looking for:
put word 1 to -1 of aSentence into aSentence
> replace "!" with "! " in aSentence
>
> # doesn't help unless next word is in my dictionary.
> # so I need something to "see" exclamation marks as delimiters, not sure
> # how to tackle that if dot is already set as delimiter
I tried using tokens instead of words, which avoids a lot of the problem.
>
> function capitalizeWords theWord
> put "ganesha, pancha, gurudeva!,gurudev!,Satguru, ganapathi, ganapati,
> yogaswami, siva, shiva, muruga, bodhinatha,lord, nataraja, aum" into
> tCapsDictionary
>
> set the itemdel to comma
>
> if tCapsDictionary contains theWord then
>
> --if theWord is among the items of tCapsDictionary
> ## doesn't work; dunno why...
> # so I used "contains"
The reason it fails is due to the spaces in the comma-delimited
dictionary string. "Among the items of" looks for exact matches, and so
won't match a word without a leading space. Remove the spaces from the
dictionary list and it works.
Here is what I ended up with, but there are still problems with it. It
doesn't account for all possibilities, but it's a start:
on mouseUp
local tNewSentence
put the clipboarddata["text"] into tInput
set the linedel to "."
repeat for each line aSentence in tInput
put toLower(word 1 to -1 of aSentence) into aSentence -- the shortcut
put toUpper(char 1 of aSentence) into char 1 of aSentence
repeat for each token theWord in aSentence
if len(theWord) = 1 and charToNum(theWord) < 65 then -- punctuation
if theWord is in "(<{[" then
put space & theWord after tNewSentence
else
put theWord & space into last char of tNewSentence
end if
else -- real words
put capitalizeWords(theWord) into tWord
put tWord & space after tNewSentence
end if
end repeat
delete char -1 of tNewSentence -- can incorrectly delete; probably
should data-check it
put tNewSentence &". " after tOutPut
put empty into tNewSentence
end repeat
if char 1 of tOutput = "." then delete char 1 of tOutput
set the clipboarddata["text"] to tOutput
put tOutput
end mouseUp
function capitalizeWords theWord
put
"ganesha,pancha,gurudeva!,gurudev!,Satguru,ganapathi,ganapati,yogaswami,siva,shiva,muruga,bodhinatha,lord,nataraja,aum"
into tCapsDictionary
if theWord is among the items of tCapsDictionary then
put toUpper(char 1 of theWord) into char 1 of theWord
end if
return theWord
end capitalizeWords
I still think it could be done with regex in way fewer lines...
--
Jacqueline Landman Gay | jacque at hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
More information about the use-livecode
mailing list