2017-01-02 19 views
0

私は現在、学校の休憩中に個人的なアシスタントボットをチャレンジとして構築しています。現在、ChatterBotSpeechRecognitionを統合する際に問題が発生しています。私は、両方が同じファイルに呼び出すと、私はすべてのエラーを得ることはありません:Python音声認識とChatterbot

import speech_recognition as sr 
from chatterbot import ChatBot 

def setup(): 
    chatbot = ChatBot(
     'Ron Obvious', 
     trainer='chatterbot.trainers.ChatterBotCorpusTrainer' 
    ) 

    # Train based on the english corpus 
    chatbot.train("chatterbot.corpus.english") 
    while True: 
     get_response(chatbot) 

def get_response(chatbot): 
    # Get a response to an input statement 
    speech = return_audio() 
    print chatbot.get_response(speech) 

def return_audio(): 
    r = sr.Recognizer() 
    with sr.Microphone() as source: 
     audio = r.listen(source) 
    try: 
     speech = r.recognize_google(audio) 
     try: 
      speech = str(speech) 
      print speech 
      return speech 
     except TypeError: 
      print "Error! Could not convert speech to string!" 
    except sr.UnknownValueError: 
     print "Error! Could not process that audio." 
     return "Error!" 
    except sr.RequestError as e: 
     print "Error! No internet connection to Google Sound Recognizer." 
     return "Error!" 

setup() 

をしかし、私は2つの2の異なるファイルを保存する場合、私ははAttributeErrorを取得します。私は例外を発生させることによってプログラムがクラッシュするのを回避しますが、この例外はボットが実際にユーザーと対話しないことを意味します。

ベローは、2つのファイルです:

import speech_recognition as sr 
from chatterbot import ChatBot 

import Messanger_Alert as MA 
import Weather 
import Email_Alert 
import Chat 
import Run 


def start(): 
    bot = Chat.start() 
    MA_client = MA.login() 
    print "Hello Luke!" 
    print "I am ready for your command! :D" 
    while True: 
     speech = str(return_audio()) 
     if not speech: 
      print "Error! Speech returned nonetype!" 
      continue 
     else: 
      try: 
       if any(word in speech for word in ("Jason", "jason")): #Old key words: "SysGen", "Sysgen", "System", "system" - will reimpliment if needed 
        if any(word in speech for word in ("Message", "message", "Messenger", "messenger", "Facebook", "facebook")): 
         if any(word in speech for word in ("Send", "send")): 
          MA.send_message(MA_client) #now getting an attribute error. 
          print "Ready for next command!" 
         elif any(word in speech for word in ("Search Friend", "search friend", "Seach friend", "search Friend", "friend", "Friend", "friends", "Friends")): 
          MA.friend_search(MA_client) 
          print "Ready for next command!" 
         elif any(word in speech for word in ("Check", "check")): 
          MA.check_message(MA_client) 
          print "Ready for next command!" 
        elif any(word in speech for word in ("Email", "email")): 
         if any(word in speech for word in ("Personal", "personal", "Home", "home")): 
          Email_Alert.login1() 
          print "Ready for next command!" 
         elif any(word in speech for word in ("School", "school", "Dorm", "dorm", "UCSD", "ucsd")): 
          Email_Alert.login2() 
          print "Ready for next command!" 
        elif any(word in speech for word in ("Weather", "weather", "Forecast", "forecast")): 
         Weather.decide() 
        elif any(word in speech for word in ("Goodnight", "goodnight", "Bye", "bye", "Goodbye", "goodbye")): 
         print "Goodnight Luke!" 
         exit() 
        else: 
         Chat.talk(bot, speech) 
       elif speech == "Error!": 
        return_audio() 
       else: 
        Chat.talk(bot, speech) 
      except sr.UnknownValueError: 
       print "Error! Could not process that audio." 
      except sr.RequestError as e: 
       print "Error! No internet connection to Google Sound Recognizer." 


def return_audio(): 
    r = sr.Recognizer() 
    with sr.Microphone() as source: 
     audio = r.listen(source) 
    try: 
     speech = r.recognize_google(audio) 
     try: 
      speech = str(speech) 
      print speech 
      return speech 
     except TypeError: 
      print "Error! Could not convert speech to string!" 
    except sr.UnknownValueError: 
     print "Error! Could not process that audio." 
     return "Error!" 
    except sr.RequestError as e: 
     print "Error! No internet connection to Google Sound Recognizer." 
     return "Error!" 

チャット聞く:私は、彼らが同じファイル内にある場合、それらの両方が働くためである理由は完全にはわからない

from chatterbot import ChatBot 
from chatterbot.trainers import ListTrainer 
import os.path 

import Listen 

def start(): 
    file_list = ["Jason_logs.txt", "Sample1.txt"] 
    chatbot = ChatBot('Jason', trainer='chatterbot.trainers.ChatterBotCorpusTrainer') 
    for path in file_list: 
     if os.path.exists(path) == "True": 
      train_from_text(chatbot, path) 
    train_bot(chatbot) 

def train_from_text(chatbot, path): 
    conversation = [] 
    with open(path, 'r') as f: 
     while True: 
      line1 = f.readline() 
      line2 = f.readline() 
      if not line2: 
       break 
      else: 
       conversation.append(line1) 
       conversation.append(line2) 
    chatbot.set_trainer(ListTrainer) 
    chatbot.train(conversation) 

def train_bot(chatbot): 
    # Train based on the english corpus 
    chatbot.train("chatterbot.corpus.english") 
    # Train based on english greetings corpus 
    chatbot.train("chatterbot.corpus.english.greetings") 
    # Train based on the english conversations corpus 
    chatbot.train("chatterbot.corpus.english.conversations") 

def talk(chatbot, entry): 
    try: 
     response = chatbot.get_response(str(entry)) 
     print response 
     with open("Jason_logs.txt", "a") as f: 
      f.write(entry) 
      f.write(response) 
    except TypeError: 
     print "Error! Could not convert speech to string!" 
    except AttributeError: 
     print "Error! Speech not accepted by Jason's Chatterbot libraries." 

を(コードの最初の塊)。しかし、組織的な目的のために、可能であれば、2つのファイルを別々にしておきたいと思います。

お寄せいただきありがとうございました!

**編集:**ここに(私はチャットで属性エラーをコメントアウトした場合に発生する)トレースバックエラーがある:

Traceback (most recent call last): 
    File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 24, in <module> 
    startup() 
    File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 13, in startup 
    voice() 
    File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 19, in voice 
call Jason 
    Listen.start() 
    File "C:\Users\lukec\PycharmProjects\Sysgen_AI\Listen.py", line 47, in start 
    Chat.talk(bot, speech) 
    File "C:\Users\lukec\PycharmProjects\Sysgen_AI\Chat.py", line 39, in talk 
    response = chatbot.get_response(str(entry)) 
AttributeError: 'NoneType' object has no attribute 'get_response' 
+1

受信した実際のエラーを含める必要があります(トレースバックには非常に重要な情報が含まれています)。あなたのコードは2つのファイルに分割されているため、1つのファイルバージョンとまったく同じことをしていないようです(この問題は、あなたが導入したいくつかの他の変更かもしれないし、存在することとは関係ありません2つのファイルで)。 –

+0

エラーから完全なトレースバックを投稿してください。 –

+0

トレースバックエラーが掲載されました。迅速な対応に感謝します! – TobyTobyo

答えて

1

Chat.start()chatbotを返さないので、暗黙的に返される値はNoneです。 start()の末尾にreturn chatbotを追加してください。また、モジュールを使用してチャットボットのインスタンスを渡すのではなく、独自のチャットボットラッパークラスを作成することもできます。

+0

ありがとうございました!それは私の問題だったようです。迅速な対応に感謝します。 – TobyTobyo