Skip to content

Latest commit

 

History

History
187 lines (115 loc) · 4.64 KB

DataSet.md

File metadata and controls

187 lines (115 loc) · 4.64 KB

obscenity / DataSet

Class: DataSet<MetadataType>

Holds phrases (groups of patterns and whitelisted terms), optionally associating metadata with them.

Type parameters

Name Description
MetadataType Metadata type for phrases. Note that the metadata type is implicitly nullable.

Table of contents

Constructors

Methods

Constructors

constructor

new DataSet<MetadataType>()

Type parameters

Name
MetadataType

Methods

addAll

addAll(other): DataSet<MetadataType>

Adds all the phrases from the dataset provided to this one.

Example

const customDataset = new DataSet().addAll(englishDataset);

Parameters

Name Type Description
other DataSet<MetadataType> Other dataset.

Returns

DataSet<MetadataType>

Defined in

src/dataset/DataSet.ts:29


addPhrase

addPhrase(fn): DataSet<MetadataType>

Adds a phrase to this dataset.

Example

const data = new DataSet<{ originalWord: string }>()
	.addPhrase((phrase) => phrase.setMetadata({ originalWord: 'fuck' })
		.addPattern(pattern`fuck`)
		.addPattern(pattern`f[?]ck`)
		.addWhitelistedTerm('Afck'))
	.build();

Parameters

Name Type Description
fn (builder: PhraseBuilder<MetadataType>) => PhraseBuilder<MetadataType> A function that takes a [[PhraseBuilder]], adds patterns/whitelisted terms/metadata to it, and returns it.

Returns

DataSet<MetadataType>

Defined in

src/dataset/DataSet.ts:75


build

build(): Pick<RegExpMatcherOptions, "blacklistedTerms" | "whitelistedTerms">

Returns the dataset in a format suitable for usage with the [[RegExpMatcher]].

Example

// With the RegExpMatcher:
const matcher = new RegExpMatcher({
	...dataset.build(),
	// additional options here
});

Returns

Pick<RegExpMatcherOptions, "blacklistedTerms" | "whitelistedTerms">

Defined in

src/dataset/DataSet.ts:118


getPayloadWithPhraseMetadata

getPayloadWithPhraseMetadata(payload): MatchPayloadWithPhraseMetadata<MetadataType>

Retrieves the phrase metadata associated with a pattern and returns a copy of the match payload with said metadata attached to it.

Example

const matches = matcher.getAllMatches(input);
const matchesWithPhraseMetadata = matches.map((match) => dataset.getPayloadWithPhraseMetadata(match));
// Now we can access the 'phraseMetadata' property:
const phraseMetadata = matchesWithPhraseMetadata[0].phraseMetadata;

Parameters

Name Type Description
payload MatchPayload Original match payload.

Returns

MatchPayloadWithPhraseMetadata<MetadataType>

Defined in

src/dataset/DataSet.ts:94


removePhrasesIf

removePhrasesIf(predicate): DataSet<MetadataType>

Removes phrases that match the predicate given.

Example

const customDataset = new DataSet<{ originalWord: string }>()
	.addAll(englishDataset)
	.removePhrasesIf((phrase) => phrase.metadata.originalWord === 'fuck');

Parameters

Name Type Description
predicate (phrase: PhraseContainer<MetadataType>) => boolean A predicate that determines whether or not a phrase should be removed. Return true to remove, false to keep.

Returns

DataSet<MetadataType>

Defined in

src/dataset/DataSet.ts:46