Open Chinese Convert
1.1.0
A project for conversion between Traditional and Simplified Chinese
src
DictEntry.hpp
1
/*
2
* Open Chinese Convert
3
*
4
* Copyright 2010-2020 BYVoid <byvoid@byvoid.com>
5
*
6
* Licensed under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
9
*
10
* http://www.apache.org/licenses/LICENSE-2.0
11
*
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
17
*/
18
19
#pragma once
20
21
#include "Common.hpp"
22
#include "Segments.hpp"
23
#include "UTF8Util.hpp"
24
25
namespace
opencc {
30
class
OPENCC_EXPORT
DictEntry
{
31
public
:
32
virtual
~
DictEntry
() {}
33
34
virtual
std::string Key()
const
= 0;
35
36
virtual
vector<std::string> Values()
const
= 0;
37
38
virtual
std::string GetDefault()
const
= 0;
39
40
virtual
size_t
NumValues()
const
= 0;
41
42
virtual
string
ToString()
const
= 0;
43
44
size_t
KeyLength()
const
{
return
Key().length(); }
45
46
bool
operator<(
const
DictEntry
& that)
const
{
return
Key() < that.Key(); }
47
48
bool
operator==(
const
DictEntry
& that)
const
{
return
Key() == that.Key(); }
49
50
static
bool
UPtrLessThan(
const
std::unique_ptr<DictEntry>& a,
51
const
std::unique_ptr<DictEntry>& b) {
52
return
*a < *b;
53
}
54
};
55
56
class
OPENCC_EXPORT
NoValueDictEntry
:
public
DictEntry
{
57
public
:
58
NoValueDictEntry
(
const
string
& _key) : key(_key) {}
59
60
virtual
~
NoValueDictEntry
() {}
61
62
virtual
std::string Key()
const
{
return
key; }
63
64
virtual
vector<std::string> Values()
const
{
return
vector<std::string>(); }
65
66
virtual
std::string GetDefault()
const
{
return
key; }
67
68
virtual
size_t
NumValues()
const
{
return
0; }
69
70
virtual
string
ToString()
const
{
return
key; }
71
72
private
:
73
string
key;
74
};
75
76
class
OPENCC_EXPORT
SingleValueDictEntry
:
public
DictEntry
{
77
public
:
78
virtual
std::string Value()
const
= 0;
79
80
virtual
vector<std::string> Values()
const
{
81
return
vector<std::string>{Value()};
82
}
83
84
virtual
std::string GetDefault()
const
{
return
Value(); }
85
86
virtual
size_t
NumValues()
const
{
return
1; }
87
88
virtual
string
ToString()
const
{
return
string(Key()) +
"\t"
+ Value(); }
89
};
90
91
class
OPENCC_EXPORT
StrSingleValueDictEntry
:
public
SingleValueDictEntry
{
92
public
:
93
StrSingleValueDictEntry
(
const
string
& _key,
const
string
& _value)
94
: key(_key), value(_value) {}
95
96
virtual
~
StrSingleValueDictEntry
() {}
97
98
virtual
std::string Key()
const
{
return
key; }
99
100
virtual
std::string Value()
const
{
return
value; }
101
102
private
:
103
string
key;
104
string
value;
105
};
106
107
class
OPENCC_EXPORT
MultiValueDictEntry
:
public
DictEntry
{
108
public
:
109
virtual
std::string GetDefault()
const
{
110
if
(NumValues() > 0) {
111
return
Values().at(0);
112
}
else
{
113
return
Key();
114
}
115
}
116
117
virtual
string
ToString()
const
;
118
};
119
120
class
OPENCC_EXPORT
StrMultiValueDictEntry
:
public
MultiValueDictEntry
{
121
public
:
122
StrMultiValueDictEntry
(
const
string
& _key,
const
vector<std::string>& _values)
123
: key(_key), values(_values) {}
124
125
virtual
~
StrMultiValueDictEntry
() {}
126
127
virtual
std::string Key()
const
{
return
key; }
128
129
size_t
NumValues()
const
{
return
values.size(); }
130
131
vector<std::string> Values()
const
{
return
values; }
132
133
private
:
134
string
key;
135
vector<string> values;
136
};
137
138
class
OPENCC_EXPORT
DictEntryFactory
{
139
public
:
140
static
DictEntry
* New(
const
string
& key) {
return
new
NoValueDictEntry
(key); }
141
142
static
DictEntry
* New(
const
string
& key,
const
string
& value) {
143
return
new
StrSingleValueDictEntry
(key, value);
144
}
145
146
static
DictEntry
* New(
const
string
& key,
const
vector<string>& values) {
147
if
(values.size() == 0) {
148
return
New(key);
149
}
else
if
(values.size() == 1) {
150
return
New(key, values.front());
151
}
152
return
new
StrMultiValueDictEntry
(key, values);
153
}
154
155
static
DictEntry
* New(
const
DictEntry
* entry) {
156
if
(entry->NumValues() == 0) {
157
return
new
NoValueDictEntry
(entry->Key());
158
}
else
if
(entry->NumValues() == 1) {
159
const
auto
svEntry =
static_cast<
const
SingleValueDictEntry
*
>
(entry);
160
return
new
StrSingleValueDictEntry
(svEntry->Key(), svEntry->Value());
161
}
else
{
162
const
auto
mvEntry =
static_cast<
const
MultiValueDictEntry
*
>
(entry);
163
return
new
StrMultiValueDictEntry
(mvEntry->Key(), mvEntry->Values());
164
}
165
}
166
};
167
}
// namespace opencc
opencc::StrSingleValueDictEntry
Definition:
DictEntry.hpp:91
opencc::SingleValueDictEntry
Definition:
DictEntry.hpp:76
opencc::NoValueDictEntry
Definition:
DictEntry.hpp:56
opencc::DictEntry
Key-values pair entry.
Definition:
DictEntry.hpp:30
opencc::DictEntryFactory
Definition:
DictEntry.hpp:138
opencc::StrMultiValueDictEntry
Definition:
DictEntry.hpp:120
opencc::MultiValueDictEntry
Definition:
DictEntry.hpp:107
Generated on Wed May 13 2020 19:18:49 for Open Chinese Convert by
1.8.18