Pythonism

code and the oracular

How to make a Talking Chatbot

with 7 comments


import sys,os
import pyaudio
import wave
import aiml

hmdir = "/usr/share/pocketsphinx/model/hmm/wsj1"
lmd = "/usr/share/pocketsphinx/model/lm/wsj/wlist5o.3e-7.vp.tg.lm.DMP"
dictd = "/usr/share/pocketsphinx/model/lm/wsj/wlist5o.dic"

def decodeSpeech(hmmd,lmdir,dictp,wavfile):

    import pocketsphinx as ps
    import sphinxbase

    speechRec = ps.Decoder(hmm = hmmd, lm = lmdir, dict = dictp)
    wavFile = file(wavfile,'rb')
    wavFile.seek(44)
    speechRec.decode_raw(wavFile)
    result = speechRec.get_hyp()

    return result[0]

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
RECORD_SECONDS = 4

k = aiml.Kernel()
k.learn("wildcard.aiml")

for x in range(10):
    fn = "o"+str(x)+".wav"
    p = pyaudio.PyAudio()
    stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
    print("* recording")
    frames = []
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)
    print("* done recording")
    stream.stop_stream()
    stream.close()
    p.terminate()
    wf = wave.open(fn, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
    wf.close()
    wavfile = fn
    recognised = decodeSpeech(hmdir,lmd,dictd,wavfile)
    reply = k.respond(recognised)
    cm = 'espeak -s 155 "'+reply+'"'
    os.system(cm)
    

Advertisements

Written by Luke Dunn

February 5, 2014 at 10:31 am

7 Responses

Subscribe to comments with RSS.

  1. Thanks for your chatbot code. It really inspired me a lot. I am a leader of a smart robot workshop. May I know your Email address? Hope to have further communication. Best regards.

    Mark Young

    October 24, 2014 at 3:15 pm

  2. Hello, please send for me whole project

    Anton

    April 9, 2015 at 4:26 pm

    • will try my friend but currently have system failure and have lost data. which part you need most ? maybe try re-write

      Luke Dunn

      April 9, 2015 at 5:40 pm

  3. Really no matter if someone doesn’t understand afterward its up to
    other people that they will help, so here it happens.

    천안휴게텔

    April 24, 2017 at 5:56 am

  4. Please help.
    I am trying to run above code on my machine, I got following error:

    Traceback (most recent call last):
    File “chatbot.py”, line 52, in
    recognised = decodeSpeech(hmdir,lmd,dictd,wavfile)
    File “chatbot.py”, line 15, in decodeSpeech
    speechRec = ps.Decoder(hmm = hmmd, lm = lmdir, dict = dictp)
    TypeError: __init__() got an unexpected keyword argument ‘hmm’

    What is the solution for this?
    I think arguments of method Decoder is changed now.

    DJ

    June 5, 2017 at 1:31 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: