| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2010-2014 Carbo Kuo <[email protected]> |
| 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 | #pragma once |
| 20 | |
| 21 | #include <cstddef> |
| 22 | #include <string> |
| 23 | #include <string_view> |
| 24 | |
| 25 | #include "Common.hpp" |
| 26 | #include "ConversionInspection.hpp" |
| 27 | #include "Segmentation.hpp" |
| 28 | |
| 29 | namespace opencc { |
| 30 | /** |
| 31 | * Abstract base for full-text converters. |
| 32 | * |
| 33 | * A @c Converter accepts a complete UTF-8 string and returns the converted |
| 34 | * result. Internally it coordinates @c Segmentation (splitting text into |
| 35 | * words) and a @c ConversionChain (applying one or more dictionaries to each |
| 36 | * segment). Concrete implementations: |
| 37 | * |
| 38 | * - @c SingleStageConverter — one segmentation pass + one @c ConversionChain. |
| 39 | * - @c PipelineConverter — passes text through a sequence of @c Converter |
| 40 | * stages in order, each with its own segmentation and chain. |
| 41 | * |
| 42 | * Config-backed converters may additionally expose the main conversion chain |
| 43 | * via @c GetConversionChain() even when an internal normalization pre-pass is |
| 44 | * present. |
| 45 | * |
| 46 | * @ingroup opencc_cpp_api |
| 47 | */ |
| 48 | class OPENCC_EXPORT Converter { |
| 49 | public: |
| 50 | virtual ~Converter() = default; |
| 51 | |
| 52 | /** |
| 53 | * Converts @p text and returns the result. |
| 54 | * @param text UTF-8 input; need not be null-terminated. |
| 55 | */ |
| 56 | virtual std::string Convert(std::string_view text) const = 0; |
| 57 | |
| 58 | /** |
| 59 | * Converts @p text and returns a detailed inspection result that includes |
| 60 | * the initial segmentation, per-stage intermediate segments, and final |
| 61 | * output. Intended for debugging and tooling rather than production |
| 62 | * conversion. |
| 63 | */ |
| 64 | virtual ConversionInspectionResult Inspect(std::string_view text) const = 0; |
| 65 | |
| 66 | /** |
| 67 | * Returns the segmentation used by this converter, or @c nullptr if the |
| 68 | * converter has no single segmentation (e.g. @c PipelineConverter). |
| 69 | */ |
| 70 | virtual SegmentationPtr GetSegmentation() const = 0; |
| 71 | |
| 72 | /** |
| 73 | * Returns the conversion chain used by this converter, or @c nullptr if the |
| 74 | * converter has no single chain (e.g. @c PipelineConverter). |
| 75 | */ |
| 76 | virtual ConversionChainPtr GetConversionChain() const = 0; |
| 77 | }; |
| 78 | |
| 79 | class OPENCC_EXPORT ConverterStream { |
| 80 | public: |
| 81 | /** |
| 82 | * @param converter The converter applied to each flushed chunk. |
| 83 | * @param maxKeepChars Number of Unicode code points (not bytes) to retain |
| 84 | * at the end of the pending buffer after each ConvertChunk() call, so |
| 85 | * that a phrase or IDS straddling two consecutive chunks is not split |
| 86 | * across separate Convert() invocations. Defaults to 16 code points. |
| 87 | */ |
| 88 | explicit ConverterStream(ConverterPtr converter, size_t maxKeepChars = 16) |
| 89 | : converter(converter), maxKeepChars(maxKeepChars) {} |
| 90 | |
| 91 | /** |
| 92 | * Appends @p input to pending, then emits everything up to the last |
| 93 | * @c maxKeepChars code points (extended if an incomplete IDS is detected). |
| 94 | * The kept tail remains in pending so that a phrase or IDS spanning two |
| 95 | * consecutive calls is not split across separate Convert() invocations. |
| 96 | */ |
| 97 | std::string ConvertChunk(std::string_view input); |
| 98 | |
| 99 | /** |
| 100 | * Appends @p input to pending and flushes everything at once. |
| 101 | * @note Not equivalent to ConvertChunk(@p input) + Finish(): ConvertChunk |
| 102 | * applies the keepStart window and issues two Convert() calls, which |
| 103 | * breaks phrase or IDS matches that span the boundary. |
| 104 | * Use this overload for the final chunk when no more data is coming. |
| 105 | */ |
| 106 | std::string Finish(std::string_view input); |
| 107 | |
| 108 | std::string Finish(); |
| 109 | |
| 110 | private: |
| 111 | ConverterPtr converter; |
| 112 | size_t maxKeepChars; |
| 113 | std::string pending; |
| 114 | }; |
| 115 | } // namespace opencc |