Skip to content

Commit

Permalink
增加英英词典
Browse files Browse the repository at this point in the history
  • Loading branch information
yjmthu committed Aug 1, 2023
1 parent d489f7a commit 80065ac
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 13 deletions.
6 changes: 5 additions & 1 deletion plugins/neotranslate_core/include/translate.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct LanPair {
class Translate {
public:
// enum class Lan { AUTO, ZH_CN, ZH_TW, EN_US, JA_JP, FR_LU, RU_RU, MAX };
enum Source { Baidu, Youdao, BingSimple, Iciba, None } m_Source;
enum Source { Baidu, Youdao, BingSimple, Iciba, Dictionary, None } m_Source;
typedef std::vector<std::u8string> LanList;
typedef std::pair<std::u8string, LanList> LanItem;
typedef std::vector<LanItem> LanMap;
Expand Down Expand Up @@ -54,6 +54,8 @@ class Translate {
case Iciba:
GetResultIciba(array);
break;
case Dictionary:
GetResultDictionary(array);
default:
break;
}
Expand All @@ -77,9 +79,11 @@ class Translate {
void GetResultYoudao(const Utf8Array& text);
void GetResultBingSimple(const Utf8Array& text);
void GetResultIciba(const Utf8Array& text);
void GetResultDictionary(const Utf8Array& text);

void FormatYoudaoResult(const class YJson& data);
void FormatIcibaResult(const class YJson& data);
void FormatDictionaryResult(const class YJson& data);
};

#endif // TRANSLATE_H
105 changes: 103 additions & 2 deletions plugins/neotranslate_core/src/translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ const Translate::LanMaps Translate::m_LanguageCanFromTo
Translate::LanMap {
{u8"auto", {u8"auto"}}
},
Translate::LanMap {
{u8"en", {u8"en"}}
},
};

LanPair::LanPair(const YJson::ArrayType& array)
Expand All @@ -160,6 +163,7 @@ Translate::Translate(TranslateCfg& setting, Translate::Callback&& callback)
setting.GetPairYoudao(),
std::array<int, 2> {0, 0},
std::array<int, 2> {0, 0},
std::array<int, 2> {0, 0},
})
, m_LanPair(nullptr)
{
Expand Down Expand Up @@ -572,7 +576,104 @@ void Translate::FormatIcibaResult(const YJson& data)
#else
auto const view = html.str();
#endif
//auto sss = data.toString();
//m_Callback(sss.data(), sss.size());
m_Callback(view.data(), view.size());
}

void Translate::GetResultDictionary(const Utf8Array& text)
{
auto invalid = u8"只能翻译英文单词!"s;
auto iter = std::find_if(text.begin, text.end, [](char8_t c){
return !std::isalnum(c) && !std::isalpha(c);
});
if (iter != text.end) {
m_Callback(invalid.data(), invalid.size());
return;
}

auto url = u8"https://api.dictionaryapi.dev/api/v2/entries/en/"s;
url.append(text.begin, text.end);

m_Request = std::make_unique<HttpLib>(url, true);

HttpLib::Callback callback = {
.m_FinishCallback = [this](auto message, auto res) {
if (message.empty() && (res->status / 100 == 2)) {
#if 0
m_Callback(res->body.data(), res->body.size());
return;
#else
YJson root(res->body.begin(), res->body.end());
FormatDictionaryResult(root);
#endif
} else {
std::wstring msg = std::format(L"error: {}\ncode:{}", message, res->status);
auto u8msg = Wide2Utf8String(msg);
m_Callback(u8msg.data(), u8msg.size());
}
m_Request = nullptr;
}
};

m_Request->GetAsync(std::move(callback));
}

void Translate::FormatDictionaryResult(const class YJson& data)
{
std::ostringstream html;

for (auto& wordData: data.getArray()) {
auto& word = wordData[u8"word"];
html << "<h3>" << word.getValueString() << "</h3><hr>";
auto& phonetics = wordData[u8"phonetics"];

if (phonetics.isArray() && !phonetics.emptyA()) {
html << "<h4>phonetics</h4><ul><li>";
for (auto& item: phonetics.getArray()) {
auto& text = item[u8"text"].getValueString();
html << text << "; ";
}
html << "</li></ul><hr>";
}

auto& meanings = wordData[u8"meanings"];
if (meanings.isArray() && !meanings.emptyA()) {
html << "<h4>meaning</h4><ul>";
for (auto& item: meanings.getArray()) {
html << "<li>[" << item[u8"partOfSpeech"].getValueString() << "]";
auto& definitions = item[u8"definitions"];
if (definitions.isArray() && !definitions.emptyA()) {
html << "<ol>";
for (auto& mean: definitions.getArray()) {
auto& definition = mean[u8"definition"];
auto& example = mean[u8"example"];
html << "<li>" << definition.getValueString();
if (example.isString()) {
html << "<br><span style=\"color: green;\">e.g.</span> "
<< example.getValueString();
}
html << "</li>";
}
html << "</ol>";
}
html << "</li>";
}
html << "</ul><hr>";
}

auto& sourceUrls = wordData[u8"sourceUrls"];
if (sourceUrls.isArray() && !sourceUrls.emptyA()) {
html << "<h4>sourceUrls</h4><ol>";
for (auto& url: sourceUrls.getArray()) {
html << "<li><a href=\"" << url.getValueString() << "\">"
<< url.getValueString() << "</a></li>";
}
html << "</ul><hr>";
}
}
#ifdef _WIN32
auto const view = html.rdbuf()->view();
#else
auto const view = html.str();
#endif
m_Callback(view.data(), view.size());
}
2 changes: 1 addition & 1 deletion plugins/neotranslateplg/include/heightctrl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HeightCtrl: public QFrame
private:
Translate::Source& m_Source;
class QPlainTextEdit& m_TextFrom;
class QTextEdit& m_TextTo;
class QTextBrowser& m_TextTo;
protected:
static constexpr int m_MaxRatio = 200;
static constexpr int m_MinRatio = 20;
Expand Down
6 changes: 3 additions & 3 deletions plugins/neotranslateplg/include/translatedlg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <widgetbase.hpp>
#include <QPlainTextEdit>
#include <QTextEdit>
#include <QTextBrowser>

#include <translate.h>
#include <pluginobject.h>
Expand All @@ -30,8 +30,8 @@ class NeoTranslateDlg : public WidgetBase
friend class HeightCtrl;
TranslateCfg& m_Settings;
QWidget* m_CenterWidget;
class QPlainTextEdit *m_TextFrom;
class QTextEdit *m_TextTo;
QPlainTextEdit *m_TextFrom;
QTextBrowser *m_TextTo;
class QComboBox *m_BoxFrom, *m_BoxTo;
class Translate* m_Translate;
class HeightCtrl* m_HeightCtrl;
Expand Down
2 changes: 1 addition & 1 deletion plugins/neotranslateplg/src/heightctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <QWheelEvent>
#include <QPlainTextEdit>
#include <QTextEdit>
#include <QTextBrowser>

using namespace std::literals;

Expand Down
2 changes: 1 addition & 1 deletion plugins/neotranslateplg/src/neotranslateplg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ YJson& PluginName::InitSettings(YJson& settings)
}
if (version.getValueInt() == 2) {
version = 3;
settings[u8"HeightRatio"].joinA({180, 180});
settings[u8"HeightRatio"].joinA({180, 180, 180});
}
return settings;
// we may not need to call SaveSettings;
Expand Down
9 changes: 5 additions & 4 deletions plugins/neotranslateplg/src/translatedlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <QComboBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QTextEdit>
#include <QTextBrowser>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QTextBlock>
Expand All @@ -26,7 +26,7 @@ NeoTranslateDlg::NeoTranslateDlg(TranslateCfg& settings)
, m_Settings(settings)
, m_CenterWidget(new QWidget(this))
, m_TextFrom(new QPlainTextEdit(m_CenterWidget))
, m_TextTo(new QTextEdit(m_CenterWidget))
, m_TextTo(new QTextBrowser(m_CenterWidget))
, m_BoxFrom(new QComboBox(m_CenterWidget))
, m_BoxTo(new QComboBox(m_CenterWidget))
, m_Translate(new Translate(m_Settings, [this](const void* data, size_t size){
Expand All @@ -41,6 +41,7 @@ NeoTranslateDlg::NeoTranslateDlg(TranslateCfg& settings)
, m_BoxTransMode(new QComboBox(m_CenterWidget))
{
SetupUi();
m_TextTo->setOpenExternalLinks(true);
m_BtnCopyFrom->setText("复制");
m_BtnCopyTo->setText("复制");
connect(m_BtnCopyFrom, &QPushButton::clicked, this, [this]() {
Expand Down Expand Up @@ -349,7 +350,7 @@ void NeoTranslateDlg::SetupUi()
SetShadowAround(m_CenterWidget);

m_BoxTransMode->addItems({
"百度翻译", "有道翻译", "必应翻译", "词霸翻译"
"百度翻译", "有道翻译", "必应翻译", "词霸翻译", "英文词典"
});

m_TextFrom->setObjectName("neoTextFrom");
Expand Down Expand Up @@ -501,7 +502,7 @@ void NeoTranslateDlg::CreateToRightMenu(QMouseEvent* event)
// m_TextTo->setContextMenuPolicy(Qt::CustomContextMenu);
auto const toMenu = m_TextTo->createStandardContextMenu();
auto const clearAction = toMenu->addAction("Clear");
QObject::connect(clearAction, &QAction::triggered, m_TextTo, &QTextEdit::clear);
QObject::connect(clearAction, &QAction::triggered, m_TextTo, &QTextBrowser::clear);
auto const searchAction = toMenu->addAction("Search");
QObject::connect(searchAction, &QAction::triggered, m_TextTo, [this](){
auto const text = m_TextTo->textCursor().selectedText();
Expand Down

0 comments on commit 80065ac

Please sign in to comment.