【问题标题】:Fetch "transcript" values from Google speech api从 Google 语音 api 获取“成绩单”值
【发布时间】:2017-12-28 05:55:17
【问题描述】:

我正在尝试从以下结果中获取“成绩单”值:

{

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"
}

}

我写的代码是:

对于results.resultsArray中的结果! {

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

因此,当我尝试将值放入 textview.text 时,我收到一条错误消息:

“类型'StreamingRecognitionResult'没有下标成员”。

请帮忙。

【问题讨论】:

    标签: ios swift speech-to-text google-speech-api google-cloud-speech


    【解决方案1】:

    重点是:

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

    这些行在 streamAudioData() 中的位置如下所示:

    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!)
                            }
                        }
                })
    

    快乐编码 ;)

    【讨论】:

      【解决方案2】:

      所以代码会改成这样:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-28
        • 2014-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多