yum-archive/TaSTT-Whisper
High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model
git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper
7ef711a
master
1using System . Globalization ; 2using System . Text . RegularExpressions ; 3 4namespace CompressShaders 5{ 6static class LanguageCodes 7{ 8record struct Row 9{ 10public string keySource ; 11public uint keyValue ; 12public int code ; 13public string name ; 14} 15 16static uint makeKey ( string str ) 17{ 18if ( str . Length > 4 ) 19throw new ArgumentException (); 20uint k = 0 ; 21int shift = 0 ; 22foreach ( char c in str ) 23{ 24if ( c >= 0x80 ) 25throw new ArgumentException (); 26uint u = ( uint ) c ; 27k |= ( u << shift ); 28shift += 8 ; 29} 30return k ; 31} 32 33static IEnumerable < Row > load ( string path ) 34{ 35using var stm = File . OpenText ( path ); 36while ( true ) 37{ 38string ? line = stm . ReadLine (); 39if ( null == line ) 40break ; 41if ( string . IsNullOrWhiteSpace ( line ) ) 42continue ; 43string [] fields = line . Split ( '\t' ); 44yield return new Row () 45{ 46keySource = fields [ 0 ], 47keyValue = makeKey ( fields [ 0 ] ), 48code = int . Parse ( fields [ 1 ] ), 49name = fields [ 2 ] 50}; 51} 52} 53 54static void writeCpp ( string inl , Row [] data ) 55{ 56// TODO [very low]: sort them by the key here, then in C++ use binary search instead of the hash map 57using var stm = File . CreateText ( inl ); 58stm . WriteLine ( "// This file is generated by a tool, from the `languageCodez.tsv` file in this repository" ); 59foreach ( Row row in data ) 60stm . WriteLine ( "Lang{{ 0x{0:X}, {1}, \"{2}\" }}," , row . keyValue , row . code , row . name ); 61} 62 63static readonly CultureInfo ci = new CultureInfo ( "en-US" , false ); 64static string titleCase ( this string name ) => 65ci . TextInfo . ToTitleCase ( name . ToLower ( ci ) ); 66 67static void writeCs ( string cs , Row [] data ) 68{ 69using var stm = File . CreateText ( cs ); 70stm . WriteLine ( @"// This file is generated by a tool, from the `languageCodez.tsv` file in this repository 71namespace Whisper 72{ 73/// <summary>Supported languages</summary> 74/// <remarks>The values of this enum are zero-padded ASCII strings.<br/> 75/// It seems OpenAI tried to implement ISO 639-1, but they used the version of the standard from 1988.</remarks> 76public enum eLanguage: uint 77{" ); 78 79foreach ( Row row in data ) 80{ 81string tc = row . name . titleCase (); 82stm . WriteLine ( " /// <summary>{0}</summary>" , tc ); 83tc = Regex . Replace ( tc , @"\s+" , string . Empty ); 84stm . WriteLine ( " {0} = 0x{1:X}, // \"{2}\"" , tc , row . keyValue , row . keySource ); 85} 86stm . Write ( @" } 87}" ); 88} 89 90static void produce ( string tsv , string inl , string cs ) 91{ 92Row [] data = load ( tsv ). OrderBy ( r=> r . name ). ToArray (); 93writeCpp ( inl , data ); 94writeCs ( cs , data ); 95} 96 97public static void produce ( string solutionRoot ) 98{ 99string tsv = Path . Combine ( solutionRoot , "Whisper\\Whisper\\languageCodez.tsv" ); 100string inl = Path . Combine ( solutionRoot , "Whisper\\Whisper\\languageCodez.inl" ); 101string cs = Path . Combine ( solutionRoot , "WhisperNet\\API\\eLanguage.cs" ); 102produce ( tsv , inl , cs ); 103} 104} 105}