Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sorting Vector problem sort Word to Language detection
Qt 6.11 is out! See what's new in the release blog

Sorting Vector problem sort Word to Language detection

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    patrik08
    wrote on last edited by
    #1

    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();
    }
    
    
    
    
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

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

      1 Reply Last reply
      2
      • P Offline
        P Offline
        patrik08
        wrote on last edited by
        #3

        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>'

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved