/
Mecab.cs
251 lines (232 loc) · 8.52 KB
/
Mecab.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace ChiiTrans
{
public enum MecabInitStatus
{
NOT_INITIALIZED,
SUCCESS,
FAILURE
}
class Mecab
{
private static string InstallPath;
private static bool InstallPathInitialized = false;
private static IntPtr dll;
private static Encoding MecabEncoding;
public static MecabInitStatus status = MecabInitStatus.NOT_INITIALIZED;
private static Mutex mutex = new Mutex();
private static IntPtr mecab;
public static string GetInstallPath()
{
if (InstallPathInitialized)
return InstallPath;
InstallPathInitialized = true;
InstallPath = null;
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Mecab");
if (key == null)
{
key = Registry.LocalMachine.OpenSubKey(@"Software\Mecab");
if (key == null)
return null;
}
string res = (string)key.GetValue("mecabrc");
if (res == null)
return null;
int x = res.LastIndexOf(@"etc\");
if (x < 0)
return null;
InstallPath = Path.Combine(res.Substring(0, x), "bin");
return InstallPath;
}
private static IntPtr LoadLibrary(string name)
{
string path = Path.Combine(InstallPath, name);
return PInvokeFunc.LoadLibraryEx(path, IntPtr.Zero, PInvokeFunc.LOAD_WITH_ALTERED_SEARCH_PATH);
}
private static bool UnloadLibrary(IntPtr handle)
{
if (handle != IntPtr.Zero)
return PInvokeFunc.FreeLibrary(handle);
else
return false;
}
private static bool LoadLibraries()
{
dll = LoadLibrary("libmecab.dll");
if (dll == IntPtr.Zero)
return false;
return true;
}
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)]
private delegate IntPtr mecab_new2Type(string arg);
private static mecab_new2Type mecab_new2;
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)]
private delegate void mecab_destroyType(IntPtr mecab);
private static mecab_destroyType mecab_destroy;
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)]
private delegate IntPtr mecab_sparse_tostrType(IntPtr mecab, byte[] str);
private static mecab_sparse_tostrType mecab_sparse_tostr;
[StructLayout(LayoutKind.Sequential)]
private class mecab_dictionary_info_t
{
public IntPtr filename;
public IntPtr charset;
public uint size;
public int type;
public uint lsize;
public uint rsize;
public ushort version;
public IntPtr next;
}
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)]
private delegate IntPtr mecab_dictionary_infoType(IntPtr mecab);
private static mecab_dictionary_infoType mecab_dictionary_info;
private static Delegate LoadFunc(IntPtr module, string name, Type T)
{
IntPtr addr = PInvokeFunc.GetProcAddress(module, name);
if (addr != IntPtr.Zero)
return Marshal.GetDelegateForFunctionPointer(addr, T);
else
throw new Exception("Cannot load function " + name + "!");
}
private static void LoadInterface()
{
mecab_new2 = (mecab_new2Type)LoadFunc(dll, "mecab_new2", typeof(mecab_new2Type));
mecab_destroy = (mecab_destroyType)LoadFunc(dll, "mecab_destroy", typeof(mecab_destroyType));
mecab_sparse_tostr = (mecab_sparse_tostrType)LoadFunc(dll, "mecab_sparse_tostr", typeof(mecab_sparse_tostrType));
mecab_dictionary_info = (mecab_dictionary_infoType)LoadFunc(dll, "mecab_dictionary_info", typeof(mecab_dictionary_infoType));
}
public static bool Initialize(string installPath)
{
while (initializing)
Thread.Sleep(1);
if (status == MecabInitStatus.NOT_INITIALIZED)
{
if (InitializeInt(installPath))
{
status = MecabInitStatus.SUCCESS;
return true;
}
else
{
status = MecabInitStatus.FAILURE;
Deinitialize();
return false;
}
}
else
{
return status == MecabInitStatus.SUCCESS;
}
}
public static void Deinitialize()
{
try
{
if (mecab != IntPtr.Zero)
mecab_destroy(mecab);
UnloadLibrary(dll);
}
catch (Exception)
{ }
}
private static bool initializing = false;
public static bool Ready()
{
return Initialize(null);
}
private static bool InitializeInt(string installPath)
{
try
{
initializing = true;
if (installPath != null)
{
InstallPath = installPath;
InstallPathInitialized = true;
}
else
{
GetInstallPath();
if (InstallPath == null)
return false;
}
if (!LoadLibraries())
return false;
LoadInterface();
mecab = mecab_new2("");
if (mecab == IntPtr.Zero)
return false;
if (!GetEncoding())
return false;
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
finally
{
initializing = false;
}
}
private static bool GetEncoding()
{
try
{
IntPtr info_ptr = mecab_dictionary_info(mecab);
mecab_dictionary_info_t info = new mecab_dictionary_info_t();
Marshal.PtrToStructure(info_ptr, info);
string charset = Marshal.PtrToStringAnsi(info.charset);
if (charset == "SHIFT-JIS")
MecabEncoding = Encoding.GetEncoding(932);
else if (charset == "EUC-JP")
MecabEncoding = Encoding.GetEncoding(20932);
else if (charset == "UTF-8")
MecabEncoding = Encoding.UTF8;
else
throw new Exception(string.Format("MeCab dictionary encoding {0} is not supported.", charset));
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public static string Translate(string source)
{
if (string.IsNullOrEmpty(source))
return source;
mutex.WaitOne();
try
{
byte[] inp = MecabEncoding.GetBytes(source);
IntPtr outp = mecab_sparse_tostr(mecab, inp);
string result;
if (outp != IntPtr.Zero)
{
result = MecabEncoding.GetString(PInvokeFunc.ByteArrayFromPtr(outp));
}
else
{
result = null;
}
return result;
}
finally
{
mutex.ReleaseMutex();
}
}
}
}