-
Notifications
You must be signed in to change notification settings - Fork 37
/
SentencePiece.cs
277 lines (239 loc) · 11.9 KB
/
SentencePiece.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using M = System.Runtime.CompilerServices.MethodImplAttribute;
using O = System.Runtime.CompilerServices.MethodImplOptions;
using CC = System.Runtime.InteropServices.CallingConvention;
namespace Seq2SeqSharp._SentencePiece
{
/// <summary>
///
/// </summary>
public sealed class SentencePiece : IDisposable
{
/// <summary>
///
/// </summary>
private static class Native
{
static Native() => Init();
private static bool Isx64() => (IntPtr.Size == 8);
private static bool IsLinux()
{
var p = (int) Environment.OSVersion.Platform;
return (p == 4) || (p == 6) || (p == 128);
}
#region [.DllImport.]
private const string DLL_WIN_x64 = "sentencepiece.dll"; //"sentencepiece_x64.dll";
private const string DLL_WIN_x86 = "sentencepiece_x86.dll";
private const string DLL_LIN_x64 = "libsentencepiece.so";
private const string DLL_LIN_x86 = DLL_LIN_x64;
private const string __SP_Init__name = "__SP_Init";
private const string __SP_Finalize__name = "__SP_Finalize";
private const string __SP_Encode__name = "__SP_Encode";
private const string __SP_Decode__name = "__SP_Decode";
private const string __SP_Free__name = "__SP_Free";
public delegate IntPtr __SP_Init_Delegate( string modelFilename, string vocabFilename, int threshold );
public delegate void __SP_Finalize_Delegate( IntPtr sp );
public delegate IntPtr __SP_Encode_Delegate( IntPtr sp, IntPtr input, int len );
public delegate IntPtr __SP_Decode_Delegate( IntPtr sp, IntPtr input, int len );
public delegate void __SP_Free_Delegate( IntPtr result );
#region [.win.]
#region [.x64.]
[DllImport(DLL_WIN_x64, CallingConvention=CC.Cdecl, EntryPoint=__SP_Init__name)]
private extern static IntPtr __SP_Init_win_x64( string modelFilename, string vocabFilename, int threshold );
[DllImport(DLL_WIN_x64, CallingConvention=CC.Cdecl, EntryPoint=__SP_Finalize__name)]
private extern static void __SP_Finalize_win_x64( IntPtr sp );
[DllImport(DLL_WIN_x64, CallingConvention=CC.Cdecl, EntryPoint=__SP_Encode__name)]
private extern static IntPtr __SP_Encode_win_x64( IntPtr sp, IntPtr input, int len );
[DllImport(DLL_WIN_x64, CallingConvention=CC.Cdecl, EntryPoint=__SP_Decode__name)]
private extern static IntPtr __SP_Decode_win_x64( IntPtr sp, IntPtr input, int len );
[DllImport(DLL_WIN_x64, CallingConvention=CC.Cdecl, EntryPoint=__SP_Free__name)]
private extern static void __SP_Free_win_x64( IntPtr sp );
#endregion
#region [.x86.]
[DllImport(DLL_WIN_x86, CallingConvention=CC.Cdecl, EntryPoint=__SP_Init__name)]
private extern static IntPtr __SP_Init_win_x86( string modelFilename, string vocabFilename, int threshold );
[DllImport(DLL_WIN_x86, CallingConvention=CC.Cdecl, EntryPoint=__SP_Finalize__name)]
private extern static void __SP_Finalize_win_x86( IntPtr sp );
[DllImport(DLL_WIN_x86, CallingConvention=CC.Cdecl, EntryPoint=__SP_Encode__name)]
private extern static IntPtr __SP_Encode_win_x86( IntPtr sp, IntPtr input, int len );
[DllImport(DLL_WIN_x86, CallingConvention=CC.Cdecl, EntryPoint=__SP_Decode__name)]
private extern static IntPtr __SP_Decode_win_x86( IntPtr sp, IntPtr input, int len );
[DllImport(DLL_WIN_x86, CallingConvention=CC.Cdecl, EntryPoint=__SP_Free__name)]
private extern static void __SP_Free_win_x86( IntPtr sp );
#endregion
#endregion
#region [.linux.]
#region [.x64.]
[DllImport(DLL_LIN_x64, CallingConvention=CC.Cdecl, EntryPoint=__SP_Init__name)]
private extern static IntPtr __SP_Init_lin_x64( string modelFilename, string vocabFilename, int threshold );
[DllImport(DLL_LIN_x64, CallingConvention=CC.Cdecl, EntryPoint=__SP_Finalize__name)]
private extern static void __SP_Finalize_lin_x64( IntPtr sp );
[DllImport(DLL_LIN_x64, CallingConvention=CC.Cdecl, EntryPoint=__SP_Encode__name)]
private extern static IntPtr __SP_Encode_lin_x64( IntPtr sp, IntPtr input, int len );
[DllImport(DLL_LIN_x64, CallingConvention=CC.Cdecl, EntryPoint=__SP_Decode__name)]
private extern static IntPtr __SP_Decode_lin_x64( IntPtr sp, IntPtr input, int len );
[DllImport(DLL_LIN_x64, CallingConvention=CC.Cdecl, EntryPoint=__SP_Free__name)]
private extern static void __SP_Free_lin_x64( IntPtr sp );
#endregion
#region [.x86.]
[DllImport(DLL_LIN_x86, CallingConvention=CC.Cdecl, EntryPoint=__SP_Init__name)]
private extern static IntPtr __SP_Init_lin_x86( string modelFilename, string vocabFilename, int threshold );
[DllImport(DLL_LIN_x86, CallingConvention=CC.Cdecl, EntryPoint=__SP_Finalize__name)]
private extern static void __SP_Finalize_lin_x86( IntPtr sp );
[DllImport(DLL_LIN_x86, CallingConvention=CC.Cdecl, EntryPoint=__SP_Encode__name)]
private extern static IntPtr __SP_Encode_lin_x86( IntPtr sp, IntPtr input, int len );
[DllImport(DLL_LIN_x86, CallingConvention=CC.Cdecl, EntryPoint=__SP_Decode__name)]
private extern static IntPtr __SP_Decode_lin_x86( IntPtr sp, IntPtr input, int len );
[DllImport(DLL_LIN_x86, CallingConvention=CC.Cdecl, EntryPoint=__SP_Free__name)]
private extern static void __SP_Free_lin_x86( IntPtr sp );
#endregion
#endregion
#endregion
public static __SP_Init_Delegate __SP_Init { [M(O.AggressiveInlining)] get; private set; }
public static __SP_Finalize_Delegate __SP_Finalize { [M(O.AggressiveInlining)] get; private set; }
public static __SP_Encode_Delegate __SP_Encode { [M(O.AggressiveInlining)] get; private set; }
public static __SP_Decode_Delegate __SP_Decode { [M(O.AggressiveInlining)] get; private set; }
public static __SP_Free_Delegate __SP_Free { [M(O.AggressiveInlining)] get; private set; }
private static void Init()
{
if ( IsLinux() )
{
if ( Isx64() )
{
__SP_Init = __SP_Init_lin_x64;
__SP_Finalize = __SP_Finalize_lin_x64;
__SP_Encode = __SP_Encode_lin_x64;
__SP_Decode = __SP_Decode_lin_x64;
__SP_Free = __SP_Free_lin_x64;
}
else
{
__SP_Init = __SP_Init_lin_x86;
__SP_Finalize = __SP_Finalize_lin_x86;
__SP_Encode = __SP_Encode_lin_x86;
__SP_Decode = __SP_Decode_lin_x86;
__SP_Free = __SP_Free_lin_x86;
}
}
else
{
if ( Isx64() )
{
__SP_Init = __SP_Init_win_x64;
__SP_Finalize = __SP_Finalize_win_x64;
__SP_Encode = __SP_Encode_win_x64;
__SP_Decode = __SP_Decode_win_x64;
__SP_Free = __SP_Free_win_x64;
}
else
{
__SP_Init = __SP_Init_win_x86;
__SP_Finalize = __SP_Finalize_win_x86;
__SP_Encode = __SP_Encode_win_x86;
__SP_Decode = __SP_Decode_win_x86;
__SP_Free = __SP_Free_win_x86;
}
}
}
}
/// <summary>
///
/// </summary>
public struct InitParams
{
public string ModelFilename { get; set; }
public string VocabFilename { get; set; }
public int? Threshold { get; set; }
}
#region [.ctor().]
private const char SPACE = ' ';
private IntPtr _SP;
public SentencePiece( string modelFilename, string vocabFilename = null, int threshold = 1_000 )
{
if ( !File.Exists( modelFilename ) ) throw (new FileNotFoundException( null, (modelFilename != null) ? Path.GetFullPath( modelFilename ) : modelFilename ));
_SP = Native.__SP_Init( modelFilename, vocabFilename, threshold );
}
public SentencePiece( in InitParams p ) : this( p.ModelFilename, p.VocabFilename, p.Threshold.GetValueOrDefault( 1_000 ) ) { }
~SentencePiece() => Native.__SP_Finalize( _SP );
public void Dispose()
{
Native.__SP_Finalize( _SP );
GC.SuppressFinalize( this );
}
#endregion
[M(O.AggressiveInlining)] unsafe private static string ConvertToString( IntPtr ptr )
{
//#region comm. EQUAL/SAME METHOD
////*
//var result_bytes = (byte*) ptr.ToPointer();
//var len = 0;
//for ( ; ; len++ )
//{
// if ( result_bytes[ len ] == 0 )
// {
// break;
// }
//}
//var result = Encoding.UTF8.GetString( result_bytes, len );
////*/
//#endregion
var result = Marshal.PtrToStringUTF8( ptr );
return (result);
}
unsafe public string Encode( string input, bool toLower = false )
{
if ( toLower )
{
input = input.ToLowerInvariant();
}
var bytes = Encoding.UTF8.GetBytes( input );
fixed ( byte* bytes_ptr = bytes )
{
var ptr = Native.__SP_Encode( _SP, (IntPtr) bytes_ptr, bytes.Length );
var result = ConvertToString( ptr );
Native.__SP_Free( ptr );
return (result);
}
}
public string Decode( IList< string > words, int startIndex, int count, StringBuilder buff = null )
{
if ( buff == null ) buff = new StringBuilder( 0x100 );
else buff.Clear();
for ( var end = startIndex + count; startIndex < end; startIndex++ )
{
var w = words[ startIndex ];
if ( buff.Length != 0 ) buff.Append( SPACE );
buff.Append( w );
}
var text = buff.ToString();
return (Decode( text ));
}
public string Decode( IList< string > words, StringBuilder buff = null ) //=> Decode( string.Join( " ", words ) );
{
if ( buff == null ) buff = new StringBuilder( 0x100 );
else buff.Clear();
foreach ( var w in words )
{
if ( buff.Length != 0 ) buff.Append( SPACE );
buff.Append( w );
}
var text = buff.ToString();
return (Decode( text ));
}
unsafe public string Decode( string input )
{
var bytes = Encoding.UTF8.GetBytes( input );
fixed ( byte* bytes_ptr = bytes )
{
var ptr = Native.__SP_Decode( _SP, (IntPtr) bytes_ptr, bytes.Length );
var result = ConvertToString( ptr );
Native.__SP_Free( ptr );
return (result.Trim()/*.Replace( '▁', SPACE ).TrimStart( SPACE )*/);
}
}
}
}