Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure Weighted Suggestions are generated #2384

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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