Wrap punctuation APIs to C#. (#945)

This commit is contained in:
Fangjun Kuang
2024-05-30 16:19:56 +08:00
committed by GitHub
parent 082f230dfb
commit a99c7cb35c
31 changed files with 263 additions and 55 deletions

View File

@@ -29,4 +29,4 @@ namespace SherpaOnnx
public int FeatureDim;
}
}
}

View File

@@ -8,7 +8,6 @@ using System;
namespace SherpaOnnx
{
[StructLayout(LayoutKind.Sequential)]
public struct OfflineLMConfig
{
@@ -22,5 +21,4 @@ namespace SherpaOnnx
public float Scale;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -18,5 +18,4 @@ namespace SherpaOnnx
[MarshalAs(UnmanagedType.LPStr)]
public string Model;
}
}
}

View 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);
}
}

View 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;
}
}

View 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;
}
}

View File

@@ -72,5 +72,4 @@ namespace SherpaOnnx
[DllImport(Dll.Filename, EntryPoint = "DecodeMultipleOfflineStreams")]
private static extern void Decode(IntPtr handle, IntPtr[] streams, int n);
}
}
}

View File

@@ -8,7 +8,6 @@ using System;
namespace SherpaOnnx
{
[StructLayout(LayoutKind.Sequential)]
public struct OfflineRecognizerConfig
{
@@ -38,6 +37,4 @@ namespace SherpaOnnx
public float HotwordsScore;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -18,5 +18,4 @@ namespace SherpaOnnx
[MarshalAs(UnmanagedType.LPStr)]
public string Model;
}
}
}

View File

@@ -26,5 +26,4 @@ namespace SherpaOnnx
[MarshalAs(UnmanagedType.LPStr)]
public string Joiner;
}
}
}

View File

@@ -28,5 +28,4 @@ namespace SherpaOnnx
[MarshalAs(UnmanagedType.LPStr)]
public string RuleFars;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -33,5 +33,4 @@ namespace SherpaOnnx
public int TailPaddings;
}
}
}

View File

@@ -24,5 +24,4 @@ namespace SherpaOnnx
public int MaxActive;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -10,7 +10,6 @@ using System;
namespace SherpaOnnx
{
[StructLayout(LayoutKind.Sequential)]
public struct OnlineRecognizerConfig
{
@@ -66,5 +65,4 @@ namespace SherpaOnnx
public OnlineCtcFstDecoderConfig CtcFstDecoderConfig;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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;
}
}
}

View 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>