java 生成汉字同音字,拼音相同(二)

汉字内码,如涡,内码为CED0,而CED1,CED2皆与涡同音.

代码如下:

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;


public class test4 {



public static void main(String[] args) {
System.out.println(test("个"));
}
public static String test(String s){
char[] c = getCode(s).toCharArray();
String test =""+ c[0] + c[1] + c[2];
for (int i = 0; i < 9; i++) {


String access = getCharacter(test + i);

if(translation(access).equals(translation(s))&&(char)(i + 48)!=c[3]){
return access;
}
}
int ii = c[2];
c[2] = (char)(ii + 49);
test =""+ c[0] + c[1] + c[2];
for (int i = 0; i < 9; i++) {


String access = getCharacter(test + i);

if(translation(access).equals(translation(s))&&(char)(i + 48)!=c[3]){
return access;
}
}

c[2] = (char)(ii + 47);
for (int i = 0; i < 9; i++) {


String access = getCharacter(test + i);

if(translation(access).equals(translation(s))&&(char)(i + 48)!=c[3]){
return access;
}
}
return null;


private static byte charToByteAscii(char ch) {
byte byteAscii = (byte) ch;


return byteAscii;
}
private static char byteAsciiToChar(int ascii) {


char ch = (char) ascii;
;
return ch;
}
private static String getCode(String chineseName) {
StringBuffer sb = new StringBuffer();
try {
char[] ch = chineseName.toCharArray();
for (char c : ch) {
if (isCharacter(String.valueOf(c))) {
byte[] by = String.valueOf(c).getBytes("GBK");
for (byte b : by) {
sb.append(Integer.toHexString(b & 0xff));
}
} else {
byte b = (byte) c;
sb.append(Integer.toHexString(b & 0xff));
}
}
} catch (Exception e) {
e.printStackTrace();
}


return sb.toString().toUpperCase().trim();
}
private static String getCharacter(String jiNeiMa){  
        byte b1 = (byte) Integer.parseInt(jiNeiMa.substring(0, 2), 16);  
        byte b2 = (byte) Integer.parseInt(jiNeiMa.substring(2, 4), 16);  
          
        String str = "";  
        try {  
            str += new String(new byte[]{b1,b2}, "GBK");  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        return str;  
    }
private static boolean isCharacter(String str) {
Pattern p_str = Pattern.compile("[\\u4e00-\\u9fa5]+");
Matcher m = p_str.matcher(str);
if (m.find() && m.group(0).equals(str)) {
return true;
}
return false;
}
public static String translation(String src) {


char[] t1 = null;
t1 = src.toCharArray();
String[] t2 = new String[t1.length];
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();


t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
String t4 = "";
int t0 = t1.length;
try {
for (int i = 0; i < t0; i++) {
// 判断是否为汉字字符
if (java.lang.Character.toString(t1[i]).matches(
"[\\u4E00-\\u9FA5]+")) {
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
t4 += t2[0];
} else
t4 += java.lang.Character.toString(t1[i]);
}
// System.out.println(t4);
return t4;
} catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
return t4;
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值