`

convert chinese to pinyin

    博客分类:
  • J2EE
 
阅读更多
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * convert Chinese to Pinyin
 * @author Administrator
 *
 */
public class GBKPinyin {
	private final Map<Character, String> dictionary = new HashMap<Character, String>();
	private static GBKPinyin instance;
	
	public GBKPinyin() {
		initDB(); //init the dictionary when construct
	}
	/**
	 * get a app global instance of GBKPinyin
	 * @return
	 */
	public static GBKPinyin getInstance() {
		if (instance == null) {
			instance = new GBKPinyin();
		}
		return instance;
	}
	//init the internal dictionary
	private void initDB() {
		String GBIndex = loadStreamToString(loadResource("GBIndex.txt"));
		String[] pinyinTable = loadStreamToString(loadResource("PinYin.txt")).split("\\s+");
		//every 3 bytes as a group
		String pinyinIndex = loadStreamToString(loadResource("PinYinIndex.txt"));
		int[] index = new int[GBIndex.length()];
		char[] position = new char[3];
		for (int i = 0; i < pinyinIndex.length(); i++) {
			int mod = i % 3;
			switch(mod) {
			case 0:
				position[0] = pinyinIndex.charAt(i);
				break;
			case 1:
				position[1] = pinyinIndex.charAt(i);
				break;
			case 2:
				position[2] = pinyinIndex.charAt(i);
				//do with the position
				String octor = new String(position);
				int pos = Integer.valueOf(octor, 8);
				index[i / 3] = pos;
			}
		}
		//put the Character and pinyin to the dictionary
		for (int i = 0; i < GBIndex.length(); i++) {
			char cn = GBIndex.charAt(i);
			String pinyin = pinyinTable[index[i]];
			dictionary.put(cn, pinyin);
		}
	}
	/**
	 * load resource from class path
	 * @param path
	 * @return
	 */
	private InputStream loadResource(String path) {
		return GBKPinyin.class.getResourceAsStream(path);
	}
	/**
	 * read stream and read as GBK encoding
	 * @param in
	 * @return
	 */
	private String loadStreamToString(InputStream is) {
		StringBuilder sb = new StringBuilder();
		
		String line = null;
		
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, "GBK"));
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	/**
	 * convert Chinese to pinyin use travel sky rules
	 * A single word
	 * @param chin
	 * @return
	 */
	public String getPinyin(char character) {
		return dictionary.get(character);
	}
	/**
	 * convert a Chinese string to a pinyin string
	 * @param word
	 * @param seprator
	 * @return
	 */
	public String getPinyin(String word, String seprator) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0 ; i < word.length(); i++) {
			String converted = getPinyin(word.charAt(i));
			if (converted != null) {
				sb.append(getPinyin(word.charAt(i)));
			} else {
				//if you cann't translate the character
				sb.append(word.charAt(i)); //this shouldn't happen
			}
			//add seperator or not
			if (seprator != null && !"".equals(seprator) && i < word.length() - 1) {
				sb.append(seprator);
			}
		}
		return sb.toString().trim();
	}
	/**
	 * default: no seprator for the returned pinyin string
	 * @param word
	 * @return
	 */
	public String getPinyin(String word) {
		return getPinyin(word, "");
	}
}


使用:
GBKPinyin.getInstance().getPinyin(memberName).toUpperCase();
分享到:
评论

相关推荐

    将汉语转换成拼音(普通话)「Convert Chinese to Pinyin (Mand)」-crx插件

    这个扩展翻译所有中文网页拼音(普通话)。更多细节,请参阅http://mand.jon-long.ca/web 在中文网页上添加拼音注释。 支持语言:English

    chinese-convert-to-english-of-first.zip_english

    标题中的"chinese-convert-to-english-of-first.zip_english"暗示了这是一个关于将中文转换为英文首字母的程序或工具。这个压缩包可能包含了实现这一功能的源代码或者脚本,其中"english"标签进一步确认了这个工具与...

    根据中文获取拼音首字母

    例如,`TextInfo.ToTitleCase()`方法可以将字符串转换为标题格式,但它主要针对英文,对中文的支持有限。对于获取拼音首字母,可以使用`System.Globalization.ChineseLunisolarCalendar`类,但这需要一些复杂的处理...

    中国工具「Chinese Tools」-crx插件

    Useful Chinese learning tools, allows you to convert selected Chinese into Pinyin/Zhuyin, Trad./Simp. version, English or generate vocabulary list. Tools provided in this extension includes: - Chinese...

    简体转繁体,汉字到拼音,数字到大写 类库使用 代码示例

    def convert_to_traditional(simplified_text): converter = OpenCC('s2t') # 创建一个简体到繁体的转换器 traditional_text = converter.convert(simplified_text) return traditional_text ``` 接下来是“汉字...

    汉字获取首字母 ,拼音 繁体等

    public static String toPinyin(String chinese) { StringBuilder pinyin = new StringBuilder(); for (char c : chinese.toCharArray()) { String[] temp = PinyinHelper.toHanyuPinyinStringArray(c); if ...

    mysql的中文数据按拼音排序的2个方法

    客服那边需要我对一些酒店进行中文拼音排序,以前没有接触过,在php群里问...COLLATE gbk_chinese_ci 方法二、 代码如下: SELECT `hotel_id` , `hotel_name` , ELT( INTERVAL( CONV( HEX( left( CONVERT( `hotel_name`

    汉字转拼音

    public class ChineseToEnglish { // ------------------------将汉字转换为全拼------------------------------- public static String getPingYin(String src) { char[] t1 = null; t1 = src.toCharArray(); ...

    php中文转全拼

    在PHP中,可以使用`mb_convert_punctuation`和`iconv`等函数处理多语言字符,但它们并不能直接转换为拼音。因此,我们需要一个特定的库,比如“php-pinyin”库,它可能包含了汉字到拼音的映射表或者使用了开源的拼音...

    MySQL对中文进行排序详解及实例

    ALTER TABLE user CONVERT TO CHARACTER SET gbk COLLATE gbk_chinese_ci; ``` 3. 使用自定义的排序规则:MySQL允许创建自定义的校对集,这可能更复杂但能提供更高的定制性。如果你需要特定的排序规则,例如按...

Global site tag (gtag.js) - Google Analytics