Skip to content

Commit a72a373

Browse files
committed
added kiwi::IOException & kiwi::FormatException
1 parent 2204b4e commit a72a373

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

Diff for: include/kiwi/Types.h

+12
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ namespace kiwi
8282
using std::runtime_error::runtime_error;
8383
};
8484

85+
class IOException : public Exception
86+
{
87+
public:
88+
using Exception::Exception;
89+
};
90+
91+
class FormatException : public Exception
92+
{
93+
public:
94+
using Exception::Exception;
95+
};
96+
8597
class UnicodeException : public Exception
8698
{
8799
public:

Diff for: src/FileUtils.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace kiwi
2323
}
2424
catch (const ios_base::failure&)
2525
{
26-
throw Exception{ "Cannot open file : " + filePath };
26+
throw IOException{ "Cannot open file : " + filePath };
2727
}
2828
f.exceptions(exc);
2929
return f;
@@ -43,7 +43,7 @@ namespace kiwi
4343
}
4444
catch (const ios_base::failure&)
4545
{
46-
throw Exception{ "Cannot open file : " + filePath };
46+
throw IOException{ "Cannot open file : " + filePath };
4747
}
4848
f.exceptions(exc);
4949
return f;

Diff for: src/Form.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <cassert>
1+
#include <cassert>
22
#include <algorithm>
33
#include <kiwi/Utils.h>
44
#include <kiwi/Form.h>

Diff for: src/KiwiBuilder.cpp

+21-21
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ auto KiwiBuilder::loadMorphemesFromTxt(std::istream& is, Fn&& filter) -> Morphem
142142
auto fields = split(wstr, u'\t');
143143
if (fields.size() < 3)
144144
{
145-
throw Exception{ "wrong line: " + line };
145+
throw FormatException{ "wrong line: " + line };
146146
}
147147

148148
auto form = normalizeHangul(fields[0]);
@@ -180,7 +180,7 @@ auto KiwiBuilder::loadMorphemesFromTxt(std::istream& is, Fn&& filter) -> Morphem
180180
}
181181
else if (f == u"non_vowel")
182182
{
183-
if (cvowel != CondVowel::none) throw Exception{ "wrong line: " + line };
183+
if (cvowel != CondVowel::none) throw FormatException{ "wrong line: " + line };
184184
cvowel = CondVowel::non_vowel;
185185
if (i + 1 < fields.size())
186186
{
@@ -189,7 +189,7 @@ auto KiwiBuilder::loadMorphemesFromTxt(std::istream& is, Fn&& filter) -> Morphem
189189
}
190190
else if (f == u"vocalic")
191191
{
192-
if (cvowel != CondVowel::none) throw Exception{ "wrong line: " + line };
192+
if (cvowel != CondVowel::none) throw FormatException{ "wrong line: " + line };
193193
cvowel = CondVowel::vocalic;
194194
if (i + 1 < fields.size())
195195
{
@@ -198,18 +198,18 @@ auto KiwiBuilder::loadMorphemesFromTxt(std::istream& is, Fn&& filter) -> Morphem
198198
}
199199
else if (f == u"non_adj")
200200
{
201-
if (cpolar != CondPolarity::none) throw Exception{ "wrong line: " + line };
201+
if (cpolar != CondPolarity::none) throw FormatException{ "wrong line: " + line };
202202
cpolar = CondPolarity::non_adj;
203203
}
204204
else if (f.starts_with(u"complex "))
205205
{
206-
if (complex) throw Exception{ "wrong line: " + line };
206+
if (complex) throw FormatException{ "wrong line: " + line };
207207
complex = true;
208208
complexChunks.emplace(make_pair(form, tag), f.substr(8).to_string());
209209
}
210210
else if (f[0] == u'=')
211211
{
212-
if (!origMorphemeOfAlias.empty()) throw Exception{ "wrong line: " + line };
212+
if (!origMorphemeOfAlias.empty()) throw FormatException{ "wrong line: " + line };
213213
if (f[1] == u'=')
214214
{
215215
origMorphemeOfAlias = normalizeHangul(f.substr(2));
@@ -233,7 +233,7 @@ auto KiwiBuilder::loadMorphemesFromTxt(std::istream& is, Fn&& filter) -> Morphem
233233
}
234234
else if (f[0] == u'~')
235235
{
236-
if (!origMorphemeOfAlias.empty()) throw Exception{ "wrong line: " + line };
236+
if (!origMorphemeOfAlias.empty()) throw FormatException{ "wrong line: " + line };
237237
if (f.find('/') == f.npos)
238238
{
239239
origMorphemeOfAlias = normalizeHangul(f.substr(1));
@@ -248,7 +248,7 @@ auto KiwiBuilder::loadMorphemesFromTxt(std::istream& is, Fn&& filter) -> Morphem
248248
}
249249
else if (f[0] == u'-')
250250
{
251-
if (altWeight < 0) throw Exception{ "wrong line: " + line };
251+
if (altWeight < 0) throw FormatException{ "wrong line: " + line };
252252
altWeight = stof(f.begin(), f.end());
253253
}
254254
else if (f[0] == '>')
@@ -270,7 +270,7 @@ auto KiwiBuilder::loadMorphemesFromTxt(std::istream& is, Fn&& filter) -> Morphem
270270
}
271271
else
272272
{
273-
throw Exception{ "wrong line: " + line };
273+
throw FormatException{ "wrong line: " + line };
274274
}
275275
}
276276
}
@@ -306,12 +306,12 @@ auto KiwiBuilder::loadMorphemesFromTxt(std::istream& is, Fn&& filter) -> Morphem
306306

307307
if (it != morphMap.end() && it2 != morphMap.end())
308308
{
309-
throw Exception{ "ambiguous base morpheme: " + utf16To8(p.origForm) + "/" + tagToString(clearIrregular(p.origTag)) };
309+
throw FormatException{ "ambiguous base morpheme: " + utf16To8(p.origForm) + "/" + tagToString(clearIrregular(p.origTag)) };
310310
}
311311
it = (it == morphMap.end()) ? it2 : it;
312312
if (it == morphMap.end())
313313
{
314-
throw Exception{ "cannot find base morpheme: " + utf16To8(p.origForm) + "/" + tagToString(p.origTag) };
314+
throw FormatException{ "cannot find base morpheme: " + utf16To8(p.origForm) + "/" + tagToString(p.origTag) };
315315
}
316316
p.origMorphId = it->second.first;
317317
if (!p.addAlias) continue;
@@ -385,27 +385,27 @@ auto KiwiBuilder::loadMorphemesFromTxt(std::istream& is, Fn&& filter) -> Morphem
385385

386386
if (fd.back().size() != (fd.size() - 1) * 2)
387387
{
388-
throw Exception{ "wrong position information : " + utf16To8(fd[0]) + " " + utf16To8(fd.back())};
388+
throw FormatException{ "wrong position information : " + utf16To8(fd[0]) + " " + utf16To8(fd.back())};
389389
}
390390
auto posMap = normalizeHangulWithPosition(joinHangul(it->first.first)).second;
391391
for (size_t i = 0; i < fd.size() - 1; ++i)
392392
{
393393
auto f = split(fd[i], u'/');
394-
if (f.size() != 2) throw Exception{ "wrong format of morpheme : " + utf16To8(fd[i]) };
394+
if (f.size() != 2) throw FormatException{ "wrong format of morpheme : " + utf16To8(fd[i]) };
395395
auto norm = normalizeHangul(f[0]);
396396
auto tag = toPOSTag(f[1]);
397397
auto it = morphMap.find(make_pair(norm, tag));
398398
if (it == morphMap.end())
399399
{
400-
throw Exception{ "cannot find morpheme : " + utf16To8(fd[i]) };
400+
throw FormatException{ "cannot find morpheme : " + utf16To8(fd[i]) };
401401
}
402402
size_t lmId = it->second.first;
403403
if (!morphemes[lmId].kform)
404404
{
405405
auto it = longTailMap.find(make_pair(norm, tag));
406406
if (it == longTailMap.end())
407407
{
408-
throw Exception{ "cannot find morpheme : " + utf16To8(fd[i]) };
408+
throw FormatException{ "cannot find morpheme : " + utf16To8(fd[i]) };
409409
}
410410
lmId = it->second;
411411
}
@@ -1514,7 +1514,7 @@ size_t KiwiBuilder::loadDictionary(const string& dictPath)
15141514
size_t fieldSize = split(wstr, u'\t', fields.begin(), 2) - fields.begin();
15151515
if (fieldSize < 2)
15161516
{
1517-
throw Exception("[loadUserDictionary] Wrong dictionary format at line " + to_string(lineNo) + " : " + line);
1517+
throw FormatException("[loadUserDictionary] Wrong dictionary format at line " + to_string(lineNo) + " : " + line);
15181518
}
15191519

15201520
while (!fields[0].empty() && fields[0][0] == ' ') fields[0] = fields[0].substr(1);
@@ -1536,26 +1536,26 @@ size_t KiwiBuilder::loadDictionary(const string& dictPath)
15361536
size_t p = m.rfind(u'/');
15371537
if (p == m.npos)
15381538
{
1539-
throw Exception("[loadUserDictionary] Wrong dictionary format at line " + to_string(lineNo) + " : " + line);
1539+
throw FormatException("[loadUserDictionary] Wrong dictionary format at line " + to_string(lineNo) + " : " + line);
15401540
}
15411541
auto pos = toPOSTag(m.substr(p + 1));
15421542
if (pos == POSTag::max)
15431543
{
1544-
throw Exception("[loadUserDictionary] Unknown Tag '" + utf16To8(fields[1]) + "' at line " + to_string(lineNo));
1544+
throw FormatException("[loadUserDictionary] Unknown Tag '" + utf16To8(fields[1]) + "' at line " + to_string(lineNo));
15451545
}
15461546
morphemes.emplace_back(m.substr(0, p), pos);
15471547
}
15481548

15491549
if (fields[0].empty())
15501550
{
1551-
throw Exception("[loadUserDictionary] Wrong dictionary format at line " + to_string(lineNo) + " : " + line);
1551+
throw FormatException("[loadUserDictionary] Wrong dictionary format at line " + to_string(lineNo) + " : " + line);
15521552
}
15531553

15541554
if (fields[0].back() == '$')
15551555
{
15561556
if (morphemes.size() > 1)
15571557
{
1558-
throw Exception("[loadUserDictionary] Replace rule cannot have 2 or more forms '" + utf16To8(fields[1]) + "' at line " + to_string(lineNo));
1558+
throw FormatException("[loadUserDictionary] Replace rule cannot have 2 or more forms '" + utf16To8(fields[1]) + "' at line " + to_string(lineNo));
15591559
}
15601560

15611561
auto suffix = fields[0].substr(0, fields[0].size() - 1);
@@ -1583,7 +1583,7 @@ size_t KiwiBuilder::loadDictionary(const string& dictPath)
15831583
auto pos = toPOSTag(fields[1]);
15841584
if (pos == POSTag::max)
15851585
{
1586-
throw Exception("[loadUserDictionary] Unknown Tag '" + utf16To8(fields[1]) + "' at line " + to_string(lineNo));
1586+
throw FormatException("[loadUserDictionary] Unknown Tag '" + utf16To8(fields[1]) + "' at line " + to_string(lineNo));
15871587
}
15881588
addedCnt += addWord(fields[0], pos, score).second ? 1 : 0;
15891589
}

0 commit comments

Comments
 (0)