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/"
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')
    result = speechRec.get_hyp()

    return result[0]

CHUNK = 1024
FORMAT = pyaudio.paInt16
RATE = 16000

k = aiml.Kernel()

for x in range(10):
    fn = "o"+str(x)+".wav"
    p = pyaudio.PyAudio()
    stream =, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
    print("* recording")
    frames = []
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data =
    print("* done recording")
    wf =, 'wb')
    wavfile = fn
    recognised = decodeSpeech(hmdir,lmd,dictd,wavfile)
    reply = k.respond(recognised)
    cm = 'espeak -s 155 "'+reply+'"'


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


    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 “”, line 52, in
    recognised = decodeSpeech(hmdir,lmd,dictd,wavfile)
    File “”, 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.


    June 5, 2017 at 1:31 pm

Leave a Reply

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

You are commenting using your 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: