Wrap punctuation APIs to C#. (#945)
This commit is contained in:
@@ -29,4 +29,4 @@ namespace SherpaOnnx
|
||||
public int FeatureDim;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OfflineLMConfig
|
||||
{
|
||||
@@ -22,5 +21,4 @@ namespace SherpaOnnx
|
||||
|
||||
public float Scale;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OfflineModelConfig
|
||||
{
|
||||
@@ -44,6 +43,4 @@ namespace SherpaOnnx
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string ModelType;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OfflineNemoEncDecCtcModelConfig
|
||||
{
|
||||
@@ -19,4 +18,4 @@ namespace SherpaOnnx
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Model;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,5 +18,4 @@ namespace SherpaOnnx
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Model;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
88
scripts/dotnet/OfflinePunctuation.cs
Normal file
88
scripts/dotnet/OfflinePunctuation.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)
|
||||
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
21
scripts/dotnet/OfflinePunctuationConfig.cs
Normal file
21
scripts/dotnet/OfflinePunctuationConfig.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)
|
||||
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OfflinePunctuationConfig
|
||||
{
|
||||
public OfflinePunctuationConfig()
|
||||
{
|
||||
Model = new OfflinePunctuationModelConfig();
|
||||
}
|
||||
public OfflinePunctuationModelConfig Model;
|
||||
}
|
||||
}
|
||||
|
||||
32
scripts/dotnet/OfflinePunctuationModelConfig.cs
Normal file
32
scripts/dotnet/OfflinePunctuationModelConfig.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)
|
||||
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OfflinePunctuationModelConfig
|
||||
{
|
||||
public OfflinePunctuationModelConfig()
|
||||
{
|
||||
CtTransformer = "";
|
||||
NumThreads = 1;
|
||||
Debug = 0;
|
||||
Provider = "cpu";
|
||||
}
|
||||
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string CtTransformer;
|
||||
|
||||
public int NumThreads;
|
||||
|
||||
public int Debug;
|
||||
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Provider;
|
||||
}
|
||||
}
|
||||
@@ -72,5 +72,4 @@ namespace SherpaOnnx
|
||||
[DllImport(Dll.Filename, EntryPoint = "DecodeMultipleOfflineStreams")]
|
||||
private static extern void Decode(IntPtr handle, IntPtr[] streams, int n);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OfflineRecognizerConfig
|
||||
{
|
||||
@@ -38,6 +37,4 @@ namespace SherpaOnnx
|
||||
|
||||
public float HotwordsScore;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
public class OfflineRecognizerResult
|
||||
{
|
||||
public OfflineRecognizerResult(IntPtr handle)
|
||||
@@ -44,6 +43,4 @@ namespace SherpaOnnx
|
||||
private String _text;
|
||||
public String Text => _text;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
public class OfflineStream : IDisposable
|
||||
{
|
||||
public OfflineStream(IntPtr p)
|
||||
@@ -68,5 +67,4 @@ namespace SherpaOnnx
|
||||
[DllImport(Dll.Filename, EntryPoint = "DestroyOfflineRecognizerResult")]
|
||||
private static extern void DestroyResult(IntPtr handle);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,5 +18,4 @@ namespace SherpaOnnx
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Model;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,5 +26,4 @@ namespace SherpaOnnx
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Joiner;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,5 +28,4 @@ namespace SherpaOnnx
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string RuleFars;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OfflineTtsModelConfig
|
||||
{
|
||||
@@ -26,4 +25,4 @@ namespace SherpaOnnx
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Provider;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,5 +33,4 @@ namespace SherpaOnnx
|
||||
|
||||
public int TailPaddings;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,5 +24,4 @@ namespace SherpaOnnx
|
||||
|
||||
public int MaxActive;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OnlineModelConfig
|
||||
{
|
||||
@@ -45,5 +44,4 @@ namespace SherpaOnnx
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string ModelType;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OnlineParaformerModelConfig
|
||||
{
|
||||
@@ -26,5 +25,4 @@ namespace SherpaOnnx
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Decoder;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OnlineRecognizerConfig
|
||||
{
|
||||
@@ -66,5 +65,4 @@ namespace SherpaOnnx
|
||||
|
||||
public OnlineCtcFstDecoderConfig CtcFstDecoderConfig;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
public class OnlineRecognizerResult
|
||||
{
|
||||
public OnlineRecognizerResult(IntPtr handle)
|
||||
@@ -103,4 +102,4 @@ namespace SherpaOnnx
|
||||
private float[] _timestamps;
|
||||
public float[] Timestamps => _timestamps;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
public class OnlineStream : IDisposable
|
||||
{
|
||||
public OnlineStream(IntPtr p)
|
||||
@@ -61,5 +60,4 @@ namespace SherpaOnnx
|
||||
[DllImport(Dll.Filename)]
|
||||
private static extern void InputFinished(IntPtr handle);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ using System;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OnlineTransducerModelConfig
|
||||
{
|
||||
@@ -30,5 +29,4 @@ namespace SherpaOnnx
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Joiner;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
19
scripts/dotnet/examples/offline-punctuation.csproj
Normal file
19
scripts/dotnet/examples/offline-punctuation.csproj
Normal file
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>offline_punctuation</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<RestoreSources>/tmp/packages;$(RestoreSources);https://api.nuget.org/v3/index.json</RestoreSources>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="org.k2fsa.sherpa.onnx" Version="*" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user