Skip to content

Commit 2e9d967

Browse files
committed
Linting and docs
1 parent b1c5d62 commit 2e9d967

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/importer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export class Importer {
121121
// Push refs to array so we can save them all at once later
122122
const refs = importFragment.refs.map((ref) => {
123123
const { string } = ref; // Save string as the following delete will delete ref.string as well
124+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
124125
const customData: Record<string, any> = ref;
125126
delete customData.string; // Only keep custom data
126127
const markovInputData = new MarkovInputData();

src/index.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
21
/* eslint-disable no-await-in-loop */
32
import { assignIn, isString, slice } from 'lodash';
43
import { Connection, ConnectionOptions, createConnection, getConnectionOptions, In } from 'typeorm';
@@ -33,6 +32,7 @@ export type MarkovDataMembers = {
3332

3433
export interface AddDataProps {
3534
string: string;
35+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3636
custom?: any;
3737
}
3838

@@ -51,7 +51,6 @@ export type MarkovGenerateOptions<CustomData> = {
5151
export type Corpus = { [key: string]: MarkovFragment[] };
5252

5353
export default class Markov {
54-
// public data: MarkovInputData
5554
public db: MarkovRoot;
5655

5756
public options: MarkovOptions | MarkovDataMembers;
@@ -66,9 +65,6 @@ export default class Markov {
6665

6766
/**
6867
* Creates an instance of Markov generator.
69-
*
70-
* @param {MarkovConstructorProps} [options={}]
71-
* @memberof Markov
7268
*/
7369
constructor(props?: MarkovConstructorProps) {
7470
// this.data = []
@@ -114,13 +110,13 @@ export default class Markov {
114110
}
115111
}
116112

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> {
121117
let queryCondition;
122-
if (condition instanceof MarkovRoot) {
123-
queryCondition = { startWordMarkov: condition };
118+
if (!condition) {
119+
queryCondition = { startWordMarkov: this.db };
124120
} else {
125121
queryCondition = { corpusEntry: condition };
126122
}
@@ -134,7 +130,6 @@ export default class Markov {
134130

135131
/**
136132
* Imports a corpus. This overwrites existing data
137-
* @param data
138133
*/
139134
public async import(data: MarkovRoot | MarkovV3ImportExport): Promise<void> {
140135
if ('id' in data) {
@@ -199,6 +194,10 @@ export default class Markov {
199194
return db;
200195
}
201196

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+
*/
202201
public async addData(rawData: AddDataProps[] | string[]) {
203202
// Format data if necessary
204203
let input: AddDataProps[] = [];
@@ -217,8 +216,6 @@ export default class Markov {
217216

218217
/**
219218
* Builds the corpus. You must call this before generating sentences.
220-
*
221-
* @memberof Markov
222219
*/
223220
private async buildCorpus(data: AddDataProps[]): Promise<void> {
224221
const { options } = this.db;
@@ -352,11 +349,8 @@ export default class Markov {
352349

353350
/**
354351
* Generates a result, that contains a string and its references
355-
*
356-
* @param {MarkovGenerateOptions} [options={}]
357-
* @returns {MarkovResult}
358-
* @memberof Markov
359352
*/
353+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
360354
public async generate<CustomData = any>(
361355
options: MarkovGenerateOptions<CustomData> = {}
362356
): Promise<MarkovResult<CustomData>> {
@@ -379,7 +373,12 @@ export default class Markov {
379373

380374
// Create an array of MarkovCorpusItems
381375
// 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];
383382

384383
let score = 0;
385384

0 commit comments

Comments
 (0)