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();
分享到:
相关推荐
这个扩展翻译所有中文网页拼音(普通话)。更多细节,请参阅http://mand.jon-long.ca/web 在中文网页上添加拼音注释。 支持语言:English
convert mysql to oracle 工具,可以将MySQL轻松转换成Oracle
convert pdf to word , small convient tool
博文链接:https://robinwu.iteye.com/blog/146012
Convert Mysql to Oracle功能特点可能转换所有的Mysql字段类型 支持所有版本的Mysql 可以将数据合并到已经存在的ORACLE表中 非常容易使用的向导模式 支持所有版本的Mysql字符集 对超大数据表的导出进行了优化处理
包括天宝的数据解码软件Convert To RINEX 3.07版本和TrimbleCFGUpdate程序。安装时首先安装TrimbleCFGUpdate,再安装Convert To RINEX。
this code convert bmp pictures to jpg pictures (with compression)..and convert compressed jpg pictures back to bmp!!
用java中的JPEGImageEncoder类的压缩实现图片格式的转换
CFG 与PDA之间的等价转换 We will convert a CFG to a PDA using the LL parsing method.
If there are bugs in the BibTeX file, or if some things can't be converted to EndNote (e.g. keywords), then warnings will appear in the left pane. You can preview the XML in the right pane. To ...
Fast Report File format Convert fx3 to frx
convert elf to bin, easy to use
Convert RTF to HTML
该工具可以将Mysql数据库迁移到Oracle数据库,亲测有用
double_click_to_convert_01.wemtv
labview Convert Retangle To ROI
A source-to-source transpiler to convert Clojure to multip
AR_to_SS convert_to_lagged_form 源码