Skip to content

Commit

Permalink
fix: Ensure Weighted Suggestions are generated (#2384)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Feb 1, 2022
1 parent 7b7e1c9 commit 7e72b91
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
SuggestOptions,
} from './SpellingDictionary';
import {
createWeightMapFromDictionaryInformation,
defaultNumSuggestions,
hasOptionToSearchOption,
impersonateCollector,
Expand Down Expand Up @@ -48,7 +49,7 @@ export class SpellingDictionaryFromTrie implements SpellingDictionary {
this.isDictionaryCaseSensitive = options.caseSensitive ?? !trie.isLegacy;
this.containsNoSuggestWords = options.noSuggest || false;
this._size = size || 0;
this.weightMap = options.weightMap;
this.weightMap = options.weightMap || createWeightMapFromDictionaryInformation(options.dictionaryInformation);
}

public get size(): number {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ export function suggestArgsToSuggestOptions(args: SuggestArgs): SuggestOptions {
};
return suggestOptions;
}

export function createWFromDictionaryInformation(di: DictionaryInformation): WeightMap {
return mapDictionaryInformationToWeightMap(di);
export function createWeightMapFromDictionaryInformation(di: undefined): undefined;
export function createWeightMapFromDictionaryInformation(di: DictionaryInformation): WeightMap;
export function createWeightMapFromDictionaryInformation(di: DictionaryInformation | undefined): WeightMap | undefined;
export function createWeightMapFromDictionaryInformation(di: DictionaryInformation | undefined): WeightMap | undefined {
return di ? mapDictionaryInformationToWeightMap(di) : undefined;
}

export const __testMethods = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AutoWeakCache, SimpleCache } from '../util/simpleCache';
import { SpellingDictionary, SpellingDictionaryOptions } from './SpellingDictionary';
import { SpellingDictionaryLoadError } from './SpellingDictionaryError';
import { SpellingDictionaryFromTrie } from './SpellingDictionaryFromTrie';
import { createWFromDictionaryInformation } from './SpellingDictionaryMethods';
import { createWeightMapFromDictionaryInformation } from './SpellingDictionaryMethods';

const defaultOptions: SpellingDictionaryOptions = Object.freeze({
weightMap: undefined,
Expand Down Expand Up @@ -56,7 +56,7 @@ function _createSpellingDictionary(params: CreateSpellingDictionaryParams): Spel
const trie = buildTrieFast(words);
const opts = { ...(options || defaultOptions) };
if (opts.weightMap === undefined && opts.dictionaryInformation) {
opts.weightMap = createWFromDictionaryInformation(opts.dictionaryInformation);
opts.weightMap = createWeightMapFromDictionaryInformation(opts.dictionaryInformation);
}
return new SpellingDictionaryFromTrie(trie, name, opts, source);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export function suggestionCollector(wordToMatch: string, options: SuggestionColl
timeout = DEFAULT_COLLECTOR_TIMEOUT,
weightMap,
} = options;

const numSuggestions = Math.max(options.numSuggestions, 0) || 0;
const sugs = new Map<string, SuggestionResult>();
let maxCost: number = BASE_COST * Math.min(wordToMatch.length * MAX_ALLOWED_COST_SCALE, changeLimit);
Expand Down

0 comments on commit 7e72b91

Please sign in to comment.