mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-24 00:14:06 +00:00
Again auto groups creation:
1) Parse dictionary name for languages pair if it not defined 2) Place morphology dictionaries into corresponding groups
This commit is contained in:
parent
b10becde3c
commit
ca52fa0eee
|
@ -16,6 +16,7 @@
|
|||
#include <QIcon>
|
||||
#include <QMap>
|
||||
#include <QVector>
|
||||
#include <QFileInfo>
|
||||
|
||||
using std::vector;
|
||||
|
||||
|
@ -597,20 +598,48 @@ void DictGroupsWidget::addAutoGroups()
|
|||
return;
|
||||
|
||||
QMap< QString, QVector< sptr<Dictionary::Class> > > dictMap;
|
||||
QMap< QString, QVector< sptr<Dictionary::Class> > > morphoMap;
|
||||
|
||||
// Put active dictionaries into lists
|
||||
|
||||
for ( unsigned i = 0; i < activeDicts->size(); i++ )
|
||||
{
|
||||
sptr<Dictionary::Class> dict = activeDicts->at( i );
|
||||
|
||||
QString lfrom = LangCoder::intToCode2( dict->getLangFrom() );
|
||||
QString lto = LangCoder::intToCode2( dict->getLangTo() );
|
||||
QString name("Unassigned");
|
||||
if ( !lfrom.isEmpty() && !lto.isEmpty() )
|
||||
int idFrom = dict->getLangFrom();
|
||||
int idTo = dict->getLangTo();
|
||||
if( idFrom == 0)
|
||||
{
|
||||
// Attempt to find language pair in dictionary name
|
||||
|
||||
QPair<quint32,quint32> ids = LangCoder::findIdsForName( QString::fromUtf8( dict->getName().c_str() ) );
|
||||
idFrom = ids.first;
|
||||
idTo = ids.second;
|
||||
}
|
||||
|
||||
QString name("Unassigned");
|
||||
if ( idFrom != 0 && idTo != 0 )
|
||||
{
|
||||
QString lfrom = LangCoder::intToCode2( idFrom );
|
||||
QString lto = LangCoder::intToCode2( idTo );
|
||||
lfrom[ 0 ] = lfrom[ 0 ].toTitleCase();
|
||||
lto[ 0 ] = lto[ 0 ].toTitleCase();
|
||||
name = lfrom + " - " + lto;
|
||||
}
|
||||
else if( !dict->getDictionaryFilenames().empty() )
|
||||
{
|
||||
// Handle special case - morphology dictionaries
|
||||
|
||||
QString fileName = QFileInfo( FsEncoding::decode( dict->getDictionaryFilenames()[ 0 ].c_str() ) ).fileName();
|
||||
if( fileName.endsWith( ".aff", Qt::CaseInsensitive ) )
|
||||
{
|
||||
QString code = fileName.left( 2 ).toLower();
|
||||
QVector<sptr<Dictionary::Class> > vd = morphoMap[ code ];
|
||||
vd.append( dict );
|
||||
morphoMap[ code ] = vd;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
QVector<sptr<Dictionary::Class> > vd = dictMap[ name ];
|
||||
vd.append( dict );
|
||||
|
@ -618,6 +647,23 @@ void DictGroupsWidget::addAutoGroups()
|
|||
}
|
||||
|
||||
QStringList groupList = dictMap.uniqueKeys();
|
||||
QStringList morphoList = morphoMap.uniqueKeys();
|
||||
|
||||
// Insert morphology dictionaries into corresponding lists
|
||||
|
||||
for( QStringList::ConstIterator ln = morphoList.begin(); ln != morphoList.end(); ++ln )
|
||||
{
|
||||
for( QStringList::ConstIterator gr = groupList.begin(); gr != groupList.end(); ++gr )
|
||||
if( ln->compare( gr->left( 2 ), Qt::CaseInsensitive ) == 0 )
|
||||
{
|
||||
QVector<sptr<Dictionary::Class> > vdg = dictMap[ *gr ];
|
||||
vdg += morphoMap[ *ln ];
|
||||
dictMap[ *gr ] = vdg;
|
||||
}
|
||||
}
|
||||
|
||||
// Make groups
|
||||
|
||||
for( QStringList::ConstIterator gr = groupList.begin(); gr != groupList.end(); ++gr )
|
||||
{
|
||||
int n;
|
||||
|
|
16
langcoder.cc
16
langcoder.cc
|
@ -133,19 +133,14 @@ quint32 LangCoder::guessId( const QString & lang )
|
|||
return code2toInt( lstr.left(2).toAscii().data() );
|
||||
}
|
||||
|
||||
QPair<quint32,quint32> LangCoder::findIdsForFilename( QString const & name )
|
||||
QPair<quint32,quint32> LangCoder::findIdsForName( QString const & name )
|
||||
{
|
||||
QString nameFolded = "|" + QFileInfo( name ).fileName().toCaseFolded() + "|";
|
||||
|
||||
// qDebug() << nameFolded;
|
||||
|
||||
QString nameFolded = "|" + name.toCaseFolded() + "|";
|
||||
QRegExp reg( "[^a-z]([a-z]{2,3})-([a-z]{2,3})[^a-z]" ); reg.setMinimal(true);
|
||||
int off = 0;
|
||||
|
||||
while ( reg.indexIn( nameFolded, off ) >= 0 )
|
||||
{
|
||||
// qDebug() << reg.cap(1);
|
||||
// qDebug() << reg.cap(2);
|
||||
|
||||
quint32 from = guessId( reg.cap(1) );
|
||||
quint32 to = guessId( reg.cap(2) );
|
||||
if (from && to)
|
||||
|
@ -157,6 +152,11 @@ QPair<quint32,quint32> LangCoder::findIdsForFilename( QString const & name )
|
|||
return QPair<quint32,quint32>(0, 0);
|
||||
}
|
||||
|
||||
QPair<quint32,quint32> LangCoder::findIdsForFilename( QString const & name )
|
||||
{
|
||||
return findIdsForName( QFileInfo( name ).fileName() );
|
||||
}
|
||||
|
||||
/*
|
||||
LangStruct& LangCoder::CodeToLangStruct(const QString &code)
|
||||
{
|
||||
|
|
|
@ -235,6 +235,7 @@ public:
|
|||
|
||||
static quint32 findIdForLanguageCode3( const char * );
|
||||
|
||||
static QPair<quint32,quint32> findIdsForName( QString const & );
|
||||
static QPair<quint32,quint32> findIdsForFilename( QString const & );
|
||||
|
||||
static quint32 guessId( const QString & lang );
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include "language.hh"
|
||||
#include "fsencoding.hh"
|
||||
#include <algorithm>
|
||||
#include <QPair>
|
||||
|
||||
using std::vector;
|
||||
using std::sort;
|
||||
|
@ -29,8 +30,26 @@ bool dictNameLessThan( sptr< Dictionary::Class > const & dict1,
|
|||
bool dictLessThan( sptr< Dictionary::Class > const & dict1,
|
||||
sptr< Dictionary::Class > const & dict2 )
|
||||
{
|
||||
QString str1 = LangCoder::decode( dict1->getLangFrom() );
|
||||
QString str2 = LangCoder::decode( dict2->getLangFrom() );
|
||||
int idFrom1 = dict1->getLangFrom();
|
||||
int idTo1 = dict1->getLangTo();
|
||||
if( idFrom1 == 0)
|
||||
{
|
||||
QPair<quint32,quint32> ids = LangCoder::findIdsForName( QString::fromUtf8( dict1->getName().c_str() ) );
|
||||
idFrom1 = ids.first;
|
||||
idTo1 = ids.second;
|
||||
}
|
||||
|
||||
int idFrom2 = dict2->getLangFrom();
|
||||
int idTo2 = dict2->getLangTo();
|
||||
if( idFrom2 == 0)
|
||||
{
|
||||
QPair<quint32,quint32> ids = LangCoder::findIdsForName( QString::fromUtf8( dict2->getName().c_str() ) );
|
||||
idFrom2 = ids.first;
|
||||
idTo2 = ids.second;
|
||||
}
|
||||
|
||||
QString str1 = LangCoder::decode( idFrom1 );
|
||||
QString str2 = LangCoder::decode( idFrom2 );
|
||||
if( str1.isEmpty() && !str2.isEmpty() )
|
||||
return false;
|
||||
if( !str1.isEmpty() && str2.isEmpty() )
|
||||
|
@ -39,8 +58,8 @@ bool dictLessThan( sptr< Dictionary::Class > const & dict1,
|
|||
if( res )
|
||||
return res < 0;
|
||||
|
||||
str1 = LangCoder::decode( dict1->getLangTo() );
|
||||
str2 = LangCoder::decode( dict2->getLangTo() );
|
||||
str1 = LangCoder::decode( idTo1 );
|
||||
str2 = LangCoder::decode( idTo2 );
|
||||
if( str1.isEmpty() && !str2.isEmpty() )
|
||||
return false;
|
||||
if( !str1.isEmpty() && str2.isEmpty() )
|
||||
|
|
Loading…
Reference in a new issue