-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictloader_edict.cpp
109 lines (79 loc) · 2.83 KB
/
dictloader_edict.cpp
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
#include "dictloader_edict.h"
#include <QFile>
#include <QRegExp>
#include <QTextStream>
DictLoaderEdict::DictLoaderEdict()
{
int simpCharPos = 2; // the position of the character definition in a dic line, default: simplified
int tradCharPos = 0; // trad chars are at the beginning
// file 12mb to big for embedding into resource?
{
QFile file(":cedict_ts.u8");
if(!file.open(QFile::ReadOnly))
{
qDebug("Error in AdjacentCharacters::loadData() : could not open file");
}
QTextStream in(&file);
in.setCodec("UTF-8");
//鬧 闹 [nao4] /lärmend, auslassen, an etw. leiden (u.E.) (V)/
while (!in.atEnd())
{
// for other uses can also be split with regexp for [ ] chars and QString::split
QString line = in.readLine();
if(line.at(1) != QChar(' ')) // we are only interested in single char definitions
continue;
QRegExp removePar(QRegExp::escape("(") + "[^)]*" + QRegExp::escape(")"));
QString definition = line.right(line.size()-5).remove(removePar).remove("]").replace("/",", ").simplified();
QChar simpChar = line.at(simpCharPos);
QChar tradChar = line.at(tradCharPos);
addChar(&charDefSimp, simpChar, definition);
addChar(&charDefTrad,tradChar,definition);
if(!sTMap.contains(simpChar,tradChar))
{
sTMap.insert(simpChar,tradChar);
}
}
}
}
/*static*/ void DictLoaderEdict::addChar(QHash<QString, QString> *charDef, QString chineseChar, QString definition)
{
if(charDef->contains(chineseChar))
{
QString val = charDef->value(chineseChar);
charDef->insert(chineseChar,val + " " + definition); // append multiple definitions
}
else
{
charDef->insert(chineseChar,definition);
}
}
QMultiHash<QChar, QChar> DictLoaderEdict::getSTMap() const
{
return sTMap;
}
QChar DictLoaderEdict::s2T(QChar simpChar)
{
if(getSTMap().contains(simpChar)) // contains key (simp char)
{
return getSTMap().value(simpChar);
}
//else if(getSTMap().values().contains(simpChar)) // not required
// is already traditional or unkown
// when unkown return at least any char, to avoid returning an empty string
return simpChar;
}
QChar DictLoaderEdict::t2S(QChar tradChar)
{
// exists at least oney key (simp char), return it
if(!getSTMap().keys(tradChar).isEmpty())
{
return getSTMap().key(tradChar);
}
// either already simplified or unknown
// when unkown return at least any char, to avoid returning an empty string
return tradChar;
}
QHash<QString, QString> DictLoaderEdict::getDictT() const
{
return charDefTrad;
}