科大訊飛語音轉文字以及中文分詞的Java測試代碼

我錄了一段音存儲在這個test.m4a文件裏,語音內容爲"測試一下Netweaver對於併發請求的響應性能"。java

使用以下Java代碼進行測試:git

package com.iflytek.msp.lfasr;

import java.util.HashMap;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSON;
import com.iflytek.msp.cpdb.lfasr.client.LfasrClientImp;
import com.iflytek.msp.cpdb.lfasr.exception.LfasrException;
import com.iflytek.msp.cpdb.lfasr.model.LfasrType;
import com.iflytek.msp.cpdb.lfasr.model.Message;
import com.iflytek.msp.cpdb.lfasr.model.ProgressStatus;

// SDK document: http://www.xfyun.cn/doccenter/lfasr#go_sdk_doc_v2
public class TestLfasr 
{
	// original media path
	private static final String local_file = "c:\\temp\\test.m4a";

	private static final LfasrType type = LfasrType.LFASR_STANDARD_RECORDED_AUDIO;
	
	private static int sleepSecond = 20;
	
	public static void main(String[] args) {
		LfasrClientImp lc = null;
		try {
			lc = LfasrClientImp.initLfasrClient();
		} catch (LfasrException e) {
			Message initMsg = JSON.parseObject(e.getMessage(), Message.class);
			System.out.println("ecode=" + initMsg.getErr_no());
			System.out.println("failed=" + initMsg.getFailed());
		}
				
		// get upload task id
		String task_id = "";
		HashMap<String, String> params = new HashMap<>();
		params.put("has_participle", "true");
		try {
			Message uploadMsg = lc.lfasrUpload(local_file, type, params);
			int ok = uploadMsg.getOk();
			if (ok == 0) {
				task_id = uploadMsg.getData();
				System.out.println("task_id=" + task_id);
			} else {
				System.out.println("ecode=" + uploadMsg.getErr_no());
				System.out.println("failed=" + uploadMsg.getFailed());
			}
		} catch (LfasrException e) {
			Message uploadMsg = JSON.parseObject(e.getMessage(), Message.class);
			System.out.println("ecode=" + uploadMsg.getErr_no());
			System.out.println("failed=" + uploadMsg.getFailed());					
		}
		while (true) {
			try {
				Thread.sleep(sleepSecond * 1000);
				System.out.println("waiting ...");
			} catch (InterruptedException e) {
			}
			try {
				Message progressMsg = lc.lfasrGetProgress(task_id);
				if (progressMsg.getOk() != 0) {
					System.out.println("task was fail. task_id:" + task_id);
					System.out.println("ecode=" + progressMsg.getErr_no());
					System.out.println("failed=" + progressMsg.getFailed());
					continue;
				} else {
					ProgressStatus progressStatus = JSON.parseObject(progressMsg.getData(), ProgressStatus.class);
					if (progressStatus.getStatus() == 9) {
						System.out.println("task was completed. task_id:" + task_id);
						break;	
					} else {
						System.out.println("task was incomplete. task_id:" + task_id + ", status:" + progressStatus.getDesc());
						continue;
					}
				}
			} catch (LfasrException e) {
				Message progressMsg = JSON.parseObject(e.getMessage(), Message.class);
				System.out.println("ecode=" + progressMsg.getErr_no());
				System.out.println("failed=" + progressMsg.getFailed());
			}
		}
		try {
			Message resultMsg = lc.lfasrGetResult(task_id);
			System.out.println(resultMsg.getData());
			if (resultMsg.getOk() == 0) {
				System.out.println(resultMsg.getData());
			} else {
				System.out.println("ecode=" + resultMsg.getErr_no());
				System.out.println("failed=" + resultMsg.getFailed());
			}
		} catch (LfasrException e) {
			Message resultMsg = JSON.parseObject(e.getMessage(), Message.class);
			System.out.println("ecode=" + resultMsg.getErr_no());
			System.out.println("failed=" + resultMsg.getFailed());
		}
	}
}

測試結果

(1) 全部中文均能成功轉成文字; 但英文Netweaver的語音轉換成了Net ballgithub

(2) 智能分詞也能按照指望工做,好比「測試一下」成功地分詞成了「測試」和「一下」。apache

完整的Java項目在個人github上:https://github.com/i042416/voice2text 要獲取更多Jerry的原創技術文章,請關注公衆號"汪子熙"或者掃描下面二維碼:json

相關文章
相關標籤/搜索