forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransliteration.cc
143 lines (110 loc) · 4.01 KB
/
transliteration.cc
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
/* This file is (c) 2008-2012 Konstantin Isakov <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "transliteration.hh"
#include "utf8.hh"
#include "folding.hh"
#include "gddebug.hh"
namespace Transliteration {
using gd::wchar;
BaseTransliterationDictionary::BaseTransliterationDictionary( string const & id,
string const & name_,
QIcon icon_,
bool caseSensitive_ ):
Dictionary::Class( id, vector< string >() ),
name( name_ ),
caseSensitive( caseSensitive_ )
{
dictionaryIcon = dictionaryNativeIcon = icon_;
dictionaryIconLoaded = true;
}
string BaseTransliterationDictionary::getName() throw()
{ return name; }
map< Dictionary::Property, string > BaseTransliterationDictionary::getProperties() throw()
{ return map< Dictionary::Property, string >(); }
unsigned long BaseTransliterationDictionary::getArticleCount() throw()
{ return 0; }
unsigned long BaseTransliterationDictionary::getWordCount() throw()
{ return 0; }
sptr< Dictionary::WordSearchRequest > BaseTransliterationDictionary::prefixMatch( wstring const &,
unsigned long ) throw( std::exception )
{ return new Dictionary::WordSearchRequestInstant(); }
sptr< Dictionary::DataRequest > BaseTransliterationDictionary::getArticle( wstring const &,
vector< wstring > const &,
wstring const & )
throw( std::exception )
{ return new Dictionary::DataRequestInstant( false ); }
sptr< Dictionary::WordSearchRequest > BaseTransliterationDictionary::findHeadwordsForSynonym( wstring const & str )
throw( std::exception )
{
sptr< Dictionary::WordSearchRequestInstant > result = new Dictionary::WordSearchRequestInstant();
vector< wstring > alts = getAlternateWritings( str );
GD_DPRINTF( "alts = %u\n", (unsigned) alts.size() );
for( unsigned x = 0; x < alts.size(); ++x )
result->getMatches().push_back( alts[ x ] );
return result;
}
void Table::ins( char const * from, char const * to )
{
wstring fr = Utf8::decode( std::string( from ) );
if ( fr.size() > maxEntrySize )
maxEntrySize = fr.size();
insert( std::pair< wstring, wstring >( fr,
Utf8::decode( std::string( to ) ) ) );
}
TransliterationDictionary::TransliterationDictionary( string const & id,
string const & name_,
QIcon icon_,
Table const & table_,
bool caseSensitive_ ):
BaseTransliterationDictionary(id, name_, icon_, caseSensitive_),
table( table_ )
{
}
vector< wstring > TransliterationDictionary::getAlternateWritings( wstring const & str )
throw()
{
vector< wstring > results;
wstring result, folded;
wstring const * target;
if ( caseSensitive )
{
// Don't do any transform -- the transliteration is case-sensitive
target = &str;
}
else
{
folded = Folding::applySimpleCaseOnly( str );
target = &folded;
}
wchar const * ptr = target->c_str();
size_t left = target->size();
Table::const_iterator i;
while( left )
{
unsigned x;
for( x = table.getMaxEntrySize(); x >= 1; --x )
{
if ( left >= x )
{
i = table.find( wstring( ptr, x ) );
if ( i != table.end() )
{
result.append( i->second );
ptr += x;
left -= x;
break;
}
}
}
if ( !x )
{
// No matches -- add this char as it is
result.push_back( *ptr++ );
--left;
}
}
if ( result != *target )
results.push_back( result );
return results;
}
}