📄 dictionary.cpp
字号:
// Purpose : Serialise the node into or out of a file //
// Params : ar - Reference to archive to / from which we transfer data //
// Returns : None //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
void CNode::Serialise(CArchive & ar)
{
if (ar.IsLoading())
{
// Loading - get the character
ar >> m_Character;
// Get the bitmask indicating what children we have
BYTE byBitmask;
ar >> byBitmask;
// And load the children according to the bitmask
if (byBitmask & NODE_HAS_NEXT)
{
m_pNext = new CNode;
m_pNext->Serialise(ar);
}
if (byBitmask & NODE_HAS_ALTERN)
{
m_pAlternative = new CNode;
m_pAlternative->Serialise(ar);
}
}
else
{
// Saving - save the character
ar << m_Character;
// Create and save a bitmask indicating which children we have
BYTE byBitmask = 0;
if (m_pNext) byBitmask |= NODE_HAS_NEXT;
if (m_pAlternative) byBitmask |= NODE_HAS_ALTERN;
ar << byBitmask;
// Serialise our children
if (m_pNext) m_pNext->Serialise(ar);
if (m_pAlternative) m_pAlternative->Serialise(ar);
}
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : GetWordCount //
// Purpose : Get the word count in the tree //
// Params : nCount - Reference to the count variable //
// Returns : None //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
void CNode::GetWordCount(int & nCount)
{
if (m_Character == '\0') nCount++;
if (m_pNext) m_pNext->GetWordCount(nCount);
if (m_pAlternative) m_pAlternative->GetWordCount(nCount);
}
//////////////////////////////////////////////////////////////////////////////
// //
// CDictionary //
// //
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// //
// Function : CDictionary //
// Purpose : Constructor //
// Params : None //
// Returns : N/A //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
CDictionary::CDictionary()
{
m_pRootNode = NULL;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : ~CDictionary //
// Purpose : Destructor //
// Params : None //
// Returns : N/A //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
CDictionary::~CDictionary()
{
delete m_pRootNode;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : InsertWord //
// Purpose : Insert a word into the dictionary //
// Params : szWord - Word to be inserted //
// Returns : None //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
void CDictionary::InsertWord(LPCSTR szWord)
{
ASSERT(szWord);
if (m_pRootNode == NULL)
{
// We have no root node, so start the tree off with the first character of the word
m_pRootNode = new CNode(szWord[0]);
}
// Insert the word into the tree
m_pRootNode->InsertWord(szWord);
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : IsWordListed //
// Purpose : Determines if a word is listed in the dictionary //
// Params : szWord - Word to check //
// bMatchCase - true if case must be matched, else false //
// Returns : Word match enumeration showing if word was found //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
WordMatch CDictionary::IsWordListed(LPCSTR szWord)
{
if (m_pRootNode == NULL) return eMatchNone;
// First off, see if we have an exact case match
WordMatch match;
m_pRootNode->IsWordListed(szWord, true, match, true);
if (match != eMatchPerfect)
{
// Nope, see if we have anything remotely close
m_pRootNode->IsWordListed(szWord, false, match, true);
}
return match;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : GetSuggestions //
// Purpose : Get suggestions for a given word //
// Params : szWord - Word to suggest for //
// straSuggestions - Reference to array for results //
// bCaseSuggestionsOnly - true to return only suggestions that //
// are the same as the word, but with differing case //
// Returns : Number of suggestions found //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
int CDictionary::GetSuggestions(LPCSTR szWord, CStringArray & straSuggestions, bool bCaseSuggestionsOnly)
{
if (m_pRootNode == NULL) return 0;
// Check that the word is not listed - it is silly to suggest for a valid word!
ASSERT(IsWordListed(szWord) != eMatchPerfect);
if (IsWordListed(szWord) != eMatchPerfect)
{
// Get the suggestions that match the word but with difference cases
m_pRootNode->GetPatternMatchingWords(szWord, straSuggestions);
if (!bCaseSuggestionsOnly)
{
int nWordLength = strlen(szWord), nCount;
char * szBuffer = new char[nWordLength + 2];
// Get the substitution suggestions
// For each character, we consider that it may be wrong and see if there is anything else matching
for (nCount = 0; nCount < nWordLength; nCount++)
{
strcpy(szBuffer, szWord);
szBuffer[nCount] = '?';
m_pRootNode->GetPatternMatchingWords(szBuffer, straSuggestions);
}
// Get the deletion suggestions
// For each character position, we assume that there is a character missing
for (nCount = 0; nCount <= nWordLength; nCount++)
{
memset(szBuffer, 0, nWordLength + 2);
strncpy(szBuffer, szWord, nCount);
strcat(szBuffer, "?");
strcat(szBuffer, &szWord[nCount]);
m_pRootNode->GetPatternMatchingWords(szBuffer, straSuggestions);
}
// Get the insertion suggestions
// For each character , assume that the character is extraneous
for (nCount = 0; nCount < nWordLength; nCount++)
{
memset(szBuffer, 0, nWordLength + 2);
strncpy(szBuffer, szWord, nCount);
strcat(szBuffer, &szWord[nCount + 1]);
if (IsWordListed(szBuffer) != eMatchNone)
straSuggestions.Add(szBuffer);
}
// Get the transposition suggestions
// For each pair of characters, do a swap and see if we can exactly match the string
for (nCount = 0; nCount < nWordLength - 1; nCount++)
{
strcpy(szBuffer, szWord);
char cTemp = szBuffer[nCount];
szBuffer[nCount] = szBuffer[nCount + 1];
szBuffer[nCount + 1] = cTemp;
if (IsWordListed(szBuffer) != eMatchNone)
straSuggestions.Add(szBuffer);
}
delete [] szBuffer;
}
}
// Tidy up the suggestions list
SortSuggestions(straSuggestions);
RemoveDuplicates(straSuggestions);
return straSuggestions.GetSize();
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : PatternMatchWord //
// Purpose : Pattern match a given word (? represents any single char) //
// Params : szWord - Pattern to match //
// straSuggestions - Reference to array for results //
// Returns : Number of suggestions found //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -