C++ (Qt) QClipboard *clipboard = QApplication::clipboard(); QString xt = clipboard->text(); QString originalText = xt.trimmed();
C++ (Qt) trimmedStr = itemStr.trimmed();// Убираем пустышку if( trimmedStr.isEmpty() ) continue;
C++ (Qt) QClipboard *clipboard = QApplication::clipboard(); QString originalText = clipboard->text(); originalText = originalText.toLower(); originalText.replace(QRegExp("[a-z]"), "");
C++ (Qt) originalText.replace(QRegExp("[а-я]"), "");
QString str;QStringList list;str = "Some text\n\twith strange whitespace.";list = str.split(QRegExp("\\s+"));// list: [ "Some", "text", "with", "strange", "whitespace." ]
QString Text = tr("Это, входной: текст");QStringList List = Text.split(QRegExp("\\s+"),QString::SkipEmptyParts);QMap <QString, int> MyMap;foreach (QString Item, List) MyMap[Item] = (MyMap.contains(Item)) ? MyMap[Item]+1 : 1;
QTextCodec *Сodec = QTextCodec::codecForName("UTF-8");QTextCodec::setCodecForLocale(Сodec);QString Text = tr("Это, входной: текст");QStringList List = Text.split(QRegExp("[\\s\.,:;-()]+"),QString::SkipEmptyParts);QMap <QString, int> MyMap;foreach (QString Item, List) MyMap[Item] = (MyMap.contains(Item)) ? MyMap[Item]+1 : 1;
C++ (Qt)void MainWindow::bufferButtonSave(){ QClipboard *clipboard = QApplication::clipboard(); QString originalText = clipboard->text(); originalText = originalText.toLower(); originalText.replace(QRegExp("\\W"), " "); originalText.replace(QRegExp("\\d"), " "); // ЭТО НЕ РАБОТАЕТ originalText.replace(QRegExp("[а-я]"), ""); QTextStream fileStream( & originalText ); QString itemStr, trimmedStr; QStringList itemList; QString countItemString; QString fullCountItemString; int fullCountItem(0); textory.clear(); do { // Формируем список пословно fileStream >>(itemStr); trimmedStr = itemStr.trimmed(); if( trimmedStr.isEmpty() ) continue; QRegExp rx("\\b(" + trimmedStr + ")\\b", Qt::CaseInsensitive); int countItem(0); int pos(0); while ((pos = rx.indexIn(originalText, pos)) != -1) { ++countItem; ++pos; } countItemString.setNum(countItem); ++fullCountItem; trimmedStr = trimmedStr + "\t" + countItemString; itemList.prepend( trimmedStr ); } while( !fileStream.atEnd() ); // Выводим список на экран: textory.enableAdd( true ); fullCountItemString.setNum(fullCountItem); // СОРТИРУЕМ: РАБОТАЕТ В ПОРЯДКЕ ВОЗРАСТАНИЯ. // НАДО1 - В ПОРЯДКЕ УБЫВАНИЯ. // НАДО2 - ОТСОРТИРОВАТЬ В ПОРЯДКЕ ЧАСТОТНОСТИ. qSort(itemList); foreach (QString itm, itemList)textory.addItem( Textory::Item( 1, itm ) ); // Заголовок спсика: ui.textoryWidget->getTextoryLabel().setText( "Frequency list (" + fullCountItemString + ")" );}
C++ (Qt)struct CFreq { CFreq( int count = 1, const QString * str = 0 ) : mCount(count), mStr(str) {} bool operator < ( const CFreq & sec ) const { return mCount > sec.mCount; } int mCount; const QString * mStr;}; typedef QMap <QString. int> TMapStr;TMapStr theMap;... // заполняем мапу - см выше // заполняем векторQVector <CFreq> theFreq;for (TMapStr::const_iterator it = theMap.begin(); it != theMp.end(); ++it) theFreq.push_back(CFreq(it->value(), &it->key())); // сортируемqSort(theFreq.begin(), theFreq.end()); // печатаемfor (int i = 0; i < theFreq.size(); ++i) qDebug() << *theFreq[i].mStr << "\n"
C++ (Qt)void stat(){ QString src = "erwerwer ewr werwerwerwerew werwerwe sdfsdfsdf tyutyu\tdsfsdfds+werewrer-ewr"; QString sep = "[ \\t\\n\\r+-]"; QHash<QString, int> hash; QRegExp rx( sep ); int start = 0; int cur = 0; for(;;) { cur = rx.indexIn( src, cur ); QString word = src.mid( start, (cur != -1)? cur - start : -1 ); if( !word.isEmpty() ) { qDebug() << word; hash[ word ]++; } if( cur == -1 ) break; start = ++cur; } qDebug() << hash;}
C++ (Qt)#include <iostream>#include <boost/xpressive/xpressive.hpp>#include <boost/algorithm/string/case_conv.hpp>#include <string>#include <unordered_map> class word_statistics{public: typedef std::string string_type; typedef std::unordered_map<string_type, unsigned> hash_type; typedef typename hash_type::size_type size_type; typedef typename hash_type::const_iterator const_iterator; word_statistics(const string_type & source) : m_source(source), m_total_count(0) {} void compile(const string_type & expr, bool icase = false) { using namespace boost::xpressive; sregex e = (icase) ? sregex::compile(expr, regex_constants::icase) : sregex::compile(expr); sregex_iterator it(m_source.begin(), m_source.end(), e); sregex_iterator end; m_hash.clear(); for (m_total_count = 0; it != end; ++it, ++m_total_count) { if (icase) ++m_hash[boost::algorithm::to_lower_copy(it->str(0))]; else ++m_hash[it->str(0)]; } } size_type total_count() const { return m_total_count; } size_type size() const { return m_hash.size(); } const_iterator begin() const { return m_hash.begin(); } const_iterator end() const { return m_hash.end(); } private: string_type m_source; size_type m_total_count; hash_type m_hash;}; int main(){ std::string str("text1, text2 and a lot of TextT 3"); word_statistics word_stat(str); word_stat.compile("(\\d+)", true); std::cout << "only digit:\n"; for (auto s : word_stat) { std::cout << s.first << " = " << s.second << std::endl; } std::cout << "total count = " << word_stat.total_count() << "\n\n"; std::cout << "only word:\n"; word_stat.compile("([a-zA-Z]+)", false); for (auto s : word_stat) { std::cout << s.first << " = " << s.second << std::endl; } std::cout << "total count = " << word_stat.total_count() << "\n\n"; std::cout << "only [a e t]:\n"; word_stat.compile("([aet])", true); for (auto s : word_stat) { std::cout << s.first << " = " << s.second << std::endl; } std::cout << "total count = " << word_stat.total_count() << "\n\n"; return 0;}
Bashsource = text1, text2 and a lot of TextT 3 only digit:1 = 12 = 13 = 1total count = 3 only word:TextT = 1of = 1lot = 1a = 1and = 1text = 2total count = 7 only [a e t]:e = 3a = 2t = 8total count = 13