Sign in to follow this  
Followers 0

Pluck random words from a text file

7 posts in this topic

Posted

I wonder if someone could help me write a quick program to scan a text file containing a bunch of words and randomly choose words from it, then store them in a string so the chosen words can be put in a new text file or maybe copied to the clipboard?

I'm not sure how to actually select an entire word to add to the string.

-Scott

Share this post


Link to post
Share on other sites



Posted (edited)

I did it like this

$_SHARENUM = Random(1, 20, 1)  ; random asking user to share this menu
    If $_SHARENUM = 1 Then $_SHARE = "     Lets STOP the Hackers,  Share this Menu!"
    If $_SHARENUM = 2 Then $_SHARE = "     Great Program Huh?  Share it with your Friends!"
    If $_SHARENUM = 3 Then $_SHARE = "     If this works for you,  It will help others too!,  Share!"
    If $_SHARENUM = 4 Then $_SHARE = "     Its Easy to Share,  Just push the botton below!"
    If $_SHARENUM = 5 Then $_SHARE = "     Time to send this Menu on to your Friends!"
    If $_SHARENUM = 6 Then $_SHARE = "     Help me - Help you - Help Friends,  Share this Menu!"
    If $_SHARENUM = 7 Then $_SHARE = "     Don't Keep this Great thing Silent,  Share Today!"
    If $_SHARENUM = 8 Then $_SHARE = "     Over 87% of computers have ad or spyware,  Share!"
    If $_SHARENUM = 9 Then $_SHARE = "     Lets Clean-up the Computer world,  Share this Menu!"
    If $_SHARENUM = 10 Then $_SHARE = "     STOP Hackers, STOP Crashes,  Share this Menu!"
    If $_SHARENUM = 11 Then $_SHARE = "     Believe me!  This program was alot of Work,  Share it!"
    If $_SHARENUM = 12 Then $_SHARE = "     Please E-Mail us,  let us know you shared!"
    If $_SHARENUM = 13 Then $_SHARE = "     Value has many forms,  If you find Value,  Share it!"
    If $_SHARENUM = 14 Then $_SHARE = "     FREE - FREE - FREE and...    FREE to Share!"
    If $_SHARENUM = 15 Then $_SHARE = "     2 Things,  1.Close all programs and...  2.Share this Menu!"
    If $_SHARENUM = 16 Then $_SHARE = "     Show appreciation!     Just E-mail us to say you shared!"
    If $_SHARENUM = 17 Then $_SHARE = "     New Up-Dates may be Available - Contact Us"
    If $_SHARENUM = 18 Then $_SHARE = "     1/1/2005, California Law sets fines for Spyware."
    If $_SHARENUM = 19 Then $_SHARE = "     January 2005, Microsoft releases a free (Win Xp) Spyware Program."
    If $_SHARENUM = 20 Then $_SHARE = "     Late 2005, Microsoft to release *OneCare* - to compete with *XPClean Menu*"

and used "$_SHARE " throughout my program

Hope it helps

8)

Edited by Valuater

Share this post


Link to post
Share on other sites

Posted

I wonder if someone could help me write a quick program to scan a text file containing a bunch of words and randomly choose words from it, then store them in a string so the chosen words can be put in a new text file or maybe copied to the clipboard?

I'm not sure how to actually select an entire word to add to the string.

-Scott

<{POST_SNAPBACK}>

Are the words stored one per line in the file, or is it a normal file of text with multiple words per line?

Either way, read the entire file into an array.

If it is 1 word per line, then pick a random index of that array and there's your word.

If it's multiple words per line, then pick a random index of your array. Now split that line by a delimiter (I'm assuming whitespace would be your delimiter) and pick a random index from that resulting array, and then you have your word.

Share this post


Link to post
Share on other sites

Posted

I did it like this

~snip~

and used  "$_SHARE "  throughout my program

Hope it helps

8)

<{POST_SNAPBACK}>

Thanks

I just realized I won't know how many words will be in the text file. ~Sigh~ this seemed easy at first. :)

Share this post


Link to post
Share on other sites

Posted

@Valuater:

It might be easier (both easier on the eyes and easier to type) to do something like:

$aShare = _ArrayCreate(_
    'line 1',_
    'line 2',_
    'line3');etc
$_Share = $aShare[Random(0, UBound($aShare), 1)]

Plus then you could keep getting different texts throught out (if that's what you wanted) by just running that last line.

Share this post


Link to post
Share on other sites

Posted

Are the words stored one per line in the file, or is it a normal file of text with multiple words per line?

Either way, read the entire file into an array.

If it is 1 word per line, then pick a random index of that array and there's your word.

If it's multiple words per line, then pick a random index of your array.  Now split that line by a delimiter (I'm assuming whitespace would be your delimiter) and pick a random index from that resulting array, and then you have your word.

<{POST_SNAPBACK}>

I can specify how the words are stored, whitespace is what I was thinking.

"pick a random index of that array" Yes, this is what I'm having trouble figuring out.

I can go to a random whitespace in the array, but how do a "pick" the word next to it?

;Random word plucker ver 0.1
;Open the file
$file = FileOpen("words.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
; Read in 1 character at a time until the EOF is reached
While 1
    $chars = FileRead($file, 1)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Char read:", $chars)
Wend
FileClose($file)

; Start a repeat loop
Do

; Ask how many words.
$count = InputBox("Word Plucker", "Enter number of words needed", "10" )
   If @Error = 1 Then 
    MsgBox(0, "Word Plucker", "OK... C-ya!")
    Exit
   EndIf
     
; Count the words.
$lines=StringSplit($file," ")
$found=0
for $i=1 to $lines[0]
   If StringInStr($lines[$i],$word)>0 Then
   
; Run Notepad
Run("notepad.exe")
; Wait for Notepad2 become active.
WinWaitActive("Untitled - ")
Send("Here are your random words:{Enter 2}")
; Some Notepad replacements add an asterisk to the title on edit, this will whack it.
WinSetTitle("", "", "Random Words")
; Send the answer to Notepad...
WinActivate("Search Results", "")

    Send($lines[$i]&".{Enter 2}")
       $found=1
   EndIf
Next

; Change window title to drop the asterisk so we can find it next time.
WinSetTitle("", "", "Search Results")

; ...unless we didn't find anything.   
   If $found=0 Then MsgBox(1,"Error","No Sentance with "&$word&" Found")

;Try again? If no then end Do-Until loop.
$answer = MsgBox(4, "Word Plucker", "More?")
Until $answer = 7

Note: There's some chuncks of another program that I started with in there that won't make sense.

Share this post


Link to post
Share on other sites

Posted

here's kind of how I would do it:

Dim $aWords[100];make this an approximate guess of how many you'll have
;Open the file with FileOpen()
;Loop through the file, reading into $sLine with FileReadLine()
$aLine = StringSplit($sLine)
for $i = 1 to $aLine[0]
    $aWords[0] = $aWords[0] + 1
    If $aWords[0] >= UBound($aWords) Then ReDim $aWords[$aWords[0] + 100]
    $aWords[$aWrods[0]] = $aLine[$i]
next 
;Loop
;Close file with FileClose()

;Now you have an array of words, so just pick one:
$sWord = $aWords[Random(1,$aWords[0],1)]

That's if you don't mind having multiple copies of the same word (meaning that multiple copies would increase the odds of selecting that word)

If you want to have the same odds for every word, you need to put them in a unique list, such as my Binary Tree function does:

http://www.autoitscript.com/forum/index.php?showtopic=13114

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.