java利用ascii码表,把汉字转成拼音(完整版)

2024-02-14 08:32

本文主要是介绍java利用ascii码表,把汉字转成拼音(完整版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

解决的问题实际上是: 输入一个汉语拼音,从数据库或xml文件里查询出对应的拼音开头的汉字

以下为解决问题需要的资源地址:

汉字和ascii码的关系(汉字码也叫内码是ascii码的扩展)
参考url地址: http://tech.ccw.com.cn/article/199466.htm

计算机中汉字是如何表示的(即汉字表示的基础知识)
http://www.cbi.pku.edu.cn/chinese/documents/csdoc/basic/

java利用ascii码表,把汉字转成拼音(完整版)  ,即  java 代码示例:
http://blog.lequ8.cn/louis/2006/6/27/

以上三个网址:一步步看看,就可以解决:
   给个“拼音或字母”,就可以从数据库里查询到相对应的 “汉字”。

 

网上资源提供了一个“利用ascii码表,把汉字转成拼音”的源代码,如下:

java利用ascii码表,把汉字转成拼音(完整版)

由于网上的api只能处理常见的汉字对于有些汉字转不了,或者不太准确,本人利用输入法的码表制作了个java的API
该API制作过程种碰到了"exceeding the 65535 bytes limit" java中每个函数不能多余65535个字节的限制,码表的初始化过程分了很多个函数来解决码表的初始化过程。代码部分如下:

完整代码下载:http://www.easyea.com/UserFiles/0/0/0/6/File/CnToSpell.zip


import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.Date;

/**
 *
 * 汉字转化为全拼,码表利用输入法的码表导出,汉字信息非常全,平时见到的汉字
 * 转拼音的API只能处理几千个一级汉字,很多字都没有法查到,尤其是我自己的姓
 * 都查不到所以只好自己做一个汉字转拼音的API,该API除可以处理99%以上的汉字,
 * 而且可以返回多音字的读音
 *
 * JDK版本: 1.4
 */
public class CnToSpell {
    private static LinkedHashMap spellMap = null;

    static {
        if (spellMap == null) {
            spellMap = new LinkedHashMap(20901);
        }
        initialize();
    }

    private CnToSpell() {
    }

    private static void spellPut(String ascii, String spells) {
        spellMap.put(ascii, spells);
    }
   
    private static void initialize() {
        init0();
        init1();
        init2();
        init3();
        init4();
        init5();
        init6();
        init7();
        init8();
        init9();
        init10();
        init11();
        init12();
        init13();
        init14();
        init15();
        init16();
        init17();
        init18();
        init19();
        init20();
        init21();
        init22();
        init23();
        init24();
        init25();
        init26();
        init27();
        init28();
        init29();
        init30();
        init31();
        init32();
        init33();
        init34();
        init35();
        init36();
        init37();
        init38();
        init39();
        init40();
        init41();
        init42();
        init43();
        init44();
        init45();
        init46();
        init47();
        init48();
        init49();
        init50();
        init51();
        init52();
        init53();
        init54();
        init55();
        init56();
        init57();
        init58();
        init59();
        init60();
        init61();
        init62();
        init63();
        init64();
        init65();
        init66();
        init67();
        init68();
        init69();
        init70();
        init71();
        init72();
        init73();
        init74();
        init75();
        init76();
        init77();
        init78();
        init79();
        init80();
        init81();
        init82();
        init83();
        init84();
        init85();
        init86();
        init87();
        init88();
        init89();
        init90();
        init91();
        init92();
        init93();
        init94();
        init95();
        init96();
        init97();
        init98();
        init99();
        init100();
        init101();
        init102();
        init103();
        init104();
        init105();
        init106();
        init107();
        init108();
        init109();
        init110();
        init111();
        init112();
        init113();
        init114();
        init115();
    }
   
    private static void init0() {
        spellPut("129-64", "kao,qiao,yu");
        spellPut("129-65", "shang");
        spellPut("129-66", "xia");
        spellPut("129-67", "mo ye o ne");
        spellPut("129-68", "mian");
        spellPut("129-69", "chou");
        spellPut("129-70", "shi");
        spellPut("129-71", "diu");
        spellPut("129-72", "qiu");
        spellPut("129-73", "liang");
        spellPut("129-74", "you");
        spellPut("129-75", "bing");
        spellPut("129-76", "jiu");
        spellPut("129-77", "ji");
        spellPut("129-78", "jie");
        spellPut("129-79", "guan,kuang");
        spellPut("129-80", "chan");
        spellPut("129-81", "zhuo");
        spellPut("129-82", "ba");
        spellPut("129-83", "jing");
        spellPut("129-84", "fu");
        spellPut("129-85", "yi,ji");
        spellPut("129-86", "yi");
        spellPut("129-87", "shi me");
        spellPut("129-88", "jiu");
        spellPut("129-89", "ho");
        spellPut("129-90", "yin");
        spellPut("129-91", "hu");
        spellPut("129-92", "cheng,sheng");
        spellPut("129-93", "hao,yi");
        spellPut("129-94", "wBn");
        spellPut("129-95", "gai");
        spellPut("129-96", "jiu");
        spellPut("129-97", "hB ri");
        spellPut("129-98", "ho lo");
        spellPut("129-99", "dou");
        spellPut("129-100", "shi");
        spellPut("129-101", "nang");
        spellPut("129-102", "gB ri");
        spellPut("129-103", "ge ri");
        spellPut("129-104", "dBo ri");
        spellPut("129-105", "mo ri");
        spellPut("129-106", "o lo");
        spellPut("129-107", "cB lo");
        spellPut("129-108", "xue");
        spellPut("129-109", "yan");
        spellPut("129-110", "po ri");
        spellPut("129-111", "sB lo");
        spellPut("129-112", "na");
        spellPut("129-113", "qian");
        spellPut("129-114", "sBo ri");
        spellPut("129-115", "er");
        spellPut("129-116", "zu ri");
        spellPut("129-117", "zB ri");
        spellPut("129-118", "zhi,luan");
        spellPut("129-119", "gui");
        spellPut("129-120", "qian");
        spellPut("129-121", "luan");
        spellPut("129-122", "lin");
        spellPut("129-123", "yi");
        spellPut("129-124", "jue");
        spellPut("129-125", "ma");
        spellPut("129-126", "shi");
        spellPut("129-128", "yu");
        spellPut("129-129", "si");
        spellPut("129-130", "sui");
        spellPut("129-131", "gen");
        spellPut("129-132", "ya");
        spellPut("129-133", "qi,zhai");
        spellPut("129-134", "ya");
        spellPut("129-135", "da");
        spellPut("129-136", "ye");
        spellPut("129-137", "xiang");
        spellPut("129-138", "jing");
        spellPut("129-139", "ye");
        spellPut("129-140", "you");
        spellPut("129-141", "dan");
        spellPut("129-142", "lian");
        spellPut("129-143", "duo");
        spellPut("129-144", "wei,men");
        spellPut("129-145", "ji");
        spellPut("129-146", "ji");
        spellPut("129-147", "wang");
        spellPut("129-148", "ba");
        spellPut("129-149", "bing");
        spellPut("129-150", "fo");
        spellPut("129-151", "jin,san");
        spellPut("129-152", "e o");
        spellPut("129-153", "xian");
        spellPut("129-154", "tuo,cha,duo");
        spellPut("129-155", "hong");
        spellPut("129-156", "gan,han");
        spellPut("129-157", "bo");
        spellPut("129-158", "chao");
        spellPut("129-159", "chang,zhang");
        spellPut("129-160", "chang");
        spellPut("129-161", "ren");
        spellPut("129-162", "fan");
        spellPut("129-163", "chao,miao");
        spellPut("129-164", "qian");
        spellPut("129-165", "wo");
        spellPut("129-166", "yao,fo");
        spellPut("129-167", "feng");
        spellPut("129-168", "cang");
        spellPut("129-169", "wang");
        spellPut("129-170", "di");
        spellPut("129-171", "zhong");
        spellPut("129-172", "pei");
        spellPut("129-173", "yu");
        spellPut("129-174", "diao");
        spellPut("129-175", "dun");
        spellPut("129-176", "wen");
        spellPut("129-177", "yi");
        spellPut("129-178", "xin");
        spellPut("129-179", "ji");
        spellPut("129-180", "ai");
        spellPut("129-181", "jin");
        spellPut("129-182", "pi");
        spellPut("129-183", "dan");
        spellPut("129-184", "fu");
        spellPut("129-185", "tang");
        spellPut("129-186", "cui");
        spellPut("129-187", "yun");
        spellPut("129-188", "che,ju");
        spellPut("129-189", "qian");
        spellPut("129-190", "xun");
        spellPut("129-191", "xin");
        spellPut("129-192", "chi");
        spellPut("129-193", "xian,xuan");
        spellPut("129-194", "nu");
        spellPut("129-195", "ni");
        spellPut("129-196", "xie");
        spellPut("129-197", "xu");
        spellPut("129-198", "zhou");
        spellPut("129-199", "qu");
        spellPut("129-200", "peng");
        spellPut("129-201", "pi");
        spellPut("129-202", "zhi");
        spellPut("129-203", "si");
        spellPut("129-204", "yi,chi");
        spellPut("129-205", "zheng");
        spellPut("129-206", "han,gan");
        spellPut("129-207", "mai");
        spellPut("129-208", "zhu");
        spellPut("129-209", "bu");
        spellPut("129-210", "qu");
        spellPut("129-211", "bi");
        spellPut("129-212", "zhao,shao");
        spellPut("129-213", "ci");
        spellPut("129-214", "yang");
        spellPut("129-215", "zhan,dian");
        spellPut("129-216", "bi");
        spellPut("129-217", "xian");
        spellPut("129-218", "qu");
        spellPut("129-219", "shi");
        spellPut("129-220", "bBo");
        spellPut("129-221", "hui,huai");
        spellPut("129-222", "ge");
        spellPut("129-223", "xiang");
        spellPut("129-224", "ge");
        spellPut("129-225", "fa");
        spellPut("129-226", "ming");
        spellPut("129-227", "bing");
        spellPut("129-228", "hen");
        spellPut("129-229", "huo");
        spellPut("129-230", "gui");
        spellPut("129-231", "quan");
        spellPut("129-232", "ci");
        spellPut("129-233", "xing");
        spellPut("129-234", "shen");
        spellPut("129-235", "tuo");
        spellPut("129-236", "gai");
        spellPut("129-237", "lai");
        spellPut("129-238", "yi");
        spellPut("129-239", "gong");
        spellPut("129-240", "yin");
        spellPut("129-241", "mi");
        spellPut("129-242", "xu");
        spellPut("129-243", "an");
        spellPut("129-244", "lu");
        spellPut("129-245", "er");
        spellPut("129-246", "lun");
        spellPut("129-247", "cha");
        spellPut("129-248", "chi");
        spellPut("129-249", "xun");
        spellPut("129-250", "zhou");
        spellPut("129-251", "ru");
        spellPut("129-252", "cun");
        spellPut("129-253", "si");
        spellPut("129-254", "dai");
    }
    .......
    .......
    .......

    /**
     * 获得单个汉字的Ascii,并用"-"连接成一个字符串
     *
     * @param cn char 汉字字符
     * @return string 错误返回 空字符串,否则返回ascii
     */
    public static String getCnAscii(char cn) {
        byte[] bytes = (String.valueOf(cn)).getBytes();
        System.out.println(bytes.length);
        if (bytes == null || bytes.length > 2 || bytes.length <= 0) { // 错误
            return "";
        }
        if (bytes.length == 1) { // 英文字符
            return new String(bytes);
        }
        if (bytes.length == 2) { // 中文字符
            int hightByte = 256 + bytes[0];
            int lowByte = 256 + bytes[1];

            String ascii = hightByte + "-" + lowByte;

            System.out.println("ASCII=" + ascii);

            return ascii;
        }

        return ""; // 错误
    }

    /**
     * 根据ASCII码连接成的字符串到SpellMap中查找对应的拼音
     *
     * @param ascii 字符对应的ASCII连接的字符串
     * @return String 拼音,首先判断是否是中文如果是英文直接返回字符,如果是中文返回拼音,
     *
     * 否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音.
     */
    public static String getSpellByAscii(String ascii) {
        if (ascii.indexOf("-") > -1)
        {
            return (String)spellMap.get(ascii);
        } else {
            return ascii;
        }
    }

    /**
     * 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换
     *
     * @param cnStr String字符串
     * @return String 转换成全拼后的字符串
     */
    public static String getFullSpell(String cnStr)
    {
        if (null == cnStr || "".equals(cnStr.trim())) {
            return cnStr;
        }

        char[] chars = cnStr.toCharArray();
        StringBuffer retuBuf = new StringBuffer();
        for (int i = 0, Len = chars.length; i < Len; i++) {
            String ascii = getCnAscii(chars[i]);
            if (ascii.length() == 0) { // 取ascii时出错
                retuBuf.append(chars[i]);
            } else {
                String spell = getSpellByAscii(ascii);
                if (spell == null) {
                    retuBuf.append(chars[i]);
                } else {
                    retuBuf.append(spell);
                } // end of if spell == null
            } // end of if ascii <= -20400
        } // end of for

        return retuBuf.toString();
    }

    /**
     * 获取汉语字符串的声母组合,每个汉字取拼音的第一个字符组成的一个字符串
     * @param cnStr 汉字的字符串
     * @return 每个汉字拼音的第一个字母所组成的汉字
     */
    public static String getFirstSpell(String cnStr)
    {
        if (null == cnStr || "".equals(cnStr.trim())) {
            return cnStr;
        }

        char[] chars = cnStr.toCharArray();
        StringBuffer retuBuf = new StringBuffer();
        for (int i = 0, Len = chars.length; i < Len; i++) {
            String ascii = getCnAscii(chars[i]);
            System.out.println(ascii);
            if (ascii.length() == 1) { // 取ascii时出错
                retuBuf.append(chars[i]);
            } else {
               
                String spell = getSpellByAscii(ascii).substring(0, 1);
                System.out.println(spell);
                if (spell == null) {
                    retuBuf.append(chars[i]);
                } else {
                    retuBuf.append(spell);
                } // end of if spell == null
            } // end of if ascii <= -20400
        } // end of for
        return retuBuf.toString();
    }

    public static void main(String[] args) {
        Date start = new Date();
        System.out.println(start.getTime());
        String str = null;
        str = "逯 闫 乐";
        System.out.println("Spell=" + CnToSpell.getFullSpell(str));
        System.out.println("Spell=" + CnToSpell.getFirstSpell(str));
        Date end = new Date();
        System.out.println(end.getTime());
    }
}

 

这篇关于java利用ascii码表,把汉字转成拼音(完整版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/707985

相关文章

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.