| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2020-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 | |
| 21 | #include "Lexicon.hpp" |
| 22 | |
| 23 | namespace opencc { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | DictEntry* ParseKeyValues(const char* buff, size_t lineNum, |
| 28 | char keyValueDelimiter) { |
| 29 | size_t length; |
| 30 | if (buff == nullptr || UTF8Util::IsLineEndingOrFileEnding(*buff)) { |
| 31 | return nullptr; |
| 32 | } |
| 33 | const char* pbuff = UTF8Util::FindNextInline(buff, keyValueDelimiter); |
| 34 | if (UTF8Util::IsLineEndingOrFileEnding(*pbuff)) { |
| 35 | throw InvalidTextDictionary("Tabular not found " + std::string(buff), |
| 36 | lineNum); |
| 37 | } |
| 38 | length = static_cast<size_t>(pbuff - buff); |
| 39 | std::string key = UTF8Util::FromSubstr(buff, length); |
| 40 | std::vector<std::string> values; |
| 41 | while (!UTF8Util::IsLineEndingOrFileEnding(*pbuff)) { |
| 42 | buff = pbuff = UTF8Util::NextChar(pbuff); |
| 43 | pbuff = UTF8Util::FindNextInline(buff, ' '); |
| 44 | length = static_cast<size_t>(pbuff - buff); |
| 45 | const std::string& value = UTF8Util::FromSubstr(buff, length); |
| 46 | values.push_back(value); |
| 47 | } |
| 48 | if (values.size() == 0) { |
| 49 | throw InvalidTextDictionary("No value in an item", lineNum); |
| 50 | } else if (values.size() == 1) { |
| 51 | return DictEntryFactory::New(key, values.at(0)); |
| 52 | } else { |
| 53 | return DictEntryFactory::New(key, values); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | |
| 59 | void Lexicon::Sort() { |
| 60 | std::sort(entries.begin(), entries.end(), DictEntry::UPtrLessThan); |
| 61 | } |
| 62 | |
| 63 | bool Lexicon::IsSorted() { |
| 64 | return std::is_sorted(entries.begin(), entries.end(), |
| 65 | DictEntry::UPtrLessThan); |
| 66 | } |
| 67 | |
| 68 | bool Lexicon::IsUnique(std::string* dupkey) { |
| 69 | for (size_t i = 1; i < entries.size(); ++i) { |
| 70 | if (entries[i - 1]->Key() == entries[i]->Key()) { |
| 71 | if (dupkey) { |
| 72 | *dupkey = entries[i]->Key(); |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | LexiconPtr Lexicon::ParseLexiconFromFile(FILE* fp) { |
| 81 | return ParseLexiconFromFile(fp, '\t'); |
| 82 | } |
| 83 | |
| 84 | LexiconPtr Lexicon::ParseLexiconFromFile(FILE* fp, char keyValueDelimiter) { |
| 85 | const int ENTRY_BUFF_SIZE = 4096; |
| 86 | char buff[ENTRY_BUFF_SIZE]; |
| 87 | LexiconPtr lexicon(new Lexicon); |
| 88 | UTF8Util::SkipUtf8Bom(fp); |
| 89 | |
| 90 | size_t lineNum = 1; |
| 91 | while (fgets(buff, ENTRY_BUFF_SIZE, fp)) { |
| 92 | if (*buff == '#') { |
| 93 | lineNum++; |
| 94 | continue; |
| 95 | } |
| 96 | DictEntry* entry = ParseKeyValues(buff, lineNum, keyValueDelimiter); |
| 97 | if (entry != nullptr) { |
| 98 | lexicon->Add(entry); |
| 99 | } |
| 100 | lineNum++; |
| 101 | } |
| 102 | return lexicon; |
| 103 | } |
| 104 | |
| 105 | LexiconPtr Lexicon::ParseLexiconFromBuffer(const char* data, size_t size) { |
| 106 | LexiconPtr lexicon(new Lexicon); |
| 107 | size_t offset = 0; |
| 108 | if (size >= 3 && static_cast<unsigned char>(data[0]) == 0xef && |
| 109 | static_cast<unsigned char>(data[1]) == 0xbb && |
| 110 | static_cast<unsigned char>(data[2]) == 0xbf) { |
| 111 | offset = 3; |
| 112 | } |
| 113 | |
| 114 | size_t lineNum = 1; |
| 115 | while (offset < size) { |
| 116 | size_t lineEnd = offset; |
| 117 | while (lineEnd < size && data[lineEnd] != '\n') { |
| 118 | lineEnd++; |
| 119 | } |
| 120 | |
| 121 | std::string line(data + offset, lineEnd - offset); |
| 122 | if (!line.empty() && line.back() == '\r') { |
| 123 | line.pop_back(); |
| 124 | } |
| 125 | if (!line.empty() && line.front() != '#') { |
| 126 | line.push_back('\n'); |
| 127 | DictEntry* entry = ParseKeyValues(line.c_str(), lineNum, '\t'); |
| 128 | if (entry != nullptr) { |
| 129 | lexicon->Add(entry); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | offset = lineEnd < size ? lineEnd + 1 : lineEnd; |
| 134 | lineNum++; |
| 135 | } |
| 136 | return lexicon; |
| 137 | } |
| 138 | |
| 139 | } // namespace opencc |