-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMozcSystemLexicon.cpp
More file actions
1004 lines (845 loc) · 26.3 KB
/
Copy pathMozcSystemLexicon.cpp
File metadata and controls
1004 lines (845 loc) · 26.3 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "MozcSystemLexicon.h"
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
#include <algorithm>
#include <charconv>
#include <cstdint>
#include <fstream>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
namespace
{
std::wstring Utf8ToWide(const std::string& value)
{
if (value.empty())
{
return L"";
}
const int required = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value.data(), static_cast<int>(value.size()), nullptr, 0);
if (required <= 0)
{
throw std::runtime_error("MozcSystemLexicon: invalid UTF-8 input");
}
std::wstring result(static_cast<size_t>(required), L'\0');
const int converted = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value.data(), static_cast<int>(value.size()), &result[0], required);
if (converted != required)
{
throw std::runtime_error("MozcSystemLexicon: failed UTF-8 conversion");
}
return result;
}
std::wstring U16ToWide(const std::u16string& value)
{
std::wstring result;
result.reserve(value.size());
for (char16_t ch : value)
{
result.push_back(static_cast<wchar_t>(ch));
}
return result;
}
std::u16string WideToU16(const std::wstring& value)
{
std::u16string result;
result.reserve(value.size());
for (wchar_t ch : value)
{
result.push_back(static_cast<char16_t>(ch));
}
return result;
}
std::wstring HiraganaToKatakana(const std::wstring& value)
{
std::wstring result;
result.reserve(value.size());
for (wchar_t ch : value)
{
if ((ch >= 0x3041 && ch <= 0x3096) || (ch >= 0x309D && ch <= 0x309F))
{
result.push_back(static_cast<wchar_t>(ch + 0x60));
}
else
{
result.push_back(ch);
}
}
return result;
}
bool SplitFiveColumns(
std::string_view line,
std::string_view* c0,
std::string_view* c1,
std::string_view* c2,
std::string_view* c3,
std::string_view* c4)
{
const size_t p0 = line.find('\t');
if (p0 == std::string_view::npos)
{
return false;
}
const size_t p1 = line.find('\t', p0 + 1);
if (p1 == std::string_view::npos)
{
return false;
}
const size_t p2 = line.find('\t', p1 + 1);
if (p2 == std::string_view::npos)
{
return false;
}
const size_t p3 = line.find('\t', p2 + 1);
if (p3 == std::string_view::npos)
{
return false;
}
*c0 = line.substr(0, p0);
*c1 = line.substr(p0 + 1, p1 - (p0 + 1));
*c2 = line.substr(p1 + 1, p2 - (p1 + 1));
*c3 = line.substr(p2 + 1, p3 - (p2 + 1));
*c4 = line.substr(p3 + 1);
return true;
}
int ParseIntField(std::string_view field, const char* name)
{
if (field.empty())
{
throw std::runtime_error(std::string("MozcSystemLexicon: empty int field: ") + name);
}
int value = 0;
const auto parsed = std::from_chars(field.data(), field.data() + field.size(), value);
if (parsed.ec != std::errc() || parsed.ptr != field.data() + field.size())
{
throw std::runtime_error(std::string("MozcSystemLexicon: invalid int field: ") + name);
}
return value;
}
bool IsCommentOrEmpty(const std::string& line)
{
if (line.empty())
{
return true;
}
if (line[0] == '#')
{
return true;
}
if (line.size() >= 3 &&
static_cast<unsigned char>(line[0]) == 0xEF &&
static_cast<unsigned char>(line[1]) == 0xBB &&
static_cast<unsigned char>(line[2]) == 0xBF)
{
return line.size() == 3 || line[3] == '#';
}
return false;
}
bool PathExists(const std::filesystem::path& path)
{
std::error_code ec;
return std::filesystem::exists(path, ec) && !ec;
}
bool IsDirectory(const std::filesystem::path& path)
{
std::error_code ec;
return std::filesystem::is_directory(path, ec) && !ec;
}
std::filesystem::path NormalizePath(const std::filesystem::path& path)
{
std::error_code ec;
const std::filesystem::path normalized = std::filesystem::weakly_canonical(path, ec);
if (ec)
{
return path.lexically_normal();
}
return normalized;
}
class BitVector
{
public:
size_t size() const
{
return _nbits;
}
bool get(size_t index) const
{
if (index >= _nbits)
{
return false;
}
return ((_words[index >> 6] >> (index & 63)) & 1ULL) != 0;
}
int rank0(int index) const
{
if (_nbits == 0 || index < 0)
{
return 0;
}
size_t current = static_cast<size_t>(index);
if (current >= _nbits)
{
current = _nbits - 1;
}
return static_cast<int>(current + 1) - rank1Internal(current);
}
int rank1(int index) const
{
if (_nbits == 0 || index < 0)
{
return 0;
}
size_t current = static_cast<size_t>(index);
if (current >= _nbits)
{
current = _nbits - 1;
}
return rank1Internal(current);
}
int select0(int ordinal) const
{
return selectInternal(false, ordinal);
}
int select1(int ordinal) const
{
return selectInternal(true, ordinal);
}
void assignFromWords(size_t nbits, std::vector<std::uint64_t> words)
{
_nbits = nbits;
_words = std::move(words);
if (((_nbits + 63) / 64) != _words.size())
{
throw std::runtime_error("MozcSystemLexicon: bit vector size mismatch");
}
}
private:
static int Popcount64(std::uint64_t value)
{
int count = 0;
while (value != 0)
{
value &= (value - 1);
++count;
}
return count;
}
int rank1Internal(size_t index) const
{
const size_t fullWords = index >> 6;
const size_t bitInWord = index & 63;
int count = 0;
for (size_t word = 0; word < fullWords; ++word)
{
count += Popcount64(_words[word]);
}
const std::uint64_t mask = (bitInWord == 63) ? ~0ULL : ((1ULL << (bitInWord + 1)) - 1ULL);
count += Popcount64(_words[fullWords] & mask);
return count;
}
int selectInternal(bool value, int ordinal) const
{
if (ordinal <= 0)
{
return -1;
}
int count = 0;
for (size_t index = 0; index < _nbits; ++index)
{
if (get(index) == value)
{
++count;
if (count == ordinal)
{
return static_cast<int>(index);
}
}
}
return -1;
}
size_t _nbits = 0;
std::vector<std::uint64_t> _words;
};
void ReadU64(std::istream& input, std::uint64_t* value)
{
input.read(reinterpret_cast<char*>(value), sizeof(*value));
}
void ReadU32(std::istream& input, std::uint32_t* value)
{
input.read(reinterpret_cast<char*>(value), sizeof(*value));
}
void ReadI32(std::istream& input, std::int32_t* value)
{
input.read(reinterpret_cast<char*>(value), sizeof(*value));
}
void ReadU16(std::istream& input, std::uint16_t* value)
{
input.read(reinterpret_cast<char*>(value), sizeof(*value));
}
BitVector ReadBitVector(std::istream& input)
{
std::uint64_t nbits = 0;
ReadU64(input, &nbits);
std::uint64_t wordCount = 0;
ReadU64(input, &wordCount);
std::vector<std::uint64_t> words(static_cast<size_t>(wordCount));
if (wordCount > 0)
{
input.read(reinterpret_cast<char*>(words.data()), static_cast<std::streamsize>(wordCount * sizeof(std::uint64_t)));
}
BitVector bitVector;
bitVector.assignFromWords(static_cast<size_t>(nbits), std::move(words));
return bitVector;
}
struct TokenEntry
{
std::uint16_t posIndex = 0;
std::int16_t wordCost = 0;
std::int32_t nodeIndex = 0;
};
class TokenArray
{
public:
static constexpr std::int32_t kKatakanaSentinel = -1;
static constexpr std::int32_t kHiraganaSentinel = -2;
static TokenArray LoadFromFile(const std::filesystem::path& path)
{
std::ifstream input(path, std::ios::binary);
if (!input)
{
throw std::runtime_error("MozcSystemLexicon: failed to open token array");
}
TokenArray result;
std::uint32_t count = 0;
ReadU32(input, &count);
result._posIndex.resize(count);
if (count > 0)
{
input.read(reinterpret_cast<char*>(result._posIndex.data()), static_cast<std::streamsize>(count * sizeof(std::uint16_t)));
}
ReadU32(input, &count);
result._wordCost.resize(count);
if (count > 0)
{
input.read(reinterpret_cast<char*>(result._wordCost.data()), static_cast<std::streamsize>(count * sizeof(std::int16_t)));
}
ReadU32(input, &count);
result._nodeIndex.resize(count);
if (count > 0)
{
input.read(reinterpret_cast<char*>(result._nodeIndex.data()), static_cast<std::streamsize>(count * sizeof(std::int32_t)));
}
result._postingsBits = ReadBitVector(input);
return result;
}
std::vector<TokenEntry> GetTokensForTermId(std::int32_t termId) const
{
const int p0 = _postingsBits.select0(termId + 1);
const int p1 = _postingsBits.select0(termId + 2);
if (p0 < 0 || p1 < 0)
{
return {};
}
const int begin = _postingsBits.rank1(p0);
const int end = _postingsBits.rank1(p1);
std::vector<TokenEntry> result;
result.reserve(static_cast<size_t>(std::max(0, end - begin)));
for (int index = begin; index < end; ++index)
{
result.push_back(TokenEntry{
_posIndex[static_cast<size_t>(index)],
_wordCost[static_cast<size_t>(index)],
_nodeIndex[static_cast<size_t>(index)]});
}
return result;
}
private:
std::vector<std::uint16_t> _posIndex;
std::vector<std::int16_t> _wordCost;
std::vector<std::int32_t> _nodeIndex;
BitVector _postingsBits;
};
struct PosTable
{
static PosTable LoadFromFile(const std::filesystem::path& path)
{
std::ifstream input(path, std::ios::binary);
if (!input)
{
throw std::runtime_error("MozcSystemLexicon: failed to open pos table");
}
PosTable result;
std::uint32_t count = 0;
ReadU32(input, &count);
result.leftIds.resize(count);
result.rightIds.resize(count);
if (count > 0)
{
input.read(reinterpret_cast<char*>(result.leftIds.data()), static_cast<std::streamsize>(count * sizeof(std::int16_t)));
input.read(reinterpret_cast<char*>(result.rightIds.data()), static_cast<std::streamsize>(count * sizeof(std::int16_t)));
}
return result;
}
std::pair<std::int16_t, std::int16_t> GetLR(std::uint16_t posIndex) const
{
const size_t index = static_cast<size_t>(posIndex);
if (index >= leftIds.size() || index >= rightIds.size())
{
return {0, 0};
}
return {leftIds[index], rightIds[index]};
}
std::vector<std::int16_t> leftIds;
std::vector<std::int16_t> rightIds;
};
struct LoudsWithTermIdData
{
static LoudsWithTermIdData LoadFromFile(const std::filesystem::path& path)
{
std::ifstream input(path, std::ios::binary);
if (!input)
{
throw std::runtime_error("MozcSystemLexicon: failed to open yomi_termid");
}
LoudsWithTermIdData result;
result.lbs = ReadBitVector(input);
result.isLeaf = ReadBitVector(input);
std::uint64_t labelCount = 0;
ReadU64(input, &labelCount);
result.labels.resize(static_cast<size_t>(labelCount));
for (size_t i = 0; i < static_cast<size_t>(labelCount); ++i)
{
std::uint16_t value = 0;
ReadU16(input, &value);
result.labels[i] = static_cast<char16_t>(value);
}
std::uint64_t termCount = 0;
ReadU64(input, &termCount);
result.termIdByNodeId.resize(static_cast<size_t>(termCount));
for (size_t i = 0; i < static_cast<size_t>(termCount); ++i)
{
ReadI32(input, &result.termIdByNodeId[i]);
}
return result;
}
BitVector lbs;
BitVector isLeaf;
std::vector<char16_t> labels;
std::vector<std::int32_t> termIdByNodeId;
};
class LoudsWithTermIdReader
{
public:
explicit LoudsWithTermIdReader(const LoudsWithTermIdData& trie)
: _lbs(trie.lbs),
_labels(trie.labels),
_termIds(trie.termIdByNodeId)
{
_zeroPositions.push_back(-1);
for (int index = 0; index < static_cast<int>(_lbs.size()); ++index)
{
if (!_lbs.get(static_cast<size_t>(index)))
{
_zeroPositions.push_back(index);
}
}
}
std::vector<std::pair<std::u16string, std::int32_t>> CommonPrefixSearch(const std::u16string& key) const
{
std::vector<std::pair<std::u16string, std::int32_t>> result;
std::u16string current;
int position = 0;
for (char16_t ch : key)
{
position = Traverse(position, ch);
if (position < 0)
{
break;
}
current.push_back(ch);
const int nodeId = NodeIdFromPos(position);
if (nodeId >= 0 && nodeId < static_cast<int>(_termIds.size()))
{
const std::int32_t termId = _termIds[static_cast<size_t>(nodeId)];
if (termId >= 0)
{
result.push_back({current, termId});
}
}
}
return result;
}
private:
int FirstChild(int position) const
{
const int rank = _lbs.rank1(position);
if (rank <= 0 || rank >= static_cast<int>(_zeroPositions.size()))
{
return -1;
}
return _zeroPositions[static_cast<size_t>(rank)] + 1;
}
int Traverse(int position, char16_t target) const
{
int child = FirstChild(position);
if (child < 0)
{
return -1;
}
while (child < static_cast<int>(_lbs.size()) && _lbs.get(static_cast<size_t>(child)))
{
const int labelIndex = _lbs.rank1(child);
if (labelIndex >= 0 &&
labelIndex < static_cast<int>(_labels.size()) &&
_labels[static_cast<size_t>(labelIndex)] == target)
{
return child;
}
++child;
}
return -1;
}
int NodeIdFromPos(int position) const
{
return _lbs.rank1(position) - 1;
}
const BitVector& _lbs;
const std::vector<char16_t>& _labels;
const std::vector<std::int32_t>& _termIds;
std::vector<int> _zeroPositions;
};
class LoudsReaderUtf16
{
public:
static LoudsReaderUtf16 LoadFromFile(const std::filesystem::path& path)
{
std::ifstream input(path, std::ios::binary);
if (!input)
{
throw std::runtime_error("MozcSystemLexicon: failed to open tango louds");
}
LoudsReaderUtf16 result;
result._lbs = ReadBitVector(input);
result._isLeaf = ReadBitVector(input);
std::uint64_t labelCount = 0;
ReadU64(input, &labelCount);
result._labels.resize(static_cast<size_t>(labelCount));
for (size_t i = 0; i < static_cast<size_t>(labelCount); ++i)
{
std::uint16_t value = 0;
ReadU16(input, &value);
result._labels[i] = static_cast<char16_t>(value);
}
return result;
}
std::u16string GetLetter(std::int32_t nodeIndex) const
{
if (nodeIndex < 0 || static_cast<size_t>(nodeIndex) >= _lbs.size())
{
return u"";
}
std::u16string result;
int current = nodeIndex;
while (true)
{
const int nodeId = _lbs.rank1(current);
if (nodeId < 0 || nodeId >= static_cast<int>(_labels.size()))
{
break;
}
const char16_t ch = _labels[static_cast<size_t>(nodeId)];
if (ch != u' ')
{
result.push_back(ch);
}
if (nodeId == 0)
{
break;
}
const int rank0 = _lbs.rank0(current);
current = _lbs.select1(rank0);
if (current < 0)
{
break;
}
}
std::reverse(result.begin(), result.end());
return result;
}
private:
BitVector _lbs;
BitVector _isLeaf;
std::vector<char16_t> _labels;
};
}
struct MozcSystemLexicon::Impl
{
struct BinaryDictionary
{
LoudsWithTermIdData yomiTermData;
LoudsWithTermIdReader yomiTermReader;
LoudsReaderUtf16 tangoReader;
TokenArray tokenArray;
PosTable posTable;
mutable std::unordered_map<std::wstring, std::vector<LexiconEntry>> cache;
BinaryDictionary(
LoudsWithTermIdData data,
LoudsReaderUtf16 tango,
TokenArray tokens,
PosTable pos)
: yomiTermData(std::move(data)),
yomiTermReader(yomiTermData),
tangoReader(std::move(tango)),
tokenArray(std::move(tokens)),
posTable(std::move(pos))
{
}
};
std::unordered_map<std::wstring, std::vector<LexiconEntry>> entriesByReading;
std::vector<size_t> readingLengths;
std::filesystem::path sourceDirectory;
std::unique_ptr<BinaryDictionary> binary;
bool loaded = false;
};
MozcSystemLexicon::MozcSystemLexicon(const std::filesystem::path& directory)
: _impl(std::make_unique<Impl>())
{
LoadDirectory(directory);
}
DictionaryKind MozcSystemLexicon::GetKind() const
{
return DictionaryKind::System;
}
void MozcSystemLexicon::LookupPrefix(const std::wstring& reading, size_t start, std::vector<LexiconEntry>* out) const
{
if (out == nullptr)
{
return;
}
out->clear();
if (!_impl->loaded || start >= reading.size())
{
return;
}
if (_impl->binary)
{
const std::u16string remaining = WideToU16(reading.substr(start));
const auto matches = _impl->binary->yomiTermReader.CommonPrefixSearch(remaining);
for (const auto& match : matches)
{
const std::wstring key = U16ToWide(match.first);
auto cached = _impl->binary->cache.find(key);
if (cached == _impl->binary->cache.end())
{
std::vector<LexiconEntry> entries;
const std::vector<TokenEntry> tokens = _impl->binary->tokenArray.GetTokensForTermId(match.second);
entries.reserve(tokens.size());
for (size_t index = 0; index < tokens.size(); ++index)
{
const TokenEntry& token = tokens[index];
LexiconEntry entry;
entry.reading = key;
entry.wordCost = token.wordCost;
entry.posId = token.posIndex;
const auto lr = _impl->binary->posTable.GetLR(token.posIndex);
entry.leftId = lr.first;
entry.rightId = lr.second;
entry.dictionaryKind = DictionaryKind::System;
entry.sourceEntryId = static_cast<std::uint32_t>((static_cast<std::uint64_t>(match.second) << 16) | static_cast<std::uint64_t>(index));
if (token.nodeIndex == TokenArray::kHiraganaSentinel)
{
entry.surface = key;
}
else if (token.nodeIndex == TokenArray::kKatakanaSentinel)
{
entry.surface = HiraganaToKatakana(key);
}
else
{
entry.surface = U16ToWide(_impl->binary->tangoReader.GetLetter(token.nodeIndex));
}
if (!entry.surface.empty())
{
entries.push_back(std::move(entry));
}
}
std::sort(
entries.begin(),
entries.end(),
[](const LexiconEntry& lhs, const LexiconEntry& rhs)
{
if (lhs.wordCost != rhs.wordCost)
{
return lhs.wordCost < rhs.wordCost;
}
return lhs.surface < rhs.surface;
});
cached = _impl->binary->cache.emplace(key, std::move(entries)).first;
}
out->insert(out->end(), cached->second.begin(), cached->second.end());
}
return;
}
const size_t remaining = reading.size() - start;
for (size_t length : _impl->readingLengths)
{
if (length > remaining)
{
continue;
}
const std::wstring key = reading.substr(start, length);
const auto it = _impl->entriesByReading.find(key);
if (it == _impl->entriesByReading.end())
{
continue;
}
out->insert(out->end(), it->second.begin(), it->second.end());
}
}
void MozcSystemLexicon::LookupExact(const std::wstring& reading, std::vector<LexiconEntry>* out) const
{
if (out == nullptr)
{
return;
}
out->clear();
if (!_impl->loaded)
{
return;
}
if (_impl->binary)
{
std::vector<LexiconEntry> entries;
LookupPrefix(reading, 0, &entries);
for (const LexiconEntry& entry : entries)
{
if (entry.reading == reading)
{
out->push_back(entry);
}
}
return;
}
const auto it = _impl->entriesByReading.find(reading);
if (it == _impl->entriesByReading.end())
{
return;
}
out->insert(out->end(), it->second.begin(), it->second.end());
}
bool MozcSystemLexicon::IsLoaded() const
{
return _impl->loaded;
}
const std::filesystem::path& MozcSystemLexicon::GetSourceDirectory() const
{
return _impl->sourceDirectory;
}
void MozcSystemLexicon::LoadDirectory(const std::filesystem::path& directory)
{
_impl->entriesByReading.clear();
_impl->readingLengths.clear();
_impl->sourceDirectory.clear();
_impl->binary.reset();
_impl->loaded = false;
if (directory.empty() || !PathExists(directory) || !IsDirectory(directory))
{
return;
}
const std::filesystem::path yomiPath = directory / L"yomi_termid.louds";
const std::filesystem::path tangoPath = directory / L"tango.louds";
const std::filesystem::path tokenPath = directory / L"token_array.bin";
const std::filesystem::path posPath = directory / L"pos_table.bin";
if (PathExists(yomiPath) && PathExists(tangoPath) && PathExists(tokenPath) && PathExists(posPath))
{
LoudsWithTermIdData yomi = LoudsWithTermIdData::LoadFromFile(yomiPath);
LoudsReaderUtf16 tango = LoudsReaderUtf16::LoadFromFile(tangoPath);
TokenArray tokens = TokenArray::LoadFromFile(tokenPath);
PosTable pos = PosTable::LoadFromFile(posPath);
_impl->binary = std::make_unique<Impl::BinaryDictionary>(std::move(yomi), std::move(tango), std::move(tokens), std::move(pos));
_impl->sourceDirectory = NormalizePath(directory);
_impl->loaded = true;
return;
}
std::set<size_t> lengths;
for (int index = 0; index <= 9; ++index)
{
wchar_t fileName[32] = {};
swprintf_s(fileName, L"dictionary%02d.txt", index);
const std::filesystem::path filePath = directory / fileName;
if (!PathExists(filePath))
{
return;
}
std::ifstream input(filePath, std::ios::binary);
if (!input)
{
throw std::runtime_error("MozcSystemLexicon: failed to open dictionary file");
}
std::string line;
while (std::getline(input, line))
{
if (IsCommentOrEmpty(line))
{
continue;
}
if (line.size() >= 3 &&
static_cast<unsigned char>(line[0]) == 0xEF &&
static_cast<unsigned char>(line[1]) == 0xBB &&
static_cast<unsigned char>(line[2]) == 0xBF)
{
line.erase(0, 3);
}
std::string_view readingField;
std::string_view leftField;
std::string_view rightField;
std::string_view costField;
std::string_view surfaceField;
if (!SplitFiveColumns(line, &readingField, &leftField, &rightField, &costField, &surfaceField))
{
continue;
}
LexiconEntry entry;
entry.reading = Utf8ToWide(std::string(readingField));
entry.surface = Utf8ToWide(std::string(surfaceField));
entry.leftId = ParseIntField(leftField, "left_id");
entry.rightId = ParseIntField(rightField, "right_id");
entry.wordCost = ParseIntField(costField, "cost");
entry.dictionaryKind = DictionaryKind::System;
entry.sourceEntryId = static_cast<std::uint32_t>(_impl->entriesByReading.size());
if (entry.reading.empty() || entry.surface.empty())
{
continue;
}
lengths.insert(entry.reading.size());
_impl->entriesByReading[entry.reading].push_back(std::move(entry));
}
}
if (_impl->entriesByReading.empty())
{
return;
}
_impl->readingLengths.assign(lengths.begin(), lengths.end());
std::sort(_impl->readingLengths.begin(), _impl->readingLengths.end(), std::greater<size_t>());
for (auto& pair : _impl->entriesByReading)
{
std::sort(
pair.second.begin(),
pair.second.end(),
[](const LexiconEntry& lhs, const LexiconEntry& rhs)
{
if (lhs.wordCost != rhs.wordCost)
{
return lhs.wordCost < rhs.wordCost;
}
return lhs.surface < rhs.surface;
});
}