如何让Azure部署的语音机器人接收带标点大写的语音转文本结果
解决Azure Speech Service机器人部署后接收语音转写丢失标点和大写的问题
问题详情
本地运行的C#语音客户端通过Azure Speech Service的DialogServiceConnector调用ListenOnceAsync()提交语音输入后,客户端从Activity.Text能收到带有完整标点、正确大小写的语音转写结果;但部署在Azure上的EchoBot通过turnContext.Activity.Text接收到的却是无标点、全小写的纯文本。机器人本地运行时接收结果正常,且已通过微软官方《Voice-enable your bot》教程的示例代码复现该问题。
示例对比:
语音输入:"That might be true. Why? Who wants to know?"
客户端接收结果:"That might be true. Why? Who wants to know?"
Azure机器人接收结果:"that might be true why who wants to know"
相关代码片段:
客户端初始化DialogServiceConnector
private DialogServiceConnector connector = null; // ... // 创建Dialog Service Connector并注册事件 this.connector = new DialogServiceConnector(config, AudioConfig.FromDefaultMicrophoneInput()); this.connector.ActivityReceived += this.Connector_ActivityReceived; this.connector.Recognizing += this.Connector_Recognizing; this.connector.Recognized += this.Connector_Recognized; this.connector.Canceled += this.Connector_Canceled; this.connector.SessionStarted += this.Connector_SessionStarted; this.connector.SessionStopped += this.Connector_SessionStopped; // 连接到Direct Line Speech通道 this.connector.ConnectAsync(); // ... this.connector.ListenOnceAsync();
客户端接收转写结果
private void Connector_ActivityReceived(object sender, ActivityReceivedEventArgs e) { var json = e.Activity; var activity = JsonConvert.DeserializeObject<Activity>(json); // ... this.Messages.Add(new MessageDisplay(activity.Text, Sender.Bot, renderedCards)); }
Azure机器人接收结果
public class EchoBot : ActivityHandler { protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) { var replyText = $"Echo: {turnContext.Activity.Text}"; // ... } }
解决方案
1. 配置客户端SpeechConfig启用格式化
在创建DialogServiceConnectorConfig前,显式配置SpeechConfig的自动标点和大小写参数,确保语音服务返回格式化后的结果:
// 初始化SpeechConfig var speechConfig = SpeechConfig.FromSubscription("你的Speech Service订阅密钥", "你的资源区域"); // 启用自动标点 speechConfig.SetServiceProperty("punctuation", "spoken", ServicePropertyChannel.UriQueryParameter); // 确保启用自动大小写(部分区域默认启用,显式配置更稳妥) speechConfig.SetServiceProperty("capitalization", "true", ServicePropertyChannel.UriQueryParameter); // 设置正确的识别语言 speechConfig.SetSpeechRecognitionLanguage("en-US"); // 用配置好的SpeechConfig创建DialogServiceConnectorConfig var config = DialogServiceConnectorConfig.FromSpeechSubscription(speechConfig, "你的机器人应用ID");
2. 检查Azure机器人的Direct Line Speech通道配置
登录Azure门户,找到你的机器人资源:
- 进入Channels选项卡,打开Direct Line Speech通道设置
- 确认Speech Recognition Settings中已勾选
Enable automatic punctuation和Enable capitalization - 若没有这些选项,确保你的Speech Service资源为S0层级,且机器人使用最新版本的Bot Framework SDK
3. 验证区域一致性
确保客户端、Speech Service资源、Azure机器人三者使用完全相同的区域,跨区域可能导致配置不生效。
4. 排查机器人端额外处理
确认机器人代码中没有对turnContext.Activity.Text进行小写转换、去标点等额外处理(示例中的EchoBot代码无此类操作)。
验证方式
- 重新部署配置后的机器人到Azure
- 本地运行客户端测试语音输入,查看机器人返回的echo文本是否包含正确的标点和大小写
- 若问题仍存在,使用Bot Framework Emulator查看发送给机器人的原始Activity JSON,确认
text字段的格式是否正确
内容的提问来源于stack exchange,提问作者Bruce Haley
相关产品推荐
相关产品推荐

