如何解决为什么在使用for循环时gl_speech返回错误,但对于每个单独的文件却不返回错误?使用tryCatch
我最近才开始使用R和googleLanguageR软件包-太好了!
我想使用gl_speech将400多个.flac文件(每个文件约20秒)转换为文本。
我编写了以下循环:
for (i in files) {
possibleError <- tryCatch({
participantid = str_sub(i,1,3)
patha = paste(path_in,i,sep='')
result <- gl_speech(patha,sampleRateHertz = 44100L,customConfig = my_config)
transcript2 <- subset(result$transcript,select=-c(languageCode,channelTag))
transcriptid <- cbind(participantid,transcript2)
write_delim(as.data.frame(transcriptid),file.path(path_in,paste0(participantid,'_text.csv')))
},error = function(e) e)
if(inherits(possibleError,"error")) next
}
为完整起见,my_config为:
my_config <- list(audioChannelCount = 2,encoding = "FLAC",maxAlternatives = 30,languageCode = "en-US",model = "video"
)
我一次要处理2、5或10个文件。其中一些返回错误。 错误消息是:
ℹ 2020-10-08 20:07:56 > Request Status Code: 408
Error : lexical error: invalid char in json text.
<!DOCTYPE html> <html lang=en>
(right here) ------^
<!DOCTYPE html>
<html lang=en>
<Meta charset=utf-8>
<Meta name=viewport content="initial-scale=1,minimum-scale=1,width=device-width">
<title>Error 408 (Request Timeout)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>408.</b> <ins>That’s an error.</ins>
<p>Your client has taken too long to issue its request. <ins>That’s all we kNow.</ins>
但是,当我以后运行分别返回错误的文件时,gl_speech不会返回此错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。