Fetch “transcript” values from Google speech api

◇◆丶佛笑我妖孽 提交于 2019-12-11 12:26:07

问题


I am trying to fetch the "transcript" value from the following result:

{

transcript: "1 2 3 4"
confidence: 0.902119

words {
  start_time {
    nanos: 200000000
  }
  end_time {
    nanos: 700000000
  }
  word: "1"
}
words {
  start_time {
    nanos: 700000000
  }
  end_time {
    nanos: 900000000
  }
  word: "2"
}
words {
  start_time {
    nanos: 900000000
  }
  end_time {
    seconds: 1
  }
  word: "3"
}
words {
  start_time {
    seconds: 1
  }
  end_time {
    seconds: 1
    nanos: 300000000
  }
  word: "4"
}

}

The code I am writing to get it is :

for result in response.resultsArray! {

 if let result = result as? StreamingRecognitionResult {
     for alternative in result.alternativesArray {
         if let alternative = alternative as? StreamingRecognitionResult {
                 textView.text = "\(alternative["transcript"])"
           }
            }
            }
                    }

So when I am trying to put the value in textview.text I am getting an error stating :

"Type 'StreamingRecognitionResult' has no subscript members ".

Please help.


回答1:


The key lines are:

let tmpBestResult = (response.resultsArray.firstObject as! StreamingRecognitionResult)
let tmpBestAlternativeOfResult = tmpBestResult.alternativesArray.firstObject as! SpeechRecognitionAlternative
let bestTranscript = tmpBestAlternativeOfResult.transcript

The placement of these lines inside the streamAudioData() is given below:

SpeechRecognitionService.sharedInstance.streamAudioData(audioData,languageCode: self.selectedLangType.rawValue,
                                                                    completion:

                { [weak self] (response, error) in
                    guard let strongSelf = self else {
                        return
                    }

                    if let error = error {

                        debugPrint(">>)) Process_delegate error >> \(error.localizedDescription)")
                        strongSelf.stopRecordingSpeech()
                        self?.delegate.conversionDidFail(errorMsg: error.localizedDescription)
                    } else if let response = response {
                        var finished = false
                        debugPrint(response)
                        for result in response.resultsArray! {
                            if let result = result as? StreamingRecognitionResult {
                                if result.isFinal {
                                    finished = true
                                }
                            }
                        }

                        let tmpBestResult = (response.resultsArray.firstObject as! StreamingRecognitionResult)
                        let tmpBestAlternativeOfResult = tmpBestResult.alternativesArray.firstObject as! SpeechRecognitionAlternative
                        let bestTranscript = tmpBestAlternativeOfResult.transcript
                        strongSelf.delegate.conversionOnProcess(intermediateTranscript: bestTranscript!, isFinal: finished)
                        if finished {
                            //UI
                            strongSelf.stopRecordingSpeech()
                            strongSelf.delegate.conversionDidFinish(finalTranscript: bestTranscript!)
                        }
                    }
            })

Happy Coding ;)




回答2:


So the code will be changed to this:

for alternative in result.alternativesArray {
  if let alternative1 = alternative as? SpeechRecognitionAlternative {
        textView.text = "\(alternative1.transcript)"
  }
}


来源:https://stackoverflow.com/questions/48002278/fetch-transcript-values-from-google-speech-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!