User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 564
Joined: Sat Nov 02, 2013 2:06 pm

Generating and hearing a random sentence

Sat Mar 09, 2019 4:44 pm

Hi;

I have just come back from our monthly Pi gathering at our local library during which one of the Library staff asked if I could help them with the Shakespeare Random Insults Generator for an event in the Library. Basically what they want is to have this installed on the Libraries demo Raspberry Pi and visitors could 'easily' receive an insult from the man himself :-)

During the Pi day they managed to find a pre-written version of the python script and towards the end of the session I and one of the attendees gt it running but wanted to make it a little more user friendly by being able to simply start it by clicking on a desktop icon. This is where it started getting complicated..

Now at home I have managed to make the program executable using chmod +x and now when double clicking on the .py icon an 'Execute File' window opens with options to Execute, Execute in Terminal, Open, Close. The only option that seems to run the program is 'Execute in Terminal'.

What I want to be able to do now though is rather than read the insult I want the Pi to speak it.
I have installed Pico Text to Speach and tested that it works by providing the correct arguments:

Code: Select all

pico2wavw -w hello.wav "Hello World" && aplay hello.wav
The existing program works by picking a random word from each of three .csv files containing Shakespearian insulting words :-) It then simply prints them to the screen.
I guess what I need to do is to create a file containing the phrase made up in the form of a list using the three components along with the initial greeting and then supply Pico the generated text file?

Question is how?

Here is the original code:

Code: Select all

#!/usr/bin/env python

import random
from time import sleep

column1=[]
column2=[]
column3=[]

# Read contents of column1.txt and add the contents to the 1st list
file1 = open("column1.txt","r")
line = file1.readline()
while line != "":
    column1.append(line)
    line = file1.readline()
file1.close()

# Read contents of column2.txt and add the contents to the 2nd list
file2 = open("column2.txt","r")
line = file2.readline()
while line != "":
    column2.append(line)
    line = file2.readline()
file2.close()

# Read contents of column3.txt and add the contents to the 3rd list
file3 = open("column3.txt","r")
line = file3.readline()
while line != "":
    column3.append(line)
    line = file3.readline()
file3.close()

 

print("Thou art aye " + column1[random.randint(0,49)] + (" ") + column2[random.randint(0,49)] + (" ") + column3[random.randint(0,49)])

sleep(2)
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

User avatar
B.Goode
Posts: 7870
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Generating and hearing a random sentence

Sat Mar 09, 2019 5:07 pm

I haven't tried it myself, but the author of this blog post is usually reliable regarding the solutions he documents.

Maybe it could be adapted for your needs?

https://bigl.es/tooling-tuesday-gtts/

tpyo kingg
Posts: 555
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Generating and hearing a random sentence

Sat Mar 09, 2019 5:49 pm

JonnyAlpha wrote:
Sat Mar 09, 2019 4:44 pm
During the Pi day they managed to find a pre-written version of the python script and towards the end of the session I and one of the attendees gt it running but wanted to make it a little more user friendly by being able to simply start it by clicking on a desktop icon. This is where it started getting complicated.
If you have things so they work and are just missing an icon, then you can make an icon to click on and have it run a script (or a lone command) by creating a .desktop file. The specification is so hard to read that it is a curiosity and not useful. However, there are plentiful examples in the directory /usr/share/applications/ In there, the Exec= directive is what you want to focus on.

If you need to bundle several commands into the same line, then wrap them in a shell and execute them that way:

Code: Select all

sh -c "cmd01; cmd02; cmd03;"

Return to “Python”