Skip to content

Commit 1af3f24

Browse files
committed
Prettier
1 parent 958cb6b commit 1af3f24

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "es5",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true
6+
}

src/index.ts

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
import { assignIn, cloneDeep, flatten, includes, isEmpty, isString, slice, some, uniqBy } from 'lodash'
1+
import {
2+
cloneDeep,
3+
flatten,
4+
includes,
5+
isEmpty,
6+
isString,
7+
slice,
8+
some,
9+
uniqBy,
10+
} from 'lodash'
11+
import assignIn from 'lodash/assignIn' // Workaround for weird issue with jest
212

313
export type MarkovInputData = { string: string }[]
414

515
export type MarkovGenerateOptions = {
6-
maxTries?: number,
7-
prng?: () => number,
16+
maxTries?: number
17+
prng?: () => number
818
filter?: (result: MarkovResult) => boolean
919
}
1020

@@ -24,9 +34,9 @@ export type MarkovDataMembers = {
2434
}
2535

2636
export type MarkovResult = {
27-
string: string,
28-
score: number,
29-
refs: MarkovInputData,
37+
string: string
38+
score: number
39+
refs: MarkovInputData
3040
tries: number
3141
}
3242

@@ -38,13 +48,16 @@ export type MarkovFragment = {
3848
export type Corpus = { [key: string]: MarkovFragment[] }
3949

4050
export type MarkovImportExport = {
41-
corpus: Corpus,
42-
startWords: MarkovFragment[],
43-
endWords: MarkovFragment[],
51+
corpus: Corpus
52+
startWords: MarkovFragment[]
53+
endWords: MarkovFragment[]
4454
options: MarkovDataMembers
4555
}
4656

47-
function sampleWithPRNG<T>(array: T[], prng: () => number = Math.random): T | undefined {
57+
function sampleWithPRNG<T>(
58+
array: T[],
59+
prng: () => number = Math.random
60+
): T | undefined {
4861
const length = array == null ? 0 : array.length
4962
return length ? array[Math.floor(prng() * length)] : undefined
5063
}
@@ -58,7 +71,7 @@ export default class Markov {
5871
public corpus: Corpus = {}
5972

6073
private defaultOptions: MarkovDataMembers = {
61-
stateSize: 2
74+
stateSize: 2,
6275
}
6376

6477
/**
@@ -95,20 +108,18 @@ export default class Markov {
95108
options: this.options,
96109
corpus: this.corpus,
97110
startWords: this.startWords,
98-
endWords: this.endWords
111+
endWords: this.endWords,
99112
})
100113
}
101114

102115
public addData(rawData: MarkovInputData | string[]) {
103116
// Format data if necessary
104117
let input: MarkovInputData = []
105118
if (isString(rawData[0])) {
106-
input = (rawData as string[]).map(s => ({ string: s }))
107-
}
108-
else if (rawData[0].hasOwnProperty('string')) {
119+
input = (rawData as string[]).map((s) => ({ string: s }))
120+
} else if (rawData[0].hasOwnProperty('string')) {
109121
input = rawData as MarkovInputData
110-
}
111-
else {
122+
} else {
112123
throw new Error('Objects in your corpus must have a "string" property')
113124
}
114125

@@ -126,7 +137,7 @@ export default class Markov {
126137
const options = this.options
127138

128139
// Loop through all sentences
129-
data.forEach(item => {
140+
data.forEach((item) => {
130141
const line = item.string
131142
const words = line.split(' ')
132143
const stateSize = options.stateSize // Default value of 2 is set in the constructor
@@ -135,16 +146,15 @@ export default class Markov {
135146
// "Start words" is the list of words that can start a generated chain.
136147

137148
const start = slice(words, 0, stateSize).join(' ')
138-
const oldStartObj = this.startWords.find(o => o.words === start)
149+
const oldStartObj = this.startWords.find((o) => o.words === start)
139150

140151
// If we already have identical startWords
141152
if (oldStartObj) {
142153
// If the current item is not present in the references, add it
143154
if (!includes(oldStartObj.refs, item)) {
144155
oldStartObj.refs.push(item)
145156
}
146-
}
147-
else {
157+
} else {
148158
// Add the startWords (and reference) to the list
149159
this.startWords.push({ words: start, refs: [item] })
150160
}
@@ -155,7 +165,7 @@ export default class Markov {
155165
// "End words" is the list of words that can end a generated chain.
156166

157167
const end = slice(words, words.length - stateSize, words.length).join(' ')
158-
const oldEndObj = this.endWords.find(o => o.words === end)
168+
const oldEndObj = this.endWords.find((o) => o.words === end)
159169
if (oldEndObj) {
160170
if (!includes(oldEndObj.refs, item)) {
161171
oldEndObj.refs.push(item)
@@ -180,7 +190,7 @@ export default class Markov {
180190

181191
// Check if the corpus already has a corresponding "curr" block
182192
if (this.corpus.hasOwnProperty(curr)) {
183-
const oldObj = this.corpus[curr].find(o => o.words === next)
193+
const oldObj = this.corpus[curr].find((o) => o.words === next)
184194
if (oldObj) {
185195
// If the corpus already has the chain "curr -> next",
186196
// just add the current reference for this block
@@ -189,8 +199,7 @@ export default class Markov {
189199
// Add the new "next" block in the list of possible paths for "curr"
190200
this.corpus[curr].push({ words: next, refs: [item] })
191201
}
192-
}
193-
else {
202+
} else {
194203
// Add the "curr" block and link it with the "next" one
195204
this.corpus[curr] = [{ words: next, refs: [item] }]
196205
}
@@ -200,7 +209,6 @@ export default class Markov {
200209
})
201210
}
202211

203-
204212
/**
205213
* Generates a result, that contains a string and its references
206214
*
@@ -210,7 +218,9 @@ export default class Markov {
210218
*/
211219
public generate(options: MarkovGenerateOptions = {}): MarkovResult {
212220
if (isEmpty(this.corpus)) {
213-
throw new Error('Corpus is empty. There is either no data, or the data is not sufficient to create markov chains.')
221+
throw new Error(
222+
'Corpus is empty. There is either no data, or the data is not sufficient to create markov chains.'
223+
)
214224
}
215225

216226
const corpus = cloneDeep(this.corpus)
@@ -253,25 +263,31 @@ export default class Markov {
253263
}
254264

255265
const sentence = arr
256-
.map(o => o.words)
266+
.map((o) => o.words)
257267
.join(' ')
258268
.trim()
259269

260270
const result = {
261271
string: sentence,
262272
score,
263-
refs: uniqBy(flatten(arr.map(o => o.refs)), 'string'),
264-
tries
273+
refs: uniqBy(flatten(arr.map((o) => o.refs)), 'string'),
274+
tries,
265275
}
266276

267277
// sentence is not ended or incorrect
268-
if (!ended || (typeof options.filter === 'function' && !options.filter(result))) {
278+
if (
279+
!ended ||
280+
(typeof options.filter === 'function' && !options.filter(result))
281+
) {
269282
continue
270283
}
271284

272285
return result
273286
}
274-
throw new Error(`Failed to build a sentence after ${tries - 1} tries. Possible solutions: try a less restrictive filter(), give more raw data to the corpus builder, or increase the number of maximum tries.`)
287+
throw new Error(
288+
`Failed to build a sentence after ${
289+
tries - 1
290+
} tries. Possible solutions: try a less restrictive filter(), give more raw data to the corpus builder, or increase the number of maximum tries.`
291+
)
275292
}
276-
277293
}

0 commit comments

Comments
 (0)