| 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 <list> |
| 22 | |
| 23 | #include "Common.hpp" |
| 24 | #include "Conversion.hpp" |
| 25 | |
| 26 | namespace opencc { |
| 27 | /** |
| 28 | * An ordered sequence of @c Conversion objects applied to pre-segmented text. |
| 29 | * |
| 30 | * Each @c Conversion in the chain wraps one dictionary. The chain applies |
| 31 | * them in order: the output segments of conversion N become the input of |
| 32 | * conversion N+1. This enables priority-ordered substitution, such as |
| 33 | * applying phrase dictionaries before character dictionaries so that |
| 34 | * multi-character matches take precedence. |
| 35 | * |
| 36 | * A @c ConversionChain operates on @c Segments (output of @c Segmentation) |
| 37 | * and has no knowledge of how those segments were produced. |
| 38 | * |
| 39 | * @ingroup opencc_cpp_api |
| 40 | */ |
| 41 | class OPENCC_EXPORT ConversionChain { |
| 42 | public: |
| 43 | /** Constructs a chain from an ordered list of conversions. */ |
| 44 | ConversionChain(const std::list<ConversionPtr> _conversions); |
| 45 | |
| 46 | /** |
| 47 | * Passes @p input through every conversion in the chain sequentially and |
| 48 | * returns the final @c Segments. |
| 49 | */ |
| 50 | SegmentsPtr Convert(const SegmentsPtr& input) const; |
| 51 | |
| 52 | /** |
| 53 | * Converts the null-terminated @p segment through all conversions and |
| 54 | * appends the result to @p output. This is the hot-path entry point used |
| 55 | * by @c SingleStageConverter to avoid intermediate string allocations. |
| 56 | */ |
| 57 | void AppendConvertedSegment(const char* segment, std::string* output) const; |
| 58 | |
| 59 | /** |
| 60 | * Like @c AppendConvertedSegment(const char*, std::string*) but accepts the |
| 61 | * segment as a @c string_view, avoiding a copy when the caller already has a |
| 62 | * non-null-terminated view (e.g. @c SingleStageConverter with no |
| 63 | * segmentation). |
| 64 | */ |
| 65 | void AppendConvertedSegment(std::string_view segment, |
| 66 | std::string* output) const; |
| 67 | |
| 68 | /** |
| 69 | * Converts @p input through the chain and records every intermediate |
| 70 | * @c Segments after each conversion stage. |
| 71 | * @return One entry per conversion in the chain, in order. |
| 72 | */ |
| 73 | std::vector<SegmentsPtr> ConvertWithTrace(const SegmentsPtr& input) const; |
| 74 | |
| 75 | /** Returns the list of conversions in application order. */ |
| 76 | const std::list<ConversionPtr> GetConversions() const { return conversions; } |
| 77 | |
| 78 | private: |
| 79 | const std::list<ConversionPtr> conversions; |
| 80 | }; |
| 81 | } // namespace opencc |