Picking random words from a text file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ScoutHunter
Posts: 5
Joined: 29 May 2020, 18:16

Picking random words from a text file

Post by ScoutHunter » 15 Aug 2022, 07:57

Hello,
So I have this script that lets me pick and send a random word from a text file (picks up a random line number and sends the line's content, as the file has over 7000 lines I guess it's less cpu intensive).
I don't know how to make it so it never sends the same word (or same line then?) more than once, until the script is relaunched or the loop broken.

I'm also trying to make a variation of this script that only picks words starting or ending with specific letters. Though I'm a bit lost and don't really know where to start, would appreciate any help!

Code: Select all

ChooseWords:
Loop,
  {
   if (BreakLoop = 1)
   break
  
   txtfile := "codes.txt"

   FileRead, coSubleng, %txtfile%
   fileArray := StrSplit(coSubleng, "`n")
   Loop, Read, %txtfile%
	{
	   total_lines = %A_Index%
	}
	Random, Var, 1, %total_lines%
	Loop, 1
	{
	    Random, Var, 1, %total_lines%
	    Line := fileArray[Var]
		{
		Send, %Line%{Enter}
		}
	}
Sleep, 1000
  }
BreakLoop = 0
return

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Picking random words from a text file

Post by BoBo » 15 Aug 2022, 09:19

Code: Select all

#SingleInstance Force

FileRead, content, my.txt											;get content
arr := StrSplit(content, A_Space, ["`n","?","!",".",",",";"])			;split content into an array of words

F12::
	Random, No, 1, arr.Count()											;select word position
	MsgBox % word := arr.RemoveAt(No) "`t(" No "/" arr.Count() ")"	;display the word that will now be removed from the array
	if arr.Count()=0
		reload
	return
Tested (and corrected, after below mentioned issue(s))

ScoutHunter
Posts: 5
Joined: 29 May 2020, 18:16

Re: Picking random words from a text file

Post by ScoutHunter » 15 Aug 2022, 10:27

So it doesn't seem to work, it's returning a number like this: (1268616003/1)

I'm not used to arrays at all (and quite noob to ahk in general) but from what I understand it could be doable if it saved the word variable (%Line%) in an array after the Send, while also checking before the Send if this value is present in the array, and if it is then a Goto would send the script back to the Random part. Not sure if that's the most simple way tho.

User avatar
Chunjee
Posts: 1402
Joined: 18 Apr 2014, 19:05
Contact:

Re: Picking random words from a text file

Post by Chunjee » 15 Aug 2022, 10:32

low energy approach:

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

; read file
fileRead, content, my.txt
; convert to array of words
arr := A.words(content)

; msgbox a random element of the array
msgbox, % A.sample(arr)



; filter a new array that has words that begin with "m"
mWords := A.filter(arr, func("mfilter"))
mfilter(val) {
	if (biga.startsWith(val, "m")) {
		return true
	}
}

; msgbox a random m word
msgbox % A.sample(mWords)
https://biga-ahk.github.io/biga.ahk/#/?id=words
https://biga-ahk.github.io/biga.ahk/#/?id=sample

https://biga-ahk.github.io/biga.ahk/#/?id=filter
https://biga-ahk.github.io/biga.ahk/#/?id=startsWith
Last edited by Chunjee on 15 Aug 2022, 10:49, edited 1 time in total.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Picking random words from a text file

Post by BoBo » 15 Aug 2022, 10:41

@ScoutHunter

Code: Select all

#SingleInstance Force

;FileRead, content, my.txt												;get content

content =
(
So it doesn't seem to work, it's returning a number like this: (1268616003/1)
I'm not used to arrays at all (and quite noob to ahk in general) but from what I understand it could be doable 
if it saved the word variable (%Line%) in an array after the Send, while also checking before the Send if this 
value is present in the array, and if it is then a Goto would send the script back to the Random part. Not sure if that's the most simple way tho.
)

arr := StrSplit(content, A_Space, ["`n","?","!",".",",",";"])			;split content into an array of words

F12::
	Random, No, 1, arr.Count()											;select word position
	MsgBox % word := arr.RemoveAt(No) "`t(" No "/" arr.Count() ")"		;display the word that will now be removed from the array
	if arr.Count()=0
		reload
	return

ScoutHunter
Posts: 5
Joined: 29 May 2020, 18:16

Re: Picking random words from a text file

Post by ScoutHunter » 15 Aug 2022, 12:23

Thanks both of your solutions are working, I like @Chunjee's one, it's pretty simple. Also I'm trying to make the startsWith filter work with a variable (for the first letters of a word), I know it's possible to add a variable where literal string is expected in some cases, I cannot find a way to do it with biga though.. Do you know if it's even possible?

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Picking random words from a text file

Post by flyingDman » 15 Aug 2022, 13:26

Try:

Code: Select all

content =
(
So it doesn't seem to work, it's returning a number clike this: (1268616003/1)
I'm not used to arrays at all (and quite noob to ahk in general) but from what I understand it could be doable
if it saved the word variable (%Line%) in an array after the Send, while also checking before the Send if this
value is present in the array, and if it is then a Goto would send the script back to the Random part. Not sure 
if that's the most simple way tho.
)

startswith := "w"
for x,y in strsplit(content,[" ","`n"]," (),.?:`n`r")
	(substr(y,1,1) = startswith) && lst .= y "`n"
sort, lst, random
msgbox % strsplit(lst,"`n").1
To have 1268616003/1 be treated as 2 words use strsplit(content,[" ","`n","/"]," (),.?:`n`r")
14.3 & 1.3.7

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Picking random words from a text file

Post by flyingDman » 15 Aug 2022, 14:54

Also I would not be too concerned about CPU load. I tested this on a >50k line text and it took 500ms...

To generate a unique word with a hotkey without duplication, you can use this:

Code: Select all

startswith := "z", cnt := 1
for x,y in strsplit(content,[" ","`n","/"],"`r(),.?:;")
	(substr(y,1,1) = startswith) && lst .= y "`n"
sort, lst, U
sort, lst, random
return

F12::
if !(z := strsplit(lst,"`n")[cnt++])
	msgbox End of list
msgbox,,, % z,1
return
14.3 & 1.3.7

ScoutHunter
Posts: 5
Joined: 29 May 2020, 18:16

Re: Picking random words from a text file

Post by ScoutHunter » 15 Aug 2022, 16:34

flyingDman wrote:
15 Aug 2022, 14:54
Also I would not be too concerned about CPU load. I tested this on a >50k line text and it took 500ms...

To generate a unique word with a hotkey without duplication, you can use this:

Code: Select all

startswith := "z", cnt := 1
for x,y in strsplit(content,[" ","`n","/"],"`r(),.?:;")
	(substr(y,1,1) = startswith) && lst .= y "`n"
sort, lst, U
sort, lst, random
return

F12::
if !(z := strsplit(lst,"`n")[cnt++])
	msgbox End of list
msgbox,,, % z,1
return
Well that is perfect. Just edited the substr length so I can put more startswith letters. I'm now trying to find a way to read the startwith value from a variable (I'm using a regex backreference in the first place to determine what the words should start with), but that's basically it for what I initially asked, thank you.

Here is what I'm tryin to do:

Code: Select all

HotStrings("iS)code[[:space:]]{1,1}(.*)`t", "codestart")     ;  I'm typing for exemple code wo and press tab so it continues to the codestart part, backreference is now wo
return

codestart:
content =
(
word work worst worse
)

startswith := """" $1 """", cnt := 1     ;  adding double quotes so it stores "wo" in the variable
send, %startswith%    ;  just to be sure the variable is "wo"


for x,y in strsplit(content,[" ","`n","/"],"`r(),.?:;")
	(substr(y,1,2) = startswith) && lst .= y "`n"    ;  startswith is suppose to be "wo" but unless I type it directly there, it doesn't seem to work with my variable
sort, lst, U
sort, lst, random

if !(z := strsplit(lst,"`n")[cnt++])
	msgbox End of list
msgbox,,, % z,1
return
Last edited by ScoutHunter on 16 Aug 2022, 10:27, edited 1 time in total.

User avatar
Chunjee
Posts: 1402
Joined: 18 Apr 2014, 19:05
Contact:

Re: Picking random words from a text file

Post by Chunjee » 15 Aug 2022, 17:39

ScoutHunter wrote:
15 Aug 2022, 07:57
I don't know how to make it so it never sends the same word (or same line then?) more than once, until the script is relaunched or the loop broken.
The way I like to do that is to shuffle the array (and remove duplicates if needed) so that order is random

https://biga-ahk.github.io/biga.ahk/#/?id=shuffle

Post Reply

Return to “Ask for Help (v1)”