Skip to main content

Module: util/allocateHats/chooseTokenHat

Functions

chooseTokenHat

chooseTokenHat(context, hatStability, tokenRank, oldTokenHat, candidates): HatCandidate | undefined

Selects a hat for a given token from amongst {@link candidates}, trading off hat quality and hat stability

IMPORTANT: This function assumes that all tokens with a lower rank than the given token have already been assigned hats.

We proceed as follows:

  1. Decide whether to keep our own hat, if a higher ranked token hasn't already taken it
  2. Decide whether to steal a hat from a lower ranked token

See hat assignment for more info.

FIXME: Could be improved by ignoring subsequent tokens that also contain another character that can be used with lower color. To compute that, look at all the other characters in the given subsequent token, look at their current color, and add the number of times it appears in between the current token and the given subsequent token.

Here is an example where the existing algorithm falls down: "ab ax b". It will put a hat on b for token ab because b appears two tokens away whereas a appears in the next token. However, if it had chosen a, then it could use x for ax, leaving b free for the final b token.

Parameters

NameTypeDescription
contextRankingContextLookup tables with information about which graphemes / hats other tokens have
hatStabilityHatStabilityThe user settings that determine when to keep / steal hats
tokenRanknumberThe rank of the token for whom we're picking a hat
oldTokenHatundefined | TokenHatThe hat that was on the token before (or undefined if it didn't have one)
candidatesHatCandidate[]The set of candidate hats under consideration (includes both hat style and grapheme)

Returns

HatCandidate | undefined

The chosen hat, or undefined if {@link candidates} was empty

Defined in

util/allocateHats/chooseTokenHat.ts:50