// Copyright 2022 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef COMPONENTS_OMNIBOX_BROWSER_HISTORY_FUZZY_PROVIDER_H_ #define COMPONENTS_OMNIBOX_BROWSER_HISTORY_FUZZY_PROVIDER_H_ #include <array> #include <memory> #include <unordered_map> #include <vector> #include "base/memory/weak_ptr.h" #include "base/scoped_observation.h" #include "base/synchronization/waitable_event.h" #include "base/task/cancelable_task_tracker.h" #include "components/history/core/browser/history_service.h" #include "components/history/core/browser/history_service_observer.h" #include "components/history/core/browser/history_types.h" #include "components/omnibox/browser/autocomplete_input.h" #include "components/omnibox/browser/autocomplete_match.h" #include "components/omnibox/browser/autocomplete_result.h" #include "components/omnibox/browser/history_provider.h" // This namespace encapsulates the implementation details of fuzzy matching and // correction. It is used by the public (non-namespaced) HistoryFuzzyProvider // below. namespace fuzzy { // An `Edit` represents a single character change to a string. struct Edit { … }; // A `Correction` is a short list of `Edit`s required to change // an input string that escapes the trie into a string that is contained by it. struct Correction { … }; // This utility struct defines how tolerance changes across the length // of input text being processed. // Example: this schedule `{ .start_index = 1, .step_length = 4, .limit = 3 }` // means the first character must match, then starting from the second // character, one correction is tolerated per four characters, up to a maximum // of three total corrections. struct ToleranceSchedule { … }; // Nodes form a trie structure used to find potential input corrections. struct Node { … }; } // namespace fuzzy // This class is an autocomplete provider which provides URL results from // history for inputs that may match inexactly. // The mechanism that makes it "fuzzy" is a trie search that tolerates errors // and produces corrected inputs which can then be autocompleted as normal. // Relevance penalties are applied for corrections as errors reduce confidence. // The trie is built from history URL text and any text that can be formed // by tracing a path from the root is said to be "on trie" while any text // that escapes the trie with a character not present is said to be "off trie". // If the autocomplete input is fully on trie, no suggestions are provided // because exact matching should already provide the best results. If the // autocomplete input is off trie, corrections of bounded size are produced // to get the input back on trie, and these should then be eligible for // autocompletion. // The data structure could contain anything helpful, including ways to mark // terminal nodes (signaling a path as a complete string). A relevance score // could serve this purpose and make full independent autocompletion possible, // but efficiency is a top concern so node size is minimized, just enough to // get inputs back on track, not to replicate the full completion and scoring // of other autocomplete providers. class HistoryFuzzyProvider : public HistoryProvider, public history::HistoryServiceObserver { … }; #endif // COMPONENTS_OMNIBOX_BROWSER_HISTORY_FUZZY_PROVIDER_H_