获取输入汉字的拼音简码

public class GetPinYin {


private char[] alphatable = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',

'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
private static int[] table = new int[27];

private static Map<String,String> rareWords;
private static Map<String,String> bookFamilyNames;
private static Map<String,String> bookFamilySurname;

static {

table[0]=0;
table[1]=45253;
table[2]=45761;
table[3]=46318;
table[4]=46826;
table[5]=47010;
table[6]=47297;
table[7]=47614;
table[8]=47614;
table[9]=48119;
table[10]=49062;
table[11]=49324;
table[12]=49896;
table[13]=50371;
table[14]=50614;
table[15]=50622;
table[16]=50906;
table[17]=51387;
table[18]=51446;
table[19]=52218;
table[20]=52218;
table[21]=52218;
table[22]=52698;
table[23]=52980;
table[24]=53689;
table[25]=54481;
table[26]=55289;


String file_name = new Constant().getClass().getClassLoader()
.getResource("rarewords.properties").getFile();

Properties prop = new Properties();
try {
prop.load(new FileInputStream(file_name));
} catch (Exception ex) {
Logit.errorLog(ex.getMessage(), new Throwable(ex));
}

rareWords = new HashMap<String, String>();
Iterator it=prop.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry=(Map.Entry)it.next();
Object key = entry.getKey();
Object value = entry.getValue();
rareWords.put(key.toString(), value.toString());
}


String file_name_1 = new Constant().getClass().getClassLoader()
.getResource("rarewords111.properties").getFile();

Properties prop1 = new Properties();
try {
prop1.load(new FileInputStream(file_name_1));
} catch (Exception ex) {
Logit.errorLog(ex.getMessage(), new Throwable(ex));
}

bookFamilyNames = new HashMap<String, String>();
Iterator it1=prop1.entrySet().iterator();
while(it1.hasNext()){
Map.Entry entry=(Map.Entry)it1.next();
Object key = entry.getKey();
Object value = entry.getValue();
bookFamilyNames.put(key.toString(), value.toString());
}

String file_name_2 = new Constant().getClass().getClassLoader()
.getResource("rarewords222.properties").getFile();

Properties prop2 = new Properties();
try {
prop2.load(new FileInputStream(file_name_2));
} catch (Exception ex) {
Logit.errorLog(ex.getMessage(), new Throwable(ex));
}

bookFamilySurname = new HashMap<String, String>();
Iterator it2 = prop2.entrySet().iterator();
while(it2.hasNext()){
Map.Entry entry=(Map.Entry)it2.next();
Object key = entry.getKey();
Object value = entry.getValue();
bookFamilySurname.put(key.toString(), value.toString());
}


}

public String getRareWord(char ch) {
Set<String> key = rareWords.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
if(rareWords.get(s).contains(String.valueOf(ch))) {
return s.substring(0, 1).toUpperCase();
}
}
return "0";
}
//根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
public String getPinYin(String SourceStr) {
String Result = "";
int StrLength = SourceStr.length();
int i;
try {
for (i = 0; i < StrLength; i++) {

if (i == 0) {
//姓
if (SourceStr.length() >= 2) {
String tempBjx = getXin(SourceStr.substring(0, 2));
if (tempBjx != "0") {
//在百家姓中,复姓
i++;
Result += tempBjx;
continue;
} else {
tempBjx = getXin(SourceStr.substring(0, 1));
if (tempBjx != "0") {
//在百家姓里
Result += tempBjx;
} else {
char temp = Char2Alpha(SourceStr.charAt(0));
if (temp == '0') {
Result += getRareWord(SourceStr.charAt(0));
} else {
Result += temp;
}
}
}
} else {
String tempBjx = getXin(SourceStr.substring(0, 1));
if (tempBjx != "0") {
//在百家姓里
Result += tempBjx;
} else {
char temp = Char2Alpha(SourceStr.charAt(0));
if (temp == '0') {
Result += getRareWord(SourceStr.charAt(0));
} else {
Result += temp;
}
}
}
} else {
//名
char temp = Char2Alpha(SourceStr.charAt(i));
if (temp == '0') {
Result += getRareWord(SourceStr.charAt(i));
} else {
Result += temp;
}
}


}
} catch (Exception e) {
Result = "";
}
return Result;
}

@SuppressWarnings("static-access")
public String getXin(String Xin) {

if (Xin == null || Xin.equals("")) {
return "0";
}
if (Xin.length() > 1) {
Iterator it = this.bookFamilySurname.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry=(Map.Entry)it.next();
String key = entry.getKey().toString();
String value = entry.getValue().toString();
if (value.contains(Xin)) {
return key.toUpperCase();
}
}

} else {
Iterator it = this.bookFamilyNames.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry=(Map.Entry)it.next();
String key = entry.getKey().toString();
String value = entry.getValue().toString();
if (value.contains(Xin)) {
return key.substring(0, 1).toUpperCase();
}
}
}

return "0";
}

//主函数,输入字符,得到他的声母,
//英文字母返回对应的大写字母
//其他非简体汉字返回 '0'
public char Char2Alpha(char ch) {

if (ch >= 'a' && ch <= 'z')
return (char) (ch - 'a' + 'A');
if (ch >= 'A' && ch <= 'Z')
return ch;
int gb = gbValue(ch);
if (gb < table[0])
return '0';
int i;
for (i = 0; i < 26; ++i) {
if (match(i, gb))
break;
}

if (i >= 26)
return '0';
else
return alphatable[i];
}


private boolean match(int i, int gb) {
if (gb < table[i])
return false;

int j = i + 1;

//字母Z使用了两个标签
while (j < 26 && (table[j] == table[i]))
++j;

if (j == 26)
return gb <= table[j];
else
return gb < table[j];

}

//取出汉字的编码
private int gbValue(char ch) {
String str = new String();
str += ch;
try {
byte[] bytes = str.getBytes("GB2312");
if (bytes.length < 2)
return 0;
return (bytes[0] << 8 & 0xff00) + (bytes[1] &
0xff);
} catch (Exception e) {
return 0;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值