我正在编辑conversation.py的一部分,这是Jasper语音识别软件的一部分,用于添加声音效果。我关心的代码部分是init()。我正在使用代码并尝试从mic.py
复制 self.speaker = speaker原始代码:
# -*- coding: utf-8-*-
import logging
from notifier import Notifier
from brain import Brain
class Conversation(object):
def __init__(self, persona, mic, profile):
self._logger = logging.getLogger(__name__)
self.persona = persona
self.mic = mic
self.profile = profile
self.brain = Brain(mic, profile)
self.notifier = Notifier(profile)
def handleForever(self):
"""
Delegates user input to the handling function when activated.
"""
self._logger.info("Starting to handle conversation with keyword '%s'.",
self.persona)
while True:
# Print notifications until empty
notifications = self.notifier.getAllNotifications()
for notif in notifications:
self._logger.info("Received notification: '%s'", str(notif))
self._logger.debug("Started listening for keyword '%s'",
self.persona)
threshold, transcribed = self.mic.passiveListen(self.persona)
self._logger.debug("Stopped listening for keyword '%s'",
self.persona)
if not transcribed or not threshold:
self._logger.info("Nothing has been said or transcribed.")
continue
self._logger.info("Keyword '%s' has been said!", self.persona)
self._logger.debug("Started to listen actively with threshold: %r",
threshold)
input = self.mic.activeListenToAllOptions(threshold)
self._logger.debug("Stopped to listen actively with threshold: %r",
threshold)
if input:
self.brain.query(input)
else:
self.mic.say("Pardon?")
编辑代码:
# -*- coding: utf-8-*-
import logging
from notifier import Notifier
from brain import Brain
class Conversation(object):
def __init__(self, speaker, persona, mic, profile):
self._logger = logging.getLogger(__name__)
self.speaker = speaker #ADDED LINE
self.persona = persona
self.mic = mic
self.profile = profile
self.brain = Brain(mic, profile)
self.notifier = Notifier(profile)
def handleForever(self):
"""
Delegates user input to the handling function when activated.
"""
self._logger.info("Starting to handle conversation with keyword '%s'.",
self.persona)
while True:
# Print notifications until empty
notifications = self.notifier.getAllNotifications()
for notif in notifications:
self._logger.info("Received notification: '%s'", str(notif))
self._logger.debug("Started listening for keyword '%s'",
self.persona)
threshold, transcribed = self.mic.passiveListen(self.persona)
self._logger.debug("Stopped listening for keyword '%s'",
self.persona)
if not transcribed or not threshold:
self._logger.info("Nothing has been said or transcribed.")
continue
self._logger.info("Keyword '%s' has been said!", self.persona)
self._logger.debug("Started to listen actively with threshold: %r",
threshold)
input = self.mic.activeListenToAllOptions(threshold)
self._logger.debug("Stopped to listen actively with threshold: %r",
threshold)
if input:
self.brain.query(input)
else:
self.speaker.play(jasperpath.data('audio', 'SpeechMis.wav')) #ADDED LINE
self.mic.say("Pardon?")
当我运行包含此修改代码的Jasper软件时,我收到以下错误:
Traceback (most recent call last):
File "/home/pi/jasper/jasper.py", line 151, in <module>
app.run()
File "/home/pi/jasper/jasper.py", line 120, in run
conversation = Conversation("JASPER", self.mic, self.config)
TypeError: int() takes exactly 5 arguments (4 given)
我查看了其他TypeError问题并仔细阅读了代码。我仍然不知道我做错了什么。
答案 0 :(得分:2)
class Conversation(object):
def __init__(self, speaker, persona, mic, profile):
...
conversation = Conversation("JASPER", self.mic, self.config)
您修改了Conversation.__init__
签名,现在它需要4个参数(speaker
,persona
,mic
,profile
),但调用代码仍提供值仅限前3名。
为了在不破坏现有代码的同时修改类,将新参数添加到结尾并使用默认值:
class Conversation(object):
def __init__(self, persona, mic, profile, speaker=None):