nicuf

shuffle-words

Aug 1st, 2021 (edited)
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. Romanian:
  2. https://neculaifantanaru.com/python-shuffle-words-how-to-mix-random.html
  3.  
  4. English:
  5. https://neculaifantanaru.com/en/python-shuffle-words-how-to-mix-random.html
  6.  
  7.  
  8. ------------------
  9. # Option 1 - Simple Version
  10.  
  11. import random
  12. import re # import for regular expressions
  13.  
  14. listOfStrings = ["Ana are mere", "Ana are pere", "Eu am mure"]
  15. for s in listOfStrings: # for each s in the list I go through, I do something, and that something is in the body of the forum
  16.     print(re.split(r' ', s)) # what is obtained after dividing a string by " " (space)
  17.     result = re.split(r' ', s) # we save the result in a variable (the variable is a memory area where something is stored)
  18.     random.shuffle(result) #
  19.     print(result)
  20. -----------------------------------------
  21. # Option 2 - Complete Version
  22.  
  23. # extract the words directly from all the sentences with the help of a regular expression
  24. mix = r"[a-zA-Z]+"
  25. listOfWords = re.findall(mix, "To provide you with the best experiences, we and our partners use technologies. Such as cookies to store and / or access information. It is all about the device used.")
  26. print("Result: ", listOfWords)
  27.  
  28. random.Random(4).shuffle(listOfWords)
  29. print("Shuffled stuff: ", listOfWords)
  30.  
  31.  
  32. # The first method to combine values from a list
  33. finalString = "" # we initialize the variable in which the final text will be put with the string empty => does not contain anything
  34. for cuvant in listOfWords: # FOR repeats what is in his body (what is in the bottom line)
  35.     finalString = finalString + cuvant + " "
  36. finalString = finalString.strip()
  37. print(finalString)
  38.  
  39. #SAve
  40.  
  41. with open("shuffle-words.txt", "w") as some_file_handle:
  42.     some_file_handle.write(finalString)
  43. -----------------------------------------
  44. # Option 3 - Complete Version (+ input pop-up window)
  45.  
  46. import re
  47. import random
  48.  
  49. def shuffle_Bebe(text):
  50.     regex = r"[a-zA-Z]+"
  51.     listOfWords = re.findall(regex, text)
  52.     random.shuffle(listOfWords)
  53.     return listOfWords
  54.  
  55. def concatenate_Bebe(listOfWords): # concatenates a list of words and the returned result is a text
  56.     finalString = "" # we initialize the variable in which the final text will be put with the string empty => does not contain anything
  57.     for cuvant in listOfWords: # FOR repeats what is in his body (what is in the bottom line)
  58.         finalString = finalString + cuvant + " "
  59.     finalString = finalString.strip()
  60.     return finalString
  61.  
  62. def save_text(cale_fisier, text):
  63.     with open(cale_fisier, "w", encoding = 'utf-8') as file:
  64.         file.write(text)
  65.  
  66. if __name__ == "__main__":
  67.  
  68.     print("Enter a word: ")
  69.     text = str(input()) # you see the text box that is started at the input () command, and what you type in the box will be converted to a string with the str () command
  70.     shuffled_list = shuffle_Bebe(text) # save the list of words that were shuffled
  71.     print("Lista de cuvinte amestecate este: ", shuffled_list)
  72.     text_final = concatenate_Bebe(shuffled_list)
  73.     print("Textul obtinut din lista de mai sus este: ", text_final)
  74.  
  75.     save_text("c:\\Folder2\\test_final.txt", text_final)
  76.  
  77. ------------
  78. # Option 4 - Complete Version (+ input pop-up window) + Open File and Save File
  79.  
  80. import re
  81. import random
  82.  
  83. def shuffle(text):
  84.     regex = r"[a-zA-Z]+"
  85.     listOfWords = re.findall(regex, text)
  86.     random.shuffle(listOfWords)
  87.     return listOfWords
  88.  
  89. def concatenate(listOfWords):
  90.     finalString = ""
  91.     for cuvant in listOfWords:
  92.         finalString = finalString + cuvant + " "
  93.     finalString = finalString.strip()
  94.     return finalString
  95.  
  96. def read_text_from_file(cale_fisier):
  97.     f = open(cale_fisier, "r")
  98.     text = f.read()
  99.     print("ce am citit: ", text)
  100.     return text
  101.  
  102. def save_text_into_file(cale_fisier, text):
  103.     f = open(cale_fisier, "w", encoding = 'utf-8')
  104.     print("Ce am scris: ", text)
  105.     f.write(text)
  106.  
  107. text = read_text_from_file("test_final.txt")
  108.  
  109. lista_cuvinte_amestecate = shuffle(text)
  110.  
  111. text_final = concatenate(lista_cuvinte_amestecate)
  112.  
  113. save_text_into_file("test_final.txt", text_final)
  114.  
Add Comment
Please, Sign In to add comment