1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
1
/* eslint-disable no-await-in-loop */
3
2
import { assignIn , isString , slice } from 'lodash' ;
4
3
import { Connection , ConnectionOptions , createConnection , getConnectionOptions , In } from 'typeorm' ;
@@ -33,6 +32,7 @@ export type MarkovDataMembers = {
33
32
34
33
export interface AddDataProps {
35
34
string : string ;
35
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
36
custom ?: any ;
37
37
}
38
38
@@ -51,7 +51,6 @@ export type MarkovGenerateOptions<CustomData> = {
51
51
export type Corpus = { [ key : string ] : MarkovFragment [ ] } ;
52
52
53
53
export default class Markov {
54
- // public data: MarkovInputData
55
54
public db : MarkovRoot ;
56
55
57
56
public options : MarkovOptions | MarkovDataMembers ;
@@ -66,9 +65,6 @@ export default class Markov {
66
65
67
66
/**
68
67
* Creates an instance of Markov generator.
69
- *
70
- * @param {MarkovConstructorProps } [options={}]
71
- * @memberof Markov
72
68
*/
73
69
constructor ( props ?: MarkovConstructorProps ) {
74
70
// this.data = []
@@ -114,13 +110,13 @@ export default class Markov {
114
110
}
115
111
}
116
112
117
- // eslint-disable-next-line class-methods-use-this
118
- private async sampleFragment (
119
- condition : MarkovRoot | CorpusEntry
120
- ) : Promise < MarkovFragment | undefined > {
113
+ /**
114
+ * Gets a random fragment for a startWord or corpusEntry from the database.
115
+ */
116
+ private async sampleFragment ( condition ?: CorpusEntry ) : Promise < MarkovFragment | undefined > {
121
117
let queryCondition ;
122
- if ( condition instanceof MarkovRoot ) {
123
- queryCondition = { startWordMarkov : condition } ;
118
+ if ( ! condition ) {
119
+ queryCondition = { startWordMarkov : this . db } ;
124
120
} else {
125
121
queryCondition = { corpusEntry : condition } ;
126
122
}
@@ -134,7 +130,6 @@ export default class Markov {
134
130
135
131
/**
136
132
* Imports a corpus. This overwrites existing data
137
- * @param data
138
133
*/
139
134
public async import ( data : MarkovRoot | MarkovV3ImportExport ) : Promise < void > {
140
135
if ( 'id' in data ) {
@@ -199,6 +194,10 @@ export default class Markov {
199
194
return db ;
200
195
}
201
196
197
+ /**
198
+ * To function correctly, the Markov generator needs its internal data to be correctly structured. This allows you add raw data, that is automatically formatted to fit the internal structure.
199
+ * You can call this as often as you need, with new data each time. Multiple calls with the same data is not recommended, because it will skew the random generation of results.
200
+ */
202
201
public async addData ( rawData : AddDataProps [ ] | string [ ] ) {
203
202
// Format data if necessary
204
203
let input : AddDataProps [ ] = [ ] ;
@@ -217,8 +216,6 @@ export default class Markov {
217
216
218
217
/**
219
218
* Builds the corpus. You must call this before generating sentences.
220
- *
221
- * @memberof Markov
222
219
*/
223
220
private async buildCorpus ( data : AddDataProps [ ] ) : Promise < void > {
224
221
const { options } = this . db ;
@@ -352,11 +349,8 @@ export default class Markov {
352
349
353
350
/**
354
351
* Generates a result, that contains a string and its references
355
- *
356
- * @param {MarkovGenerateOptions } [options={}]
357
- * @returns {MarkovResult }
358
- * @memberof Markov
359
352
*/
353
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
360
354
public async generate < CustomData = any > (
361
355
options : MarkovGenerateOptions < CustomData > = { }
362
356
) : Promise < MarkovResult < CustomData > > {
@@ -379,7 +373,12 @@ export default class Markov {
379
373
380
374
// Create an array of MarkovCorpusItems
381
375
// The first item is a random startWords element
382
- const arr = [ ( await this . sampleFragment ( this . db ) ) ! ] ;
376
+ const firstSample = await this . sampleFragment ( ) ;
377
+ if ( ! firstSample )
378
+ throw new Error (
379
+ 'Could not get a random fragment. There is either no data, or the data is not sufficient to create markov chains.'
380
+ ) ;
381
+ const arr = [ firstSample ] ;
383
382
384
383
let score = 0 ;
385
384
0 commit comments