#include "SingleStageConverter.hpp"
#include "ConversionChain.hpp"
#include "ConversionInspection.hpp"
#include "Segments.hpp"
using namespace opencc;
std::string SingleStageConverter::Convert(std::string_view text) const {
std::string converted;
converted.reserve(text.length() + text.length() / 5);
if (segmentation == nullptr) {
conversionChain->AppendConvertedSegment(text, &converted);
return converted;
}
const SegmentsPtr& segments = segmentation->Segment(text);
for (const char* segment : *segments) {
conversionChain->AppendConvertedSegment(segment, &converted);
}
return converted;
}
ConversionInspectionResult
SingleStageConverter::Inspect(std::string_view text) const {
ConversionInspectionResult result;
result.input = text;
SegmentsPtr initialSegments;
if (segmentation == nullptr) {
initialSegments.reset(new Segments);
initialSegments->AddSegment(text);
} else {
initialSegments = segmentation->Segment(text);
}
result.segments = initialSegments->ToVector();
const std::vector<SegmentsPtr> trace =
conversionChain->ConvertWithTrace(initialSegments);
result.stages.reserve(trace.size());
for (size_t i = 0; i < trace.size(); i++) {
ConversionInspectionStage stage;
stage.index = i + 1;
stage.segments = trace[i]->ToVector();
result.stages.push_back(std::move(stage));
}
if (!trace.empty()) {
result.output = trace.back()->ToString();
} else {
result.output = initialSegments->ToString();
}
return result;
}