]> Git trees. - libqmvoc.git/commitdiff
Enable writing of synonym pairs.
authorFrederik Gladhorn <gladhorn@kde.org>
Sun, 23 Mar 2008 03:26:57 +0000 (03:26 +0000)
committerFrederik Gladhorn <gladhorn@kde.org>
Sun, 23 Mar 2008 03:26:57 +0000 (03:26 +0000)
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=789088

keduvocdocument/keduvocexpression.h
keduvocdocument/keduvockvtml2reader.cpp
keduvocdocument/keduvockvtml2reader.h
keduvocdocument/keduvockvtml2writer.cpp
keduvocdocument/keduvockvtml2writer.h
keduvocdocument/kvtml2defs.h

index b2d842b3867a37833e950713ec76ceef780af169..33790c8769bbc66521844f748b3840a2450b75d4 100644 (file)
@@ -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
index 40700f2da24e435cff7a94550d0b6ae8966133d2..951b3f3393086155e317db7aa0bc4bb95b5a02a2 100644 (file)
@@ -5,7 +5,7 @@
 
                           (C) 2005 Eric Pignet <eric at erixpage.com>
                           (C) 2007 Peter Hedlund <peter.hedlund@kdemail.net>
-                          (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
+    Copyright 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
  ***************************************************************************/
 
 /***************************************************************************
index c917072165ec0ec1e43af73b56135a9c89be7da9..42f8d14ec8e0098c5939acb43d553d786d959554 100644 (file)
@@ -2,6 +2,7 @@
                      read a KEduVocDocument from a KVTML2 file
     -----------------------------------------------------------------------
     copyright      : (C) 2007 Jeremy Whiting <jeremy@scitools.com>
+    Copyright 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
  ***************************************************************************/
 
 /***************************************************************************
index 68cbd21a9dc1a34912e5333797a3b59b05d43cf4..0370b9ae5a8bf4faac8ef9c4cfd528859522620b 100644 (file)
@@ -2,7 +2,7 @@
                    export a KEduVocDocument to a KVTML file
     -----------------------------------------------------------------------
     copyright           : (C) 2007 Jeremy Whiting <jeremy@scitools.com>
-                          (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
+                          (C) 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
  ***************************************************************************/
 
 /***************************************************************************
@@ -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 <translation id="123">
+                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 <translation id="123">
+                        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 <translation id="123">
+                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
-    // <falsefriend fromid="0"></falsefriend>
-    // 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
+    // <falsefriend fromid="0"></falsefriend>
+    // 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 )
 /*
  <comparison>
index 5e7d3064cd913edf7129dd25d11a8a3452f32827..dab473d7dea8abb8a630298e114babc83a7cb658 100644 (file)
@@ -2,7 +2,7 @@
                      export a KEduVocDocument to a KVTML file
     -----------------------------------------------------------------------
     copyright       : (C) 2007 Jeremy Whiting <jeremy@scitools.com>
-                      (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
+                      (C) 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
  ***************************************************************************/
 
 /***************************************************************************
@@ -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<KEduVocTranslation*> 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<KEduVocExpression*>  m_allEntries;
+    QList<KEduVocTranslation*> m_synonyms;
+    QList<KEduVocTranslation*> m_antonyms;
+    QList<KEduVocTranslation*> m_falseFriends;
 
     QDomDocument m_domDoc;
 };
index 4a23b93de2f9fe9e8eaacdab633bbe6e97d5ba6c..ac816a2be991d5eb36eb4634188141e774cda393 100644 (file)
@@ -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"