提交 89ad5d05 authored 作者: Chad Phillips's avatar Chad Phillips

adding experimental speech to text module.

上级 31ad6a95
...@@ -62,6 +62,7 @@ modules = { ...@@ -62,6 +62,7 @@ modules = {
"play", "play",
"record", "record",
"service", "service",
"speech_to_text",
"system", "system",
"tracker", "tracker",
} }
......
jester.action_map.speech_to_text_from_file = {
mod = "speech_to_text",
handlers = {
google = "speech_to_text_from_file_google",
default = "speech_to_text_from_file_google",
}
}
jester.help_map.speech_to_text = {}
jester.help_map.speech_to_text.description_short = [[Speech to text translation. WARNING: experimental.]]
jester.help_map.speech_to_text.description_long = [[This module provides speech to text translation. WARNING: totally experimental, no guarantees it will work!
The module requires the Lua JSON package, more details at http://luaforge.net/projects/json]]
jester.help_map.speech_to_text.handlers = {
google = [[Uses Google's Speech to Text service.]],
}
jester.help_map.speech_to_text.actions = {}
jester.help_map.speech_to_text.actions.speech_to_text_from_file = {}
jester.help_map.speech_to_text.actions.speech_to_text_from_file.description_short = [[Translates a sound file to text.]]
jester.help_map.speech_to_text.actions.speech_to_text_from_file.description_long = [[Translates a sound file to text.
Translations are stored in the specified storage area with the following keys, where X is the chunk number:
translation_X: The translated text for the chunk.
confidence_X: The confidence level of the translated chunk, a decimal number in the range of 0 to 1.
A 'status' key is also place in the storage area, indicating the result of the translation. A value of 0 indicates the translation was successful.
This action requires that sox is installed and executable by FreeSWITCH.]]
jester.help_map.speech_to_text.actions.speech_to_text_from_file.params = {
filepath = [[The full path to the file to translate.]],
storage_area = [[(Optional) The storage area to store the response in. Defaults to 'speech_to_text'.]],
}
module(..., package.seeall)
local io = require("io")
local http = require("socket.http")
local ltn12 = require("ltn12")
require "jester.support.file"
require("json.json")
--[[
Speech to text using Google's API.
]]
function speech_to_text_from_file_google(action)
require "lfs"
local filepath = action.filepath
local area = action.storage_area or "speech_to_text"
if filepath then
-- Verify file exists.
success, file_error = lfs.attributes(filepath, "mode")
if success then
flac_file = os.tmpname() .. ".flac"
command = string.format("sox %s --rate 8k %s", filepath, flac_file)
result = os.execute(command)
if result == 0 then
file = io.open(flac_file, "rb")
filesize = (filesize(file))
local response = {}
local body, status_code, headers, status_description = http.request({
method = "POST",
headers = {
["content-length"] = filesize,
["content-type"] = "audio/x-flac; rate=8000",
},
url = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US",
sink = ltn12.sink.table(response),
source = ltn12.source.file(file),
})
os.remove(flac_file)
if status_code == 200 then
response_string = table.concat(response)
data = json.decode(response_string)
jester.set_storage(area, "status", data.status or 1)
if data.status == 0 and data.hypotheses then
for k, chunk in ipairs(data.hypotheses) do
jester.set_storage(area, "translation_" .. k, chunk.utterance)
jester.set_storage(area, "confidence_" .. k, chunk.confidence)
end
end
else
jester.debug("ERROR: Request to Google API server failed: %s", status_description)
jester.set_storage(area, "status", 1)
end
else
jester.debug("ERROR: Unable to convert file %s to FLAC format via sox", filepath)
end
else
jester.debug("ERROR: File %s does not exist", filepath)
end
end
end
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论