From 061900282df17821bb887a9d72f947012814919e Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn Date: Sun, 23 Mar 2008 03:26:57 +0000 Subject: [PATCH] Enable writing of synonym pairs. svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=789088 --- keduvocdocument/keduvocexpression.h | 3 - keduvocdocument/keduvockvtml2reader.cpp | 2 +- keduvocdocument/keduvockvtml2reader.h | 1 + keduvocdocument/keduvockvtml2writer.cpp | 129 +++++++++++++++++++++--- keduvocdocument/keduvockvtml2writer.h | 41 +++++--- keduvocdocument/kvtml2defs.h | 4 +- 6 files changed, 144 insertions(+), 36 deletions(-) diff --git a/keduvocdocument/keduvocexpression.h b/keduvocdocument/keduvocexpression.h index b2d842b..33790c8 100644 --- a/keduvocdocument/keduvocexpression.h +++ b/keduvocdocument/keduvocexpression.h @@ -90,15 +90,12 @@ public: */ void setTranslation( int index, const QString &expression ); -// void setTranslation( const QString &identifier, const QString & expression ); - /** removes a translation * * @param index number of translation 1..x */ void removeTranslation( int index ); - /** * Get a pointer to the translation * @param index of the language identifier diff --git a/keduvocdocument/keduvockvtml2reader.cpp b/keduvocdocument/keduvockvtml2reader.cpp index 40700f2..951b3f3 100644 --- a/keduvocdocument/keduvockvtml2reader.cpp +++ b/keduvocdocument/keduvockvtml2reader.cpp @@ -5,7 +5,7 @@ (C) 2005 Eric Pignet (C) 2007 Peter Hedlund - (C) 2007 Frederik Gladhorn + Copyright 2007-2008 Frederik Gladhorn ***************************************************************************/ /*************************************************************************** diff --git a/keduvocdocument/keduvockvtml2reader.h b/keduvocdocument/keduvockvtml2reader.h index c917072..42f8d14 100644 --- a/keduvocdocument/keduvockvtml2reader.h +++ b/keduvocdocument/keduvockvtml2reader.h @@ -2,6 +2,7 @@ read a KEduVocDocument from a KVTML2 file ----------------------------------------------------------------------- copyright : (C) 2007 Jeremy Whiting + Copyright 2007-2008 Frederik Gladhorn ***************************************************************************/ /*************************************************************************** diff --git a/keduvocdocument/keduvockvtml2writer.cpp b/keduvocdocument/keduvockvtml2writer.cpp index 68cbd21..0370b9a 100644 --- a/keduvocdocument/keduvockvtml2writer.cpp +++ b/keduvocdocument/keduvockvtml2writer.cpp @@ -2,7 +2,7 @@ export a KEduVocDocument to a KVTML file ----------------------------------------------------------------------- copyright : (C) 2007 Jeremy Whiting - (C) 2007 Frederik Gladhorn + (C) 2007-2008 Frederik Gladhorn ***************************************************************************/ /*************************************************************************** @@ -84,6 +84,8 @@ bool KEduVocKvtml2Writer::writeDoc( KEduVocDocument *doc, const QString &generat domElementKvtml.appendChild( currentElement ); } + writeSynonymAntonymFalseFriend(domElementKvtml); + m_domDoc.appendChild( domElementKvtml ); QTextStream ts( m_outputFile ); @@ -196,6 +198,84 @@ bool KEduVocKvtml2Writer::writeLessons( KEduVocLesson *parentLesson, QDomElement } + +void KEduVocKvtml2Writer::writeSynonymAntonymFalseFriend(QDomElement & parentElement) +{ + // synonym, antonym, false friend + QDomElement synonymElement = m_domDoc.createElement( KVTML_SYNONYM ); +kDebug() << "writing list of synonyms: " << m_synonyms.count(); + while (!m_synonyms.isEmpty()) { + // after writing a translation, remove it from the list + KEduVocTranslation* translation = m_synonyms.takeAt(0); + + // fill the entry element but only add later if it is valid + QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY ); + entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(translation->entry())) ); + // find out which id that is... silly + foreach(int index, translation->entry()->translationIndices()) { + if (translation->entry()->translation(index) == translation) { + // create + QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION ); + translationElement.setAttribute( KVTML_ID, QString::number(index) ); + entryElement.appendChild(translationElement); + break; + } + } + + + QDomElement relatedElement; + foreach (KEduVocTranslation* synonym, translation->synonyms()) { + // if it is not in the list it has already been written and we can move on + if (m_synonyms.contains(synonym)) { + relatedElement = m_domDoc.createElement( KVTML_PAIR ); + synonymElement.appendChild(relatedElement); + relatedElement.appendChild(entryElement); + + + entryElement = m_domDoc.createElement( KVTML_ENTRY ); + entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(synonym->entry())) ); + + // find out which id that is + foreach(int index, synonym->entry()->translationIndices()) { + if (synonym->entry()->translation(index) == synonym) { + // create + QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION ); + translationElement.setAttribute( KVTML_ID, QString::number(index) ); + entryElement.appendChild(translationElement); + break; + } + } + relatedElement.appendChild( entryElement ); + } + if (relatedElement.hasChildNodes()) { + synonymElement.appendChild( relatedElement ); + } + } + } + if (synonymElement.hasChildNodes()) { + parentElement.appendChild( synonymElement ); + } +} +/* +bool KEduVocKvtml2Writer::writeRelated(QDomElement & parentElement, QList< KEduVocTranslation * > relatedList) +{ + foreach (KEduVocTranslation* synonym, translation->synonyms()) { + QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY ); + entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(translation->entry())) ); + + // find out which id that is... silly + foreach(int index, translation->entry()->translationIndices()) { + if (translation->entry()->translation(index) == translation) { + // create + QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION ); + translationElement.setAttribute( KVTML_ID, QString::number(index) ); + entryElement.appendChild(translationElement); + } + } + parentElement.appendChild( entryElement ); + } +}*/ + bool KEduVocKvtml2Writer::writeArticle( QDomElement &articleElement, int language ) { ///@todo only write if not empty @@ -337,20 +417,6 @@ bool KEduVocKvtml2Writer::writeTranslation( QDomElement &translationElement, KEd // so far only for KEduVocWord - text and grades translation->toKVTML2(translationElement); - ///@todo write false friends - // - // loop through the identifiers -// for ( int i = 0; i < m_doc->identifierCount(); ++i ) { -// // see if this identifier has a falsefriend in this translation -// QString thisFriend = translation->falseFriend( i ); -// if ( !thisFriend.isEmpty() ) { -// // if so, create it, and set the fromid to i -// QDomElement thisFriendElement = newTextElement( KVTML_FALSEFRIEND, thisFriend ); -// thisFriendElement.setAttribute( KVTML_FROMID, QString::number( i ) ); -// translationElement.appendChild( thisFriendElement ); -// } -// } - // comparison if ( !(translation->comparative().isEmpty() || translation->comparative().isEmpty())) { QDomElement comparisonElement = m_domDoc.createElement( KVTML_COMPARISON ); @@ -389,9 +455,42 @@ bool KEduVocKvtml2Writer::writeTranslation( QDomElement &translationElement, KEd translationElement.appendChild( newTextElement( KVTML_SOUND, urlString ) ); } + + // synonym, antonym, false friend + // add to the list if it has any, write later since we want them seperate + if (!translation->synonyms().isEmpty()) { + m_synonyms.append(translation); + } + if (!translation->antonyms().isEmpty()) { + m_antonyms.append(translation); + } + if (!translation->falseFriends().isEmpty()) { + m_falseFriends.append(translation); + } return true; } + + + ///@todo write false friends + // + // loop through the identifiers +// for ( int i = 0; i < m_doc->identifierCount(); ++i ) { +// // see if this identifier has a falsefriend in this translation +// QString thisFriend = translation->falseFriend( i ); +// if ( !thisFriend.isEmpty() ) { +// // if so, create it, and set the fromid to i +// QDomElement thisFriendElement = newTextElement( KVTML_FALSEFRIEND, thisFriend ); +// thisFriendElement.setAttribute( KVTML_FROMID, QString::number( i ) ); +// translationElement.appendChild( thisFriendElement ); +// } +// } + + + + + + bool KEduVocKvtml2Writer::writeComparison( QDomElement &comparisonElement, KEduVocTranslation* translation ) /* diff --git a/keduvocdocument/keduvockvtml2writer.h b/keduvocdocument/keduvockvtml2writer.h index 5e7d306..dab473d 100644 --- a/keduvocdocument/keduvockvtml2writer.h +++ b/keduvocdocument/keduvockvtml2writer.h @@ -2,7 +2,7 @@ export a KEduVocDocument to a KVTML file ----------------------------------------------------------------------- copyright : (C) 2007 Jeremy Whiting - (C) 2007 Frederik Gladhorn + (C) 2007-2008 Frederik Gladhorn ***************************************************************************/ /*************************************************************************** @@ -41,6 +41,16 @@ public: bool writeDoc( KEduVocDocument *doc, const QString &generator ); + /** + * Helper function, appends a new element AND a text child to @p parent + * Only appends if @p text is NOT empty. + * @param parent + * @param elementName + * @param text + */ + static void appendTextElement( QDomElement &parent, const QString &elementName, const QString &text ); + +private: /** write information entries * @param informationElement QDomElement information to write to * @param generator text describing generator @@ -69,11 +79,6 @@ public: */ bool writeTenses( QDomElement &tensesElement ); - /** write usages - * @param usagesElement QDomElement usages to write to - */ - bool writeUsages( QDomElement &usagesElement ); - /** write entries * @param entriesElement QDomElement entries to write to */ @@ -85,12 +90,23 @@ public: */ bool writeTranslation( QDomElement &translationElement, KEduVocTranslation* translation ); + /** + * Used to write synonym, antonym and false friend lists + * @param typesElement + * @param parentContainer + * @return + */ + bool writeRelated( QDomElement &parentElement, QList relatedList ); + /** write the lesson group * @param parentLesson the parent lesson of the current lesson * @param lessonsElement QDomElement lessons to write to */ bool writeLessons( KEduVocLesson *parentLesson, QDomElement &lessonsElement ); + + void writeSynonymAntonymFalseFriend(QDomElement & parentElement); + /** write a comparison * @param comparisonElement QDomElement comparison to write to * @param comparison object to write @@ -104,22 +120,15 @@ public: */ bool writeMultipleChoice( QDomElement &multipleChoiceElement, KEduVocTranslation* translation ); - /** - * Helper function, appends a new element AND a text child to @p parent - * Only appends if @p text is NOT empty. - * @param parent - * @param elementName - * @param text - */ - static void appendTextElement( QDomElement &parent, const QString &elementName, const QString &text ); - -private: QDomElement newTextElement( const QString &elementName, const QString &text ); QFile *m_outputFile; KEduVocDocument *m_doc; QList m_allEntries; + QList m_synonyms; + QList m_antonyms; + QList m_falseFriends; QDomDocument m_domDoc; }; diff --git a/keduvocdocument/kvtml2defs.h b/keduvocdocument/kvtml2defs.h index 4a23b93..ac816a2 100644 --- a/keduvocdocument/kvtml2defs.h +++ b/keduvocdocument/kvtml2defs.h @@ -128,10 +128,12 @@ static const QString KVTML_GRAMMATICAL_DEFINITENESS[] = { #define KVTML_TRANSLATION "translation" #define KVTML_TEXT "text" #define KVTML_PRONUNCIATION "pronunciation" + +#define KVTML_PAIR "pair" #define KVTML_FALSEFRIEND "falsefriend" -#define KVTML_FROMID "fromid" #define KVTML_ANTONYM "antonym" #define KVTML_SYNONYM "synonym" + #define KVTML_EXAMPLE "example" #define KVTML_PARAPHRASE "paraphrase" -- 2.47.3