The namegen5 website.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.2 KiB

3 years ago
  1. export function load(): Promise<void>
  2. export default class NameGenerator {
  3. /**
  4. * Create or load a name generator.
  5. * @param data Pass data to load the generator from it. NO VALIDATION is applied, so this should only come from
  6. * a trusted source.
  7. */
  8. constructor(data?: NameData)
  9. /**
  10. * This gets the data that can be passed in to recreate this exact generator. It will
  11. * cross the WASM boundary to grab this data, so use this sparingly.
  12. */
  13. get data(): NameData
  14. /**
  15. * Generate one name.
  16. *
  17. * @param formatName The format name to generate
  18. * @param seed Optionally, a seed for the generator.
  19. * @returns A string if the format exists, null otherwise.
  20. */
  21. generate(formatName: string, seed?: BigInt): string | null
  22. /**
  23. * Generate many names. If you need more than a few names, this method will
  24. * get them for you without crossing the WASM border more than once.
  25. *
  26. * Use case: For a website, generating a dozen of them with this and popping
  27. * them off an array on subsequent generator presses.
  28. *
  29. * @param formatName The format name to generate.
  30. * @param amount Amount of names to generate.
  31. * @param seed Optionally, a seed for the generator.
  32. * @returns An array of strings if it works, null otherwise.
  33. */
  34. generateMany(formatName: string, amount: number, seed?: BigInt): string[] | null
  35. /**
  36. * Add a new name part. If your IDE is worth its salt, it will hide any options you don't need to
  37. * care about for the specific name.
  38. *
  39. * @param options
  40. */
  41. addPart(options: AddPartOptions): void
  42. /**
  43. * Add a new formatting rule.
  44. *
  45. * Examples:
  46. * * `{first_name} {last_name}`: The referred name parts with a space between.
  47. * * `{first}'{clan} {=vas|=nar} {ship}`: The third `{...}` is either one of these two words.
  48. * * `{:full_name|:first_name}, the {title}`: The first `{...}` chooses between these two PREVIOUS formats.
  49. *
  50. * @param formatName
  51. * @param formatStr
  52. */
  53. addFormat(formatName: string, formatStr: string): void
  54. /**
  55. * Add a sample set to the generator. Different generators want different samples.
  56. *
  57. * * `markov`: SampleSet can only include Word samples. WordWeighted is allowed, but the weight is ignored. Labels are ignored.
  58. * * `cfgrammar`: SampleSet must only include Tokens samples, and the amount of tokens needs to match Labels. Use `*` for an anon. set.
  59. * * `wordlist`: SampleSet can include Word and WordWeighted samples. Labels are ignored.
  60. *
  61. * @param partName
  62. * @param sampleSet
  63. */
  64. learn(partName: string, sampleSet: SampleSet)
  65. /**
  66. * Calls the `data` getter.
  67. */
  68. toJSON(): NameData
  69. }
  70. type AddPartOptions =
  71. | ({ kind: "markov", initialTokens: string[], lrs: boolean, lrm: boolean, lre: boolean, rlf: boolean} & AddPartOptionsCommon)
  72. | ({ kind: "cfgrammar", initialSubtokens: string[], ral: boolean, rlf: boolean} & AddPartOptionsCommon)
  73. | ({ kind: "wordlist" } & AddPartOptionsCommon)
  74. interface AddPartOptionsCommon {name: string, formatRules: NamePartFormatRule[]}
  75. /**
  76. * A SampleSet is a versatile structure for feeding name generators learning data. The sample set should
  77. * be homogenous.
  78. */
  79. interface SampleSet {
  80. labels: string[]
  81. samples: Sample[]
  82. }
  83. type Sample =
  84. | { word: string }
  85. | { wordWeighted: [string, number] }
  86. | { tokens: string[] }
  87. interface NameData {
  88. parts: NamePartData[]
  89. formats: NameFormatData[]
  90. }
  91. interface NameFormatData {
  92. name: string
  93. parts: NameFormatPartData[]
  94. }
  95. type NameFormatPartData = { part: number } | { format: number } | { text: string } | { random: NameFormatPartData[] }
  96. interface NamePartData {
  97. name: string
  98. generator: NamePartGeneratorEnum
  99. formatRules: NamePartFormatRule[]
  100. }
  101. type NamePartGeneratorEnum =
  102. | {markov: MarkovData}
  103. | {cfgrammar: CFGrammarData}
  104. | {wordlist: WordListData}
  105. /**
  106. * The rust enum is showing here.
  107. */
  108. type NamePartFormatRule =
  109. | "capitalizeFirst"
  110. | "capitalizeDefault"
  111. | {capitalizeAfter: string}
  112. | {removeChar: string}
  113. | {replaceChar: {from: string, to: string}}
  114. interface MarkovData {
  115. tokens: string[]
  116. maxTokens: number[]
  117. starts: MarkovStartData[]
  118. totalStarts: number[]
  119. nodes: MarkovNodeData[]
  120. lengths: number[]
  121. totalLengths: number
  122. lrs: boolean
  123. lrm: boolean
  124. lre: boolean
  125. rtf: boolean
  126. }
  127. interface MarkovStartData {
  128. /**
  129. * The tokens to start with.
  130. */
  131. t: [number, number]
  132. /**
  133. * Weight, based on frequency in sample data.
  134. */
  135. w: number
  136. /**
  137. * The length if restrictions apply.
  138. */
  139. l?: number
  140. /**
  141. * The children (indices in nodes array)
  142. */
  143. c: number[]
  144. }
  145. interface MarkovNodeData {
  146. /**
  147. * Parents (sibling indices)
  148. */
  149. p: number[]
  150. /**
  151. * Token index
  152. */
  153. t: number
  154. /**
  155. * Node's weight
  156. */
  157. w: number
  158. /**
  159. * The length of the sample set (if restrictions apply).
  160. */
  161. l?: number
  162. /**
  163. * Children (sibling indices, omitted if empty)
  164. */
  165. c?: number[]
  166. }
  167. interface CFGrammarData {
  168. }
  169. interface WordListData {
  170. }