KTextAddons

voskengineutils.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "voskengineutils.h"
8#include "libvoskspeechtotext_debug.h"
9#include <QDir>
10#include <QJsonDocument>
11#include <QJsonObject>
12#include <QStandardPaths>
13
14QString VoskEngineUtils::defaultVoskRepository()
15{
16 return QStringLiteral("https://alphacephei.com/vosk/models/model-list.json");
17}
18
19QString VoskEngineUtils::storageLanguagePath()
20{
21 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/vosk-texttospeech");
22}
23
24VoskEngineUtils::LanguageInstalled VoskEngineUtils::loadInstalledLanguageInfo(const QString &modelInfoPath)
25{
26 VoskEngineUtils::LanguageInstalled info;
27 QFile file(modelInfoPath + QStringLiteral("/model_info.json"));
28 if (!file.open(QFile::ReadOnly)) {
29 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to open model_info.json in " << modelInfoPath;
30 return info;
31 }
32 const QByteArray ba = file.readAll();
33 const QJsonDocument loadDoc(QJsonDocument::fromJson(ba));
34 const QJsonObject o = loadDoc.object();
35 info.absoluteLanguageModelPath = o[QLatin1String("absolutePath")].toString();
36 info.name = o[QLatin1String("name")].toString();
37 info.url = o[QLatin1String("url")].toString();
38 info.versionStr = o[QLatin1String("version")].toString();
39 return info;
40}
41
42bool VoskEngineUtils::createInstalledLanguageInfo(const QString &modelInfoPath, const LanguageInstalled &installed)
43{
44 QFile file(modelInfoPath + QStringLiteral("/model_info.json"));
45 if (!file.open(QFile::WriteOnly)) {
46 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to save model_info.json in :" << modelInfoPath;
47 return false;
48 }
51 o[QLatin1String("absolutePath")] = installed.absoluteLanguageModelPath;
52 o[QLatin1String("name")] = installed.name;
53 o[QLatin1String("url")] = installed.url;
54 o[QLatin1String("version")] = installed.versionStr;
55 d.setObject(o);
56 const QByteArray ba = d.toJson();
57 file.write(d.toJson());
58 file.close();
59 return true;
60}
61
62QVector<VoskEngineUtils::LanguageInstalled> VoskEngineUtils::languageLocallyStored(const QString &path)
63{
64 QString newPath = path;
65 if (newPath.isEmpty()) {
66 newPath = VoskEngineUtils::storageLanguagePath();
67 }
68 const QDir dir(newPath);
71 // qCDebug(LIBVOSKSPEECHTOTEXT_LOG) << " list " << list;
72 for (const auto &name : list) {
73 // qCDebug(LIBVOSKSPEECHTOTEXT_LOG) << " name " << dir;
74 const QString modelLanguagePath{dir.absolutePath() + QLatin1Char('/') + name};
75 const VoskEngineUtils::LanguageInstalled info = loadInstalledLanguageInfo(modelLanguagePath);
76 if (info.isValid()) {
77 languages.append(info);
78 }
79 }
80 return languages;
81}
82
83QDebug operator<<(QDebug d, const VoskEngineUtils::LanguageInstalled &t)
84{
85 d << " shortName " << t.name;
86 d << " absoluteLanguageModelPath " << t.absoluteLanguageModelPath;
87 d << " version " << t.versionStr;
88 d << " url " << t.url;
89 return d;
90}
91
92bool VoskEngineUtils::LanguageInstalled::operator==(const LanguageInstalled &other) const
93{
94 return name == other.name && absoluteLanguageModelPath == other.absoluteLanguageModelPath && versionStr == other.versionStr && url == other.url;
95}
96
97bool VoskEngineUtils::LanguageInstalled::isValid() const
98{
99 return !absoluteLanguageModelPath.isEmpty();
100}
101
102void VoskEngineUtils::saveSettings()
103{
104 // TODO
105}
106
107void VoskEngineUtils::loadSettings()
108{
109 // TODO
110}
111
112QString VoskEngineUtils::defaultLanguage()
113{
114 // TODO
115 return {};
116}
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
QString name(GameStandardAction id)
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
KEDUVOCDOCUMENT_EXPORT QStringList languages()
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
void setObject(const QJsonObject &object)
QByteArray toJson(JsonFormat format) const const
void append(QList< T > &&value)
QString writableLocation(StandardLocation type)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.