카테고리 없음

[씨#] Watson의 Speech-To-Text Unity SDK의 경우 키워드를 어떻게 지정할 수 있습니까?

필살기쓰세요 2021. 2. 20. 15:07

다음과 같이 "SendStart"함수에 키워드를 지정해야했습니다.

private void SendStart() {
        if (m_ListenSocket == null)
                    throw new WatsonException("SendStart() called with null connector.");
                    
                            Dictionary<string, object> start = new Dictionary<string, object>();
                                    start["action"] = "start";
                                            start["content-type"] = "audio/l16;rate=" + m_RecordingHZ.ToString() + ";channels=1;";
                                                    start["continuous"] = EnableContinousRecognition;
                                                            start["max_alternatives"] = m_MaxAlternatives;
                                                                    start["interim_results"] = EnableInterimResults;
                                                                            start["word_confidence"] = m_WordConfidence;
                                                                                    start["timestamps"] = m_Timestamps;
                                                                                    
                                                                                            //specify keywords here
                                                                                                    start["keywords"] = keywordsToCheck.ToArray();
                                                                                                            start["keywords_threshold"] = 0.05;
                                                                                                                    //end additions here 
                                                                                                                    
                                                                                                                            m_ListenSocket.Send(new WSConnector.TextMessage(Json.Serialize(start)));
                                                                                                                                    m_LastStartSent = DateTime.Now;
                                                                                                                                        }
                                                                                                                                        

"ParseRecognizeResponse"함수에서 keyword_results를 올바르게 구문 분석하는 코드를 작성하십시오.

private SpeechRecognitionEvent ParseRecognizeResponse(IDictionary resp){

        if (resp == null)
                    return null;
                    
                    
                            List<SpeechRecognitionResult> results = new List<SpeechRecognitionResult>();
                                    IList iresults = resp["results"] as IList;
                                            if (iresults == null)
                                                        return null;
                                                        
                                                                foreach (var r in iresults)
                                                                        {
                                                                                    IDictionary iresult = r as IDictionary;
                                                                                                if (iresults == null)
                                                                                                                continue;
                                                                                                                
                                                                                                                            SpeechRecognitionResult result = new SpeechRecognitionResult();
                                                                                                                            
                                                                                                                                        //added this section, starting here
                                                                                                                                                    IDictionary iKeywords_result = iresult["keywords_result"] as IDictionary;
                                                                                                                                                                result.keywords_result = new KeywordResults();
                                                                                                                                                                            List<KeywordResult> keywordResults = new List<KeywordResult>();
                                                                                                                                                                                        foreach (string key in keywordsToCheck) {
                                                                                                                                                                                                        if (iKeywords_result[key] != null) {
                                                                                                                                                                                                                            IList keyword_Results = iKeywords_result[key] as IList;
                                                                                                                                                                                                                                                if (keyword_Results == null) {
                                                                                                                                                                                                                                                                        continue;
                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                foreach (var res in keyword_Results) {
                                                                                                                                                                                                                                                                                                                                        IDictionary kw_resultDic = res as IDictionary;
                                                                                                                                                                                                                                                                                                                                                                KeywordResult keyword_Result = new KeywordResult();
                                                                                                                                                                                                                                                                                                                                                                                        keyword_Result.confidence = (double)kw_resultDic["confidence"];
                                                                                                                                                                                                                                                                                                                                                                                                                keyword_Result.end_time = (double)kw_resultDic["end_time"];
                                                                                                                                                                                                                                                                                                                                                                                                                                        keyword_Result.start_time = (double)kw_resultDic["start_time"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                keyword_Result.normalized_text = (string)kw_resultDic["normalized_text"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        keywordResults.Add(keyword_Result);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    result.keywords_result.keyword = keywordResults.ToArray();                   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                //ends here
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            result.final = (bool)iresult["final"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        IList ialternatives = iresult["alternatives"] as IList;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if (ialternatives == null)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    continue;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                List<SpeechRecognitionAlternative> alternatives = new List<SpeechRecognitionAlternative>();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            foreach (var a in ialternatives)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        IDictionary ialternative = a as IDictionary;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (ialternative == null)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            continue;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            SpeechRecognitionAlternative alternative = new SpeechRecognitionAlternative();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            alternative.transcript = (string)ialternative["transcript"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (ialternative.Contains("confidence"))
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                alternative.confidence = (double)ialternative["confidence"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (ialternative.Contains("timestamps"))
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    IList itimestamps = ialternative["timestamps"] as IList;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        TimeStamp[] timestamps = new TimeStamp[itimestamps.Count];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            for (int i = 0; i < itimestamps.Count; ++i)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        IList itimestamp = itimestamps[i] as IList;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (itimestamp == null)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            continue;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    TimeStamp ts = new TimeStamp();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ts.Word = (string)itimestamp[0];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ts.Start = (double)itimestamp[1];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ts.End = (double)itimestamp[2];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    timestamps[i] = ts;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            alternative.Timestamps = timestamps;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (ialternative.Contains("word_confidence"))
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                IList iconfidence = ialternative["word_confidence"] as IList;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    WordConfidence[] confidence = new WordConfidence[iconfidence.Count];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        for (int i = 0; i < iconfidence.Count; ++i)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    IList iwordconf = iconfidence[i] as IList;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (iwordconf == null)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        continue;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                WordConfidence wc = new WordConfidence();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        wc.Word = (string)iwordconf[0];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                wc.Confidence = (double)iwordconf[1];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        confidence[i] = wc;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                alternative.WordConfidence = confidence;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                alternatives.Add(alternative);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        result.alternatives = alternatives.ToArray();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    results.Add(result);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return new SpeechRecognitionEvent(results.ToArray());                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

이제 OnRecognize가이 SpeechRecognitionEvent를 통과하면 대체 단어와 해당 신뢰도 점수를 표시하는 코드를 키워드 결과와 신뢰도 점수를 표시하는 코드로 변경했습니다.

private void OnRecognize(SpeechRecognitionEvent result) {
    //Debug.Log("Recognizing!");
        m_ResultOutput.SendData(new SpeechToTextData(result));
        
            if (result != null && result.results.Length > 0) {
                    if (m_Transcript != null)
                                m_Transcript.text = "";
                                
                                        foreach (var res in result.results) {
                                                    //start keyword recognition changes here
                                                                if (res.keywords_result != null) {
                                                                                if (res.keywords_result.keyword != null) {
                                                                                                    foreach (var keyword in res.keywords_result.keyword) {
                                                                                                                            m_Transcript.text += string.Format("{0} ({1}, {2:0.00})\n",
                                                                                                                                                        keyword.normalized_text, res.final ? "Final" : "Interim", keyword.confidence);
                                                                                                                                                                            }
                                                                                                                                                                                            }
                                                                                                                                                                                                        }
                                                                                                                                                                                                                    //end here                
                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                

키워드 결과 신뢰도 값을 사용하는 것은 대체 단어 Watson이 키워드와 일치하는지 확인하기 위해 하드 코딩 된 검사를 수행 한 다음 거기에서 신뢰도 값을 사용하는 것보다 훨씬 더 중요합니다. keyword_results.keyword []. confidence 값은 이미 해당 단어를 확인하고 있기 때문에 신뢰도 값은 훨씬 더 높아집니다. 이것이이 프로세스를 진행하고 Keywords_result 값을 적절하게 포함하기 위해 SpeechRecognitionEvent 결과 값을 구문 분석하는 원동력이었습니다.

약간의 배경 지식을 위해 난독증이있는 아이들이 단어 형성을 배우기 위해 리듬 게임을 만들고 있으니 Guitar Hero가 Sesame Street를 만난다고 생각하세요.



출처
https://stackoverflow.com/questions/39940174