forked from terslang/LocalTranslate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (113 loc) · 4.33 KB
/
main.cpp
File metadata and controls
140 lines (113 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <QDebug>
#include <QDir>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QStandardPaths>
#include <kotki/kotki.h>
#include <mecab.h>
#include <memory>
#include <unicode/translit.h>
#include <unicode/unistr.h>
class TranslationBridge : public QObject
{
Q_OBJECT
public:
explicit TranslationBridge(QObject *parent = nullptr) : QObject(parent) {
kotki = std::make_unique<Kotki>();
QString registryFile = QStandardPaths::locate(
QStandardPaths::GenericDataLocation,
QStringLiteral("localtranslate/models/firefox/registry.json")
);
if (!QFileInfo::exists(registryFile)) {
throw std::runtime_error(QString("Registry file does not exist: %1").arg(registryFile).toStdString());
}
kotki->scan(std::filesystem::path(registryFile.toStdString()));
}
Q_INVOKABLE QString translate(const QString &text, const QString &langPair) const
{
std::string result = kotki->translate(text.toStdString(), langPair.toStdString());
if(result.empty())
return "Language pair \"" + langPair + "\" not available.";
return QString::fromStdString(result);
}
Q_INVOKABLE QString transliterate(const QString &text, const QString &langCode) const
{
std::string input;
// if input text is in japanese, first convert to katakana and then transliterate
if (langCode == "ja") {
input = kanjiToKatakana(text.toStdString());
} else {
input = text.toStdString();
}
UErrorCode status = U_ZERO_ERROR;
std::unique_ptr<icu::Transliterator> trans(icu::Transliterator::createInstance("Any-Latin; Latin-ASCII", UTRANS_FORWARD, status));
if (U_FAILURE(status) || !trans) {
qWarning() << "Failed to create transliterator:" << u_errorName(status);
return "";
}
icu::UnicodeString uText = icu::UnicodeString::fromUTF8(input);
trans->transliterate(uText);
std::string output;
uText.toUTF8String(output);
return QString::fromStdString(output);
}
private:
std::unique_ptr<Kotki> kotki = nullptr;
std::string kanjiToKatakana(const std::string &text) const
{
std::unique_ptr<MeCab::Tagger> tagger(MeCab::createTagger(""));
if (!tagger) {
throw std::runtime_error("Failed to create MeCab tagger!");
}
std::istringstream input_stream(text);
std::string line;
std::ostringstream katakana_stream;
bool first_line = true;
while (std::getline(input_stream, line)) {
if (!first_line) {
katakana_stream << "\n";
}
first_line = false;
const MeCab::Node *node = tagger->parseToNode(line.c_str());
for (; node; node = node->next) {
if (node->feature) {
std::string feature(node->feature);
std::vector<std::string> parts;
std::istringstream ss(feature);
std::string token;
while (std::getline(ss, token, ',')) {
parts.push_back(token);
}
if (parts.size() > 8 && parts[8] != "*") {
katakana_stream << parts[8] << " ";
}
}
}
}
return katakana_stream.str();
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QGuiApplication::setDesktopFileName(
"dev.ters.LocalTranslate"); // specify name of the desktop file
// Specify org details
QCoreApplication::setOrganizationName("ters");
QCoreApplication::setOrganizationDomain("ters.dev");
QCoreApplication::setApplicationName("LocalTranslate");
TranslationBridge bridge;
QQmlApplicationEngine engine;
// Expose the bridge to QML
engine.rootContext()->setContextProperty("translationBridge", &bridge);
QObject::connect(
&engine,
&QQmlApplicationEngine::objectCreationFailed,
&app,
[]() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.loadFromModule("LocalTranslate", "Main");
return app.exec();
}