@@ -152,35 +152,54 @@ static string CreateVigesimalCompoundExpression(JsonElement root) =>
152152 /// token stripping, multiplier behavior, and ordinal handling, and the generated output
153153 /// becomes a single immutable rules object consumed by <c>TokenMapWordsToNumberConverter</c>.
154154 /// </summary>
155- static string CreateTokenMapRulesExpression ( JsonElement root ) =>
156- "new() { " +
157- "CardinalMap = " + CreateStringLongFrozenDictionaryExpression ( EngineContractUtilities . GetRequiredElement ( root , "cardinalMap" ) ) + ", " +
158- "ExactOrdinalMap = " + CreateOptionalStringLongFrozenDictionaryExpression ( root , "ordinalMap" ) + ", " +
159- "OrdinalScaleMap = " + ( root . TryGetProperty ( "ordinalScaleMap" , out var ordinalScaleMap ) ? CreateStringLongFrozenDictionaryExpression ( ordinalScaleMap ) : "null" ) + ", " +
160- "GluedOrdinalScaleSuffixes = " + ( root . TryGetProperty ( "gluedOrdinalScaleSuffixes" , out var gluedOrdinalScaleSuffixes ) ? CreateStringLongFrozenDictionaryExpression ( gluedOrdinalScaleSuffixes ) : "null" ) + ", " +
161- "CompositeScaleMap = " + ( root . TryGetProperty ( "compositeScaleMap" , out var compositeScaleMap ) ? CreateStringLongFrozenDictionaryExpression ( compositeScaleMap ) : "null" ) + ", " +
162- "NormalizationProfile = TokenMapNormalizationProfile." + ToEnumMemberName ( GetRequiredString ( root , "normalizationProfile" ) ) + ", " +
163- "NegativePrefixes = " + CreateOptionalStringArrayExpression ( root , "negativePrefixes" ) + ", " +
164- "NegativeSuffixes = " + CreateOptionalStringArrayExpression ( root , "negativeSuffixes" ) + ", " +
165- "OrdinalPrefixes = " + CreateOptionalStringArrayExpression ( root , "ordinalPrefixes" ) + ", " +
166- "IgnoredTokens = " + CreateOptionalStringArrayExpression ( root , "ignoredTokens" ) + ", " +
167- "LeadingTokenPrefixesToTrim = " + CreateOptionalStringArrayExpression ( root , "leadingTokenPrefixesToTrim" ) + ", " +
168- "MultiplierTokens = " + CreateOptionalStringArrayExpression ( root , "multiplierTokens" ) + ", " +
169- "TokenSuffixesToStrip = " + CreateOptionalStringArrayExpression ( root , "tokenSuffixesToStrip" ) + ", " +
170- "OrdinalAbbreviationSuffixes = " + CreateOptionalStringArrayExpression ( root , "ordinalAbbreviationSuffixes" ) + ", " +
171- "TeenSuffixTokens = " + CreateOptionalStringArrayExpression ( root , "teenSuffixTokens" ) + ", " +
172- "HundredSuffixTokens = " + CreateOptionalStringArrayExpression ( root , "hundredSuffixTokens" ) + ", " +
173- "AllowTerminalOrdinalToken = " + ( GetBoolean ( root , "allowTerminalOrdinalToken" ) ? "true" : "false" ) + ", " +
174- "UseHundredMultiplier = " + ( GetBoolean ( root , "useHundredMultiplier" ) ? "true" : "false" ) + ", " +
175- "AllowInvariantIntegerInput = " + ( GetBoolean ( root , "allowInvariantIntegerInput" ) ? "true" : "false" ) + ", " +
176- "TeenBaseValue = " + ( GetOptionalInt64 ( root , "teenBaseValue" ) ? . ToString ( CultureInfo . InvariantCulture ) ?? "10" ) + ", " +
177- "HundredSuffixValue = " + ( GetOptionalInt64 ( root , "hundredSuffixValue" ) ? . ToString ( CultureInfo . InvariantCulture ) ?? "100" ) + ", " +
178- "UnitTokenMinValue = " + ( GetOptionalInt64 ( root , "unitTokenMinValue" ) ? . ToString ( CultureInfo . InvariantCulture ) ?? "1" ) + ", " +
179- "UnitTokenMaxValue = " + ( GetOptionalInt64 ( root , "unitTokenMaxValue" ) ? . ToString ( CultureInfo . InvariantCulture ) ?? "9" ) + ", " +
180- "HundredSuffixMinValue = " + ( GetOptionalInt64 ( root , "hundredSuffixMinValue" ) ? . ToString ( CultureInfo . InvariantCulture ) ?? "long.MaxValue" ) + ", " +
181- "HundredSuffixMaxValue = " + ( GetOptionalInt64 ( root , "hundredSuffixMaxValue" ) ? . ToString ( CultureInfo . InvariantCulture ) ?? "long.MinValue" ) + ", " +
182- "ScaleThreshold = " + ( GetOptionalInt64 ( root , "scaleThreshold" ) ? . ToString ( CultureInfo . InvariantCulture ) ?? "1000" ) +
183- " }" ;
155+ static string CreateTokenMapRulesExpression ( JsonElement root )
156+ {
157+ var assignments = new [ ]
158+ {
159+ RuleAssignment ( "CardinalMap" , CreateStringLongFrozenDictionaryExpression ( EngineContractUtilities . GetRequiredElement ( root , "cardinalMap" ) ) ) ,
160+ RuleAssignment ( "ExactOrdinalMap" , CreateOptionalStringLongFrozenDictionaryExpression ( root , "ordinalMap" ) ) ,
161+ RuleAssignment ( "OrdinalScaleMap" , CreateOptionalStringLongMapOrNull ( root , "ordinalScaleMap" ) ) ,
162+ RuleAssignment ( "GluedOrdinalScaleSuffixes" , CreateOptionalStringLongMapOrNull ( root , "gluedOrdinalScaleSuffixes" ) ) ,
163+ RuleAssignment ( "CompositeScaleMap" , CreateOptionalStringLongMapOrNull ( root , "compositeScaleMap" ) ) ,
164+ RuleAssignment ( "NormalizationProfile" , "TokenMapNormalizationProfile." + ToEnumMemberName ( GetRequiredString ( root , "normalizationProfile" ) ) ) ,
165+ RuleAssignment ( "NegativePrefixes" , CreateOptionalStringArrayExpression ( root , "negativePrefixes" ) ) ,
166+ RuleAssignment ( "NegativeSuffixes" , CreateOptionalStringArrayExpression ( root , "negativeSuffixes" ) ) ,
167+ RuleAssignment ( "OrdinalPrefixes" , CreateOptionalStringArrayExpression ( root , "ordinalPrefixes" ) ) ,
168+ RuleAssignment ( "IgnoredTokens" , CreateOptionalStringArrayExpression ( root , "ignoredTokens" ) ) ,
169+ RuleAssignment ( "LeadingTokenPrefixesToTrim" , CreateOptionalStringArrayExpression ( root , "leadingTokenPrefixesToTrim" ) ) ,
170+ RuleAssignment ( "MultiplierTokens" , CreateOptionalStringArrayExpression ( root , "multiplierTokens" ) ) ,
171+ RuleAssignment ( "TokenSuffixesToStrip" , CreateOptionalStringArrayExpression ( root , "tokenSuffixesToStrip" ) ) ,
172+ RuleAssignment ( "OrdinalAbbreviationSuffixes" , CreateOptionalStringArrayExpression ( root , "ordinalAbbreviationSuffixes" ) ) ,
173+ RuleAssignment ( "TeenSuffixTokens" , CreateOptionalStringArrayExpression ( root , "teenSuffixTokens" ) ) ,
174+ RuleAssignment ( "HundredSuffixTokens" , CreateOptionalStringArrayExpression ( root , "hundredSuffixTokens" ) ) ,
175+ RuleAssignment ( "AllowTerminalOrdinalToken" , CreateBooleanLiteral ( GetBoolean ( root , "allowTerminalOrdinalToken" ) ) ) ,
176+ RuleAssignment ( "UseHundredMultiplier" , CreateBooleanLiteral ( GetBoolean ( root , "useHundredMultiplier" ) ) ) ,
177+ RuleAssignment ( "AllowInvariantIntegerInput" , CreateBooleanLiteral ( GetBoolean ( root , "allowInvariantIntegerInput" ) ) ) ,
178+ RuleAssignment ( "TeenBaseValue" , CreateOptionalInt64Literal ( root , "teenBaseValue" , "10" ) ) ,
179+ RuleAssignment ( "HundredSuffixValue" , CreateOptionalInt64Literal ( root , "hundredSuffixValue" , "100" ) ) ,
180+ RuleAssignment ( "UnitTokenMinValue" , CreateOptionalInt64Literal ( root , "unitTokenMinValue" , "1" ) ) ,
181+ RuleAssignment ( "UnitTokenMaxValue" , CreateOptionalInt64Literal ( root , "unitTokenMaxValue" , "9" ) ) ,
182+ RuleAssignment ( "HundredSuffixMinValue" , CreateOptionalInt64Literal ( root , "hundredSuffixMinValue" , "long.MaxValue" ) ) ,
183+ RuleAssignment ( "HundredSuffixMaxValue" , CreateOptionalInt64Literal ( root , "hundredSuffixMaxValue" , "long.MinValue" ) ) ,
184+ RuleAssignment ( "ScaleThreshold" , CreateOptionalInt64Literal ( root , "scaleThreshold" , "1000" ) )
185+ } ;
186+
187+ return "new() { " + string . Join ( ", " , assignments ) + " }" ;
188+ }
189+
190+ static string RuleAssignment ( string propertyName , string expression ) =>
191+ propertyName + " = " + expression ;
192+
193+ static string CreateOptionalStringLongMapOrNull ( JsonElement root , string propertyName ) =>
194+ root . TryGetProperty ( propertyName , out var property )
195+ ? CreateStringLongFrozenDictionaryExpression ( property )
196+ : "null" ;
197+
198+ static string CreateBooleanLiteral ( bool value ) =>
199+ value ? "true" : "false" ;
200+
201+ static string CreateOptionalInt64Literal ( JsonElement root , string propertyName , string defaultExpression ) =>
202+ GetOptionalInt64 ( root , propertyName ) ? . ToString ( CultureInfo . InvariantCulture ) ?? defaultExpression ;
184203
185204 static string CreateGreedyCompoundOrdinalMapExpression ( WordsToNumberProfileDefinition profile )
186205 {
0 commit comments