Debian Codegraph @

src/Conversion.hpp

raw symbols SCIP
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 <string_view>
22
23 #include "Common.hpp"
24 #include "Segmentation.hpp"
25
26 namespace opencc {
27 class PrefixMatch;
28
29 /**
30 * Single-dictionary phrase conversion.
31 *
32 * Applies prefix-match replacement using one dictionary (@c DictPtr) to an
33 * already-segmented piece of text. This is the lowest-level conversion
34 * primitive: it knows nothing about segmentation and operates on a single
35 * segment or a pre-built @c Segments object.
36 *
37 * Multiple @c Conversion objects are composed in a @c ConversionChain to
38 * apply several dictionaries in order (e.g. phrase dictionary first, then
39 * character dictionary).
40 *
41 * @ingroup opencc_cpp_api
42 */
43 class OPENCC_EXPORT Conversion {
44 public:
45 /** Constructs a Conversion backed by @p dict. */
46 Conversion(DictPtr _dict);
47
48 /**
49 * Converts @p phrase using prefix-match replacement and returns the result.
50 * @param phrase UTF-8 text; need not be null-terminated.
51 */
52 std::string Convert(std::string_view phrase) const;
53
54 /**
55 * Converts @p phrase using prefix-match replacement and returns the result.
56 * @param phrase Null-terminated UTF-8 text.
57 */
58 std::string Convert(const char* phrase) const;
59
60 /**
61 * Converts @p phrase and appends the result to @p output.
62 * Preferred in hot paths (e.g. ConversionChain) to avoid extra allocations.
63 * @param phrase UTF-8 text; need not be null-terminated.
64 * @param output Destination buffer; content is appended, not replaced.
65 */
66 void AppendConverted(std::string_view phrase, std::string* output) const;
67
68 /**
69 * Converts @p phrase and appends the result to @p output.
70 * @param phrase Null-terminated UTF-8 text.
71 * @param output Destination buffer; content is appended, not replaced.
72 */
73 void AppendConverted(const char* phrase, std::string* output) const;
74
75 /**
76 * Converts every segment in @p input and returns a new @c Segments object.
77 * Each segment is converted independently via Convert(const char*).
78 */
79 SegmentsPtr Convert(const SegmentsPtr& input) const;
80
81 /** Returns the backing dictionary. */
82 const DictPtr GetDict() const { return dict; }
83
84 private:
85 const DictPtr dict;
86 const std::shared_ptr<PrefixMatch> prefixMatch;
87 };
88 } // namespace opencc