2009-02-05 14:21:47 +00:00
|
|
|
/* This file is (c) 2008-2009 Konstantin Isakov <ikm@users.berlios.de>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "bgl.hh"
|
|
|
|
#include "btreeidx.hh"
|
|
|
|
#include "bgl_babylon.hh"
|
|
|
|
#include "file.hh"
|
|
|
|
#include "folding.hh"
|
|
|
|
#include "utf8.hh"
|
|
|
|
#include "chunkedstorage.hh"
|
2009-05-06 18:17:13 +00:00
|
|
|
#include "langcoder.hh"
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <list>
|
|
|
|
#include <zlib.h>
|
|
|
|
#include <ctype.h>
|
2009-01-30 01:20:37 +00:00
|
|
|
#include <string.h>
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-29 23:18:26 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <stub_msvc.h>
|
|
|
|
#endif
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
#include <QSemaphore>
|
|
|
|
#include <QThreadPool>
|
|
|
|
#include <QAtomicInt>
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
namespace Bgl {
|
|
|
|
|
|
|
|
using std::map;
|
|
|
|
using std::multimap;
|
|
|
|
using std::set;
|
2009-04-18 17:20:12 +00:00
|
|
|
using gd::wstring;
|
|
|
|
using gd::wchar;
|
2009-01-28 20:55:45 +00:00
|
|
|
using std::list;
|
|
|
|
using std::pair;
|
|
|
|
|
|
|
|
using BtreeIndexing::WordArticleLink;
|
|
|
|
using BtreeIndexing::IndexedWords;
|
2009-04-14 16:35:47 +00:00
|
|
|
using BtreeIndexing::IndexInfo;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
Signature = 0x584c4742, // BGLX on little-endian, XLGB on big-endian
|
2009-05-06 18:17:13 +00:00
|
|
|
CurrentFormatVersion = 14 + BtreeIndexing::FormatVersion
|
2009-01-28 20:55:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct IdxHeader
|
|
|
|
{
|
|
|
|
uint32_t signature; // First comes the signature, BGLX
|
|
|
|
uint32_t formatVersion; // File format version, currently 1.
|
|
|
|
uint32_t parserVersion; // Version of the parser used to parse the BGL file.
|
|
|
|
// If it's lower than the current one, the file is to
|
|
|
|
// be re-parsed.
|
|
|
|
uint32_t foldingVersion; // Version of the folding algorithm used when building
|
|
|
|
// index. If it's different from the current one,
|
|
|
|
// the file is to be rebuilt.
|
|
|
|
uint32_t articleCount; // Total number of articles, for informative purposes only
|
|
|
|
uint32_t wordCount; // Total number of words, for informative purposes only
|
|
|
|
/// Add more fields here, like name, description, author and such.
|
|
|
|
uint32_t chunksOffset; // The offset to chunks' storage
|
2009-04-14 16:35:47 +00:00
|
|
|
uint32_t indexBtreeMaxElements; // Two fields from IndexInfo
|
|
|
|
uint32_t indexRootOffset;
|
2009-01-28 20:55:45 +00:00
|
|
|
uint32_t resourceListOffset; // The offset of the list of resources
|
|
|
|
uint32_t resourcesCount; // Number of resources stored
|
2009-05-06 18:17:13 +00:00
|
|
|
uint32_t langFrom; // Source language
|
|
|
|
uint32_t langTo; // Target language
|
|
|
|
}
|
2009-04-29 23:18:26 +00:00
|
|
|
#ifndef _MSC_VER
|
2009-05-06 18:17:13 +00:00
|
|
|
__attribute__((packed))
|
2009-04-29 23:18:26 +00:00
|
|
|
#endif
|
|
|
|
;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
bool indexIsOldOrBad( string const & indexFile )
|
|
|
|
{
|
|
|
|
File::Class idx( indexFile, "rb" );
|
|
|
|
|
|
|
|
IdxHeader header;
|
|
|
|
|
|
|
|
return idx.readRecords( &header, sizeof( header ), 1 ) != 1 ||
|
|
|
|
header.signature != Signature ||
|
|
|
|
header.formatVersion != CurrentFormatVersion ||
|
|
|
|
header.parserVersion != Babylon::ParserVersion ||
|
|
|
|
header.foldingVersion != Folding::Version;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes the $1$-like postfix
|
|
|
|
string removePostfix( string const & in )
|
|
|
|
{
|
|
|
|
if ( in.size() && in[ in.size() - 1 ] == '$' )
|
|
|
|
{
|
|
|
|
// Find the end of it and cut it, barring any unexpectedness
|
|
|
|
for( long x = in.size() - 2; x >= 0; x-- )
|
|
|
|
{
|
|
|
|
if ( in[ x ] == '$' )
|
|
|
|
return in.substr( 0, x );
|
|
|
|
else
|
|
|
|
if ( !isdigit( in[ x ] ) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2009-02-02 00:56:58 +00:00
|
|
|
// Since the standard isspace() is locale-specific, we need something
|
|
|
|
// that would never mess up our utf8 input. The stock one worked fine under
|
|
|
|
// Linux but was messing up strings under Windows.
|
|
|
|
bool isspace_c( int c )
|
|
|
|
{
|
|
|
|
switch( c )
|
|
|
|
{
|
|
|
|
case ' ':
|
|
|
|
case '\f':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\t':
|
|
|
|
case '\v':
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// Removes any leading or trailing whitespace
|
|
|
|
void trimWs( string & word )
|
|
|
|
{
|
|
|
|
if ( word.size() )
|
|
|
|
{
|
|
|
|
unsigned begin = 0;
|
|
|
|
|
2009-02-02 00:56:58 +00:00
|
|
|
while( begin < word.size() && isspace_c( word[ begin ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
++begin;
|
|
|
|
|
|
|
|
if ( begin == word.size() ) // Consists of ws entirely?
|
|
|
|
word.clear();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned end = word.size();
|
|
|
|
|
|
|
|
// Doesn't consist of ws entirely, so must end with just isspace()
|
|
|
|
// condition.
|
2009-02-02 00:56:58 +00:00
|
|
|
while( isspace_c( word[ end - 1 ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
--end;
|
|
|
|
|
|
|
|
if ( end != word.size() || begin )
|
|
|
|
word = string( word, begin, end - begin );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addEntryToIndex( string & word,
|
|
|
|
uint32_t articleOffset,
|
|
|
|
IndexedWords & indexedWords,
|
2009-04-18 17:20:12 +00:00
|
|
|
vector< wchar > & wcharBuffer )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// Strip any leading or trailing whitespaces
|
|
|
|
trimWs( word );
|
|
|
|
|
2009-04-27 12:23:10 +00:00
|
|
|
// If the word starts with a slash, we drop it. There are quite a lot
|
|
|
|
// of them, and they all seem to be redudant duplicates.
|
|
|
|
|
|
|
|
if ( word.size() && word[ 0 ] == '/' )
|
|
|
|
return;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// Check the input word for a superscript postfix ($1$, $2$ etc), which
|
|
|
|
// signifies different meaning in Bgl files. We emit different meaning
|
|
|
|
// as different articles, but they appear in the index as the same word.
|
|
|
|
|
|
|
|
if ( word.size() && word[ word.size() - 1 ] == '$' )
|
|
|
|
{
|
|
|
|
word = removePostfix( word );
|
|
|
|
trimWs( word );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the word from utf8 to wide chars
|
|
|
|
|
|
|
|
if ( wcharBuffer.size() <= word.size() )
|
|
|
|
wcharBuffer.resize( word.size() + 1 );
|
|
|
|
|
|
|
|
long result = Utf8::decode( word.c_str(), word.size(),
|
|
|
|
&wcharBuffer.front() );
|
|
|
|
|
|
|
|
if ( result < 0 )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Failed to decode utf8 of headword %s, skipping it.\n",
|
|
|
|
word.c_str() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
indexedWords.addWord( wstring( &wcharBuffer.front(), result ), articleOffset );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DEF_EX( exFailedToDecompressArticle, "Failed to decompress article's body", Dictionary::Ex )
|
|
|
|
DEF_EX( exChunkIndexOutOfRange, "Chunk index is out of range", Dictionary::Ex )
|
|
|
|
|
|
|
|
class BglDictionary: public BtreeIndexing::BtreeDictionary
|
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
Mutex idxMutex;
|
2009-01-28 20:55:45 +00:00
|
|
|
File::Class idx;
|
|
|
|
IdxHeader idxHeader;
|
|
|
|
string dictionaryName;
|
|
|
|
ChunkedStorage::Reader chunks;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
BglDictionary( string const & id, string const & indexFile,
|
|
|
|
string const & dictionaryFile );
|
|
|
|
|
|
|
|
virtual string getName() throw()
|
|
|
|
{ return dictionaryName; }
|
|
|
|
|
|
|
|
virtual map< Dictionary::Property, string > getProperties() throw()
|
|
|
|
{ return map< Dictionary::Property, string >(); }
|
|
|
|
|
|
|
|
virtual unsigned long getArticleCount() throw()
|
|
|
|
{ return idxHeader.articleCount; }
|
|
|
|
|
|
|
|
virtual unsigned long getWordCount() throw()
|
|
|
|
{ return idxHeader.wordCount; }
|
|
|
|
|
2009-04-21 19:03:16 +00:00
|
|
|
virtual QIcon getIcon() throw()
|
|
|
|
{ return QIcon(":/icons/icon32_bgl.png"); }
|
|
|
|
|
2009-05-06 18:17:13 +00:00
|
|
|
inline virtual quint32 getLangFrom() const
|
|
|
|
{ return idxHeader.langFrom; }
|
|
|
|
|
|
|
|
inline virtual quint32 getLangTo() const
|
|
|
|
{ return idxHeader.langTo; }
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
virtual sptr< Dictionary::WordSearchRequest > findHeadwordsForSynonym( wstring const & )
|
2009-01-28 20:55:45 +00:00
|
|
|
throw( std::exception );
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
virtual sptr< Dictionary::DataRequest > getArticle( wstring const &,
|
|
|
|
vector< wstring > const & alts )
|
|
|
|
throw( std::exception );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
virtual sptr< Dictionary::DataRequest > getResource( string const & name )
|
|
|
|
throw( std::exception );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
|
|
/// Loads an article with the given offset, filling the given strings.
|
|
|
|
void loadArticle( uint32_t offset, string & headword,
|
|
|
|
string & displayedHeadword, string & articleText );
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
static void replaceCharsetEntities( string & );
|
|
|
|
|
|
|
|
friend class BglHeadwordsRequest;
|
|
|
|
friend class BglArticleRequest;
|
|
|
|
friend class BglResourceRequest;
|
2009-01-28 20:55:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BglDictionary::BglDictionary( string const & id, string const & indexFile,
|
|
|
|
string const & dictionaryFile ):
|
|
|
|
BtreeDictionary( id, vector< string >( 1, dictionaryFile ) ),
|
|
|
|
idx( indexFile, "rb" ),
|
|
|
|
idxHeader( idx.read< IdxHeader >() ),
|
|
|
|
chunks( idx, idxHeader.chunksOffset )
|
|
|
|
{
|
|
|
|
idx.seek( sizeof( idxHeader ) );
|
|
|
|
|
|
|
|
// Read the dictionary's name
|
|
|
|
|
|
|
|
size_t len = idx.read< uint32_t >();
|
|
|
|
|
|
|
|
vector< char > nameBuf( len );
|
|
|
|
|
|
|
|
idx.read( &nameBuf.front(), len );
|
|
|
|
|
|
|
|
dictionaryName = string( &nameBuf.front(), len );
|
|
|
|
|
|
|
|
// Initialize the index
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
openIndex( IndexInfo( idxHeader.indexBtreeMaxElements,
|
|
|
|
idxHeader.indexRootOffset ),
|
|
|
|
idx, idxMutex );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BglDictionary::loadArticle( uint32_t offset, string & headword,
|
|
|
|
string & displayedHeadword,
|
|
|
|
string & articleText )
|
|
|
|
{
|
|
|
|
vector< char > chunk;
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
Mutex::Lock _( idxMutex );
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
char * articleData = chunks.getBlock( offset, chunk );
|
|
|
|
|
|
|
|
headword = articleData;
|
|
|
|
|
|
|
|
displayedHeadword = articleData + headword.size() + 1;
|
|
|
|
|
|
|
|
articleText =
|
2009-04-21 19:03:16 +00:00
|
|
|
string( articleData + headword.size() +
|
2009-01-28 20:55:45 +00:00
|
|
|
displayedHeadword.size() + 2 );
|
|
|
|
}
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
/// BglDictionary::findHeadwordsForSynonym()
|
|
|
|
|
|
|
|
class BglHeadwordsRequest;
|
|
|
|
|
|
|
|
class BglHeadwordsRequestRunnable: public QRunnable
|
|
|
|
{
|
|
|
|
BglHeadwordsRequest & r;
|
|
|
|
QSemaphore & hasExited;
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
BglHeadwordsRequestRunnable( BglHeadwordsRequest & r_,
|
|
|
|
QSemaphore & hasExited_ ): r( r_ ),
|
|
|
|
hasExited( hasExited_ )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~BglHeadwordsRequestRunnable()
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-16 11:33:12 +00:00
|
|
|
hasExited.release();
|
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
virtual void run();
|
|
|
|
};
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
class BglHeadwordsRequest: public Dictionary::WordSearchRequest
|
|
|
|
{
|
|
|
|
friend class BglHeadwordsRequestRunnable;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
wstring str;
|
|
|
|
BglDictionary & dict;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
QAtomicInt isCancelled;
|
|
|
|
QSemaphore hasExited;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
public:
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
BglHeadwordsRequest( wstring const & word_,
|
|
|
|
BglDictionary & dict_ ):
|
|
|
|
str( word_ ), dict( dict_ )
|
|
|
|
{
|
|
|
|
QThreadPool::globalInstance()->start(
|
|
|
|
new BglHeadwordsRequestRunnable( *this, hasExited ) );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
void run(); // Run from another thread by BglHeadwordsRequestRunnable
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
virtual void cancel()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
~BglHeadwordsRequest()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
hasExited.acquire();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-04-16 11:33:12 +00:00
|
|
|
};
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
void BglHeadwordsRequestRunnable::run()
|
|
|
|
{
|
|
|
|
r.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BglHeadwordsRequest::run()
|
|
|
|
{
|
|
|
|
if ( isCancelled )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-16 11:33:12 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::WordSearchRequestInstant > result =
|
|
|
|
new Dictionary::WordSearchRequestInstant;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
vector< WordArticleLink > chain = dict.findArticles( str );
|
|
|
|
|
|
|
|
wstring caseFolded = Folding::applySimpleCaseOnly( str );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < chain.size(); ++x )
|
|
|
|
{
|
|
|
|
if ( isCancelled )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-16 11:33:12 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string headword, displayedHeadword, articleText;
|
|
|
|
|
|
|
|
dict.loadArticle( chain[ x ].articleOffset,
|
|
|
|
headword, displayedHeadword, articleText );
|
|
|
|
|
|
|
|
wstring headwordDecoded = Utf8::decode( removePostfix( headword ) );
|
|
|
|
|
|
|
|
if ( caseFolded != Folding::applySimpleCaseOnly( headwordDecoded ) )
|
|
|
|
{
|
|
|
|
// The headword seems to differ from the input word, which makes the
|
|
|
|
// input word its synonym.
|
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
matches.push_back( headwordDecoded );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::WordSearchRequest >
|
|
|
|
BglDictionary::findHeadwordsForSynonym( wstring const & word )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
return new BglHeadwordsRequest( word, *this );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts a $1$-like postfix to a <sup>1</sup> one
|
|
|
|
string postfixToSuperscript( string const & in )
|
|
|
|
{
|
|
|
|
if ( !in.size() || in[ in.size() - 1 ] != '$' )
|
|
|
|
return in;
|
|
|
|
|
|
|
|
for( long x = in.size() - 2; x >= 0; x-- )
|
|
|
|
{
|
|
|
|
if ( in[ x ] == '$' )
|
|
|
|
{
|
|
|
|
if ( in.size() - x - 2 > 2 )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-16 11:33:12 +00:00
|
|
|
// Large postfixes seem like something we wouldn't want to show --
|
|
|
|
// some dictionaries seem to have each word numbered using the
|
|
|
|
// postfix.
|
|
|
|
return in.substr( 0, x );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
else
|
2009-04-16 11:33:12 +00:00
|
|
|
return in.substr( 0, x ) + "<sup>" + in.substr( x + 1, in.size() - x - 2 ) + "</sup>";
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-04-16 11:33:12 +00:00
|
|
|
else
|
|
|
|
if ( !isdigit( in[ x ] ) )
|
|
|
|
break;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// BglDictionary::getArticle()
|
|
|
|
|
|
|
|
class BglArticleRequest;
|
|
|
|
|
|
|
|
class BglArticleRequestRunnable: public QRunnable
|
|
|
|
{
|
|
|
|
BglArticleRequest & r;
|
|
|
|
QSemaphore & hasExited;
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
BglArticleRequestRunnable( BglArticleRequest & r_,
|
|
|
|
QSemaphore & hasExited_ ): r( r_ ),
|
|
|
|
hasExited( hasExited_ )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~BglArticleRequestRunnable()
|
|
|
|
{
|
|
|
|
hasExited.release();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
virtual void run();
|
|
|
|
};
|
|
|
|
|
|
|
|
class BglArticleRequest: public Dictionary::DataRequest
|
|
|
|
{
|
|
|
|
friend class BglArticleRequestRunnable;
|
|
|
|
|
|
|
|
wstring word;
|
|
|
|
vector< wstring > alts;
|
|
|
|
BglDictionary & dict;
|
|
|
|
|
|
|
|
QAtomicInt isCancelled;
|
|
|
|
QSemaphore hasExited;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
BglArticleRequest( wstring const & word_,
|
|
|
|
vector< wstring > const & alts_,
|
|
|
|
BglDictionary & dict_ ):
|
|
|
|
word( word_ ), alts( alts_ ), dict( dict_ )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-16 11:33:12 +00:00
|
|
|
QThreadPool::globalInstance()->start(
|
|
|
|
new BglArticleRequestRunnable( *this, hasExited ) );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
void run(); // Run from another thread by BglArticleRequestRunnable
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
virtual void cancel()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
~BglArticleRequest()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
hasExited.acquire();
|
|
|
|
}
|
|
|
|
};
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
void BglArticleRequestRunnable::run()
|
|
|
|
{
|
|
|
|
r.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BglArticleRequest::run()
|
|
|
|
{
|
|
|
|
if ( isCancelled )
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
vector< WordArticleLink > chain = dict.findArticles( word );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
for( unsigned x = 0; x < alts.size(); ++x )
|
|
|
|
{
|
|
|
|
/// Make an additional query for each alt
|
|
|
|
|
|
|
|
vector< WordArticleLink > altChain = dict.findArticles( alts[ x ] );
|
|
|
|
|
|
|
|
chain.insert( chain.end(), altChain.begin(), altChain.end() );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
multimap< wstring, pair< string, string > > mainArticles, alternateArticles;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
set< uint32_t > articlesIncluded; // Some synonims make it that the articles
|
|
|
|
// appear several times. We combat this
|
|
|
|
// by only allowing them to appear once.
|
|
|
|
|
|
|
|
wstring wordCaseFolded = Folding::applySimpleCaseOnly( word );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < chain.size(); ++x )
|
|
|
|
{
|
|
|
|
if ( isCancelled )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-16 11:33:12 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
if ( articlesIncluded.find( chain[ x ].articleOffset ) != articlesIncluded.end() )
|
|
|
|
continue; // We already have this article in the body.
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
// Now grab that article
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
string headword, displayedHeadword, articleText;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
dict.loadArticle( chain[ x ].articleOffset,
|
|
|
|
headword, displayedHeadword, articleText );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
// Ok. Now, does it go to main articles, or to alternate ones? We list
|
|
|
|
// main ones first, and alternates after.
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
// We do the case-folded and postfix-less comparison here.
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
wstring headwordStripped =
|
|
|
|
Folding::applySimpleCaseOnly( Utf8::decode( removePostfix( headword ) ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-21 19:03:16 +00:00
|
|
|
multimap< wstring, pair< string, string > > & mapToUse =
|
2009-04-16 11:33:12 +00:00
|
|
|
( wordCaseFolded == headwordStripped ) ?
|
|
|
|
mainArticles : alternateArticles;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
mapToUse.insert( pair< wstring, pair< string, string > >(
|
|
|
|
Folding::applySimpleCaseOnly( Utf8::decode( headword ) ),
|
2009-04-21 19:03:16 +00:00
|
|
|
pair< string, string >(
|
2009-04-16 11:33:12 +00:00
|
|
|
displayedHeadword.size() ? displayedHeadword : headword,
|
|
|
|
articleText ) ) );
|
|
|
|
|
|
|
|
articlesIncluded.insert( chain[ x ].articleOffset );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
if ( mainArticles.empty() && alternateArticles.empty() )
|
|
|
|
{
|
|
|
|
// No such word
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
string result;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
multimap< wstring, pair< string, string > >::const_iterator i;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
string cleaner = "</font>""</font>""</font>""</font>""</font>""</font>"
|
|
|
|
"</font>""</font>""</font>""</font>""</font>""</font>"
|
|
|
|
"</b></b></b></b></b></b></b></b>"
|
|
|
|
"</i></i></i></i></i></i></i></i>";
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
for( i = mainArticles.begin(); i != mainArticles.end(); ++i )
|
|
|
|
{
|
|
|
|
result += "<h3>";
|
|
|
|
result += postfixToSuperscript( i->second.first );
|
|
|
|
result += "</h3>";
|
|
|
|
result += i->second.second;
|
|
|
|
result += cleaner;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
for( i = alternateArticles.begin(); i != alternateArticles.end(); ++i )
|
|
|
|
{
|
|
|
|
result += "<h3>";
|
|
|
|
result += postfixToSuperscript( i->second.first );
|
|
|
|
result += "</h3>";
|
|
|
|
result += i->second.second;
|
|
|
|
result += cleaner;
|
|
|
|
}
|
|
|
|
// Do some cleanups in the text
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
BglDictionary::replaceCharsetEntities( result );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
Mutex::Lock _( dataMutex );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
data.resize( result.size() );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
memcpy( &data.front(), result.data(), result.size() );
|
|
|
|
|
|
|
|
hasAnyData = true;
|
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > BglDictionary::getArticle( wstring const & word,
|
|
|
|
vector< wstring > const & alts )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
return new BglArticleRequest( word, alts, *this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//// BglDictionary::getResource()
|
|
|
|
|
|
|
|
class BglResourceRequest;
|
|
|
|
|
|
|
|
class BglResourceRequestRunnable: public QRunnable
|
|
|
|
{
|
|
|
|
BglResourceRequest & r;
|
|
|
|
QSemaphore & hasExited;
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
BglResourceRequestRunnable( BglResourceRequest & r_,
|
|
|
|
QSemaphore & hasExited_ ): r( r_ ),
|
|
|
|
hasExited( hasExited_ )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~BglResourceRequestRunnable()
|
|
|
|
{
|
|
|
|
hasExited.release();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
virtual void run();
|
|
|
|
};
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
class BglResourceRequest: public Dictionary::DataRequest
|
|
|
|
{
|
|
|
|
friend class BglResourceRequestRunnable;
|
|
|
|
|
|
|
|
Mutex & idxMutex;
|
|
|
|
File::Class & idx;
|
|
|
|
uint32_t resourceListOffset, resourcesCount;
|
|
|
|
string name;
|
|
|
|
|
|
|
|
QAtomicInt isCancelled;
|
|
|
|
QSemaphore hasExited;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
BglResourceRequest( Mutex & idxMutex_,
|
|
|
|
File::Class & idx_,
|
|
|
|
uint32_t resourceListOffset_,
|
|
|
|
uint32_t resourcesCount_,
|
|
|
|
string const & name_ ):
|
|
|
|
idxMutex( idxMutex_ ),
|
|
|
|
idx( idx_ ),
|
|
|
|
resourceListOffset( resourceListOffset_ ),
|
|
|
|
resourcesCount( resourcesCount_ ),
|
|
|
|
name( name_ )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-16 11:33:12 +00:00
|
|
|
QThreadPool::globalInstance()->start(
|
|
|
|
new BglResourceRequestRunnable( *this, hasExited ) );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
void run(); // Run from another thread by BglResourceRequestRunnable
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
virtual void cancel()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
~BglResourceRequest()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
hasExited.acquire();
|
|
|
|
}
|
|
|
|
};
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
void BglResourceRequestRunnable::run()
|
|
|
|
{
|
|
|
|
r.run();
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
void BglResourceRequest::run()
|
|
|
|
{
|
|
|
|
if ( isCancelled )
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
string nameLowercased = name;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
for( string::iterator i = nameLowercased.begin(); i != nameLowercased.end();
|
|
|
|
++i )
|
|
|
|
*i = tolower( *i );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
Mutex::Lock _( idxMutex );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
idx.seek( resourceListOffset );
|
|
|
|
|
|
|
|
for( size_t count = resourcesCount; count--; )
|
|
|
|
{
|
|
|
|
if ( isCancelled )
|
|
|
|
break;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
vector< char > nameData( idx.read< uint32_t >() );
|
|
|
|
idx.read( &nameData.front(), nameData.size() );
|
|
|
|
|
|
|
|
for( size_t x = nameData.size(); x--; )
|
|
|
|
nameData[ x ] = tolower( nameData[ x ] );
|
|
|
|
|
|
|
|
uint32_t offset = idx.read< uint32_t >();
|
|
|
|
|
|
|
|
if ( string( &nameData.front(), nameData.size() ) == nameLowercased )
|
|
|
|
{
|
|
|
|
// We have a match.
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
idx.seek( offset );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
Mutex::Lock _( dataMutex );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
data.resize( idx.read< uint32_t >() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
vector< unsigned char > compressedData( idx.read< uint32_t >() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
idx.read( &compressedData.front(), compressedData.size() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
unsigned long decompressedLength = data.size();
|
|
|
|
|
|
|
|
if ( uncompress( (unsigned char *) &data.front(),
|
|
|
|
&decompressedLength,
|
|
|
|
&compressedData.front(),
|
|
|
|
compressedData.size() ) != Z_OK ||
|
|
|
|
decompressedLength != data.size() )
|
|
|
|
{
|
|
|
|
printf( "Failed to decompress resource %s, ignoring it.\n",
|
|
|
|
name.c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-04-16 11:33:12 +00:00
|
|
|
else
|
|
|
|
hasAnyData = true;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > BglDictionary::getResource( string const & name )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
return new BglResourceRequest( idxMutex, idx, idxHeader.resourceListOffset,
|
|
|
|
idxHeader.resourcesCount, name );
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
/// Replaces <CHARSET c="t">1234;</CHARSET> occurences with ሴ
|
|
|
|
void BglDictionary::replaceCharsetEntities( string & text )
|
|
|
|
{
|
|
|
|
string lowercased = text;
|
|
|
|
|
|
|
|
// Make a lowercased version of text, used for searching only. Only touch
|
|
|
|
// symbols < 0x80 to avoid any weird results.
|
|
|
|
for( unsigned x = lowercased.size(); x--; )
|
|
|
|
if ( (unsigned char )lowercased[ x ] < 0x80 )
|
|
|
|
lowercased[ x ] = tolower( lowercased[ x ] );
|
|
|
|
|
|
|
|
size_t prevPos = 0;
|
|
|
|
|
|
|
|
for( ;; )
|
|
|
|
{
|
|
|
|
size_t pos = lowercased.find( "<charset c=\"t\">", prevPos );
|
|
|
|
|
|
|
|
if ( pos == string::npos )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ( lowercased.size() - pos < 30 )
|
|
|
|
{
|
|
|
|
// This is not right, the string is too short, leave it alone
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
prevPos = pos + 1;
|
|
|
|
|
|
|
|
if ( lowercased.substr( pos + 15 + 4, 11 ) != ";</charset>" )
|
|
|
|
{
|
|
|
|
// The ending doesn't match
|
|
|
|
printf( "!!!!!!ending mismatch\n" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if digits are all hex
|
|
|
|
|
|
|
|
if ( !isxdigit( lowercased[ pos + 15 ] ) ||
|
|
|
|
!isxdigit( lowercased[ pos + 16 ] ) ||
|
|
|
|
!isxdigit( lowercased[ pos + 17 ] ) ||
|
|
|
|
!isxdigit( lowercased[ pos + 18 ] ) )
|
|
|
|
{
|
|
|
|
printf( "!!!!!!!!not hex digits\n" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, replace now.
|
|
|
|
|
|
|
|
lowercased.replace( pos, 15, "&#x" );
|
|
|
|
lowercased.erase( pos + 8, 10 );
|
|
|
|
|
|
|
|
text.replace( pos, 15, "&#x" );
|
|
|
|
text.erase( pos + 8, 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
prevPos = 0;
|
|
|
|
|
|
|
|
// Copy-pasted version for <charset c=t>. This should all be replaced
|
|
|
|
// by regexps.
|
|
|
|
for( ;; )
|
|
|
|
{
|
|
|
|
size_t pos = lowercased.find( "<charset c=t>", prevPos );
|
|
|
|
|
|
|
|
if ( pos == string::npos )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ( lowercased.size() - pos < 28 )
|
|
|
|
{
|
|
|
|
// This is not right, the string is too short, leave it alone
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
prevPos = pos + 1;
|
|
|
|
|
|
|
|
if ( lowercased.substr( pos + 13 + 4, 11 ) != ";</charset>" )
|
|
|
|
{
|
|
|
|
// The ending doesn't match
|
|
|
|
printf( "!!!!!!ending mismatch\n" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if digits are all hex
|
|
|
|
|
|
|
|
if ( !isxdigit( lowercased[ pos + 13 ] ) ||
|
|
|
|
!isxdigit( lowercased[ pos + 14 ] ) ||
|
|
|
|
!isxdigit( lowercased[ pos + 15 ] ) ||
|
|
|
|
!isxdigit( lowercased[ pos + 16 ] ) )
|
|
|
|
{
|
|
|
|
printf( "!!!!!!!!not hex digits\n" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, replace now.
|
|
|
|
|
|
|
|
lowercased.replace( pos, 13, "&#x" );
|
|
|
|
lowercased.erase( pos + 8, 10 );
|
|
|
|
|
|
|
|
text.replace( pos, 13, "&#x" );
|
|
|
|
text.erase( pos + 8, 10 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ResourceHandler: public Babylon::ResourceHandler
|
|
|
|
{
|
|
|
|
File::Class & idxFile;
|
|
|
|
list< pair< string, uint32_t > > resources;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
ResourceHandler( File::Class & idxFile_ ): idxFile( idxFile_ )
|
|
|
|
{}
|
|
|
|
|
|
|
|
list< pair< string, uint32_t > > const & getResources() const
|
|
|
|
{ return resources; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void handleBabylonResource( string const & filename,
|
|
|
|
char const * data, size_t size );
|
|
|
|
};
|
|
|
|
|
|
|
|
void ResourceHandler::handleBabylonResource( string const & filename,
|
|
|
|
char const * data, size_t size )
|
|
|
|
{
|
|
|
|
//printf( "Handling resource file %s (%u bytes)\n", filename.c_str(), size );
|
|
|
|
|
|
|
|
vector< unsigned char > compressedData( compressBound( size ) );
|
|
|
|
|
|
|
|
unsigned long compressedSize = compressedData.size();
|
|
|
|
|
|
|
|
if ( compress( &compressedData.front(), &compressedSize,
|
|
|
|
(unsigned char const *) data, size ) != Z_OK )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Failed to compress the body of resource %s, dropping it.\n",
|
|
|
|
filename.c_str() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resources.push_back( pair< string, uint32_t >( filename, idxFile.tell() ) );
|
|
|
|
|
|
|
|
idxFile.write< uint32_t >( size );
|
|
|
|
idxFile.write< uint32_t >( compressedSize );
|
|
|
|
idxFile.write( &compressedData.front(), compressedSize );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
vector< sptr< Dictionary::Class > > makeDictionaries(
|
|
|
|
vector< string > const & fileNames,
|
|
|
|
string const & indicesDir,
|
|
|
|
Dictionary::Initializing & initializing )
|
2009-01-28 20:55:45 +00:00
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > dictionaries;
|
|
|
|
|
|
|
|
for( vector< string >::const_iterator i = fileNames.begin(); i != fileNames.end();
|
|
|
|
++i )
|
|
|
|
{
|
|
|
|
// Skip files with the extensions different to .bgl to speed up the
|
|
|
|
// scanning
|
|
|
|
if ( i->size() < 4 ||
|
|
|
|
strcasecmp( i->c_str() + ( i->size() - 4 ), ".bgl" ) != 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Got the file -- check if we need to rebuid the index
|
|
|
|
|
|
|
|
vector< string > dictFiles( 1, *i );
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
string dictId = Dictionary::makeDictionaryId( dictFiles );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
string indexFile = indicesDir + dictId;
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( Dictionary::needToRebuildIndex( dictFiles, indexFile ) ||
|
|
|
|
indexIsOldOrBad( indexFile ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// Building the index
|
|
|
|
|
2009-04-14 13:25:16 +00:00
|
|
|
Babylon b( *i );
|
|
|
|
|
|
|
|
if ( !b.open() )
|
|
|
|
continue;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
std::string sourceCharset, targetCharset;
|
|
|
|
|
|
|
|
if ( !b.read( sourceCharset, targetCharset ) )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Failed to start reading from %s, skipping it\n", i->c_str() );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
initializing.indexingDictionary( b.title() );
|
|
|
|
|
|
|
|
File::Class idx( indexFile, "wb" );
|
|
|
|
|
|
|
|
IdxHeader idxHeader;
|
|
|
|
|
|
|
|
memset( &idxHeader, 0, sizeof( idxHeader ) );
|
|
|
|
|
|
|
|
// We write a dummy header first. At the end of the process the header
|
|
|
|
// will be rewritten with the right values.
|
|
|
|
|
|
|
|
idx.write( idxHeader );
|
|
|
|
|
|
|
|
idx.write< uint32_t >( b.title().size() );
|
|
|
|
idx.write( b.title().data(), b.title().size() );
|
|
|
|
|
|
|
|
// This is our index data that we accumulate during the loading process.
|
|
|
|
// For each new word encountered, we emit the article's body to the file
|
|
|
|
// immediately, inserting the word itself and its offset in this map.
|
|
|
|
// This map maps folded words to the original words and the corresponding
|
|
|
|
// articles' offsets.
|
|
|
|
IndexedWords indexedWords;
|
|
|
|
|
|
|
|
// We use this buffer to decode utf8 into it.
|
2009-04-18 17:20:12 +00:00
|
|
|
vector< wchar > wcharBuffer;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
ChunkedStorage::Writer chunks( idx );
|
|
|
|
|
|
|
|
uint32_t articleCount = 0, wordCount = 0;
|
|
|
|
|
|
|
|
ResourceHandler resourceHandler( idx );
|
|
|
|
|
|
|
|
b.setResourcePrefix( string( "bres://" ) + dictId + "/" );
|
|
|
|
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
bgl_entry e = b.readEntry( &resourceHandler );
|
|
|
|
|
|
|
|
if ( e.headword.empty() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Save the article's body itself first
|
|
|
|
|
|
|
|
uint32_t articleAddress = chunks.startNewBlock();
|
|
|
|
|
|
|
|
chunks.addToBlock( e.headword.c_str(), e.headword.size() + 1 );
|
|
|
|
chunks.addToBlock( e.displayedHeadword.c_str(), e.displayedHeadword.size() + 1 );
|
|
|
|
chunks.addToBlock( e.definition.c_str(), e.definition.size() + 1 );
|
|
|
|
|
|
|
|
// Add entries to the index
|
|
|
|
|
|
|
|
addEntryToIndex( e.headword, articleAddress, indexedWords, wcharBuffer );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < e.alternates.size(); ++x )
|
|
|
|
addEntryToIndex( e.alternates[ x ], articleAddress, indexedWords, wcharBuffer );
|
|
|
|
|
|
|
|
wordCount += 1 + e.alternates.size();
|
|
|
|
++articleCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish with the chunks
|
|
|
|
|
|
|
|
idxHeader.chunksOffset = chunks.finish();
|
|
|
|
|
|
|
|
printf( "Writing index...\n" );
|
|
|
|
|
|
|
|
// Good. Now build the index
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
IndexInfo idxInfo = BtreeIndexing::buildIndex( indexedWords, idx );
|
|
|
|
|
|
|
|
idxHeader.indexBtreeMaxElements = idxInfo.btreeMaxElements;
|
|
|
|
idxHeader.indexRootOffset = idxInfo.rootOffset;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
// Save the resource's list.
|
|
|
|
|
|
|
|
idxHeader.resourceListOffset = idx.tell();
|
|
|
|
idxHeader.resourcesCount = resourceHandler.getResources().size();
|
|
|
|
|
2009-05-06 18:17:13 +00:00
|
|
|
for( list< pair< string, uint32_t > >::const_iterator j =
|
2009-01-28 20:55:45 +00:00
|
|
|
resourceHandler.getResources().begin();
|
2009-05-06 18:17:13 +00:00
|
|
|
j != resourceHandler.getResources().end(); ++j )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-05-06 18:17:13 +00:00
|
|
|
idx.write< uint32_t >( j->first.size() );
|
|
|
|
idx.write( j->first.data(), j->first.size() );
|
|
|
|
idx.write< uint32_t >( j->second );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// That concludes it. Update the header.
|
|
|
|
|
|
|
|
idxHeader.signature = Signature;
|
|
|
|
idxHeader.formatVersion = CurrentFormatVersion;
|
|
|
|
idxHeader.parserVersion = Babylon::ParserVersion;
|
|
|
|
idxHeader.foldingVersion = Folding::Version;
|
|
|
|
idxHeader.articleCount = articleCount;
|
|
|
|
idxHeader.wordCount = wordCount;
|
|
|
|
|
2009-05-06 18:17:13 +00:00
|
|
|
// read languages
|
|
|
|
QPair<quint32,quint32> langs =
|
|
|
|
LangCoder::findIdsForFilename( QString::fromStdString( *i ) );
|
|
|
|
|
|
|
|
// if no languages found, try dictionary's name
|
|
|
|
if ( langs.first == 0 || langs.second == 0 )
|
|
|
|
{
|
|
|
|
langs =
|
|
|
|
LangCoder::findIdsForFilename( QString::fromStdString( b.title() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
idxHeader.langFrom = langs.first;
|
|
|
|
idxHeader.langTo = langs.second;
|
|
|
|
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
idx.rewind();
|
|
|
|
|
|
|
|
idx.write( &idxHeader, sizeof( idxHeader ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
dictionaries.push_back( new BglDictionary( dictId,
|
|
|
|
indexFile,
|
|
|
|
*i ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return dictionaries;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|