Powershell: Shuffle-words / Generate Random words / Mix words Puzzle

Suzana Eree 811 Reputation points
2021-05-17T14:26:12.407+00:00

hello, I need to make a shuffle-words just like this one: https://onlinerandomtools.com/shuffle-words

I have a .txt file with some different words, which I have to mix, just like a puzzle. So I made a PowerShell script, but it doesn't make any change on my file.

$resultsdir = "C:\Folder1\"
Get-ChildItem -Path $sourcedir -Filter *.txt | ForEach-Object {
$words = Get-Content  -Path $_.FullName -Raw -Encoding UTF8 | Sort-Object {Get-Random} -unique | select -first 100

Function Create-Words
{ 
  # query the smaller list of words for single entry (2 times)
  $word1 = $words | Sort-Object {Get-Random} -unique | select -first 1  
  $word2 = $words | Sort-Object {Get-Random} -unique | select -first 1  

  # create randomwords
  $random = Get-Random

  # mix and return new random words
  return (Get-Culture).TextInfo.ToTitleCase($word1) + (Get-Culture).TextInfo.ToTitleCase($word2) + $random

}

for($i=1; $i -le 20; $i++){

}

$Allines = $_ -replace '\s+(.*?)','\x20' #put all words on the same line  FIND: \s+(.*?) REPLACE BY: \x20
 $words -replace $Allines | Out-File -FilePath 

}
Windows for business Windows Server User experience PowerShell
Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. Suzana Eree 811 Reputation points
    2021-05-18T13:48:30.227+00:00

    @Anonymous @Andreas Baumgarten @Chris

    DONE. The code of mr. @Anonymous had a small problem on Split. Instead of Split(",. ") I change it to Split(" ") Works beautiful now.

    I hope that also the code of @Andreas Baumgarten to be updates, so as to work as well.

    $file = "C:\Folder1\file.txt"  
     $newfile = "C:\Folder1\newfile.txt"  
     $content = (Get-Content -Path $file -Encoding UTF8) -join ''  
     [System.Collections.ArrayList]$words = $content.Trim(",. ").Split(" ")  
     $counter = $words.count  
     $newwords = @()  
     for($i=0; $i -lt $counter; $i++){  
        $randomword = Get-Random -InputObject $words  
        $newwords += $randomword  
        $words.Remove($randomword)  
     }  
     $newwords -join ' ' | Out-File -FilePath $newfile  
    
    0 comments No comments

11 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-05-17T16:54:13.61+00:00

    Hi @Suzana Eree ,

    you are defining a function in line 5 and following lines, ending in line 17.
    But you didn't execute the function in your script.

    Here you will find my approach for a very simple random word generator. The lines starting with Write-Host are just to show the respective output of each word and not necessary for the script.

    # Word list  
    $words = @('rabbit', 'peccary', 'colt', 'anteater',  
        'meerkat', 'eagle', 'owl', 'cow', 'turtle', 'bull',  
        'baselisk', 'snake', 'lizzard', 'panda', 'bear', 'pig',  
        'lion', 'tiger', 'bunny', 'wolf', 'deer', 'pronghorn',  
        'fish', 'rabbit', 'gorilla', 'puma', 'mustang', 'sheep',  
        'wolverine', 'hyena', 'beaver', 'rooster', 'ox', 'frog'  
    )  
    # Definition of funtion named "Create-Words"  
    Function Create-Words {  
        $word1 = Get-Random -InputObject $words # get first random word from word list  
        $word2 = Get-Random -InputObject $words  # get second random word from word list  
        Write-Host "First word " $word1 -ForegroundColor Green # write output word1  
        Write-Host "Second word " $word2 -ForegroundColor Magenta # write output of word2  
        $script:newWord = "$word1-$word2" # combine word1 wirh word2 / delimiter = "-""  
        Write-Host "New word " $newWord -ForegroundColor Yellow # write putput new word  
    }  
      
    # Execution of function "Create-Words"  
    Create-Words  
      
    # Output of new generated word in script  
    $newWord  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    2 people found this answer helpful.

  2. Suzana Eree 811 Reputation points
    2021-05-17T19:13:20.96+00:00

    my file, for example, looks like this. It is just text. But after mixing the words, I will get the magic. The real magic is to build something from the pieces, and to obtain another pattern.

    Vawn7N.jpg

    0 comments No comments

  3. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-05-17T20:22:54.273+00:00

    Hi @Suzana Eree ,

    I didn't understand the "real magic" but anyway.

    My wordfile (Wordlist.txt)

    What is Lorem Ipsum?  
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.  
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,  
    when an unknown printer took a galley of type and scrambled it to make a type specimen book.  
    It has survived not only five centuries, but also the leap into electronic typesetting,  
    remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets  
    containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.  
    Why do we use it?  
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.  
    The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here,  
    content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum  
    as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions  
    have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).  
    Where does it come from?  
    Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC,  
    making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of  
    the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical  
    literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum"  
    The Extremes of Good and Evil by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the  
    Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.  
    The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de  
    Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914  
    translation by H. Rackham.  
    Where can I get some?  
    There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form,  
    by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum,  
    you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet  
    tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200  
    Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem  
    Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.  
    

    The script to get 2 random words from text file:

    # Define file name  
    $fileName = "./Junk/1/Wordlist.txt"  
    # Get content of file  
    $words = (Get-Content -Path $fileName -Raw -Encoding UTF8).Split(" ")  
    #Define function  
    Function Create-Words {  
        $script:word1 = Get-Random -InputObject $words # get first random word from word list  
        $script:word2 = Get-Random -InputObject $words  # get second random word from word list  
        Write-Host "First word: " $script:word1 -ForegroundColor Green # write output word1  
        Write-Host "Second word: " $script:word2 -ForegroundColor Magenta # write output of word2  
    }  
    # Execute function  
    Create-Words   
    # Output random word 1  
    $word1  
    # Output random word 2  
    $word2  
    

    The random words are in variable $word1 and $word2 .

    How to replace a string and write a file in a folder should not be the problem after scripting the last two days.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  4. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-05-17T21:16:37.8+00:00

    Hi @Suzana Eree ,

    "nothing happens"? But you see the output of the two random words from the text ( $word1 and $word2 )?

    The last script I posted is not replacing anything anywhere. As I wrote before, how to replace a word in a text and write the text in a file should be easy, because it's the same we did the last two days with your other scripts.

    This should do the replace and write a new file:

    # Define file name  
    $fileName = "./Junk/1/Wordlist.txt"  
    $newfilename = "./Junk/1/NewWordlist.txt"  
    # Get content of file  
    $content = Get-Content -Path $fileName -Raw -Encoding UTF8  
    $words = ((($content.Split(" ")).Replace(".","")).Replace(",","")).Replace("`n","")  
    #Define function  
    Function Create-Words {  
        $script:word1 = Get-Random -InputObject $words # get first random word from word list  
        $script:word2 = Get-Random -InputObject $words  # get second random word from word list  
        Write-Host "First word: " $script:word1 -ForegroundColor Green # write output word1  
        Write-Host "Second word: " $script:word2 -ForegroundColor Magenta # write output of word2  
    }  
    # Execute function  
    Create-Words  
    # Replace random word1 with random word2 and write file  
    $content.Replace("$word1", "$word2") | Out-File -FilePath "$newfilename" -force  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.