Debian Codegraph @

src/TextDict.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2026 Carbo Kuo and contributors
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 #include <algorithm>
20 #include <cassert>
21
22 #include "Lexicon.hpp"
23 #include "TextDict.hpp"
24
25 using namespace opencc;
26
27 static size_t GetKeyMaxLength(const LexiconPtr& lexicon) {
28 size_t maxLength = 0;
29 for (const auto& entry : *lexicon) {
30 size_t keyLength = entry->KeyLength();
31 maxLength = (std::max)(keyLength, maxLength);
32 }
33 return maxLength;
34 }
35
36 TextDict::TextDict(const LexiconPtr& _lexicon)
37 : maxLength(GetKeyMaxLength(_lexicon)), lexicon(_lexicon) {
38 assert(lexicon->IsSorted());
39 assert(lexicon->IsUnique());
40 }
41
42 TextDict::~TextDict() {}
43
44 TextDictPtr TextDict::NewFromSortedFile(FILE* fp) {
45 const LexiconPtr& lexicon = Lexicon::ParseLexiconFromFile(fp);
46 return TextDictPtr(new TextDict(lexicon));
47 }
48
49 TextDictPtr TextDict::NewFromFile(FILE* fp) {
50 return NewFromFile(fp, '\t');
51 }
52
53 TextDictPtr TextDict::NewFromFile(FILE* fp, char keyValueDelimiter) {
54 const LexiconPtr& lexicon =
55 Lexicon::ParseLexiconFromFile(fp, keyValueDelimiter);
56 lexicon->Sort();
57 std::string dupkey;
58 if (!lexicon->IsUnique(&dupkey)) {
59 throw InvalidFormat(
60 "The text dictionary contains duplicated keys: " + dupkey + ".");
61 }
62 return TextDictPtr(new TextDict(lexicon));
63 }
64
65 TextDictPtr TextDict::NewFromBuffer(const char* data, size_t size) {
66 const LexiconPtr& lexicon = Lexicon::ParseLexiconFromBuffer(data, size);
67 lexicon->Sort();
68 std::string dupkey;
69 if (!lexicon->IsUnique(&dupkey)) {
70 throw InvalidFormat(
71 "The text dictionary contains duplicated keys: " + dupkey + ".");
72 }
73 return TextDictPtr(new TextDict(lexicon));
74 }
75
76 TextDictPtr TextDict::NewFromDict(const Dict& dict) {
77 return TextDictPtr(new TextDict(dict.GetLexicon()));
78 }
79
80 size_t TextDict::KeyMaxLength() const { return maxLength; }
81
82 Optional<const DictEntry*> TextDict::Match(const char* word, size_t len) const {
83 std::unique_ptr<DictEntry> entry(
84 new NoValueDictEntry(std::string(word, len)));
85 const auto& found = std::lower_bound(lexicon->begin(), lexicon->end(), entry,
86 DictEntry::UPtrLessThan);
87 if ((found != lexicon->end()) && ((*found)->Key() == entry->Key())) {
88 return Optional<const DictEntry*>(found->get());
89 } else {
90 return Optional<const DictEntry*>::Null();
91 }
92 }
93
94 LexiconPtr TextDict::GetLexicon() const { return lexicon; }
95
96 void TextDict::SerializeToFile(FILE* fp) const {
97 for (const auto& entry : *lexicon) {
98 fprintf(fp, "%s\n", entry->ToString().c_str());
99 }
100 }