User:Mxrwho/Lists: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "== 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 le...")
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 11: Line 11:
Python code:
Python code:


import random
  import random


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


def randomize_and_format_text(text):
  def randomize_and_format_text(text):
    words = text.split()
      words = text.split()
    words = [word.strip('*') for word in words]  
      words = [word.strip('*') for word in words]  
    random.shuffle(words)
      random.shuffle(words)
    formatted_lines = []
      formatted_lines = []
    current_line = []
      current_line = []
    for word in words:
      for word in words:
        current_line.append(word)
          current_line.append(word)
        if len(current_line) >= 3 and len(current_line) <= 5:
          if len(current_line) >= 3 and len(current_line) <= 5:
            formatted_lines.append(' '.join(current_line))
              formatted_lines.append(' '.join(current_line))
            current_line = []
              current_line = []
    if current_line:
      if current_line:
        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)
  sorted_text = sort_and_format_text(input_text)
  print("Sorted and formatted text (3 to 5 words per line):")
  print(sorted_text)




sorted_text = sort_and_format_text(input_text)
  randomized_text = randomize_and_format_text(input_text)
print("Sorted and formatted text (3 to 5 words per line):")
  print("Randomized and formatted text (3 to 5 words per line):")
print(sorted_text)
  print(randomized_text)


  with open('alphalist.txt', 'w') as file:
      file.write(sorted_text)


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


Combined texts and then:
  print("Text saved to 'alphalist.txt' and 'randylist.txt'")


say -o alllists.aiff -v Whisper -r 130 -f alllists.txt  
Combine texts and then:
 
  say -o alllists.aiff -v Whisper -r 130 -f alllists.txt  


and then:
and then:


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

Latest revision as of 14: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