This repository has been archived on 2025-08-26. You can view files and clone it, but cannot push or open issues or pull requests.
Files
enginex_bi_series-sherpa-onnx/scripts/dotnet/OfflinePunctuation.cs
东风破 00de2bd00b Refactor .Net example project (#1049)
Co-authored-by: 东风破 <birdfishs@163.com>
2024-06-24 10:10:13 +08:00

87 lines
2.3 KiB
C#

/// 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)
{
IntPtr p = SherpaOfflinePunctuationAddPunct(_handle.Handle, text);
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.LPStr)] string text);
[DllImport(Dll.Filename)]
private static extern void SherpaOfflinePunctuationFreeText(IntPtr p);
}
}