2017-08-30 84 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:無法從源類型轉換爲目標類型 –