Japanese to Romaji with Kakasi

*爱你&永不变心* 提交于 2019-12-23 04:01:49

问题


I want to transliterate Japanese to Romaji with Kakasi tool, using C#. For this, I created a wrapper:

    [DllImport("kakasi.dll")]
    static extern int kakasi_getopt_argv(int size, IntPtr param);
    [DllImport("kakasi.dll")]
    static extern IntPtr kakasi_do([MarshalAs(UnmanagedType.LPStr)]string str);

    public static void SetParams(string [] paramz)
    {
        kakasi_getopt_argv(paramz.Length, StringToIntPtr(paramz));
    }

    public static string DoKakasi(string japanese)
    {
        return Marshal.PtrToStringAuto(kakasi_do(japanese));
    }

    private static IntPtr StringToIntPtr(string[] strings)
    {
        int bytesCount;
        IntPtr ptr = IntPtr.Zero;
        ArrayList stringBytes = new ArrayList();
        foreach (string str in strings)
        {
            stringBytes.AddRange(Encoding.Unicode.GetBytes(str));
            stringBytes.Add((byte)'\0');
        }
        bytesCount = stringBytes.Count;
        try
        {
            ptr = Marshal.AllocHGlobal(bytesCount);
            Marshal.Copy((byte[])stringBytes.ToArray(typeof(byte))
                , 0
                , ptr
                , bytesCount);
            return ptr;
        }
        catch
        {
            if (ptr != IntPtr.Zero)
                Marshal.FreeHGlobal(ptr);
            throw;
        }
    }

And then:

KakasiCs.SetParams(new[] { "kakasi", "-ja", "-ga", "-ka", "-Ea", "-Ka", "-Ha", "-Ja", "-U", "-s",});
var x = KakasiCs.DoKakasi("さかき");

I have 2 problems:

  1. Bad output - I receive no romaji, but something strange: "㼿?Äꈎᅵ鄠".
  2. In VS2010 every time I receive a warning with PInvokeStackImbalance exception.

Any help is appreciated. Thanks.


回答1:


I have used this library(only with c++ builder). Before passing the string to the kakasi, you should konvert string to the SHIFT-JIS code page. After processing convert it back to the unicode. Here the code that I use

    ...
    char*shift_jis=CodePageConverter::fromUnicode(932,InputTextBox->Text.c_bstr());
    char*converted_text=ProcessText(shift_jis);
    OutputTextBox->Text=CodePageConverter::toUnicode(932,converted_text);


    ...
    char* TForm1::ProcessText(char*string)
    {
      int paramscount=0;
      char**argv=CreateParameters(paramscount);

      kakasi_getopt_argv(paramscount, argv);
      char*result=kakasi_do(string);
      DeleteArguments(argv,paramscount);
      return result;
    }
    ...


来源:https://stackoverflow.com/questions/6442397/japanese-to-romaji-with-kakasi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!