#70272 - 2002-09-25 04:21 PM
Wordplay for fun [2] Sentence maker
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
This script will take a string of letters and try to generate a sentence from it.
It uses Word for spell checking, and turns up a lot of invalid two letter words for some reason
Long words will take a very long time to run.
You will have to order the words into a sensible sentence yourself.
Example: the word "football" generated: 1: foot ball 2: football 3: fob all to 4: fob lot al 5: fob lot la 6: foal bolt 7: foal blot Before I stopped it.
code:
Global $v,$iBoringCount,$iBoringLength,$iAnagramCount,$iCombo,$asPatterns,$iPatternCount,$t Global $oWord,$oDoc ; ; Change the following values to suit your locale. ; $iBoringCount=3 ; Maximum repeated small word size $iBoringLength=2 ; Length of boring words $t="aio" ; Letters that are valid on their own as "words" $v="123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; Base 34 digits. Global $asPatternHistory[Len($v)+1] ; Start word, and open a blank document. $oWord=CreateObject("word.application") If @ERROR "Could not start Word. Spell checking cannot be done." ? Exit 1 Else $oDoc=$oWord.Documents.Add If @ERROR "Could not create blank document in Word. Spell checking cannot be done." ? $oWord.Quit(0) $oWord=0 Exit 1 EndIf EndIf Do ? "Enter a string to generate sentences (shorter is quicker!): " Gets $sString $iLen=Len($sString) If $iLen If $iLen>Len($v) "Sorry! maximum " Len($v) " characters in the string." ? Else If NOT $asPatternHistory[$iLen] "Creating word combinations patterns for $iLen letters..." ? udfMakeCombo($iLen) ; Drop the trailing space character $asPatternHistory[$iLen]=Trim($asPatternHistory[$iLen]) EndIf $asPatterns=Split($asPatternHistory[$iLen]) $iPatternCount=Ubound($asPatterns) "Ok, generating sentences now..." ? $iAnagramCount=0 udfAnagram($sString,"udfCheck($$sAnagram)") If $iAnagramCount=0 Chr(13) " No sentences found. " ? Else Chr(13) $iAnagramCount " sentences found." ? EndIf EndIf EndIf Until $iLen=0 $oDoc=0 $oWord.Quit(0) $oWord=0 Exit 0 Function udfMakeCombo($iLen,Optional $iLast,Optional $sCombination,Optional $iStart) Dim $i If Not $iStart $iStart=$iLen $iLast=$iLen EndIf If $iLast>$iLen $iLast=$iLen EndIf If $iLen For $i=1 To $iLast udfMakeCombo($iLen-$i,$i,""+$sCombination+SubStr($v,$i,1),$iStart) Next Else ; Drop combinations with too many repeated characters ; as these are likely to be boring. ; This will need to be disabled for very long strings. For $i=2 To Len($sCombination) If SubStr($sCombination,$i,1) <= $iBoringLength AND (SubStr($sCombination,$i-1,1)=SubStr($sCombination,$i,1)) $iLen=$iLen+1 If $iLen>=$iBoringCount Return EndIf Else $iLen=0 EndIf Next $asPatternHistory[$iStart]=""+$asPatternHistory[$iStart]+$sCombination+" " EndIf EndFunction Function udfCheck($) Dim $i,$s,$l,$o,$p,$e $iCombo=$iCombo+1 Chr(13) "$iCombo" For Each $p in $asPatterns $s="" $o=1 $l=Len($p) For $i=1 To $l $e=InStr($v,SubStr($p,$i,1)) If $e=1 If InStr($t,SubStr($,$o,1))=0 Goto Done EndIf EndIf $s=$s+SubStr($,$o,$e)+" " $o=$o+$e Next $oWord.selection.typeText("Spell "+$s) If @ERROR Return EndIf $oSpelling=$oDoc.SpellingErrors If @ERROR Return EndIf If $oSpelling.Count=0 $iAnagramCount=$iAnagramCount+1 Chr(13) " " $iAnagramCount ": " $s" " ? EndIf ; Undo the typing. $null=$oDoc.Undo :Done Next EndFunction Function udfAnagram($sSeed,$sFunction,Optional $sAnagram) Dim $i If($sSeed) If $sAnagram="" For $i=1 to Len($sSeed)-1 If SubStr($sSeed,$i,1)>SubStr($sSeed,$i+1,1) $sSeed=""+ SubStr($sSeed,1,$i-1)+ SubStr($sSeed,$i+1,1)+ SubStr($sSeed,$i,1)+ SubStr($sSeed,$i+2) EndIf Next EndIf For $i=1 to Len($sSeed) If SubStr($sSeed,$i,1)<>SubStr($sSeed,$i+1,1) $udfAnagram=udfAnagram(Left($sSeed,$i-1)+SubStr($sSeed,$i+1),$sFunction,$sAnagram+SubStr($sSeed,$i,1)) EndIf Next Else $=Execute($sFunction) EndIf EndFunction
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 657 anonymous users online.
|
|
|