[D66] Ex-journal.py

René Oudeweg roudeweg at gmail.com
Wed May 31 19:49:36 CEST 2023


Projectje: met voiceherkenning een transcriptie van het Journaal maken, 
de zinnen vervolgens random verhaspelen en vervangen en mbv AI Text-to 
speech een alternatief journaal maken... Met teletekstnieuws is het 
wellicht makkelijker te implementeren. MMM, als ik de tijd er voor vind 
is het misschien een artistieke poging waard...

RO


import random
import re

def substitute_subject_object(sentence):
     subjects = ["I", "You", "He", "She", "They", "We", "The cat", "The 
dog", "The bird", "The robot"]
     objects = ["apple", "book", "car", "dog", "house", "pen", "table", 
"tree", "umbrella", "watch"]

     # Find the subject and object using regular expressions
     match = re.match(r'(.+?)(\b(?:loves|likes)\b)(.+)', sentence)
     if not match:
         return sentence

     subject = match.group(1).strip()
     verb = match.group(2).strip()
     direct_object = match.group(3).strip()

     # Randomly substitute the subject and object
     new_subject = random.choice(subjects)
     new_object = random.choice(objects)

     # Build the new sentence
     new_sentence = f"{new_subject} {verb} {new_object}."
     return new_sentence

# Example usage
sentence = "She likes dogs."
new_sentence = substitute_subject_object(sentence)
print("Original sentence:", sentence)
print("New sentence:", new_sentence)

--

Voorbeeld:


reinold at fedora test]$ python directobject2.py
Original sentence: She likes dogs.
New sentence: The robot likes umbrella.

Original sentence: She likes dogs.
New sentence: The robot likes pen.

Original sentence: She likes dogs.
New sentence: He likes tree.

Original sentence: She likes dogs.
New sentence: I likes book.

Original sentence: She likes dogs.
New sentence: The cat likes house.

--


RO: improve this program with a large set of words of verbs, objects and 
subjects

ChatGPT: Certainly!


import random
import re


def load_word_list(filename):
     with open(filename, 'r') as file:
         word_list = [word.strip() for word in file.readlines()]
     return word_list

def substitute_subject_verb_object(sentence, subject_list, verb_list, 
object_list):
     # Find the subject, verb, and object using regular expressions
     match = re.match(r'(.+?)\b(.*?)\b(.+)', sentence)
     if not match:
         return sentence

     subject = match.group(1).strip()
     verb = match.group(2).strip()
     direct_object = match.group(3).strip()

     # Randomly substitute the subject, verb, and object
     new_subject = random.choice(subject_list)
     new_verb = random.choice(verb_list)
     new_object = random.choice(object_list)

     # Build the new sentence
     new_sentence = f"{new_subject} {new_verb} {new_object}."
     return new_sentence

# Load subject, verb, and object word lists
subject_word_list = load_word_list('subject_words.txt')
verb_word_list = load_word_list('verb_words.txt')
object_word_list = load_word_list('object_words.txt')

# Example usage
sentence = "She likes dogs."
new_sentence = substitute_subject_verb_object(sentence, 
subject_word_list, verb_word_list, object_word_list)
print("Original sentence:", sentence)
print("New sentence:", new_sentence)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.tuxtown.net/pipermail/d66/attachments/20230531/6db9719e/attachment-0001.html>


More information about the D66 mailing list