시작은 미미하나 끝은 쥬쥬하리라.

Programming/QT

[QT] Qt4 - List 데이터에서 특정 단어의 빈도수 산출하기

코딩뚜벅이 2024. 1. 18. 22:44

Qt4 데이터에서 특정 단어 빈도수 산출하기

  • 코드 구현
  • 결과

코드 구현

데이터를 QStringList 형식이라 가정하고 진행해보겠습니다. 저는 미리 QStringList 형식의 데이터를 선언했습니다. 저는 이 List가 가진 데이터 중 title이라는 key값의 value에 해당하는 데이터만을 1차적으로 추출하고 이들을 대상으로 빈도수를 산출하겠습니다.

 

#include <QtCore/QCoreApplication>
#include <QStringList>
#include <QDebug>
#include <QMap>
#include <QList>
#include <QString>
#include <cstdlib>
#include <ctime>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // 데이터 List.
    QStringList qList;
    qList << "{ Index=\"1\", title=\"Apple_Tree\", link=\"https://example.com/1\" }"
          << "{ Index=\"2\", title=\"Apple_Tree\", link=\"https://example.com/2\" }"
          << "{ Index=\"3\", title=\"Cherry_Blossom\", link=\"https://example.com/3\" }"
          << "{ Index=\"4\", title=\"Dragonfly_Wings\", link=\"https://example.com/4\" }"
          << "{ Index=\"5\", title=\"Elephant_Trunk\", link=\"https://example.com/5\" }"
          << "{ Index=\"6\", title=\"Flamingo_Feathers\", link=\"https://example.com/6\" }"
          << "{ Index=\"7\", title=\"Giraffe_Spots\", link=\"https://example.com/7\" }"
          << "{ Index=\"8\", title=\"Honey_Bee\", link=\"https://example.com/8\" }"
          << "{ Index=\"9\", title=\"Iguana_Scales\", link=\"https://example.com/9\" }"
          << "{ Index=\"10\", title=\"Jellyfish_Tentacles\", link=\"https://example.com/10\" }"
          << "{ Index=\"11\", title=\"Kiwi_Seed\", link=\"https://example.com/11\" }"
          << "{ Index=\"12\", title=\"Lion_Mane\", link=\"https://example.com/12\" }"
          << "{ Index=\"13\", title=\"Mango_Pulp\", link=\"https://example.com/13\" }"
          << "{ Index=\"14\", title=\"Nightingale_Song\", link=\"https://example.com/14\" }"
          << "{ Index=\"15\", title=\"Orange_Zest\", link=\"https://example.com/15\" }"
          << "{ Index=\"16\", title=\"Penguin_Waddle\", link=\"https://example.com/16\" }"
          << "{ Index=\"17\", title=\"Quokka_Smile\", link=\"https://example.com/17\" }"
          << "{ Index=\"18\", title=\"Raspberry_Jam\", link=\"https://example.com/18\" }"
          << "{ Index=\"19\", title=\"Sunflower_Petals\", link=\"https://example.com/19\" }"
          << "{ Index=\"20\", title=\"Tiger_Stripes\", link=\"https://example.com/20\" }"
          << "{ Index=\"21\", title=\"Umbrella_Handle\", link=\"https://example.com/21\" }"
          << "{ Index=\"22\", title=\"Vanilla_Bean\", link=\"https://example.com/22\" }"
          << "{ Index=\"23\", title=\"Watermelon_Slice\", link=\"https://example.com/23\" }"
          << "{ Index=\"24\", title=\"Xylophone_Keys\", link=\"https://example.com/24\" }"
          << "{ Index=\"25\", title=\"Yellowjacket_Buzz\", link=\"https://example.com/25\" }"
          << "{ Index=\"26\", title=\"Zebra_Stripes\", link=\"https://example.com/26\" }"
          << "{ Index=\"27\", title=\"Antelope_Horns\", link=\"https://example.com/27\" }"
          << "{ Index=\"28\", title=\"Bluebird_Feather\", link=\"https://example.com/28\" }"
          << "{ Index=\"29\", title=\"Cactus_Thorn\", link=\"https://example.com/29\" }"
          << "{ Index=\"30\", title=\"Dolphin_Click\", link=\"https://example.com/30\" }";

    // 추출한 title 데이터를 담을 List.
    QStringList extractedTitles;

    // for문을 통한 List 순회.
    for (int i = 0; i < qList.size(); ++i) {
        // 현재 List 추출.
        const QString& item = qList.at(i);

        // title 데이터 추출.
        QString title = item.section("title=\"", 1, 1).section("\"", 0, 0);

        // 추출한 title 데이터를 List에 저장.
        extractedTitles << title;
    }

    // QMap을 생성하여 중복되는 단어 카운트.
    QMap<QString, int> wordCount;
    for (int i = 0; i < extractedTitles.size(); ++i) {
        // 제목에서 단어 추출.
        QStringList words = extractedTitles.at(i).split(QRegExp("\\W+"), QString::SkipEmptyParts);
        for (int j = 0; j < words.size(); ++j) {
            wordCount[words.at(j)] += 1;
        }
    }

    // 단어의 빈도수 오름차순 정렬 및 콘솔 출력.
    qDebug() << "Top 10 Frequent Words:";
    int count = 0;
    for (int i = 0; i < extractedTitles.size(); ++i) {
        const QString& word = extractedTitles.at(i);
        if (wordCount[word] > 0) {
            qDebug() << "Word:" << word << "Frequency:" << wordCount[word];
            ++count;
            if (count >= 10) {
                break;
            }
        }
    }

    return a.exec();
}

 

 

1. 추출한 title의 value 데이터를 담을 QStringList를 하나 생성해주었습니다. List의 사이즈로 데이터를 순회하여 title의 데이터를 List에 담아주었습니다.

 

2. QMap을 이용해서 단어와 단어의 중복(빈도수)를 계산해주었습니다. 이제 QMap 객체인 wordCount에는 특정 단어와 단어의 개수가 포함됩니다.

 

3. 이대로 출력해서 결과를 확인해도 괜찮지만 오름차순으로 QMap 데이터를 정렬하고 상위 10개의 단어만 출력하는 로직을 구현하였습니다.

 


 

결과

아래 결과를 보시면 제가 사전에 준비한 List 데이터의 title에 해당하는 부분이 거의 중복되지 않은 데이터로 구성되어 있기 때문에 빈도수가 2 혹은 1로 계산되어 나온 것을 확인할 수 있습니다. 그래도 단어 카운팅과 오름차순 정렬은 적용되어있는 것을 확인할 수 있습니다.