2008-01-08 14:52:32 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Eliot
|
2009-06-23 23:39:07 +02:00
|
|
|
* Copyright (C) 2002-2009 Antoine Fraboulet & Olivier Teulière
|
2008-01-08 14:52:32 +01:00
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet @@ free.fr>
|
2008-07-07 19:29:59 +02:00
|
|
|
* Olivier Teulière <ipkiss @@ gmail.com>
|
2008-01-08 14:52:32 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cwchar>
|
|
|
|
#include <cwctype>
|
2009-07-03 23:40:14 +02:00
|
|
|
#include <algorithm>
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
#include "dic_internals.h"
|
2008-08-31 13:48:11 +02:00
|
|
|
#include "dic_exception.h"
|
2008-01-08 14:52:32 +01:00
|
|
|
#include "dic.h"
|
|
|
|
#include "header.h"
|
|
|
|
#include "encoding.h"
|
|
|
|
#include "regexp.h"
|
|
|
|
#include "automaton.h"
|
2008-07-07 19:29:59 +02:00
|
|
|
#include "grammar.h"
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
|
2008-03-02 19:45:10 +01:00
|
|
|
static const unsigned int DEFAULT_VECT_ALLOC = 100;
|
|
|
|
|
|
|
|
|
2008-11-22 14:11:48 +01:00
|
|
|
const DicEdge* Dictionary::seekEdgePtr(const wchar_t* s, const DicEdge *eptr) const
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
if (*s)
|
|
|
|
{
|
2008-11-22 14:11:48 +01:00
|
|
|
const DicEdge *p = getEdgeAt(eptr->ptr);
|
2008-01-08 14:52:32 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (p->chr == getHeader().getCodeFromChar(*s))
|
|
|
|
return seekEdgePtr(s + 1, p);
|
|
|
|
} while (!(*p++).last);
|
2008-11-22 14:11:48 +01:00
|
|
|
return getEdgeAt(0);
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return eptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Dictionary::searchWord(const wstring &iWord) const
|
|
|
|
{
|
|
|
|
if (!validateLetters(iWord))
|
|
|
|
return false;
|
|
|
|
|
2008-11-22 14:11:48 +01:00
|
|
|
const DicEdge *e = seekEdgePtr(iWord.c_str(), getEdgeAt(getRoot()));
|
|
|
|
return e->term;
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Global variables for searchWordByLen:
|
|
|
|
*
|
|
|
|
* A pointer to the structure is passed as a parameter
|
|
|
|
* so that all the search_* variables appear to the functions
|
|
|
|
* as global but the code remains re-entrant.
|
|
|
|
* Should be better to change the algorithm ...
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct params_7plus1_t
|
|
|
|
{
|
2009-09-21 22:10:34 +02:00
|
|
|
unsigned int added_code;
|
2009-06-23 14:41:53 +02:00
|
|
|
wdstring added_display;
|
2009-09-21 22:10:34 +02:00
|
|
|
map<unsigned int, vector<wdstring> > *results;
|
2008-01-08 14:52:32 +01:00
|
|
|
int search_len;
|
|
|
|
wchar_t search_wordtst[DIC_WORD_MAX];
|
|
|
|
char search_letters[63];
|
|
|
|
};
|
|
|
|
|
2009-06-23 14:41:53 +02:00
|
|
|
void Dictionary::searchWordByLen(struct params_7plus1_t ¶ms,
|
2008-11-22 14:11:48 +01:00
|
|
|
int i, const DicEdge *edgeptr) const
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
/* depth first search in the dictionary */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* the test is false only when reach the end-node */
|
|
|
|
if (edgeptr->chr)
|
|
|
|
{
|
|
|
|
/* is the letter available in search_letters */
|
2009-06-23 14:41:53 +02:00
|
|
|
if (params.search_letters[edgeptr->chr])
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
params.search_wordtst[i] = getHeader().getCharFromCode(edgeptr->chr);
|
|
|
|
params.search_letters[edgeptr->chr] --;
|
|
|
|
if (i == params.search_len)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
if (edgeptr->term)
|
|
|
|
{
|
2008-10-30 20:03:07 +01:00
|
|
|
// Add the solution
|
2009-09-21 22:10:34 +02:00
|
|
|
vector<wdstring> &sols = (*params.results)[params.added_code];
|
2009-06-23 14:41:53 +02:00
|
|
|
if (sols.empty() || sols.back() != params.search_wordtst)
|
2009-07-03 23:40:14 +02:00
|
|
|
sols.push_back(convertToDisplay(params.search_wordtst));
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-22 14:11:48 +01:00
|
|
|
searchWordByLen(params, i + 1, getEdgeAt(edgeptr->ptr));
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
2009-06-23 14:41:53 +02:00
|
|
|
params.search_letters[edgeptr->chr] ++;
|
|
|
|
params.search_wordtst[i] = L'\0';
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the letter is of course available if we have a joker available */
|
2009-06-23 14:41:53 +02:00
|
|
|
if (params.search_letters[0])
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
params.search_wordtst[i] = getHeader().getCharFromCode(edgeptr->chr);
|
|
|
|
params.search_letters[0] --;
|
|
|
|
if (i == params.search_len)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
if (edgeptr->term)
|
|
|
|
{
|
2008-10-30 20:03:07 +01:00
|
|
|
// Add the solution
|
2009-09-21 22:10:34 +02:00
|
|
|
vector<wdstring> &sols = (*params.results)[params.added_code];
|
2009-06-23 14:41:53 +02:00
|
|
|
if (sols.empty() || sols.back() != params.search_wordtst)
|
2009-07-03 23:40:14 +02:00
|
|
|
sols.push_back(convertToDisplay(params.search_wordtst));
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-22 14:11:48 +01:00
|
|
|
searchWordByLen(params, i + 1, getEdgeAt(edgeptr->ptr));
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
2009-06-23 14:41:53 +02:00
|
|
|
params.search_letters[0] ++;
|
|
|
|
params.search_wordtst[i] = L'\0';
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (! (*edgeptr++).last);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-22 14:11:48 +01:00
|
|
|
void Dictionary::search7pl1(const wstring &iRack,
|
2009-09-21 22:10:34 +02:00
|
|
|
map<unsigned int, vector<wdstring> > &oWordList,
|
2008-11-22 14:11:48 +01:00
|
|
|
bool joker) const
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
if (iRack == L"" || iRack.size() > DIC_WORD_MAX)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct params_7plus1_t params;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < sizeof(params.search_letters); i++)
|
|
|
|
params.search_letters[i] = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the letters are verified and changed to the dic internal
|
|
|
|
* representation (using getCodeFromChar(*r))
|
|
|
|
*/
|
|
|
|
int wordlen = 0;
|
|
|
|
for (const wchar_t* r = iRack.c_str(); *r; r++)
|
|
|
|
{
|
|
|
|
if (iswalpha(*r))
|
|
|
|
{
|
|
|
|
params.search_letters[getHeader().getCodeFromChar(*r)]++;
|
|
|
|
wordlen++;
|
|
|
|
}
|
|
|
|
else if (*r == L'?')
|
|
|
|
{
|
|
|
|
if (joker)
|
|
|
|
{
|
|
|
|
params.search_letters[0]++;
|
|
|
|
wordlen++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
oWordList[0].push_back(L"** joker **");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wordlen < 1)
|
|
|
|
return;
|
|
|
|
|
2008-11-22 14:11:48 +01:00
|
|
|
const DicEdge *root_edge = getEdgeAt(getRoot());
|
|
|
|
root_edge = getEdgeAt(root_edge->ptr);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
params.results = &oWordList;
|
|
|
|
|
|
|
|
/* search for all the words that can be done with the letters */
|
2009-06-23 14:41:53 +02:00
|
|
|
params.added_code = 0;
|
|
|
|
params.added_display = L"";
|
2008-01-08 14:52:32 +01:00
|
|
|
params.search_len = wordlen - 1;
|
|
|
|
params.search_wordtst[wordlen] = L'\0';
|
2009-06-23 14:41:53 +02:00
|
|
|
searchWordByLen(params, 0, root_edge);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
/* search for all the words that can be done with the letters +1 */
|
|
|
|
params.search_len = wordlen;
|
|
|
|
params.search_wordtst[wordlen + 1] = L'\0';
|
|
|
|
const wstring &letters = getHeader().getLetters();
|
|
|
|
for (unsigned int i = 0; i < letters.size(); i++)
|
|
|
|
{
|
|
|
|
unsigned int code = getHeader().getCodeFromChar(letters[i]);
|
2009-06-23 14:41:53 +02:00
|
|
|
params.added_code = code;
|
|
|
|
params.added_display = getHeader().getDisplayStr(code);
|
2008-01-08 14:52:32 +01:00
|
|
|
params.search_letters[code]++;
|
|
|
|
|
2009-06-23 14:41:53 +02:00
|
|
|
searchWordByLen(params, 0, root_edge);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
params.search_letters[code]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************/
|
|
|
|
/****************************************/
|
|
|
|
|
2008-11-22 14:11:48 +01:00
|
|
|
void Dictionary::searchRacc(const wstring &iWord,
|
2009-06-23 14:41:53 +02:00
|
|
|
vector<wdstring> &oWordList,
|
2008-11-22 14:11:48 +01:00
|
|
|
unsigned int iMaxResults) const
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
if (iWord == L"")
|
|
|
|
return;
|
|
|
|
|
2008-03-02 19:45:10 +01:00
|
|
|
// Allocate room for all the results
|
|
|
|
if (iMaxResults)
|
|
|
|
oWordList.reserve(iMaxResults);
|
|
|
|
else
|
|
|
|
oWordList.reserve(DEFAULT_VECT_ALLOC);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2009-06-23 14:41:53 +02:00
|
|
|
// Transform the given word to make it suitable for display
|
2009-07-03 23:40:14 +02:00
|
|
|
wdstring displayWord = convertToDisplay(iWord);
|
|
|
|
// Make it uppercase
|
|
|
|
std::transform(displayWord.begin(), displayWord.end(),
|
|
|
|
displayWord.begin(), towupper);
|
2009-06-23 14:41:53 +02:00
|
|
|
|
2008-03-02 19:45:10 +01:00
|
|
|
// Try to add a letter at the front
|
2008-01-08 14:52:32 +01:00
|
|
|
const wstring &letters = getHeader().getLetters();
|
|
|
|
for (unsigned int i = 0; i <= letters.size(); i++)
|
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
if (searchWord(letters[i] + iWord))
|
|
|
|
{
|
2009-07-03 23:40:14 +02:00
|
|
|
const wdstring &chr = getHeader().getDisplayStr(getHeader().getCodeFromChar(letters[i]));
|
2009-06-23 14:41:53 +02:00
|
|
|
oWordList.push_back(chr + displayWord);
|
|
|
|
}
|
2008-03-02 19:45:10 +01:00
|
|
|
if (iMaxResults && oWordList.size() >= iMaxResults)
|
|
|
|
return;
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
|
2008-03-02 19:45:10 +01:00
|
|
|
// Try to add a letter at the end
|
2008-11-22 14:11:48 +01:00
|
|
|
const DicEdge *edge_seek =
|
|
|
|
seekEdgePtr(iWord.c_str(), getEdgeAt(getRoot()));
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2009-06-23 14:41:53 +02:00
|
|
|
// Point to what the next letter can be
|
2008-11-22 14:11:48 +01:00
|
|
|
const DicEdge *edge = getEdgeAt(edge_seek->ptr);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2008-11-22 14:11:48 +01:00
|
|
|
if (edge != getEdgeAt(0))
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (edge->term)
|
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
oWordList.push_back(displayWord + getHeader().getDisplayStr(edge->chr));
|
2008-03-02 19:45:10 +01:00
|
|
|
if (iMaxResults && oWordList.size() >= iMaxResults)
|
|
|
|
return;
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
} while (!(*edge++).last);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************/
|
|
|
|
/****************************************/
|
|
|
|
|
2009-06-23 14:41:53 +02:00
|
|
|
void Dictionary::searchBenj(const wstring &iWord, vector<wdstring> &oWordList,
|
2008-11-22 14:11:48 +01:00
|
|
|
unsigned int iMaxResults) const
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
if (iWord == L"")
|
|
|
|
return;
|
|
|
|
|
2008-03-02 19:45:10 +01:00
|
|
|
// Allocate room for all the results
|
|
|
|
if (iMaxResults)
|
|
|
|
oWordList.reserve(iMaxResults);
|
|
|
|
else
|
|
|
|
oWordList.reserve(DEFAULT_VECT_ALLOC);
|
|
|
|
|
2009-06-23 14:41:53 +02:00
|
|
|
// Transform the given word to make it suitable for display
|
2009-07-03 23:40:14 +02:00
|
|
|
wdstring displayWord = convertToDisplay(iWord);
|
|
|
|
// Make it uppercase
|
|
|
|
std::transform(displayWord.begin(), displayWord.end(),
|
|
|
|
displayWord.begin(), towupper);
|
2009-06-23 14:41:53 +02:00
|
|
|
|
2008-11-22 14:11:48 +01:00
|
|
|
const DicEdge *edge0, *edge1, *edge2, *edgetst;
|
|
|
|
edge0 = getEdgeAt(getRoot());
|
|
|
|
edge0 = getEdgeAt(edge0->ptr);
|
2008-01-08 14:52:32 +01:00
|
|
|
do
|
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
const wdstring &chr0 = getHeader().getDisplayStr(edge0->chr);
|
2008-11-22 14:11:48 +01:00
|
|
|
edge1 = getEdgeAt(edge0->ptr);
|
2008-01-08 14:52:32 +01:00
|
|
|
do
|
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
const wdstring &chr1 = getHeader().getDisplayStr(edge1->chr);
|
2008-11-22 14:11:48 +01:00
|
|
|
edge2 = getEdgeAt(edge1->ptr);
|
2008-01-08 14:52:32 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
edgetst = seekEdgePtr(iWord.c_str(), edge2);
|
|
|
|
if (edgetst->term)
|
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
const wdstring &chr2 = getHeader().getDisplayStr(edge2->chr);
|
|
|
|
oWordList.push_back(chr0 + chr1 + chr2 + displayWord);
|
2008-03-02 19:45:10 +01:00
|
|
|
if (iMaxResults && oWordList.size() >= iMaxResults)
|
|
|
|
return;
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
} while (!(*edge2++).last);
|
|
|
|
} while (!(*edge1++).last);
|
|
|
|
} while (!(*edge0++).last);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************/
|
|
|
|
/****************************************/
|
|
|
|
|
|
|
|
struct params_regexp_t
|
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
unsigned int minlength;
|
|
|
|
unsigned int maxlength;
|
2008-01-08 14:52:32 +01:00
|
|
|
Automaton *automaton_field;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-06-23 14:41:53 +02:00
|
|
|
void Dictionary::searchRegexpRec(const struct params_regexp_t ¶ms,
|
2008-11-22 14:11:48 +01:00
|
|
|
int state,
|
|
|
|
const DicEdge *edgeptr,
|
2009-06-23 14:41:53 +02:00
|
|
|
vector<wdstring> &oWordList,
|
|
|
|
unsigned int iMaxResults,
|
|
|
|
const wdstring &iCurrWord,
|
|
|
|
unsigned int iNbChars) const
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-03-02 19:45:10 +01:00
|
|
|
if (iMaxResults && oWordList.size() >= iMaxResults)
|
|
|
|
return;
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
int next_state;
|
|
|
|
/* if we have a valid word we store it */
|
2009-06-23 14:41:53 +02:00
|
|
|
if (params.automaton_field->accept(state) && edgeptr->term)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
if (params.minlength <= iNbChars &&
|
|
|
|
params.maxlength >= iNbChars)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
oWordList.push_back(iCurrWord);
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* we now drive the search by exploring the dictionary */
|
2008-11-22 14:11:48 +01:00
|
|
|
const DicEdge *current = getEdgeAt(edgeptr->ptr);
|
2008-01-08 14:52:32 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* the current letter is current->chr */
|
2009-06-23 14:41:53 +02:00
|
|
|
next_state = params.automaton_field->getNextState(state, current->chr);
|
2008-01-08 14:52:32 +01:00
|
|
|
/* 1: the letter appears in the automaton as is */
|
|
|
|
if (next_state)
|
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
searchRegexpRec(params, next_state, current, oWordList, iMaxResults,
|
|
|
|
iCurrWord + getHeader().getDisplayStr(current->chr), iNbChars + 1);
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
} while (!(*current++).last);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-27 17:28:50 +02:00
|
|
|
/**
|
|
|
|
* Initialize the lists of letters with pre-defined lists
|
|
|
|
* 0: all tiles
|
|
|
|
* 1: vowels
|
|
|
|
* 2: consonants
|
|
|
|
* 3: user defined 1
|
|
|
|
* 4: user defined 2
|
|
|
|
* x: lists used during parsing
|
|
|
|
*/
|
|
|
|
static void initLetterLists(const Dictionary &iDic,
|
|
|
|
searchRegExpLists &iList)
|
2008-07-27 15:32:47 +02:00
|
|
|
{
|
|
|
|
memset(&iList, 0, sizeof(iList));
|
|
|
|
// Prepare the space for 5 items
|
|
|
|
iList.symbl.assign(5, 0);
|
2008-07-28 20:37:09 +02:00
|
|
|
iList.letters.assign(5, vector<bool>(DIC_LETTERS + 1, false));
|
2008-07-27 15:32:47 +02:00
|
|
|
|
2008-07-27 17:28:50 +02:00
|
|
|
iList.symbl[0] = RE_ALL_MATCH; // All letters
|
|
|
|
iList.symbl[1] = RE_VOWL_MATCH; // Vowels
|
|
|
|
iList.symbl[2] = RE_CONS_MATCH; // Consonants
|
2008-07-27 15:32:47 +02:00
|
|
|
iList.letters[0][0] = false;
|
|
|
|
iList.letters[1][0] = false;
|
|
|
|
iList.letters[2][0] = false;
|
|
|
|
const wstring &allLetters = iDic.getHeader().getLetters();
|
|
|
|
for (size_t i = 1; i <= allLetters.size(); ++i)
|
|
|
|
{
|
|
|
|
iList.letters[0][i] = true;
|
|
|
|
iList.letters[1][i] = iDic.getHeader().isVowel(i);
|
|
|
|
iList.letters[2][i] = iDic.getHeader().isConsonant(i);
|
|
|
|
}
|
|
|
|
|
2008-07-27 17:28:50 +02:00
|
|
|
iList.symbl[3] = RE_USR1_MATCH; // User defined list 1
|
|
|
|
iList.symbl[4] = RE_USR2_MATCH; // User defined list 2
|
2008-07-27 15:32:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-27 23:20:03 +02:00
|
|
|
bool Dictionary::searchRegExp(const wstring &iRegexp,
|
2009-06-23 14:41:53 +02:00
|
|
|
vector<wdstring> &oWordList,
|
2008-07-27 15:32:47 +02:00
|
|
|
unsigned int iMinLength,
|
|
|
|
unsigned int iMaxLength,
|
2008-07-07 19:29:59 +02:00
|
|
|
unsigned int iMaxResults) const
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-07 19:29:59 +02:00
|
|
|
if (iRegexp == L"")
|
2008-08-27 23:20:03 +02:00
|
|
|
return true;
|
2008-07-07 19:29:59 +02:00
|
|
|
|
2008-03-02 19:45:10 +01:00
|
|
|
// Allocate room for all the results
|
2008-08-27 23:20:03 +02:00
|
|
|
// XXX: is it really a good idea?
|
2008-03-02 19:45:10 +01:00
|
|
|
if (iMaxResults)
|
|
|
|
oWordList.reserve(iMaxResults);
|
|
|
|
else
|
|
|
|
oWordList.reserve(DEFAULT_VECT_ALLOC);
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
// Parsing
|
2008-07-07 19:29:59 +02:00
|
|
|
Node *root = NULL;
|
2008-07-27 17:28:50 +02:00
|
|
|
searchRegExpLists llist;
|
|
|
|
// Initialize the lists of letters
|
|
|
|
initLetterLists(*this, llist);
|
|
|
|
bool parsingOk = parseRegexp(*this, (iRegexp + L"#").c_str(), &root, llist);
|
2008-07-07 19:29:59 +02:00
|
|
|
|
|
|
|
if (!parsingOk)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-07 19:29:59 +02:00
|
|
|
delete root;
|
2008-08-31 13:48:11 +02:00
|
|
|
throw InvalidRegexpException(convertToMb(iRegexp));
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
|
2008-07-13 09:55:47 +02:00
|
|
|
int ptl[REGEXP_MAX+1];
|
2008-07-27 15:32:47 +02:00
|
|
|
uint64_t PS[REGEXP_MAX+1];
|
2008-07-13 09:55:47 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < REGEXP_MAX; i++)
|
|
|
|
{
|
|
|
|
PS[i] = 0;
|
|
|
|
ptl[i] = 0;
|
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
int n = 1;
|
|
|
|
int p = 1;
|
2008-07-07 19:29:59 +02:00
|
|
|
root->traverse(p, n, ptl);
|
2008-01-08 14:52:32 +01:00
|
|
|
PS [0] = p - 1;
|
|
|
|
ptl[0] = p - 1;
|
|
|
|
|
2008-07-07 19:29:59 +02:00
|
|
|
root->nextPos(PS);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2008-07-27 17:28:50 +02:00
|
|
|
Automaton *a = new Automaton(root->getFirstPos(), ptl, PS, llist);
|
2008-01-08 14:52:32 +01:00
|
|
|
if (a)
|
|
|
|
{
|
|
|
|
struct params_regexp_t params;
|
2008-07-27 15:32:47 +02:00
|
|
|
params.minlength = iMinLength;
|
|
|
|
params.maxlength = iMaxLength;
|
2008-01-08 14:52:32 +01:00
|
|
|
params.automaton_field = a;
|
2009-06-23 14:41:53 +02:00
|
|
|
searchRegexpRec(params, a->getInitId(),
|
2008-11-22 14:11:48 +01:00
|
|
|
getEdgeAt(getRoot()), oWordList,
|
|
|
|
iMaxResults ? iMaxResults + 1 : 0);
|
2008-01-08 14:52:32 +01:00
|
|
|
delete a;
|
|
|
|
}
|
2008-07-07 19:29:59 +02:00
|
|
|
delete root;
|
2008-08-27 23:20:03 +02:00
|
|
|
|
|
|
|
// Check whether the maximum number of results was reached
|
|
|
|
if (iMaxResults && oWordList.size() > iMaxResults)
|
|
|
|
{
|
|
|
|
oWordList.pop_back();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return true;
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
|