svenadamsson Posted March 20, 2012 Share Posted March 20, 2012 I am trying to make a UDF that takes a text in spintax format {word1|word2|word3} and returns one instance of that text. For some reason it deletes the previous replaced segments. The whole sentence should be replaced. I don't know how to proceed. One correct version: Howdy People! AutoIt is an amazing programming language. expandcollapse popup ;Spintax Test $nOffset = 1 ; Text in Spintax format $spintext = '{Hello|Hi|Howdy} {World|People|Dude}! {AutoIt|C++|C|Java} is an {awesome|amazing} {language|programming language}.' while 1 ;Find spintax segements using RegEx $array = StringRegExp($spintext, '\{(.*?)\}', 1, $nOffset) If @error = 0 Then $nOffset = @extended Else ExitLoop EndIf for $i = 0 to UBound($array)-1 $splitarray = StringSplit($array[$i], '|') ; Split the segements into single words $rnd = random($i+1,UBound($splitarray)) ; randomize $choice = $splitarray[$rnd] ; Choose one word at random Next $find = '\{(.*?)' & $choice & '(.*?)\}' ; concat to find spintax segnemt $replace = StringRegExpReplace($spintext, $find, $choice) ; replace spintax segment with choice msgbox(0, "RegExp Test", $replace) WEnd Link to comment Share on other sites More sharing options... qsek Posted March 20, 2012 qsek Active Members 264 Share Posted March 20, 2012 (edited) Hi to the forums! Please use AutoIt Tags My try on this: ;Spintax Test ; Text in Spintax format $spintext = '{Hello|Hi|Howdy} {World|People|Dude}! {AutoIt|C++|C|Java} is an {awesome|amazing} {language|programming language}.' ConsoleWrite(Spintax($spintext) & @CRLF) ConsoleWrite(Spintax($spintext) & @CRLF) ConsoleWrite(Spintax($spintext) & @CRLF) ConsoleWrite(Spintax($spintext) & @CRLF) Func Spintax($sInput) $SpinArray = StringRegExp($sInput, '{(.*?)}',3); Global matches $TrimmedText = StringRegExpReplace($sInput, '{(.*?)}',chr(0x1A)); or any other special char to mark where to insert $RNDText = $TrimmedText For $i = 0 To UBound($SpinArray)-1 $WordArray = StringSplit($SpinArray[$i], "|",3) $rnum = Random(0, UBound($WordArray)-1, 1); get random Array index $RNDText = StringReplace($RNDText, chr(0x1A), $WordArray[$rnum],1); replace only the next occurrence Next Return $RNDText EndFunc Edited March 20, 2012 by qsek Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite Link to comment Share on other sites More sharing options...
svenadamsson Posted March 20, 2012 Author Share Posted March 20, 2012 Thanks a lot. Looks to be working just fine. Link to comment Share on other sites More sharing options...
JasonDee Posted July 12, 2012 Share Posted July 12, 2012 Hmmm, anyone have a quick fix for nested spintax? Note the brackets within brackets: I {thought this {image|picture|impression|photograph} was {interesting|fascinating} and {wanted to|desired to|wished to} {share|reveal|discuss}|{wanted to|desired to|wished to} {share|reveal|discuss} and thought this {image|picture|impression|photograph} was {interesting|fascinating}}. Thanks. Link to comment Share on other sites More sharing options...
qsek Posted July 12, 2012 Share Posted July 12, 2012 this a good example of how exponential complicated a quite simple solution of a task can get, if you make an additional extension to the rules of the task. RegEx doesnt work anymore, because you cant tell it to skip matched brackets (at least without conditional backtracking statements O_o) So we have to mak a 1 char sequencing loop, which records the position and nesting level of all brackets in the string. Then evaluate the brackets in the reverse order. This begs for a recursive function which, i can tell you, will not be in anyway a quick fix Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite Link to comment Share on other sites More sharing options...
qsek Posted July 12, 2012 Share Posted July 12, 2012 (edited) I just had a couple of eureka moments. This is the kind of mindpuzzle where i forget to eat and drink until i solved it. expandcollapse popup;Spintax Test #include <Array.au3> ; Text in Spintax format ;~ Global $a = 1 $spintext = '{{Hello|Hi|Howdy} {World|People|Dude}!}|{{Good Day|Excuse Me|Greetings} {Everyone|Ladys and Gents|Mister},} {AutoIt|C++|C|Java} is a{n {awesome|amazing}}|{{ wonderful|n astonishing}} {language|programming language}.' ConsoleWrite(Spintax($spintext) & @CRLF) ConsoleWrite(Spintax($spintext) & @CRLF) ConsoleWrite(Spintax($spintext) & @CRLF) ConsoleWrite(Spintax($spintext) & @CRLF) Func Spintax($sInput) ;~ ConsoleWrite("- Iteration "&$a&" Start -" & @CRLF) $SpinArray = StringRegExp($sInput, '{([^{]*?|[^{]*?)}',3); Global matches ;~ _ArrayDisplay($SpinArray,"") $sfinrep = StringReplace($sInput,"}|{","|") ; look For final replacing If Not IsArray($SpinArray) Then ;~ ConsoleWrite("!No Array!" & @CRLF) If $sfinrep = $sInput then ; Nothing replaced, so free of }|{ --> end string ;~ ConsoleWrite("+EndResult: " & @CRLF) Return $sInput EndIf ;else replace workstring with the string without }|{ for further processing $sInput = $sfinrep $SpinArray = StringRegExp($sInput, '{([^{]*?|[^{]*?)}',3); Global matches ;~ ConsoleWrite("final replaced string:" & @CRLF) ;~ ConsoleWrite($sInput & @CRLF) EndIf $TrimmedText = StringRegExpReplace($sInput, '{([^{]*?|[^{]*?)}',chr(0x1A)); or any other special char to mark where to insert $RNDText = $TrimmedText For $i = 0 To UBound($SpinArray)-1 $WordArray = StringSplit($SpinArray[$i], "|",3) $rnum = Random(0, UBound($WordArray)-1, 1); get random Array index $RNDText = StringReplace($RNDText, chr(0x1A), $WordArray[$rnum],1); replace only the next occurrence Next ;~ ConsoleWrite($RNDText & @CRLF) ;~ ConsoleWrite("- Iteration "&$a&" End -" & @CRLF) ;~ $a += 1 Return Spintax($RNDText) EndFunc Edited July 12, 2012 by qsek Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite Link to comment Share on other sites More sharing options...
JasonDee Posted July 12, 2012 Share Posted July 12, 2012 Wow, very nice bro, works perfectly, if I wasn't a noob to the forum I would +Like. Thank you much, works perfectly! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now