alphacep / alphacep/vosk-api

C# Model.FindWord() method does not work for ANSI strings (for example Cyrillic)

Offen
#1,984 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Jupyter Notebook
Sterne
15.1k
Forks
1.8k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

C# Model.FindWord() method does not work for ANSI strings.

For example:
```
var myword = "привет";
model.FindWord(myword); --not work
```

I found several solutions to this problem:
1)
```
var myword = "привет";
byte[] utf8Bytes = Encoding.UTF8.GetBytes(myword + "\0");
IntPtr ansiStringPtr = Marshal.AllocHGlobal(utf8Bytes.Length);
Marshal.Copy(utf8Bytes, 0, ansiStringPtr, utf8Bytes.Length);
string example1 = Marshal.PtrToStringAnsi(ansiStringPtr);

model.FindWord(example1); --work
```

2)
```
//fixing the error:
//System.NotSupportedException: "No data is available for encoding 1251. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method."
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

var myword = "привет";
string example2 = Encoding.GetEncoding(1251).GetString(Encoding.UTF8.GetBytes(myword + "\0"));

model.FindWord(example2); --work
```

It may be necessary to add a new FindWordANSI() method.

And import from the library by specifying CharSet.Ansi:

```
[DllImport("libvosk", EntryPoint = "vosk_model_find_word", CharSet = CharSet.Ansi)] public static extern int Model_vosk_model_find_word_ansi(HandleRef jarg1, string jarg2);
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.