package com.dgtis.dorm.manager.db.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ArtificialUtil {

	public static final String WORDS_TYPE = "Words";//一段文案（客户端会自动转换成语音）
	public static final String VOICE_TYPE = "Voice";//一段音频
	public static final String IMAGE_TYPE = "Image";//图片或多个图片
	public static final String VIDEO_TYPE = "Video";//视频或多个视频

	/**
	 * 解析人工智能机器人结构化返回值
	 * @param str 结构化返回值
	 * @param key 参数key
	 * @return 参数值
	 */
	public static String getValueByKey(String str,String key) {
		if(str == null || key == null)
			return "";
		str = str + ";";
		String reg = "(?<=("+ key +":))(.*?)(?=;)";//以"key:"开头，以之后第一个";"结尾
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(str);
		if(m.find()) {
			return m.group();
		}
		return "";
	}


	/**
	 * 获取中文全拼(中文转拼音)
	 * @param src
	 * @return
	 */
	public static String getPingYin(String src) {
		char[] chars = null;
		chars = src.toCharArray();
		String[] strings = new String[chars.length];
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		format.setVCharType(HanyuPinyinVCharType.WITH_V);
		String result = "";
		int t0 = chars.length;
		try {
			for (int i = 0; i < t0; i++) {
				// 判断是否为汉字字符
				if (java.lang.Character.toString(chars[i]).matches("[\\u4E00-\\u9FA5]+")) {
					strings = PinyinHelper.toHanyuPinyinStringArray(chars[i], format);
					result += strings[0];
				} else {
					result += java.lang.Character.toString(chars[i]);
				}
			}
			return result;
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}
		return result;
	}
}
