| 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 "PipelineConverter.hpp" |
| 20 | |
| 21 | using namespace opencc; |
| 22 | |
| 23 | std::string PipelineConverter::Convert(std::string_view text) const { |
| 24 | if (stages.empty()) { |
| 25 | return std::string(text); |
| 26 | } |
| 27 | auto it = stages.begin(); |
| 28 | std::string result = (*it)->Convert(text); |
| 29 | for (++it; it != stages.end(); ++it) { |
| 30 | result = (*it)->Convert(result); |
| 31 | } |
| 32 | return result; |
| 33 | } |
| 34 | |
| 35 | ConversionInspectionResult |
| 36 | PipelineConverter::Inspect(std::string_view text) const { |
| 37 | ConversionInspectionResult result; |
| 38 | result.input = text; |
| 39 | |
| 40 | std::string current(text); |
| 41 | result.pipelineStages.reserve(stages.size()); |
| 42 | for (const ConverterPtr& stage : stages) { |
| 43 | ConversionInspectionResult stageResult = stage->Inspect(current); |
| 44 | current = stageResult.output; |
| 45 | result.pipelineStages.push_back(std::move(stageResult)); |
| 46 | } |
| 47 | |
| 48 | result.output = current; |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | SegmentationPtr PipelineConverter::GetSegmentation() const { |
| 53 | return stages.empty() ? nullptr : stages.back()->GetSegmentation(); |
| 54 | } |