| 1 |
#ifndef ICU4XWordSegmenter_HPP |
| 2 |
#define ICU4XWordSegmenter_HPP |
| 3 |
#include <stdint.h> |
| 4 |
#include <stddef.h> |
| 5 |
#include <stdbool.h> |
| 6 |
#include <algorithm> |
| 7 |
#include <memory> |
| 8 |
#include <variant> |
| 9 |
#include <optional> |
| 10 |
#include "diplomat_runtime.hpp" |
| 11 |
|
| 12 |
#include "ICU4XWordSegmenter.h" |
| 13 |
|
| 14 |
class ICU4XDataProvider; |
| 15 |
class ICU4XWordSegmenter; |
| 16 |
#include "ICU4XError.hpp" |
| 17 |
class ICU4XWordBreakIteratorUtf8; |
| 18 |
class ICU4XWordBreakIteratorUtf16; |
| 19 |
class ICU4XWordBreakIteratorLatin1; |
| 20 |
|
| 21 |
/** |
| 22 |
* A destruction policy for using ICU4XWordSegmenter with std::unique_ptr. |
| 23 |
*/ |
| 24 |
struct ICU4XWordSegmenterDeleter { |
| 25 |
void operator()(capi::ICU4XWordSegmenter* l) const noexcept { |
| 26 |
capi::ICU4XWordSegmenter_destroy(l); |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
/** |
| 31 |
* An ICU4X word-break segmenter, capable of finding word breakpoints in strings. |
| 32 |
* |
| 33 |
* See the [Rust documentation for `WordSegmenter`](https://docs.rs/icu/latest/icu/segmenter/struct.WordSegmenter.html) for more information. |
| 34 |
*/ |
| 35 |
class ICU4XWordSegmenter { |
| 36 |
public: |
| 37 |
|
| 38 |
/** |
| 39 |
* Construct an [`ICU4XWordSegmenter`] with automatically selecting the best available LSTM |
| 40 |
* or dictionary payload data. |
| 41 |
* |
| 42 |
* Note: currently, it uses dictionary for Chinese and Japanese, and LSTM for Burmese, |
| 43 |
* Khmer, Lao, and Thai. |
| 44 |
* |
| 45 |
* See the [Rust documentation for `new_auto`](https://docs.rs/icu/latest/icu/segmenter/struct.WordSegmenter.html#method.new_auto) for more information. |
| 46 |
*/ |
| 47 |
static diplomat::result<ICU4XWordSegmenter, ICU4XError> create_auto(const ICU4XDataProvider& provider); |
| 48 |
|
| 49 |
/** |
| 50 |
* Construct an [`ICU4XWordSegmenter`] with LSTM payload data for Burmese, Khmer, Lao, and |
| 51 |
* Thai. |
| 52 |
* |
| 53 |
* Warning: [`ICU4XWordSegmenter`] created by this function doesn't handle Chinese or |
| 54 |
* Japanese. |
| 55 |
* |
| 56 |
* See the [Rust documentation for `new_lstm`](https://docs.rs/icu/latest/icu/segmenter/struct.WordSegmenter.html#method.new_lstm) for more information. |
| 57 |
*/ |
| 58 |
static diplomat::result<ICU4XWordSegmenter, ICU4XError> create_lstm(const ICU4XDataProvider& provider); |
| 59 |
|
| 60 |
/** |
| 61 |
* Construct an [`ICU4XWordSegmenter`] with dictionary payload data for Chinese, Japanese, |
| 62 |
* Burmese, Khmer, Lao, and Thai. |
| 63 |
* |
| 64 |
* See the [Rust documentation for `new_dictionary`](https://docs.rs/icu/latest/icu/segmenter/struct.WordSegmenter.html#method.new_dictionary) for more information. |
| 65 |
*/ |
| 66 |
static diplomat::result<ICU4XWordSegmenter, ICU4XError> create_dictionary(const ICU4XDataProvider& provider); |
| 67 |
|
| 68 |
/** |
| 69 |
* Segments a string. |
| 70 |
* |
| 71 |
* Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according |
| 72 |
* to the WHATWG Encoding Standard. |
| 73 |
* |
| 74 |
* See the [Rust documentation for `segment_utf8`](https://docs.rs/icu/latest/icu/segmenter/struct.WordSegmenter.html#method.segment_utf8) for more information. |
| 75 |
* |
| 76 |
* Lifetimes: `this`, `input` must live at least as long as the output. |
| 77 |
*/ |
| 78 |
ICU4XWordBreakIteratorUtf8 segment_utf8(const std::string_view input) const; |
| 79 |
|
| 80 |
/** |
| 81 |
* Segments a string. |
| 82 |
* |
| 83 |
* Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according |
| 84 |
* to the WHATWG Encoding Standard. |
| 85 |
* |
| 86 |
* See the [Rust documentation for `segment_utf16`](https://docs.rs/icu/latest/icu/segmenter/struct.WordSegmenter.html#method.segment_utf16) for more information. |
| 87 |
* |
| 88 |
* Lifetimes: `this`, `input` must live at least as long as the output. |
| 89 |
*/ |
| 90 |
ICU4XWordBreakIteratorUtf16 segment_utf16(const std::u16string_view input) const; |
| 91 |
|
| 92 |
/** |
| 93 |
* Segments a Latin-1 string. |
| 94 |
* |
| 95 |
* See the [Rust documentation for `segment_latin1`](https://docs.rs/icu/latest/icu/segmenter/struct.WordSegmenter.html#method.segment_latin1) for more information. |
| 96 |
* |
| 97 |
* Lifetimes: `this`, `input` must live at least as long as the output. |
| 98 |
*/ |
| 99 |
ICU4XWordBreakIteratorLatin1 segment_latin1(const diplomat::span<const uint8_t> input) const; |
| 100 |
inline const capi::ICU4XWordSegmenter* AsFFI() const { return this->inner.get(); } |
| 101 |
inline capi::ICU4XWordSegmenter* AsFFIMut() { return this->inner.get(); } |
| 102 |
inline explicit ICU4XWordSegmenter(capi::ICU4XWordSegmenter* i) : inner(i) {} |
| 103 |
ICU4XWordSegmenter() = default; |
| 104 |
ICU4XWordSegmenter(ICU4XWordSegmenter&&) noexcept = default; |
| 105 |
ICU4XWordSegmenter& operator=(ICU4XWordSegmenter&& other) noexcept = default; |
| 106 |
private: |
| 107 |
std::unique_ptr<capi::ICU4XWordSegmenter, ICU4XWordSegmenterDeleter> inner; |
| 108 |
}; |
| 109 |
|
| 110 |
#include "ICU4XDataProvider.hpp" |
| 111 |
#include "ICU4XWordBreakIteratorUtf8.hpp" |
| 112 |
#include "ICU4XWordBreakIteratorUtf16.hpp" |
| 113 |
#include "ICU4XWordBreakIteratorLatin1.hpp" |
| 114 |
|
| 115 |
inline diplomat::result<ICU4XWordSegmenter, ICU4XError> ICU4XWordSegmenter::create_auto(const ICU4XDataProvider& provider) { |
| 116 |
auto diplomat_result_raw_out_value = capi::ICU4XWordSegmenter_create_auto(provider.AsFFI()); |
| 117 |
diplomat::result<ICU4XWordSegmenter, ICU4XError> diplomat_result_out_value; |
| 118 |
if (diplomat_result_raw_out_value.is_ok) { |
| 119 |
diplomat_result_out_value = diplomat::Ok<ICU4XWordSegmenter>(ICU4XWordSegmenter(diplomat_result_raw_out_value.ok)); |
| 120 |
} else { |
| 121 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 122 |
} |
| 123 |
return diplomat_result_out_value; |
| 124 |
} |
| 125 |
inline diplomat::result<ICU4XWordSegmenter, ICU4XError> ICU4XWordSegmenter::create_lstm(const ICU4XDataProvider& provider) { |
| 126 |
auto diplomat_result_raw_out_value = capi::ICU4XWordSegmenter_create_lstm(provider.AsFFI()); |
| 127 |
diplomat::result<ICU4XWordSegmenter, ICU4XError> diplomat_result_out_value; |
| 128 |
if (diplomat_result_raw_out_value.is_ok) { |
| 129 |
diplomat_result_out_value = diplomat::Ok<ICU4XWordSegmenter>(ICU4XWordSegmenter(diplomat_result_raw_out_value.ok)); |
| 130 |
} else { |
| 131 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 132 |
} |
| 133 |
return diplomat_result_out_value; |
| 134 |
} |
| 135 |
inline diplomat::result<ICU4XWordSegmenter, ICU4XError> ICU4XWordSegmenter::create_dictionary(const ICU4XDataProvider& provider) { |
| 136 |
auto diplomat_result_raw_out_value = capi::ICU4XWordSegmenter_create_dictionary(provider.AsFFI()); |
| 137 |
diplomat::result<ICU4XWordSegmenter, ICU4XError> diplomat_result_out_value; |
| 138 |
if (diplomat_result_raw_out_value.is_ok) { |
| 139 |
diplomat_result_out_value = diplomat::Ok<ICU4XWordSegmenter>(ICU4XWordSegmenter(diplomat_result_raw_out_value.ok)); |
| 140 |
} else { |
| 141 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 142 |
} |
| 143 |
return diplomat_result_out_value; |
| 144 |
} |
| 145 |
inline ICU4XWordBreakIteratorUtf8 ICU4XWordSegmenter::segment_utf8(const std::string_view input) const { |
| 146 |
return ICU4XWordBreakIteratorUtf8(capi::ICU4XWordSegmenter_segment_utf8(this->inner.get(), input.data(), input.size())); |
| 147 |
} |
| 148 |
inline ICU4XWordBreakIteratorUtf16 ICU4XWordSegmenter::segment_utf16(const std::u16string_view input) const { |
| 149 |
return ICU4XWordBreakIteratorUtf16(capi::ICU4XWordSegmenter_segment_utf16(this->inner.get(), input.data(), input.size())); |
| 150 |
} |
| 151 |
inline ICU4XWordBreakIteratorLatin1 ICU4XWordSegmenter::segment_latin1(const diplomat::span<const uint8_t> input) const { |
| 152 |
return ICU4XWordBreakIteratorLatin1(capi::ICU4XWordSegmenter_segment_latin1(this->inner.get(), input.data(), input.size())); |
| 153 |
} |
| 154 |
#endif |