File indexing completed on 2024-04-14 03:58:19

0001 /**
0002  * parsetrigrams.cpp
0003  *
0004  * Parse a set of trigram files into a QMap, and serialize to stdout.
0005  * Note: we allow this data to be read into QHash. We use QMap here
0006  * to get deterministic output from run to run.
0007  *
0008  * SPDX-FileCopyrightText: 2006 Jacob Rideout <kde@jacobrideout.net>
0009  *
0010  * SPDX-License-Identifier: LGPL-2.1-or-later
0011  */
0012 
0013 #include <QDataStream>
0014 #include <QDir>
0015 #include <QFile>
0016 #include <QMap>
0017 #include <QRegularExpression>
0018 #include <QString>
0019 #include <QTextStream>
0020 
0021 int main(int argc, char **argv)
0022 {
0023     if (argc < 2) {
0024         return 1;
0025     }
0026 
0027     QFile sout;
0028     sout.open(stdout, QIODevice::WriteOnly);
0029     QDataStream out(&sout);
0030 
0031     QString path = QLatin1String(argv[1]);
0032     QDir td(path);
0033 
0034     /*
0035      * We use QMap (instead of QHash) here to get deterministic output
0036      * from run to run.
0037      */
0038     QMap<QString, QMap<QString, int>> models;
0039 
0040     const QRegularExpression rx(QStringLiteral("(?:.{3})\\s+(.*)"));
0041     const QStringList files = td.entryList(QDir::Files);
0042     for (const QString &fname : files) {
0043         QFile fin(td.filePath(fname));
0044         fin.open(QFile::ReadOnly | QFile::Text);
0045         QTextStream stream(&fin);
0046 
0047         while (!stream.atEnd()) {
0048             QString line = stream.readLine();
0049             const QRegularExpressionMatch match = rx.match(line);
0050             if (match.hasMatch()) {
0051                 models[fname][line.left(3)] = match.capturedView(1).toInt();
0052             }
0053         }
0054     }
0055 
0056     out << models;
0057 }