Jump to content

Function that will return a random spun parameter's value with spin syntax


Go to solution Solved by Melba23,

Recommended Posts

How to create a function that have a single parameter that contains an article with spin syntax, e.g.:

$param = "{This|This particular|This kind of|This specific|That} {is one of the|is among the|is probably the|is just about the|is amongst the} {sentences|phrases|content|essay sentences|paragraphs} {in the|within the|inside the|inside|from the} {article|post|write-up|content|document} {with|along with|together with|using|having} {spin|rewrite|spin and rewrite|whirl|rotate} {syntax|format}."

And the function that I want to create is such like this:

$return = spinSyntax($param)

Which will return a random spun article that's stored in the variable $param , in every call such like this:

This is one of the paragraphs inside post with spin format.

This topic will be one of the important information here in the AutoIt library if it contain a right solution. Please help..

Link to comment
Share on other sites

Something like this.  Modify it to suite your needs:

$param = "{This|This particular|This kind of|This specific|That} {is one of the|is among the|is probably the|is just about the|is amongst the} {sentences|phrases|content|essay sentences|paragraphs} {in the|within the|inside the|inside|from the} {article|post|write-up|content|document} {with|along with|together with|using|having} {spin|rewrite|spin and rewrite|whirl|rotate} {syntax|format}."
$a = StringRegExp($param,"\{([^|]+)\|([^|]+)\|?([^|]+)?\|?([^|]+)?\|?([^|]+)?\}",4)
For $i = 0 to UBound($a)-1
    $aTemp = $a[$i]
    ConsoleWrite($aTemp[Random(1,UBound($aTemp)-1,1)] & @CRLF)
Next
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • Moderators

Servant,

Took me about 2 minutes to write something using a RegEx and StringSplit. What have you tried so far that has not worked? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

 

Something like this.  Modify it to suite your needs:

$param = "{This|This particular|This kind of|This specific|That} {is one of the|is among the|is probably the|is just about the|is amongst the} {sentences|phrases|content|essay sentences|paragraphs} {in the|within the|inside the|inside|from the} {article|post|write-up|content|document} {with|along with|together with|using|having} {spin|rewrite|spin and rewrite|whirl|rotate} {syntax|format}."
$a = StringRegExp($param,"\{([^|]+)\|([^|]+)\|?([^|]+)?\|?([^|]+)?\|?([^|]+)?\}",4)
For $i = 0 to UBound($a)-1
    $aTemp = $a[$i]
    ConsoleWrite($aTemp[Random(1,UBound($aTemp)-1,1)] & @CRLF)
Next

Wow, you found a good solution in a matter of minutes.. But please see below!

Servant,

Took me about 2 minutes to write something using a RegEx and StringSplit. What have you tried so far that has not worked? :huh:

M23

Sorry, 'cause I'm confused and I'm always forgot the regex because what's in my mind is the question below...

What if the value of the parameter is such like this:

$param = "Note {this is the|this is actually the|here is the|this can be a|this can be the} other {value|worth|benefit|price|importance} of parameter."

The solution of jdelaney will not work properly in that parameter..

Link to comment
Share on other sites

  • Moderators

Servant,

So the answer to my question is "nothing" - and you expect us to do all the work for you. :(

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Well, now you are changing the structure.  If you wrap the other data with the {}, then it will work with a slight modification of the regexp.  Look at the structure, and try to solve it.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • Moderators
  • Solution

Servant,

As I have nothing better to do for the moment, try this:

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

$sParam = "{This|This particular|This kind of|This specific|That} {is one of the|is among the|is probably the|is just about the|is amongst the} {sentences|phrases|content|essay sentences|paragraphs} {in the|within the|inside the|inside|from the} {article|post|write-up|content|document} {with|along with|together with|using|having} {spin|rewrite|spin and rewrite|whirl|rotate} {syntax|format}."

While 1
    If MsgBox($MB_YESNO + $MB_SYSTEMMODAL, "Result", _SpinSyntax($sParam) & @CRLF & @CRLF & "Continue?") = 7 Then ExitLoop
WEnd

$sParam = "Note {this is the|this is actually the|here is the|this can be a|this can be the} other {value|worth|benefit|price|importance} of parameter."

While 1
    If MsgBox($MB_YESNO + $MB_SYSTEMMODAL, "Result", _SpinSyntax($sParam) & @CRLF & @CRLF & "Continue?") = 7 Then ExitLoop
WEnd

Func _SpinSyntax($sParam)

    Local $aSplit = StringSplit($sParam, "{}")
    Local $sResult = ""
    For $i = 1 To $aSplit[0]
        $aOption = StringSplit(StringStripWS($aSplit[$i], $STR_STRIPLEADING + $STR_STRIPTRAILING), "|")
        If @error Then
            $sResult &= $aOption[1] & " "
        Else
            $sResult &= $aOption[Random(1, $aOption[0], 1)] & " "
        EndIf
    Next
    Return $sResult

EndFunc
Works for me. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I was making mine work, and came across this:

$iRandom = Random(1,1,1)
$iError = @error
ConsoleWrite($iRandom & @CRLF)
ConsoleWrite($iError & @CRLF)

output:

0
1

shouldn't random of 1 to 1 always return 1?  Random of 1 to 2 can return 1 or 2.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • Moderators

jdelaney,

It has been discussed many times and the Devs have always maintained that having the same value as maximum and minimum means that the result is actually not random. Hence you need the @error check as I used. At least nowadays the Help file does clearly state what happens. ;)

M23

Edited by Melba23
Typo

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Oops, didnt check that, thanks.

Anyways, for my code to work on the new string:

$param = "{Something}{This|This particular|This kind of|This specific|That} {is one of the|is among the|is probably the|is just about the|is amongst the} {sentences|phrases|content|essay sentences|paragraphs} {in the|within the|inside the|inside|from the} {article|post|write-up|content|document} {with|along with|together with|using|having} {spin|rewrite|spin and rewrite|whirl|rotate} {syntax|format} {Done}."
$a = StringRegExp($param,"\{([^{|}]+)\|?([^{|}]+)?\|?([^{|}]+)?\|?([^{|}]+)?\|?([^{|}]+)?\}",4)
For $i = 0 to UBound($a)-1
    $aTemp = $a[$i]
    If UBound($aTemp) = 2 Then
        ConsoleWrite($aTemp[1] & @CRLF)
    Else
        ConsoleWrite($aTemp[Random(1,UBound($aTemp)-1,1)] & @CRLF)
    EndIf
Next

Notice that I placed {} surrounding all the options.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

I think Random() ought to be able to handle any range, so it can be used as jdelaney did. I also believe Max and Min ought to be Val1 and Val2 with no preferance of magnitude. This seems most logical to me. This would be adding functionality without breaking anything.

Edited by czardas
Link to comment
Share on other sites

  • Moderators

czardas,

Then put in a feature request - the main opposition to the proposal in the past is no longer in posisiton to block it. I for one would support a plea for Min = Max to return the repeated value. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sure, it can always be rejected. After thinking about this, it might break scripts which rely on this error. I'll first have a think. I wasn't aware that Random() would never return 1 - that's most unfortunate.

The result will be in the range of Min to Max INCLUSIVE when using integers (just short of Max when using floats).

Edited by czardas
Link to comment
Share on other sites

Here my version:
 

#include <Array.au3>
$param = "{This|This particular|This kind of|This specific|That} {is one of the|is among the|is probably the|is just about the|is amongst the} {sentences|phrases|content|essay sentences|paragraphs} {in the|within the|inside the|inside|from the} {article|post|write-up|content|document} {with|along with|together with|using|having} {spin|rewrite|spin and rewrite|whirl|rotate} {syntax|format}."

MsgBox(0, "Spin Syntax", Spin($param))

Func Spin($param)
    Local $a = StringRegExp($param, "(?U){(.+)}", 3)
    If @error Then Return SetError(1, 0, 0)
    Local $i, $b, $sSpin
    For $i = 0 To UBound($a) - 1
        $b = StringSplit($a[$i], "|", 2)
        $sSpin &= $b[Random(0, UBound($b) - 1, 1)] & " "
    Next
    $sSpin = StringTrimRight($sSpin, 1) & "."
    Return $sSpin
EndFunc

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Commented.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...