diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -11,6 +11,7 @@ test_core test_suggest test_settings + test_autodetect NAME_PREFIX "sonnet-" LINK_LIBRARIES KF5::SonnetCore Qt5::Test ) diff --git a/autotests/test_autodetect.cpp b/autotests/test_autodetect.cpp new file mode 100644 --- /dev/null +++ b/autotests/test_autodetect.cpp @@ -0,0 +1,65 @@ +/** + * Copyright (C) 2015 Kåre Särs + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include +#include +#include +#include "guesslanguage.h" +#include + +class SonnetAutoDetectTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + + void autodetect_data(); + void autodetect(); +}; + +void SonnetAutoDetectTest::autodetect_data() +{ + QTest::addColumn("sentence"); + QTest::addColumn("correct_lang"); + QTest::addColumn("suggested_langs"); + + /* These tests will fail if you dont have the respective dictionary installed. + * They will also fail if the dictionary file name is different from 'correct_lang' + */ + QTest::newRow("English") << QStringLiteral("This is an English sentence.") << QStringLiteral("en_US-large") << QStringList{QLatin1String("en_US-large"), QLatin1String("de_DE"), QLatin1String("ur_PK")}; + + QTest::newRow("German") << QStringLiteral("Dies ist ein deutscher Satz.") << QStringLiteral("de_DE_frami") << QStringList{QLatin1String("hi_IN"), QLatin1String("pl_PL"), QLatin1String("de_DE_frami")}; + + QTest::newRow("Malayam") << QStringLiteral("ഇന്ത്യയുടെ തെക്കു ഭാഗത്തു സ്ഥിതി ചെയ്യുന്ന ഒരു സംസ്ഥാനമാണ് കേരളം.") << QStringLiteral("ml_IN") << QStringList{QLatin1String("ml_IN"), QLatin1String("ur_PK"), QLatin1String("en_US-large")}; +} + +void SonnetAutoDetectTest::autodetect() +{ + QFETCH(QString, sentence); + QFETCH(QString, correct_lang); + QFETCH(QStringList, suggested_langs); + + Sonnet::GuessLanguage gl; + QString actual_lang = gl.identify(sentence, suggested_langs); + + QCOMPARE(actual_lang, correct_lang); +} + +QTEST_GUILESS_MAIN(SonnetAutoDetectTest) + +#include "test_autodetect.moc" diff --git a/src/core/guesslanguage.cpp b/src/core/guesslanguage.cpp --- a/src/core/guesslanguage.cpp +++ b/src/core/guesslanguage.cpp @@ -710,6 +710,13 @@ guesses.append(guessFromTrigrams(sample, s_scriptLanguages.values(script))); } + //if guesses are empty, we just append the languages of the scripts + if (guesses.isEmpty() && !scripts.isEmpty()) { + for (const QChar::Script script : scripts) { + guesses.append(s_scriptLanguages.values(script)); + } + } + return guesses; }