package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import .;
import .;
import .;
import .;
import .;
public class LuochenPinyinUtils {
private static Map<String, List<String>> pinyinMap = new HashMap<String, List<String>>();
private static List<String> pinyin = new ArrayList<String>();
private static List<String> hpinyin = new ArrayList<String>();
private static String regx = "(,| |\\[|\\])";// 正则表达式,匹配字符串用
/**
* 初始化 所有的多音字词组
*/
static {
// 读取多音字的全部拼音表;
InputStream file = ("/duoyinzi_dic.txt");
BufferedReader br = null;
String s = null;
try {
br = new BufferedReader(new InputStreamReader(file,"UTF-8"));
while ((s = ()) != null) {
if (s != null) {
String[] arr = ("#");
String pinyin = arr[0];
String chinese = arr[1];
if (chinese != null) {
String[] strs = (" ");
//去空
if(strs!=null&&>0){
List<String> list = new ArrayList<String>();
for (int i = 0; i < ; i++) {
if(!"".equals(strs[i].trim())){
(strs[i].trim());
}
}
(pinyin, list);
}
}
}
}
} catch (IOException e) {
();
} finally {
try {
();
} catch (IOException e) {
();
}
}
}
public static void main(String[] args) {
String str = "重庆银行";
String py = getPinyin(str);
(str + " = " + py);
// String headP = getHeadPinyin(str);
// (str + " = " + headP);
}
public static String getPinyin(String chinese){
convertChineseToPinyin(chinese);
return (pinyin).replaceAll(regx, "");
}
public static String getHeadPinyin(String chinese){
convertChineseToPinyin(chinese);
return (hpinyin).replaceAll(regx, "");
}
/**
* 汉字转拼音 最大匹配优先
*
* @param chinese
* @return
*/
public static void convertChineseToPinyin(String chinese) {
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
();
(HanyuPinyinVCharType.WITH_V);
(HanyuPinyinToneType.WITHOUT_TONE);
char[] arr = ();
for (int i = 0; i < ; i++) {
char ch = arr[i];
if (ch > 128) { // 非ASCII码
// 取得当前汉字的所有全拼
try {
String[] results = (
ch, defaultFormat);
if (results != null) { // 非中文
int len = ;
if (len == 1) { // 不是多音字
setValue(results[0]);
} else { // 多音字
// 合并同音不同声调(去重)
List<String> duoyinziPinyins= new ArrayList<String>();// 定义一个空的数组
for(int k=0;k<len;k++){
if(!(results[k])){
(results[k]);
}
}
if(()==1){
setValue((0));// 如果新的集合长度是1,就取第一个
}else{//
//("注意!这是个多音字:" + ch);
int length = ();
boolean flag = false;
for (int x = 0; x < (); x++) {
String py = (x);
if (i + 3 <= length) { // 后向匹配2个汉字 大西洋
if(matchPinyins(py,chinese, i, i+3)){
flag = setValue(py);
break;
}
}
if (i + 2 <= length) { // 后向匹配 1个汉字 大西
if(matchPinyins(py,chinese, i, i+2)){
flag = setValue(py);
break;
}
}
if ((i - 2 >= 0) && (i + 1 <= length)) { // 前向匹配2个汉字
if(matchPinyins(py,chinese, i-2, i+1)){
flag = setValue(py);
break;
}
}
if ((i - 1 >= 0) && (i + 1 <= length)) { // 前向匹配1个汉字
// 固大
if(matchPinyins(py,chinese, i-1, i+1)){
flag = setValue(py);
break;
}
}
if ((i - 1 >= 0) && (i + 2 <= length)) { // 前向1个,后向1个
// 固大西
if(matchPinyins(py,chinese, i-1, i+2)){
flag = setValue(py);
break;
}
}
}
if (!flag) {
// 如果都没有找到,也就是常用读音
//("default = " + (0));
setValue((0));
}
}
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
();
}
} else {
setValue((ch));
}
}
}
// 截取词组,并匹配拼音表中的词组
private static boolean matchPinyins(String py,String chinese,int m,int n){
String s = (m,n);
List<String> cizus = (py);
if(cizus!=null&&(s)){
return true;
}
return false;
}
private static boolean setValue(String py) {
(py);
((0, 1));
return true;
}
}