ZeroC Ice(二)构建汉字拼音查询微服务:接口

同系列文章:

源代码下载

MandarinIce 20170906.zip

接口定义:
Mandarin.ice

#pragma once

module Com
{
	module FirstSolver
	{
		module Mandarin		
		{
			enum GBCharset {
				GB2312 = 1,
				GBK = 2,
				GB18030 = 4
			}

			dictionary<string,string> HomophoneMap;

			interface IMandarinService
			{
				// 查询汉字拼音串
				string QueryPinyin(string ucs4, bool isDigitTone, bool isUppercase);

				// 查询同音字
				HomophoneMap QueryHomophone(string ucs4, GBCharset charset);

				// 查询同音字集合
				HomophoneMap GetHomophoneGroupedList(GBCharset charset);
			}
		}
	}
}

接口实现:

/* ----------------------------------------------------------
 * 文件名称:MandarinIceService.cs
 * 
 * 作者:秦建辉
 * 
 * 微信:splashcn
 * 
 * 博客:http://www.firstsolver.com/wordpress/
 * 
 * 开发环境:
 *      Visual Studio 2017
 *      .NET Framework 4.5.2
 *      ZeroC Ice 3.7.0
 *      
 * 版本历史:
 *      V1.0    2017年09月06日
 *              基于ZeroC Ice框架实现汉字拼音查询服务-接口实现
 *
 * 参考资料:
 *      https://zeroc.com/
------------------------------------------------------------ */
using Ice;
using System;
using System.Collections.Generic;
using System.Text;

namespace Com.FirstSolver.Mandarin
{
    public class MandarinIceService : IMandarinServiceDisp_
    {
        public override Dictionary<string, string> GetHomophoneGroupedList(GBCharset charset, Current current = null)
        {
            try
            {   // 获取检索字符集
                HZCharSet SearchCharSet = (HZCharSet)Enum.Parse(typeof(HZCharSet), charset.ToString(), true);

                Dictionary<string, string> HomophoneList = new Dictionary<string, string>();
                for (int i = 0; i < Pinyin.SYLLABLE_NUM; i++)
                {
                    HomophoneList.Add(Pinyin.Syllable(i, true, true), Pinyin.Homophone(i, SearchCharSet));
                }
                return HomophoneList;
            }
            catch
            {
                return null;
            }
        }

        public override Dictionary<string, string> QueryHomophone(string ucs4, GBCharset charset, Current current = null)
        {
            try
            {   // 获取检索字符集
                HZCharSet SearchCharSet = (HZCharSet)Enum.Parse(typeof(HZCharSet), charset.ToString(), true);

                // 获取汉字拼音音节索引集合
                ushort[] SyllableCollection = Pinyin.Pronunciation(char.ConvertToUtf32(ucs4, 0));
                if (SyllableCollection == null) return null;

                Dictionary<string, string> HomophoneList = new Dictionary<string, string>();
                foreach (ushort Index in SyllableCollection)
                {
                    HomophoneList.Add(Pinyin.Syllable(Index, true, true), Pinyin.Homophone(Index, SearchCharSet));
                }
                return HomophoneList;
            }
            catch
            {
                return null;
            }
        }

        public override string QueryPinyin(string ucs4, bool isDigitTone, bool isUppercase, Current current = null)
        {
            try
            {   // 获取汉字拼音音节索引集合
                ushort[] SyllableCollection = Pinyin.Pronunciation(char.ConvertToUtf32(ucs4, 0));
                if (SyllableCollection == null) return null;

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < SyllableCollection.Length; i++)
                {
                    if (i == 0)
                    {
                        sb.Append(Pinyin.Syllable(SyllableCollection[i], isDigitTone, isUppercase));
                    }
                    else
                    {
                        sb.Append(" ").Append(Pinyin.Syllable(SyllableCollection[i], isDigitTone, isUppercase));
                    }
                }
                return sb.ToString();
            }
            catch
            {
                return null;
            }
        }
    }
}

Comments are closed.