• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#将中文词组处理成拼音

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

 

using System.Runtime.InteropServices;

using System.Runtime.CompilerServices;

 

[DllImport("ole32.dll")]

public static extern int CLSIDFromString(

    [MarshalAs(UnmanagedType.LPWStr)] string lpsz,

    out Guid clsid);

 

[DllImport("ole32.dll")]

public static extern int CoCreateInstance(

    [In, MarshalAs(UnmanagedType.LPStruct)] Guid clsid,

    IntPtr pUnkOuter, uint dwClsContext,

    [In, MarshalAs(UnmanagedType.LPStruct)] Guid iid,

    out IntPtr pv);

 

[DllImport("ole32.dll", CallingConvention = CallingConvention.StdCall)]

public static extern int CoInitialize(IntPtr pvReserved);

 

public const int FELANG_REQ_REV = 0x00030000;

public const int FELANG_CMODE_PINYIN = 0x00000100;

public const int FELANG_CMODE_NOINVISIBLECHAR = 0x40000000;0

 

[ComImport]

[Guid("019F7152-E6DB-11D0-83C3-00C04FDDB82E")]

[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]

public interface IFELanguage

{

    [MethodImpl(MethodImplOptions.InternalCall,

        MethodCodeType = MethodCodeType.Runtime)]

    int Open();

    [MethodImpl(MethodImplOptions.InternalCall,

        MethodCodeType = MethodCodeType.Runtime)]

    int Close();

    [MethodImpl(MethodImplOptions.InternalCall,

        MethodCodeType = MethodCodeType.Runtime)]

    int GetJMorphResult(

      [In] uint dwRequest,

      [In] uint dwCMode,

      [In] int cwchInput,

      [In, MarshalAs(UnmanagedType.LPWStr)] string pwchInput,

      [In] IntPtr pfCInfo,

      [Out] out IntPtr ppResult

    );

}

 

public const int CLSCTX_INPROC_SERVER = 1;

public const int CLSCTX_INPROC_HANDLER = 2;

public const int CLSCTX_LOCAL_SERVER = 4;

public const int CLSCTX_SERVER = CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER;

 

[DllImport("kernel32.dll")]

public static extern int FormatMessage(int dwFlags, IntPtr lpSource,

    int dwMessageId, int dwLanguageId,

    StringBuilder lpBuffer, int nSize, IntPtr va_list_arguments);

public const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x200;

public const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;

public const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x2000;

 

[DllImport("kernel32.dll")]

public static extern int GetLastError();

 

[DllImport("ole32.dll")]

public static extern void CoTaskMemFree(IntPtr ptr);

 

public const int S_OK = 0x00000000;

 

public static string GetMessage(int errorCode)

{

    StringBuilder lpBuffer = new StringBuilder(0x200);

    if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM |

        FORMAT_MESSAGE_ARGUMENT_ARRAY,

        IntPtr.Zero, errorCode, 0, lpBuffer, lpBuffer.Capacity, IntPtr.Zero) != 0)

    {

        return lpBuffer.ToString();

    }

    return "Unknown";

}

 

private bool coInitialized = false;

private void button1_Click(object sender, EventArgs e)

{

    if (!coInitialized)

    {

        CoInitialize(IntPtr.Zero);

        coInitialized = true;

    }

 

    textBox2.Clear();

    Guid vGuidIme;

    int vError;

    vError = CLSIDFromString("MSIME.China", out vGuidIme);

    if (vError != S_OK)      

    {

        MessageBox.Show(GetMessage(vError));

        return;

    }

    Guid vGuidLanguage = new Guid("019F7152-E6DB-11D0-83C3-00C04FDDB82E");

    IntPtr vPPV;                  

    vError = CoCreateInstance(vGuidIme, IntPtr.Zero, CLSCTX_SERVER,

        vGuidLanguage, out vPPV);

    if (vError != S_OK)

    {

        MessageBox.Show(GetMessage(vError));

        return;

    }

    IFELanguage vLanguage =

        Marshal.GetTypedObjectForIUnknown(vPPV, typeof(IFELanguage)) as IFELanguage;

    vError = vLanguage.Open();

    if (vError != S_OK)

    {

        MessageBox.Show(GetMessage(vError));

        return;

    }

    IntPtr vMorrslt;

    string vInput = textBox1.Text;

    vError = vLanguage.GetJMorphResult(FELANG_REQ_REV,

        FELANG_CMODE_PINYIN | FELANG_CMODE_NOINVISIBLECHAR,

        vInput.Length, vInput, IntPtr.Zero, out vMorrslt);

    if (vError != S_OK)

    {

        MessageBox.Show(GetMessage(vError));

        return;

    }

    string vPinYin = Marshal.PtrToStringUni(Marshal.ReadIntPtr(vMorrslt, 4),

        Marshal.ReadInt16(vMorrslt, 8));

    textBox2.AppendText("=" + vPinYin  + "\r\n");

    IntPtr vMonoRubyPos = Marshal.ReadIntPtr(vMorrslt, 28);

    IntPtr vReadIdxWDD = Marshal.ReadIntPtr(vMorrslt, 24);

    int iReadIdxWDD = 0;

    int iMonoRubyPos = Marshal.ReadInt16(vMonoRubyPos);

    vMonoRubyPos = (IntPtr)((int)vMonoRubyPos + 2);

    int i = 0;

    while (i < vInput.Length)

    {

        while (i < Marshal.ReadInt16(vReadIdxWDD))

        {

            i++;

            if (i >= Marshal.ReadInt16(vReadIdxWDD))            

            {

                Console.WriteLine(Marshal.ReadInt16(vMonoRubyPos));

                string s = vPinYin.Substring(iMonoRubyPos,

                      Marshal.ReadInt16(vMonoRubyPos) - iMonoRubyPos);

                if (s != string.Empty)

                    s = vInput.Substring(iReadIdxWDD, i - iReadIdxWDD) + "(" + s + ")";

                else s = vInput.Substring(iReadIdxWDD, i - iReadIdxWDD);

                textBox2.AppendText(s + "\r\n");

                iReadIdxWDD = i;

                iMonoRubyPos = Marshal.ReadInt16(vMonoRubyPos);

                break;

            }

            vMonoRubyPos = (IntPtr)((int)vMonoRubyPos + 2);

            vReadIdxWDD = (IntPtr)((int)vReadIdxWDD + 2);

        }

        vMonoRubyPos = (IntPtr)((int)vMonoRubyPos + 2);

        vReadIdxWDD = (IntPtr)((int)vReadIdxWDD + 2);

    }

    CoTaskMemFree(vMorrslt);

    vLanguage.Close();

}

转自 http://blog.sina.com.cn/s/blog_589d32f501000aya.html,不知道效果咋样,有人说会有指针漂移,俺也不懂,不知道是不是内存泄漏。过段时间要用,得看一下。

另一个例子:

string pinyin = PinYin.ToPinYin("哈哈 你好");
            Console.WriteLine(pinyin);

 

https://files.cnblogs.com/igrl/%E6%B1%89%E5%AD%97%E8%BD%AC%E6%8B%BC%E9%9F%B3%E5%B8%A6%E5%A3%B0%E8%B0%83.rar


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#读取EXCEL数据发布时间:2022-07-13
下一篇:
关于C#中的异常捕捉发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap