| 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 <cctype> |
| 20 | #include <fstream> |
| 21 | #include <map> |
| 22 | #include <sstream> |
| 23 | |
| 24 | #include "DictConverter.hpp" |
| 25 | #include "DictEntry.hpp" |
| 26 | #include "Exception.hpp" |
| 27 | #include "Lexicon.hpp" |
| 28 | #include "MarisaDict.hpp" |
| 29 | #include "TextDict.hpp" |
| 30 | #if defined(_WIN32) || defined(_WIN64) |
| 31 | #include "WinUtil.hpp" |
| 32 | #endif |
| 33 | |
| 34 | #include "DartsDict.hpp" |
| 35 | |
| 36 | using namespace opencc; |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | std::vector<std::string> SplitWhitespace(const std::string& line) { |
| 41 | std::vector<std::string> tokens; |
| 42 | std::istringstream iss(line); |
| 43 | std::string token; |
| 44 | while (iss >> token) { |
| 45 | tokens.push_back(token); |
| 46 | } |
| 47 | return tokens; |
| 48 | } |
| 49 | |
| 50 | bool IsCommentOrEmpty(const std::string& line) { |
| 51 | if (line.empty()) { |
| 52 | return true; |
| 53 | } |
| 54 | for (size_t i = 0; i < line.size(); ++i) { |
| 55 | const unsigned char ch = static_cast<unsigned char>(line[i]); |
| 56 | if (!std::isspace(ch)) { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | std::vector<std::string> ParseCppJiebaBaseValues( |
| 64 | const std::vector<std::string>& tokens, size_t lineNumber) { |
| 65 | if (tokens.size() != 3) { |
| 66 | throw InvalidFormat("Invalid cppjieba_utf8 base dict line: " + |
| 67 | std::to_string(lineNumber)); |
| 68 | } |
| 69 | return std::vector<std::string>{tokens[1], tokens[2], "base"}; |
| 70 | } |
| 71 | |
| 72 | std::vector<std::string> ParseCppJiebaUserValues( |
| 73 | const std::vector<std::string>& tokens, size_t lineNumber) { |
| 74 | if (tokens.empty() || tokens.size() > 3) { |
| 75 | throw InvalidFormat("Invalid cppjieba_utf8 user dict line: " + |
| 76 | std::to_string(lineNumber)); |
| 77 | } |
| 78 | if (tokens.size() == 1) { |
| 79 | return std::vector<std::string>{"", "", "user_default"}; |
| 80 | } |
| 81 | if (tokens.size() == 2) { |
| 82 | return std::vector<std::string>{"", tokens[1], "user_default"}; |
| 83 | } |
| 84 | return std::vector<std::string>{tokens[1], tokens[2], "user_freq"}; |
| 85 | } |
| 86 | |
| 87 | void LoadCppJiebaUtf8Dict( |
| 88 | const std::string& path, bool isBaseDict, |
| 89 | std::map<std::string, std::vector<std::string>>& entries) { |
| 90 | std::ifstream ifs(path.c_str()); |
| 91 | if (!ifs.is_open()) { |
| 92 | throw FileNotFound(path); |
| 93 | } |
| 94 | |
| 95 | std::string line; |
| 96 | size_t lineNumber = 0; |
| 97 | while (std::getline(ifs, line)) { |
| 98 | ++lineNumber; |
| 99 | if (IsCommentOrEmpty(line)) { |
| 100 | continue; |
| 101 | } |
| 102 | const std::vector<std::string> tokens = SplitWhitespace(line); |
| 103 | entries[tokens[0]] = |
| 104 | isBaseDict ? ParseCppJiebaBaseValues(tokens, lineNumber) |
| 105 | : ParseCppJiebaUserValues(tokens, lineNumber); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | DictPtr LoadCppJiebaUtf8Dictionaries( |
| 110 | const std::vector<std::string>& inputFileNames) { |
| 111 | if (inputFileNames.empty()) { |
| 112 | throw InvalidFormat("cppjieba_utf8 requires at least one input dictionary."); |
| 113 | } |
| 114 | |
| 115 | std::map<std::string, std::vector<std::string>> entries; |
| 116 | LoadCppJiebaUtf8Dict(inputFileNames.front(), true, entries); |
| 117 | for (size_t i = 1; i < inputFileNames.size(); ++i) { |
| 118 | LoadCppJiebaUtf8Dict(inputFileNames[i], false, entries); |
| 119 | } |
| 120 | |
| 121 | LexiconPtr lexicon(new Lexicon); |
| 122 | for (const auto& entry : entries) { |
| 123 | lexicon->Add(DictEntryFactory::New(entry.first, entry.second)); |
| 124 | } |
| 125 | return TextDictPtr(new TextDict(lexicon)); |
| 126 | } |
| 127 | |
| 128 | } // namespace |
| 129 | |
| 130 | DictPtr LoadDictionary(const std::string& format, |
| 131 | const std::string& inputFileName) { |
| 132 | if (format == "text" || format == "text_space") { |
| 133 | FILE* fp = nullptr; |
| 134 | #if defined(_WIN32) || defined(_WIN64) |
| 135 | fp = _wfopen(internal::WideFromUtf8(inputFileName).c_str(), L"r"); |
| 136 | #else |
| 137 | fp = fopen(inputFileName.c_str(), "r"); |
| 138 | #endif |
| 139 | if (!fp) { |
| 140 | throw FileNotFound(inputFileName); |
| 141 | } |
| 142 | DictPtr dict = format == "text" ? TextDict::NewFromFile(fp) |
| 143 | : TextDict::NewFromFile(fp, ' '); |
| 144 | fclose(fp); |
| 145 | return dict; |
| 146 | } else if (format == "ocd") { |
| 147 | return SerializableDict::NewFromFile<DartsDict>(inputFileName); |
| 148 | } else if (format == "ocd2") { |
| 149 | return SerializableDict::NewFromFile<MarisaDict>(inputFileName); |
| 150 | } |
| 151 | fprintf(stderr, "Unknown dictionary format: %s\n", format.c_str()); |
| 152 | exit(2); |
| 153 | return nullptr; |
| 154 | } |
| 155 | |
| 156 | SerializableDictPtr ConvertDict(const std::string& format, const DictPtr dict) { |
| 157 | if (format == "text") { |
| 158 | return TextDict::NewFromDict(*dict.get()); |
| 159 | } else if (format == "ocd") { |
| 160 | return DartsDict::NewFromDict(*dict.get()); |
| 161 | } else if (format == "ocd2") { |
| 162 | return MarisaDict::NewFromDict(*dict.get()); |
| 163 | } |
| 164 | fprintf(stderr, "Unknown dictionary format: %s\n", format.c_str()); |
| 165 | exit(2); |
| 166 | return nullptr; |
| 167 | } |
| 168 | |
| 169 | namespace opencc { |
| 170 | void ConvertDictionary(const std::string& inputFileName, |
| 171 | const std::string& outputFileName, |
| 172 | const std::string& formatFrom, |
| 173 | const std::string& formatTo) { |
| 174 | ConvertDictionary(std::vector<std::string>{inputFileName}, outputFileName, |
| 175 | formatFrom, formatTo); |
| 176 | } |
| 177 | |
| 178 | void ConvertDictionary(const std::vector<std::string>& inputFileNames, |
| 179 | const std::string& outputFileName, |
| 180 | const std::string& formatFrom, |
| 181 | const std::string& formatTo) { |
| 182 | DictPtr dictFrom; |
| 183 | if (formatFrom == "cppjieba_utf8") { |
| 184 | dictFrom = LoadCppJiebaUtf8Dictionaries(inputFileNames); |
| 185 | } else { |
| 186 | if (inputFileNames.size() != 1) { |
| 187 | throw InvalidFormat("Dictionary format '" + formatFrom + |
| 188 | "' requires exactly one input dictionary."); |
| 189 | } |
| 190 | dictFrom = LoadDictionary(formatFrom, inputFileNames.front()); |
| 191 | } |
| 192 | SerializableDictPtr dictTo = ConvertDict(formatTo, dictFrom); |
| 193 | dictTo->SerializeToFile(outputFileName); |
| 194 | } |
| 195 | } // namespace opencc |