Skip to main content

SynonymResolver

aac-voice-api


Defined in: SynonymResolver.ts:17

SynonymResolver provides synonym matching capabilities using the DataMuse API.

This is the basic version that includes:

  • Singleton pattern
  • Basic synonym fetching from DataMuse API
  • Simple in-memory caching
  • Error handling

Example

const resolver = SynonymResolver.getInstance();
const synonyms = await resolver.getSynonyms('jump');
// Returns: ['leap', 'hop', 'spring', 'bound', ...]

Methods

areSynonyms()

areSynonyms(word1, word2): Promise`<boolean>`;

Defined in: SynonymResolver.ts:158

Checks if two words are synonyms of each other.

This method checks if word2 appears in word1's synonym list.

Parameters

ParameterTypeDescription

word1

string

First word to compare

word2

string

Second word to compare

Returns

Promise<boolean>

True if the words are synonyms, false otherwise

Example

const resolver = SynonymResolver.getInstance();
const areSynonyms = await resolver.areSynonyms('jump', 'leap');
console.log(areSynonyms); // true

clearCache()

clearCache(): void;

Defined in: SynonymResolver.ts:128

Clears the entire synonym cache. Useful for testing or if you want to force fresh API calls.

Returns

void


getCacheSize()

getCacheSize(): number;

Defined in: SynonymResolver.ts:138

Returns the number of words currently cached.

Returns

number

Number of words in the cache


getMax_Results()

getMax_Results(): number;

Defined in: SynonymResolver.ts:225

Returns

number


getSynonyms()

getSynonyms(word): Promise<string[]>;

Defined in: SynonymResolver.ts:64

Fetches synonyms for a given word from the DataMuse API. Results are cached in memory to avoid repeated API calls for the same word.

The DataMuse API parameter 'ml' means "means like" which returns synonyms. API is free and doesn't require authentication.

Parameters

ParameterTypeDescription

word

string

The word to find synonyms for

Returns

Promise<string[]>

Array of synonym words (lowercase), empty array on error

Example

const resolver = SynonymResolver.getInstance();
const synonyms = await resolver.getSynonyms('jump');
console.log(synonyms); // ['leap', 'hop', 'spring', 'bound', ...]

isCached()

isCached(word): boolean;

Defined in: SynonymResolver.ts:186

Checks if synonyms for a specific word are already cached.

Parameters

ParameterTypeDescription

word

string

The word to check

Returns

boolean

True if synonyms are cached, false otherwise

Example

const resolver = SynonymResolver.getInstance();
if (resolver.isCached('jump')) {
console.log('Synonyms for "jump" are already cached');
}

prefetchSynonyms()

prefetchSynonyms(words): Promise`<void>`;

Defined in: SynonymResolver.ts:205

Pre-fetches and caches synonyms for multiple words at once. Useful for initializing the cache with commonly used words.

Parameters

ParameterTypeDescription

words

string[]

Array of words to pre-fetch synonyms for

Returns

Promise<void>

Resolves when all synonyms are fetched

Example

const resolver = SynonymResolver.getInstance();
await resolver.prefetchSynonyms(['jump', 'run', 'walk']);
// All synonyms are now cached and ready for instant lookup

setMAX_RESULTS()

setMAX_RESULTS(numberOfSynonyms): void;

Defined in: SynonymResolver.ts:215

Parameters

ParameterType

numberOfSynonyms

number

Returns

void


getInstance()

static getInstance(): SynonymResolver;

Defined in: SynonymResolver.ts:40

Returns the singleton instance of SynonymResolver.

Returns

SynonymResolver

The single shared instance