File indexing completed on 2024-09-15 12:05:28
0001 /** 0002 * Copyright (C) 2019 Waqar Ahmed <waqar.17a@gmail.com> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Lesser General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2.1 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Lesser General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Lesser General Public 0015 * License along with this library; if not, write to the Free Software 0016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0017 * 02110-1301 USA 0018 */ 0019 #include "guesslanguage.h" 0020 #include "speller.h" 0021 0022 #include <QObject> 0023 #include <QStandardPaths> 0024 #include <QTest> 0025 0026 class SonnetAutoDetectTest : public QObject 0027 { 0028 Q_OBJECT 0029 private Q_SLOTS: 0030 void initTestCase(); 0031 void autodetect_data(); 0032 void autodetect(); 0033 void benchDistance_data(); 0034 void benchDistance(); 0035 }; 0036 0037 using namespace Sonnet; 0038 0039 void SonnetAutoDetectTest::initTestCase() 0040 { 0041 QStandardPaths::setTestModeEnabled(true); 0042 0043 Speller speller(QStringLiteral("en_US")); 0044 0045 if (speller.availableBackends().empty()) { 0046 QSKIP("No backends available"); 0047 } 0048 0049 if (!speller.availableBackends().contains(QLatin1String("Hunspell"))) { 0050 QSKIP("Hunspell not available"); 0051 } 0052 0053 speller.setDefaultClient(QStringLiteral("Hunspell")); 0054 speller.setAttribute(Speller::AutoDetectLanguage, true); 0055 } 0056 0057 void SonnetAutoDetectTest::autodetect_data() 0058 { 0059 QTest::addColumn<QString>("sentence"); 0060 QTest::addColumn<QString>("correct_lang"); 0061 QTest::addColumn<QStringList>("suggested_langs"); 0062 0063 QTest::newRow("English") << QStringLiteral("This is an English sentence.") << QStringLiteral("en_US") 0064 << QStringList{QLatin1String("en_US"), QLatin1String("de_DE"), QLatin1String("ur_PK")}; 0065 QTest::newRow("German") << QStringLiteral("Dies ist ein deutscher Satz.") << QStringLiteral("de_DE") 0066 << QStringList{QLatin1String("hi_IN"), QLatin1String("pl_PL"), QLatin1String("de_DE_frami")}; 0067 QTest::newRow("Malayam") << QStringLiteral("ഇന്ത്യയുടെ തെക്കു ഭാഗത്തു സ്ഥിതി ചെയ്യുന്ന ഒരു സംസ്ഥാനമാണ് കേരളം.") << QStringLiteral("ml_IN") 0068 << QStringList{QLatin1String("ml_IN"), QLatin1String("ur_PK"), QLatin1String("en_US-large")}; 0069 } 0070 0071 void SonnetAutoDetectTest::autodetect() 0072 { 0073 QFETCH(QString, sentence); 0074 QFETCH(QString, correct_lang); 0075 QFETCH(QStringList, suggested_langs); 0076 0077 // skip if the language is not available 0078 Speller speller; 0079 if (!speller.availableLanguages().contains(correct_lang)) { 0080 const QString msg = correct_lang + QStringLiteral(" not available"); 0081 QSKIP(msg.toLocal8Bit().constData()); 0082 } 0083 0084 Sonnet::GuessLanguage gl; 0085 QString actual_lang = gl.identify(sentence, suggested_langs); 0086 0087 // get chars till _, get the language code 0088 int us = correct_lang.indexOf(QLatin1Char('_')); 0089 const QString correctLangCode = correct_lang.left(us); 0090 0091 us = actual_lang.indexOf(QLatin1Char('_')); 0092 const QString actualLangCode = actual_lang.left(us); 0093 0094 qDebug() << "Actual: " << actual_lang; 0095 qDebug() << "Expected: " << correct_lang; 0096 0097 QCOMPARE(actualLangCode, correctLangCode); 0098 } 0099 0100 void SonnetAutoDetectTest::benchDistance_data() 0101 { 0102 QTest::addColumn<QString>("sentence"); 0103 QTest::addColumn<QString>("correct_lang"); 0104 QTest::addColumn<QStringList>("suggested_langs"); 0105 0106 QTest::newRow("English") << QStringLiteral("This is an English sentence.") << QStringLiteral("en_US") 0107 << QStringList{QLatin1String("en_US"), QLatin1String("de_DE")}; 0108 QTest::newRow("German") << QStringLiteral("Dies ist ein deutscher Satz.") << QStringLiteral("de_DE") 0109 << QStringList{QLatin1String("pl_PL"), QLatin1String("de_DE_frami")}; 0110 QTest::newRow("Malayam") << QStringLiteral("ഇന്ത്യയുടെ തെക്കു ഭാഗത്തു സ്ഥിതി ചെയ്യുന്ന ഒരു സംസ്ഥാനമാണ് കേരളം.") << QStringLiteral("ml_IN") 0111 << QStringList{QLatin1String("ml_IN"), QLatin1String("en_US-large")}; 0112 } 0113 0114 void SonnetAutoDetectTest::benchDistance() 0115 { 0116 QFETCH(QString, sentence); 0117 QFETCH(QString, correct_lang); 0118 QFETCH(QStringList, suggested_langs); 0119 0120 Sonnet::GuessLanguage gl; 0121 0122 QBENCHMARK { 0123 QString actual_lang = gl.identify(sentence, suggested_langs); 0124 } 0125 } 0126 0127 QTEST_GUILESS_MAIN(SonnetAutoDetectTest) 0128 0129 #include "test_autodetect.moc"