OfflinePunctuation.cs
2.4 KB
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
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace SherpaOnnx
{
public class OfflinePunctuation : IDisposable
{
public OfflinePunctuation(OfflinePunctuationConfig config)
{
IntPtr h = SherpaOnnxCreateOfflinePunctuation(ref config);
_handle = new HandleRef(this, h);
}
public String AddPunct(String text)
{
byte[] utf8Bytes = Encoding.UTF8.GetBytes(text);
IntPtr p = SherpaOfflinePunctuationAddPunct(_handle.Handle, utf8Bytes);
string s = "";
int length = 0;
unsafe
{
byte* b = (byte*)p;
if (b != null)
{
while (*b != 0)
{
++b;
length += 1;
}
}
}
if (length > 0)
{
byte[] stringBuffer = new byte[length];
Marshal.Copy(p, stringBuffer, 0, length);
s = Encoding.UTF8.GetString(stringBuffer);
}
SherpaOfflinePunctuationFreeText(p);
return s;
}
public void Dispose()
{
Cleanup();
// Prevent the object from being placed on the
// finalization queue
System.GC.SuppressFinalize(this);
}
~OfflinePunctuation()
{
Cleanup();
}
private void Cleanup()
{
SherpaOnnxDestroyOfflinePunctuation(_handle.Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
}
private HandleRef _handle;
[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOnnxCreateOfflinePunctuation(ref OfflinePunctuationConfig config);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxDestroyOfflinePunctuation(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOfflinePunctuationAddPunct(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Text);
[DllImport(Dll.Filename)]
private static extern void SherpaOfflinePunctuationFreeText(IntPtr p);
}
}