Sorting Vector problem sort Word to Language detection



  • I can not self repair this sorting ... i like to have top word at first place sorting by int first colum...
    i need to find this mistake.... the hit word is not sorted..

    #include <iostream>
    #include <string>
    #include <map>
    #include <algorithm>
    #include <QTextStream>
    #include <QVector>
    #include <QPair>
    #include <QByteArray>
    #include <QString>
    #include <QtGlobal>
    #include <QCoreApplication>
    
    typedef QPair<qint64,QString> HitWordParade;
    typedef QVector<HitWordParade > TopWordLanguage;
    
    int main(int argc, char* argv[]){
    	
    	QCoreApplication a(argc, argv);
    	
    	QTextStream cout(stdout,QIODevice::WriteOnly);
        //// find top hit word list from all Latin Language
        //// fork to qt from https://shuyo.wordpress.com/2012/05/17/short-text-language-detection-with-infinity-gram/
        /// 
         std::map<std::string,int> strCount;
        std::string str("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, \
              sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, \
              sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita \
              kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor \
              sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et \
              dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et \
              ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. "); 
              
              std::cout << "Search 'Multi subtring' to dedect languag-> string: " << str << std::endl;
              
              
        std::string temp;
    
        // Split String Based on 
        for ( int i(0); i < str.size(); ++i ){
            temp += str[i];
            if ( str[i] == ' ' ){
                temp.pop_back();
                ++strCount[temp]; /// insert in noqt map.. if new
                temp.clear();
           }
        }
        TopWordLanguage record;
        std::map<std::string,int>::const_iterator iter;   
        for( iter = strCount.begin(); iter != strCount.end(); iter++ ) {
    		 if ( iter->second > 2) {
                std::cout << "#: " << iter->second << " string: " << iter->first << std::endl;
                record.prepend(qMakePair((int)iter->second,QString(QByteArray::fromStdString(iter->first))));
    	    }
        }
           
         qSort( record ); //// sort key first
         
         foreach (const HitWordParade &word, record ) {
    		       qint64 hit = word.first;
                   QString stringword = word.second;
                   if (stringword.trimmed().size() > 1) {
                   std::cout << "hit:" << hit << " word: " << qUtf8Printable(stringword) << std::endl;
    		       }
    	 }
    	 
          return 0;
    	/// return a.exec();
    }
    
    
    
    

  • Moderators

    Hi! std algorithm has all sorts of sorting algortihms. Or use QStringList.



  • tank...
    on linux its possibel to define:

    std::map<std::string,int ,std::greater<int> > strCount;
    

    but macosx say;

    In file included from /Users/dev/sandbox/qldig/main.cpp:3:
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:1365:17: error:
    no matching function for call to object of type 'const
    std::__1::greater<int>'


  • Lifetime Qt Champion

    Hi,

    AFAIK, it's because your compare function is declared wrong.

    template <class Key, class T, class Compare = std::less<Key>>

    So yours should be:

    std::map<std::string, int, std::greater<std::string> > strCount;

    See cppreference std::map documentation.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.