/** * @param ch * the given Chinese character * @param targetPinyinSystem * indicates target Chinese Romanization system should be * converted to * @return string representations of target Chinese Romanization system * corresponding to the given Chinese character in array format; * null if error happens * * @see PinyinRomanizationType */ private static string[] convertToTargetPinyinStringArray(char ch, PinyinRomanizationType targetPinyinSystem) { string[] hanyuPinyinStringArray = getUnformattedHanyuPinyinStringArray(ch); if (null != hanyuPinyinStringArray) { string[] targetPinyinStringArray = new string[hanyuPinyinStringArray.Length]; for (int i = 0; i < hanyuPinyinStringArray.Length; i++) { targetPinyinStringArray[i] = PinyinRomanizationTranslator.convertRomanizationSystem(hanyuPinyinStringArray[i], PinyinRomanizationType.HANYU_PINYIN, targetPinyinSystem); } return(targetPinyinStringArray); } else { return(null); } }
/** * convert the given unformatted Pinyin string from source Romanization * system to target Romanization system * * @param sourcePinyinStr * the given unformatted Pinyin string * @param sourcePinyinSystem * the Romanization system which is currently used by the given * unformatted Pinyin string * @param targetPinyinSystem * the Romanization system that should be converted to * @return unformatted Pinyin string in target Romanization system; null if * error happens */ public static string convertRomanizationSystem(string sourcePinyinStr, PinyinRomanizationType sourcePinyinSystem, PinyinRomanizationType targetPinyinSystem) { string pinyinString = TextHelper.ExtractPinyinString(sourcePinyinStr); string toneNumberStr = TextHelper.ExtractToneNumber(sourcePinyinStr); // return value string targetPinyinStr = null; try { // find the node of source Pinyin system string xpathQuery1 = "//" + sourcePinyinSystem.TagName + "[text()='" + pinyinString + "']"; XmlDocument pinyinMappingDoc = PinyinRomanizationResource.getInstance().PinyinMappingDoc; XmlNode hanyuNode = pinyinMappingDoc.SelectSingleNode(xpathQuery1); if (null != hanyuNode) { // find the node of target Pinyin system string xpathQuery2 = "../" + targetPinyinSystem.TagName + "/text()"; string targetPinyinStrWithoutToneNumber = hanyuNode.SelectSingleNode(xpathQuery2).Value; targetPinyinStr = targetPinyinStrWithoutToneNumber + toneNumberStr; } } catch (Exception e) { System.Console.WriteLine(e.ToString()); } return(targetPinyinStr); }