2017-08-30 86 views
3

我试图运行在IBM的统一沃森会话业务following here, code snippetIBM沃森交谈服务错误:无法从“方法组”转换为“conversation.onMessage”

private Conversation m_Conversation = new Conversation(); 
    private string m_WrokspaceID = "xyz"; 
    private string m_input = "help"; 


    // Use this for initialization 
    void Start() { 
     Debug.Log("user : " + m_input); 
     m_Conversation.Message(OnMessage, m_WrokspaceID, m_input); 
    } 

    void OnMessage(MessageResponse resp, string customData) { 
     foreach (Intent mi in resp.intents) 
     { 
      Debug.Log("intent : " + mi.intent + ", confidence :" + mi.confidence); 
     } 

     Debug.Log("response :" + resp.output.text); 
    } 

但我得到这个错误

cannot convert from 'method group' to 'conversation.onMessage' 

我做错了什么?我从watson官方github回购中得到的代码片段。

对象返回的回答表明: enter image description here

+0

在哪一行发生此错误?我猜'm_Converstion.Message(OnMessage ...''m_Conversation.Message'的签名是什么?它作为第一个参数而不是该方法的期望是什么? –

+0

@RenéVogt是同一行,它期望对象 –

回答

3

您可以施放响应作为字典并尝试从那里获取价值。使用泛型对象而不是静态数据模型,您可以更多地通过响应。

private void OnMessage(object resp, string customData) 
{ 
    Dictionary<string, object> respDict = resp as Dictionary<string, object>; 
    object intents; 
    respDict.TryGetValue("intents", out intents); 

    foreach(var intentObj in (intents as List<object>)) 
    { 
     Dictionary<string, object> intentDict = intentObj as Dictionary<string, object>; 

     object intentString; 
     intentDict.TryGetValue("intent", out intentString); 

     object confidenceString; 
     intentDict.TryGetValue("confidence", out confidenceString); 

     Log.Debug("ExampleConversation", "intent: {0} | confidence {1}", intentString.ToString(), confidenceString.ToString()); 
    } 
} 
+0

虽然我已经解决了这个错误,但buti也会尝试这个代码。请你能告诉我,记录意图或信心的目的是什么。我认为只有回应在谈话中很重要。我无法理解意图和置信度日志的目的,我们应该使用或记录repsonse以显示chatbot的回复 –

+0

上面的意图和信心是展示如何获取字典中不同键的值。使用上面的例子,你可以在'MessageResponse'中得到不同的对象,比如'output','context','alternate_intents','entities'和'input'。 'context'特别重要,因为你将上下文传递给下一个'MessageRequest'来保持对话。 – taj

+0

为什么github代码回购有正确的代码?此外,我想知道为什么我的发言对文本对话不工作默认 –

3

根据的Conversation源代码line 32,委托改为:

public delegate void OnMessage(object resp, string customData); 

你必须改变你的OnMessage方法,以反映即:

void OnMessage(object resp, string customData) { 
    // ... 
} 
+0

但如果我使它成为对象,那么我将无法使用MessageResponse类属性 –

+0

在这种情况下(通过事件处理程序可以在任何地方找到)的通用方法是将'object'明确地转换为'MessageResponse'。 ://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclick(v = vs.110).aspx例如 – haim770

+0

是的,我有强制转换对象但失败InvalidCastException:无法从源类型转换为目标类型 –