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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/*****************************************************************************
* This file is a part of QStarDict, a StarDict clone written using Qt *
* multitran.cpp - Plugin for multitran-data (multitran.sf.net) *
* Copyright (C) 2008 Nick Shaforostoff *
* Copyright (C) 2004 Stanislav Ievlev *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*****************************************************************************/
#include "multitran.h"
//#include "settingsdialog.h"
#include <QCoreApplication>
// #include <QSettings>
#include <QTextCodec>
/*
/usr/include/mt/query
/usr/include/btree
/usr/include/mt/support
/usr/include/facet
*/
#include <facet/identification.hh>
#include <facet/alphabet.hh>
#include <facet/typographic.hh>
#include <mt/query/linguas.hh>
#include <mt/query/translation.hh>
#include <mt/query/singleton.hh>
#include <mt/query/config.hh>
#include <mt/query/file_map.hh>
#include <mt/support/path.hh>
#include <mt/query/lgk.hh>
#include <mt/support/str.hh>
#include <mt/query/lang_info.hh>
#include <algorithm>
#include <functional>
#include <memory>
#include <iostream>
#include <stdexcept>
#include <vector>
#include <string>
typedef mt::singleton_array<mt::file_map> txtdb_type;
struct compare_names
{
compare_names(const std::string& from,const std::string& to):
from_(from),
to_(to)
{}
bool operator()(const mt::lang_pair& lng1,const mt::lang_pair& lng2)
{
return distance(lng1) < distance(lng2);
}
int distance(const mt::lang_pair& lng)
{
std::string from_name=mt::lang_name(lng.first);
std::string to_name=mt::lang_name(lng.second);
return (!from_.empty() && !from_name.compare(0,from_.size(),from_)) +
(!to_.empty() && !to_name.compare(0,to_.size(),to_));
}
std::string from_,to_;
};
int compare_articles(const mt::article& a1,const mt::article& a2)
{
if (a1.lgk() != a2.lgk())
return a2.lgk() > a1.lgk();
else
return a2.subject() > a1.subject();
}
struct show
{
show(std::string& r_, bool& found_): r(r_),found(found_) {}
void operator()(mt::article_set as)
{
mt::file_map& subj = txtdb_type::instance(mt::datapath+mt::path_separator()+"subjects.txt");
mt::file_map& spart = txtdb_type::instance(mt::datapath+mt::path_separator()+"speechparts.txt");
if (!as.articles_.empty())
{
found=true;
std::sort(as.articles_.begin(),as.articles_.end(),compare_articles);
int prev_lgk = -1;
std::string prev_subject = "x";
for(size_t i=0;i<as.articles_.size();++i)
{
const mt::article& a = as.articles_[i];
if (prev_lgk != a.lgk())
{
r+="<tr><td><b>"+a.orig()+","+
spart.any_name(mt::to_string<int>(mt::speech_part(a.lgk())))+"</b></td></tr>";
prev_lgk = a.lgk();
prev_subject = "x";//reset subject
}
if (prev_subject != a.subject())
{
r+="<tr><td></td><td><font class=\"explanation\">";
r+=subj.any_name(a.subject());
r+="</font></td><td>";
r+=a.translated();
prev_subject = a.subject();
}
else
r+=", "+a.translated();
}
r+="</td></tr>";
}
}
std::string &r;
bool& found;
};
std::string do_translate(const std::string& text,mt::lang_code from,mt::lang_code to)
{
bool found=false;
std::string r="<table>";
mt::phrase ph;
mt::fill_phrase(ph,text,from);
mt::translation tr(ph,from,to);
std::for_each(tr.asets().begin(), tr.asets().end(), show(r,found));
r+="</table>";
if (found)
return r;
return "";
}
Multitran::Multitran(QObject *parent)
: QObject(parent)
{
// QSettings settings("qstardict","qstardict");
// m_dictDirs = settings.value("Multitran/dictDirs", m_dictDirs).toStringList();
// m_reformatLists = settings.value("Multitran/reformatLists", true).toBool();
}
Multitran::~Multitran()
{
// QSettings settings("qstardict","qstardict");
// settings.setValue("Multitran/dictDirs", m_dictDirs);
// settings.setValue("Multitran/reformatLists", m_reformatLists);
}
QStringList Multitran::availableDicts() const
{
return QStringList("Multitran");
}
void Multitran::setLoadedDicts(const QStringList &loadedDicts)
{
}
Multitran::DictInfo Multitran::dictInfo(const QString &dict)
{
// ::DictInfo nativeInfo;
// nativeInfo.wordcount = 0;
DictInfo result(name(), dict);
result.setAuthor("Multitran.ru");
result.setDescription(tr("1 mln words excerpt of multitran.ru"));
result.setWordsCount(-1);
return result;
}
bool Multitran::isTranslatable(const QString &dict, const QString &word)
{
return true;
}
Multitran::Translation Multitran::translate(const QString &dict, const QString &word)
{
QTextCodec* c=QTextCodec::codecForMib(2251);
std::string text=c->fromUnicode(word).data();
std::string from_lang,to_lang;
int i=word.size();
while(--i>=0)
if (word.at(i).unicode()>127)
break;
if (i!=-1)
from_lang="russian";
else
from_lang="english";
mt::linguas avail_langs;
mt::linguas::iterator lang = std::max_element(avail_langs.begin(),
avail_langs.end(),
compare_names(from_lang,to_lang));
if (lang == avail_langs.end() ||
(!from_lang.empty() && !to_lang.empty() && (compare_names(from_lang,to_lang).distance(*lang)!=2)))
{
//std::cerr<<"illegal language names"<<std::endl;
return Translation();
}
//"<hr width=50%><center><b>multitran</b><center><hr width=50%>";
QString queryResult=c->toUnicode(do_translate(lower_str(lang->first,text),
lang->first,lang->second).c_str());
if (queryResult.isEmpty())
return Translation();
return Translation(word,"Multitran",queryResult);
}
QStringList Multitran::findSimilarWords(const QString &dict, const QString &word)
{
return QStringList();
}
int Multitran::execSettingsDialog(QWidget *parent)
{
//::SettingsDialog dialog(this, parent);
//return dialog.exec();
return 0;
}
#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2(multitran, Multitran)
#endif
// vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
|