User:Mxrwho/Lists: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 42: Line 42:
           formatted_lines.append(' '.join(current_line))
           formatted_lines.append(' '.join(current_line))
       return '\n'.join(formatted_lines)
       return '\n'.join(formatted_lines)


   with open('lists.txt', 'r') as file:
   with open('lists.txt', 'r') as file:
       input_text = file.read()
       input_text = file.read()
   print("Original text:", input_text)
   print("Original text:", input_text)


Line 51: Line 51:
   print("Sorted and formatted text (3 to 5 words per line):")
   print("Sorted and formatted text (3 to 5 words per line):")
   print(sorted_text)
   print(sorted_text)


   randomized_text = randomize_and_format_text(input_text)
   randomized_text = randomize_and_format_text(input_text)
Line 56: Line 57:
   print(randomized_text)
   print(randomized_text)


Combined texts and then:
  with open('alphalist.txt', 'w') as file:
      file.write(sorted_text)
 
  with open('randylist.txt', 'w') as file:
      file.write(randomized_text)
 
  print("Text saved to 'alphalist.txt' and 'randylist.txt'")
 
Combine texts and then:


   say -o alllists.aiff -v Whisper -r 130 -f alllists.txt  
   say -o alllists.aiff -v Whisper -r 130 -f alllists.txt  

Latest revision as of 13:32, 19 April 2024

Playing with lists, sorting and randomization in python

Documentation

https://pad.xpub.nl/p/15424_lists -> https://pad.xpub.nl/p/15424_lists_m

export as plain text

Python code:

  import random
  def sort_and_format_text(text):
      words = text.split()
      words = [word.strip('*') for word in words] 
      sorted_words = sorted(words)
      formatted_lines = []
      current_line = []
      for word in sorted_words:
          current_line.append(word)
          if len(current_line) >= 3 and len(current_line) <= 5:
              formatted_lines.append(' '.join(current_line))
              current_line = []
      if current_line:
          formatted_lines.append(' '.join(current_line))
      return '\n'.join(formatted_lines)
  def randomize_and_format_text(text):
      words = text.split()
      words = [word.strip('*') for word in words] 
      random.shuffle(words)
      formatted_lines = []
      current_line = []
      for word in words:
          current_line.append(word)
          if len(current_line) >= 3 and len(current_line) <= 5:
              formatted_lines.append(' '.join(current_line))
              current_line = []
      if current_line:
          formatted_lines.append(' '.join(current_line))
      return '\n'.join(formatted_lines)


  with open('lists.txt', 'r') as file:
      input_text = file.read()
  print("Original text:", input_text)
  sorted_text = sort_and_format_text(input_text)
  print("Sorted and formatted text (3 to 5 words per line):")
  print(sorted_text)


  randomized_text = randomize_and_format_text(input_text)
  print("Randomized and formatted text (3 to 5 words per line):")
  print(randomized_text)
  with open('alphalist.txt', 'w') as file:
      file.write(sorted_text)
  with open('randylist.txt', 'w') as file:
      file.write(randomized_text)
  print("Text saved to 'alphalist.txt' and 'randylist.txt'")

Combine texts and then:

  say -o alllists.aiff -v Whisper -r 130 -f alllists.txt 

and then:

  ffmpeg -i alllists.aiff -codec:a libmp3lame -qscale:a 2 alllists.mp3