Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SpellCheckEngine.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Frameworks_Led_SpellCheckEngine_h_
5#define _Stroika_Frameworks_Led_SpellCheckEngine_h_ 1
6
7#include "Stroika/Frameworks/StroikaPreComp.h"
8
9/*
10@MODULE: SpellCheckEngine
11@DESCRIPTION: <p></p>
12
13 */
14
15#include "Support.h"
16#include "TextBreaks.h"
17
18namespace Stroika::Frameworks::Led {
19
20 /*
21 @CLASS: SpellCheckEngine
22 @DESCRIPTION: <p>Abstract spellchecker low-level API. This just defines the basic functionality used for looking for misspelled
23 words, and for finding guesses.
24 </p>
25 */
26 class SpellCheckEngine {
27 protected:
28 SpellCheckEngine () = default;
29
30 public:
31 virtual ~SpellCheckEngine () = default;
32
33 public:
34 /*
35 @METHOD: SpellCheckEngine::ScanForUndefinedWord
36 @DESCRIPTION: <p>Look in the given buffer input buffer, starting at 'cursor' for the next undefined
37 word Set wordStartResult/wordEndResult according to what is found. Note that if
38 '*cursor' is nullptr, then it is treated as being 'startBuf' instead. Return true if something is
39 found (setting only in this case wordStartResult/wordEndResult) and return false if no undefined
40 words are found. In either case - set 'cursor' on output to reflect how far we scanned ahead.
41 It is intended that this function be used iteratively and that you repeatedly pass IN
42 the same cursor that was passed out from the last call. On the first call - set the cursor value
43 to nullptr. Please don't confuse the cursor value with the POINTER TO the cursor value which is
44 what is passed in.
45 </p>
46 <p>
47 <code>
48 const int nChars = 100;
49 Led_tChar textToSearch [nChars];
50 // fill in textToSearch from someplace
51 const Led_tChar* cursor = nullptr;
52 const Led_tChar* wordStartResult = nullptr;
53 const Led_tChar* wordEndResult = nullptr;
54 while (ScanForUndefinedWord (textToSearch, textToSearch + nChars, &cursor,
55 &wordStartResult, &wordEndResult
56 )
57 ) {
58 // we found a possible undefined word.
59 Led_tString word = Led_tString{wordStartResult, wordEndResult};
60 }
61 // no more undefined words.
62 </code>
63 </p>
64 <p>Note that in the case where you haven't examined an entire buffer, but just bits at a time
65 (chunking) - you may find apparant undefined words at the edges of the buffer. It is up to you to
66 overlap your calls to check for undefined words in such a way that you verify any found undefined
67 words truely are undefined, by examining a larger surrounding region.
68 </p>
69 */
70 virtual bool ScanForUndefinedWord (const Led_tChar* startBuf, const Led_tChar* endBuf, const Led_tChar** cursor,
71 const Led_tChar** wordStartResult, const Led_tChar** wordEndResult) = 0;
72
73 public:
74 nonvirtual bool LookupWord (const Led_tString& checkWord, Led_tString* matchedWordResult = nullptr);
75
76 protected:
77 /*
78 @METHOD: SpellCheckEngine::LookupWord_
79 @ACCESS: protected
80 @DESCRIPTION: <p>pure virtual method of @'SpellCheckEngine' - called internally by @'SpellCheckEngine::LookupWord'.
81 This method is overriden in subclasses to to the actual word lookup - returning true (and setting matchedWordResult) if the
82 given word is found (considered legitimate).</p>
83 <p>This function may do much more than just lookikng the word up in a dictionary. It may perform
84 all sorts of lingustic manipulations (stripping trailing 's' from nouns etc) to look for a match.</p>
85 */
86 virtual bool LookupWord_ (const Led_tString& checkWord, Led_tString* matchedWordResult) = 0;
87
88 public:
89 /*
90 @METHOD: SpellCheckEngine::GenerateSuggestions
91 @DESCRIPTION: <p>This generates a list of words which are close to the given misspelled word. (ORDER
92 ALPHA OR MOST LIKELY FIRST????)</p>
93 */
94 virtual vector<Led_tString> GenerateSuggestions (const Led_tString& misspelledWord) = 0;
95
96 public:
97 /*
98 @METHOD: SpellCheckEngine::PeekAtTextBreaksUsed
99 @DESCRIPTION: <p>This method can return nullptr. If it returns non-nullptr, you can use this @'TextBreaks' object
100 to break the source text at word boundaries in much the same manner as the spellcheck engine does.</p>
101 <p>This can be useful if you provide a UI which breaks the input into chunks - but wants
102 to make sure the chunks correspond at the edges to word boundaries.</p>
103 <p>Note - the lifetime of the PeekAt call is short. It is at least til the next
104 call to the spellcheck engine (should I gaurantee longer?).
105 */
106 virtual TextBreaks* PeekAtTextBreaksUsed () = 0;
107
108 public:
109 class UDInterface;
110 /*
111 @METHOD: SpellCheckEngine::GetUDInterface
112 @DESCRIPTION: <p>This method can return nullptr if there is no UDInterface supported.</p>
113 */
114 virtual UDInterface* GetUDInterface () = 0;
115
116 public:
117 /*
118 @METHOD: SpellCheckEngine::Invariant
119 @DESCRIPTION: <p>if @'qStroika_Foundation_Debug_AssertionsChecked' is on, this called the virtual @'SpellCheckEngine::Invariant' () method
120 (in subclasses) to check the state. It can be called freely of @'qStroika_Foundation_Debug_AssertionsChecked' is off - it will have no effect. If
121 it is on, however, the time used by this funcion could be significant.</p>
122 */
123 nonvirtual void Invariant () const;
124
125#if qStroika_Foundation_Debug_AssertionsChecked
126 protected:
127 virtual void Invariant_ () const;
128#endif
129 };
130
131 /*
132 @CLASS: SpellCheckEngine::UDInterface
133 @DESCRIPTION: <p>
134 </p>
135 */
136 class SpellCheckEngine::UDInterface {
137 protected:
138 UDInterface () = default;
139
140 public:
141 virtual ~UDInterface () = default;
142
143 public:
144 /*
145 @METHOD: SpellCheckEngine::UDInterface::AddWordToUserDictionarySupported
146 @DESCRIPTION: <p>This method allows for the UDInterface interface to be supported by a spellcheck engine, but still to dynamically
147 turn on/off UD support (say if the UD is loaded or not).</p>
148 */
149 virtual bool AddWordToUserDictionarySupported () const = 0;
150
151 public:
152 /*
153 @METHOD: SpellCheckEngine::UDInterface::AddWordToUserDictionary
154 @DESCRIPTION: <p>Add the given word the current open (or primary) user dictionary. This is typically called from
155 the 'add word to dictionary' button in the spellcheck dialog.</p>
156 */
157 virtual void AddWordToUserDictionary (const Led_tString& word) = 0;
158 };
159
160}
161
162/*
163 ********************************************************************************
164 ***************************** Implementation Details ***************************
165 ********************************************************************************
166 */
167#include "SpellCheckEngine.inl"
168
169#endif /*_Stroika_Frameworks_Led_SpellCheckEngine_h_*/