Make Word Combination Lists (Up To 12 Words)

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Tigerlily
Posts: 283
Joined: 04 Oct 2018, 22:31

Make Word Combination Lists (Up To 12 Words)

23 Mar 2019, 03:02

Hello Friends,

I am making a script that takes a string of words on your clipboard and gives you a list of every permutationcombination, starting with 1 word variations, then 2 word combo variations, then 3 word combo variations, and so on. I only plan to support strings that contain 10 to 12 words at maximum.

I've gotten to 4 word permutationscombinations by writing the code out line by line so I can try to distinguish some sort of algorithmic pattern, in hopes that I can write the next 8 levels of permutationcombination lists using some nice algorithm that makes sense to my brain. I've looked for word combo posts, but all I could find was just for strings that consist of letters and numbers, then reorganizing those letters and numbers. This seems more simple than that, but oddly I don't see any post about it.

Maybe you know of such post that I could glean more knowledge from? Or possibly you may want to take a stab at this for fun ;)

Here is my code so far, maybe you will see a pattern before I do :shifty: :

Also, open to help with writing my functions.. I feel very novice at it .. :?

Code: Select all

#NoEnv
#SingleInstance, force
SetBatchLines, -1


Clipboard := "QXGA+ Dynamic AMOLED Display"
#=::
	Word := StrSplit(Clipboard, A_Space) 
,	NumberOfWords := Word.Length()	
,	(NumberOfWords = 2) ? Combination := TwoWords() 
:	(NumberOfWords = 3) ? Combination := ThreeWords() 
: 	(NumberOfWords = 4) ? Combination := FourWords() : Combination := "Too many / little words"

MsgBox % Clipboard := Combination
; Clipboard := StrReplace(Combination, A_Space, A_Tab)
return

TwoWords() {

local FinalWordsList, 1WordsList, 2WordsList

	For each in Word
		1WordsList .= Word[A_Index] "`n"

		2WordsList .= Word[1] " " Word[2] "`n"
	,	2WordsList .= Word[2] " " Word[1] "`n"
	
	return	FinalWordsList := 1WordsList . 2WordsList
}			
		
ThreeWords() {

local FinalWordsList, 1WordsList, 2WordsList, 3WordsList
	
	For each in Word
		1WordsList .= Word[A_Index] "`n"

		2WordsList .= Word[1] " " Word[2] "`n"
	,	2WordsList .= Word[1] " " Word[3] "`n"
	,	2WordsList .= Word[2] " " Word[1] "`n"
	,	2WordsList .= Word[2] " " Word[3] "`n"
	,	2WordsList .= Word[3] " " Word[1] "`n"
	,	2WordsList .= Word[3] " " Word[2] "`n"
	
	,	3WordsList .= Word[1] " " Word[2] " " Word[3] "`n"
	,	3WordsList .= Word[1] " " Word[3] " " Word[2] "`n"
	,	3WordsList .= Word[2] " " Word[1] " " Word[3] "`n"
	,	3WordsList .= Word[2] " " Word[3] " " Word[1] "`n"
	,	3WordsList .= Word[3] " " Word[2] " " Word[1] "`n"
	,	3WordsList .= Word[3] " " Word[1] " " Word[2] "`n"
	
	return	FinalWordsList := 1WordsList . 2WordsList . 3WordsList
}			
		
FourWords() {

local FinalWordsList, 1WordsList, 2WordsList, 3WordsList, 4WordsList

	For each in Word
		1WordsList .= Word[A_Index] "`n"

		2WordsList .= Word[1] " " Word[2] "`n"
	,	2WordsList .= Word[1] " " Word[3] "`n"
	,	2WordsList .= Word[1] " " Word[4] "`n"
	,	2WordsList .= Word[2] " " Word[1] "`n"
	,	2WordsList .= Word[2] " " Word[3] "`n"
	,	2WordsList .= Word[2] " " Word[4] "`n"
	,	2WordsList .= Word[3] " " Word[1] "`n"
	,	2WordsList .= Word[3] " " Word[2] "`n"
	,	2WordsList .= Word[3] " " Word[4] "`n"
	,	2WordsList .= Word[4] " " Word[1] "`n"
	,	2WordsList .= Word[4] " " Word[2] "`n"
	,	2WordsList .= Word[4] " " Word[3] "`n"
	
	,	3WordsList .= Word[1] " " Word[2] " " Word[3] "`n"
	,	3WordsList .= Word[1] " " Word[2] " " Word[4] "`n"
	,	3WordsList .= Word[1] " " Word[3] " " Word[2] "`n"
	,	3WordsList .= Word[1] " " Word[3] " " Word[4] "`n"
	,	3WordsList .= Word[1] " " Word[4] " " Word[2] "`n"
	,	3WordsList .= Word[1] " " Word[4] " " Word[3] "`n"		
	,	3WordsList .= Word[2] " " Word[1] " " Word[3] "`n"
	,	3WordsList .= Word[2] " " Word[1] " " Word[4] "`n"
	,	3WordsList .= Word[2] " " Word[3] " " Word[1] "`n"
	,	3WordsList .= Word[2] " " Word[3] " " Word[4] "`n"
	,	3WordsList .= Word[2] " " Word[4] " " Word[1] "`n"
	,	3WordsList .= Word[2] " " Word[4] " " Word[3] "`n"	
	,	3WordsList .= Word[3] " " Word[1] " " Word[2] "`n"
		3WordsList .= Word[3] " " Word[1] " " Word[4] "`n"
	,	3WordsList .= Word[3] " " Word[2] " " Word[1] "`n"
	,	3WordsList .= Word[3] " " Word[2] " " Word[4] "`n"
	,	3WordsList .= Word[3] " " Word[4] " " Word[1] "`n"
	,	3WordsList .= Word[3] " " Word[4] " " Word[2] "`n"
	,	3WordsList .= Word[4] " " Word[1] " " Word[2] "`n"
	,	3WordsList .= Word[4] " " Word[1] " " Word[3] "`n"
	,	3WordsList .= Word[4] " " Word[2] " " Word[1] "`n"
	,	3WordsList .= Word[4] " " Word[2] " " Word[3] "`n"
	,	3WordsList .= Word[4] " " Word[3] " " Word[1] "`n"
	,	3WordsList .= Word[4] " " Word[3] " " Word[2] "`n"
	
		4WordsList .= Word[1] " " Word[2] " " Word[3] " " Word[4] "`n"	
	,	4WordsList .= Word[1] " " Word[2] " " Word[4] " " Word[3] "`n"	
	,	4WordsList .= Word[1] " " Word[3] " " Word[2] " " Word[4] "`n"	
	,	4WordsList .= Word[1] " " Word[3] " " Word[4] " " Word[2] "`n"	
	,	4WordsList .= Word[1] " " Word[4] " " Word[2] " " Word[3] "`n"	
	,	4WordsList .= Word[1] " " Word[4] " " Word[3] " " Word[2] "`n"		
	,	4WordsList .= Word[2] " " Word[1] " " Word[3] " " Word[4] "`n"	
	,	4WordsList .= Word[2] " " Word[1] " " Word[4] " " Word[3] "`n"	
	,	4WordsList .= Word[2] " " Word[3] " " Word[1] " " Word[4] "`n"	
	,	4WordsList .= Word[2] " " Word[3] " " Word[4] " " Word[1] "`n"	
	,	4WordsList .= Word[2] " " Word[4] " " Word[1] " " Word[3] "`n"	
	,	4WordsList .= Word[2] " " Word[4] " " Word[3] " " Word[1] "`n"			
	,	4WordsList .= Word[3] " " Word[1] " " Word[2] " " Word[4] "`n"	
	,	4WordsList .= Word[3] " " Word[1] " " Word[4] " " Word[2] "`n"	
	,	4WordsList .= Word[3] " " Word[2] " " Word[1] " " Word[4] "`n"	
	,	4WordsList .= Word[3] " " Word[2] " " Word[4] " " Word[1] "`n"	
	,	4WordsList .= Word[3] " " Word[4] " " Word[1] " " Word[2] "`n"	
		4WordsList .= Word[3] " " Word[4] " " Word[2] " " Word[1] "`n"			
	,	4WordsList .= Word[4] " " Word[1] " " Word[2] " " Word[3] "`n"	
	,	4WordsList .= Word[4] " " Word[1] " " Word[3] " " Word[2] "`n"	
	,	4WordsList .= Word[4] " " Word[2] " " Word[1] " " Word[3] "`n"	
	,	4WordsList .= Word[4] " " Word[2] " " Word[3] " " Word[1] "`n"	
	,	4WordsList .= Word[4] " " Word[3] " " Word[1] " " Word[2] "`n"	
	,	4WordsList .= Word[4] " " Word[3] " " Word[2] " " Word[1] "`n"	
	
		return	FinalWordsList := 1WordsList . 2WordsList . 3WordsList . 4WordsList
}
Last edited by Tigerlily on 24 Mar 2019, 15:02, edited 3 times in total.
-TL
User avatar
Tigerlily
Posts: 283
Joined: 04 Oct 2018, 22:31

Re: Word Combinations

23 Mar 2019, 03:40

SL5 wrote:
23 Mar 2019, 03:31
BTW its called permutation. here a c++ example: https://codereview.stackexchange.com/questions/101320/displaying-all-permutations-of-a-string
Thank you for the correction.

EDIT: actually after looking more into permutations vs. combinations, I am not looking for permutations, but combinations. Order does not matter here aside from getting all of the different orders without repetition.

Unfortunately, after studying the answer at the link that example does not help me because I'm not sure how to translate that.. and I don't know how to use the standard lib for C++ in AHK. I appreciate the resource and help though. Thanks! :thumbup:
Last edited by Tigerlily on 24 Mar 2019, 15:04, edited 1 time in total.
-TL
User avatar
SL5
Posts: 877
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Word Combinations

23 Mar 2019, 03:44

Tigerlily wrote:
23 Mar 2019, 03:40
SL5 wrote:
23 Mar 2019, 03:31
BTW its called permutation. here a c++ example: https://codereview.stackexchange.com/questions/101320/displaying-all-permutations-of-a-string
Thank you for the correction.

Unfortunately, after studying the answer at the link that example does not help me because I'm not sure how to translate that.. and I don't know how to use the standard lib for C++ in AHK. I appreciate the resource and help though. Thanks! :thumbup:
maybe the title of the posting helps a bit with your research with a new keyword BTW
User avatar
SL5
Posts: 877
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Make Word Permutation Lists (Up To 12 Words)

23 Mar 2019, 03:54

how do you like this?
getAutoKeywords
hd0202
Posts: 148
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Make Word Permutation Lists (Up To 12 Words)

23 Mar 2019, 08:12

do you know the number of full permutations ? it is computed by faculty
faculty 10: 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 = 3,628,800
faculty 12: 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12 = 479,001,600
and the number of your wanted combinations ? I only say: much much much more !!!!

Hubert
bbailar
Posts: 5
Joined: 06 Mar 2019, 13:45

Re: Make Word Permutation Lists (Up To 12 Words)

23 Mar 2019, 08:37

Sorry for hijacking this thread but I've tried submitting two questions to the "Ask for Help" forum and neither of them seems to have posted. Is there a forum curator who has to approve submissions? Has anyone else had this problem? When I hit "submit" the system said my questions had been submitted successfully, so I'm at somewhat of a loss. Any help would be greatly appreciated. Thanks
User avatar
SL5
Posts: 877
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Make Word Permutation Lists (Up To 12 Words)

23 Mar 2019, 09:22

OFF-Topic:
bbailar wrote:
23 Mar 2019, 08:37
Sorry for hijacking this thread but I've tried submitting two questions to the "Ask for Help" forum and neither of them seems to have posted. Is there a forum curator who has to approve submissions? Has anyone else had this problem? When I hit "submit" the system said my questions had been submitted successfully, so I'm at somewhat of a loss. Any help would be greatly appreciated. Thanks
this problem is not known to me. myself was last weeks sometimes blocked with a red box in the stand I would be blocked. But luckily i was not. I then sent my problem without source code and it worked. later the problem was not there again.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Make Word Permutation Lists (Up To 12 Words)

23 Mar 2019, 09:53

I had a look at this function, and modified it to fit your problem. 8 four-letter word result in 2MB result.
I get 323560 lines, It is twice as many I estimated. :(
I have to check my estimation and my result.

Code: Select all

; https://www.autohotkey.com/boards/viewtopic.php?p=158444#p158444
; example with an array containing 8 words




TestData := ["Anna", "Bert", "Carl", "Dora", "Emil", "Fred", "Greg", "Hugh"]
for each, Perm in Permutations("12345678")
    for each, Digit in StrSplit(Perm)
        Result .= TestData[Digit] "`n"



FileDelete, Result.txt
FileAppend, %Result%, Result.txt
MsgBox,,, done, 1
ExitApp



;-------------------------------------------------------------------------------
Permutations(Word) { ; return an unsorted array with all permutations of Word
;-------------------------------------------------------------------------------
    If (Len := StrLen(Word)) = 1
        Return, [Word]

    Result := []
    Loop, %Len% {

        Split1 := SubStr(Word, 1, A_Index - 1)      ; before pos
        Split2 := SubStr(Word, A_Index, 1)          ; at pos
        Split3 := SubStr(Word, A_Index + 1)         ; after pos

        For each, Perm in Permutations(Split1 Split3)
            Result.Push(Split2 Perm)
    }

    Return, Result
}
SOTE
Posts: 901
Joined: 15 Jun 2015, 06:21

Re: Make Word Permutation Lists (Up To 12 Words)

23 Mar 2019, 09:59

bbailar wrote:
23 Mar 2019, 08:37
Sorry for hijacking this thread but I've tried submitting two questions to the "Ask for Help" forum and neither of them seems to have posted. Is there a forum curator who has to approve submissions? Has anyone else had this problem? When I hit "submit" the system said my questions had been submitted successfully, so I'm at somewhat of a loss. Any help would be greatly appreciated. Thanks
Instead of hijacking threads, you might want to study the forum and the User Control Panel a bit more. You might be able to go to the Board Index and look at the bottom. In the red color, you will see a list of administrators. In the green color, a list of Global moderators. You then can try and send a private message to them, explaining your problem. They have the power to help you, much more than us regular members. If you can't PM, then try "contact us" which is at the bottom (https://www.autohotkey.com/boards/memberlist.php?mode=contactadmin) or send an e-mail to support[at]ahkscript.org. Hopefully that will help.

And welcome aboard! :wave:
Last edited by SOTE on 23 Mar 2019, 10:07, edited 2 times in total.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Make Word Permutation Lists (Up To 12 Words)

23 Mar 2019, 10:04

My estimate is right, the code produces too many permutation, please ignore, sorry. :(
My counting of the result was wrong. Fixed by improved layout. :)

New script, estimate equals result this time.

Code: Select all

; example with an array containing 8 words




TestData := ["Anna", "Bert", "Carl", "Dora", "Emil", "Fred", "Greg", "Hugh"]

Loop, % Length := TestData.Length()
    Word .= A_Index

for each, Perm in Permutations(Word)
    for i, Digit in StrSplit(Perm)
        Result .= TestData[Digit]
            . ( Mod(i, Length) ? " " : "`n" )

FileDelete, Result.txt
FileAppend, %Result%, Result.txt
MsgBox,,, done, 1
ExitApp



;-------------------------------------------------------------------------------
Permutations(Word) { ; return an unsorted array with all permutations of Word
;-------------------------------------------------------------------------------
    If (Len := StrLen(Word)) = 1
        Return, [Word]

    Result := []
    Loop, %Len% {

        Split1 := SubStr(Word, 1, A_Index - 1)      ; before pos
        Split2 := SubStr(Word, A_Index, 1)          ; at pos
        Split3 := SubStr(Word, A_Index + 1)         ; after pos

        For each, Perm in Permutations(Split1 Split3)
            Result.Push(Split2 Perm)
    }

    Return, Result
}
Last edited by wolf_II on 23 Mar 2019, 11:21, edited 1 time in total.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Make Word Permutation Lists (Up To 12 Words)

23 Mar 2019, 11:09

using memoization we can speed up the permutations step:

Code: Select all

; example with an array containing 8 words
; memoize




TestData := ["Anna", "Bert", "Carl", "Dora", "Emil", "Fred", "Greg", "Hugh"]

Loop, % Length := TestData.Length()
    Word .= A_Index

start_1 := A_TickCount
Perms := Permutations(Word)
Time_1 := A_TickCount - start_1

start_2 := A_TickCount
for each, Perm in Perms
    for i, Digit in StrSplit(Perm)
        Result .= TestData[Digit]
            . ( Mod(i, Length) ? " " : "`n" )
Time_2 := A_TickCount - start_2

Time_1 := Round(Time_1 / 1000, 3)
Time_2 := Round(Time_2 / 1000, 3)

FileDelete, Result.txt
FileAppend, %Result%, Result.txt
MsgBox,,, permutations done in %Time_1% sec`nResult constructed in %Time_2% sec
ExitApp


;-------------------------------------------------------------------------------
Permutations(Word) { ; return an unsorted array with all permutations of Word
;-------------------------------------------------------------------------------
    static memo := []
    if memo.hasKey(Word)
        return memo[Word]

    if (Len := StrLen(Word)) = 1
        return [Word]

    Result := []
    Loop, %Len% {

        Split1 := SubStr(Word, 1, A_Index - 1)      ; before pos
        Split2 := SubStr(Word, A_Index, 1)          ; at pos
        Split3 := SubStr(Word, A_Index + 1)         ; after pos

        For each, Perm in Permutations(Split1 Split3)
            Result.Push(Split2 Perm)
    }

    return (memo[Word] := Result)
}
But the encoding will only go up to 9 words. I will not try to make up a better encoding.
You have given me a opportunity to practice functional programming. Thank you.
DRocks
Posts: 550
Joined: 08 May 2018, 10:20

Re: Make Word Permutation Lists (Up To 12 Words)

23 Mar 2019, 17:00

lol I tried 12 numbers and i force quit at 680mo of memory, after 5 minutes
User avatar
Tigerlily
Posts: 283
Joined: 04 Oct 2018, 22:31

Re: Make Word Permutation Lists (Up To 12 Words)

23 Mar 2019, 23:02

DRocks wrote:
23 Mar 2019, 17:00
lol I tried 12 numbers and i force quit at 680mo of memory, after 5 minutes
lol :D
wolf_II wrote:
23 Mar 2019, 11:09
using memoization we can speed up the permutations step:

Code: Select all

; example with an array containing 8 words
; memoize




TestData := ["Anna", "Bert", "Carl", "Dora", "Emil", "Fred", "Greg", "Hugh"]

Loop, % Length := TestData.Length()
    Word .= A_Index

start_1 := A_TickCount
Perms := Permutations(Word)
Time_1 := A_TickCount - start_1

start_2 := A_TickCount
for each, Perm in Perms
    for i, Digit in StrSplit(Perm)
        Result .= TestData[Digit]
            . ( Mod(i, Length) ? " " : "`n" )
Time_2 := A_TickCount - start_2

Time_1 := Round(Time_1 / 1000, 3)
Time_2 := Round(Time_2 / 1000, 3)

FileDelete, Result.txt
FileAppend, %Result%, Result.txt
MsgBox,,, permutations done in %Time_1% sec`nResult constructed in %Time_2% sec
ExitApp


;-------------------------------------------------------------------------------
Permutations(Word) { ; return an unsorted array with all permutations of Word
;-------------------------------------------------------------------------------
    static memo := []
    if memo.hasKey(Word)
        return memo[Word]

    if (Len := StrLen(Word)) = 1
        return [Word]

    Result := []
    Loop, %Len% {

        Split1 := SubStr(Word, 1, A_Index - 1)      ; before pos
        Split2 := SubStr(Word, A_Index, 1)          ; at pos
        Split3 := SubStr(Word, A_Index + 1)         ; after pos

        For each, Perm in Permutations(Split1 Split3)
            Result.Push(Split2 Perm)
    }

    return (memo[Word] := Result)
}
But the encoding will only go up to 9 words. I will not try to make up a better encoding.
You have given me a opportunity to practice functional programming. Thank you.
wolf_II, this is really cool!! Thank you. It solves the list correlating with the amount of words given (i.e. if 8 words are given, returns all possible 8-word permutations). The 9-word limit is good enough. I still need to figure out how to make the additional lists that preceed this one (i.e. if 8 words are given, returns all possible 1-word, 2-word, 3-word, 4-word, 5-word, 6-word and 7-word permutations). Also, another thing I would be interested in creating would be if I selected a string of any number of words, and then I could get returned every 2-, 3-, 4-, and 5-word combos based on that selection of words - I think that would be cool.

I am sort of seeing what your code is doing, but will need to study it a bit more :D thanks!!
-TL
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Make Word Permutation Lists (Up To 12 Words)

24 Mar 2019, 03:45

There is a prof at MIT by the name Eric Demaine, who explains functional dynamic programming as follows:
I'm not thinking, I'm just doing.
https://youtu.be/OQ5jsbhAv_M?t=1465
Edit: fixed typo.
Last edited by wolf_II on 26 Mar 2019, 14:09, edited 1 time in total.
User avatar
Tigerlily
Posts: 283
Joined: 04 Oct 2018, 22:31

Re: Make Word Permutation Lists (Up To 12 Words)

24 Mar 2019, 15:04

wolf_II wrote:
24 Mar 2019, 03:45
There is a prof at MIT by the name Eric Demaine, who explains functional programming as follows:
I'm not thinking, I'm just doing.
https://youtu.be/OQ5jsbhAv_M?t=1465
Thanks for the resource wolf_II, I will check this out :)
-TL

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot] and 243 guests