-
-
Notifications
You must be signed in to change notification settings - Fork 16
Add Factory for MF2 ( issues/117 ) #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/* | ||
* Copyright 2025 Unicode Incorporated and others. All rights reserved. | ||
*/ | ||
#include "inflection/message2/MF2Factory.hpp" | ||
|
||
#include "inflection/dialog/InflectableStringConcept.hpp" | ||
#include "inflection/dialog/LocalizedCommonConceptFactoryProvider.hpp" | ||
#include "inflection/dialog/SemanticFeatureModel.hpp" | ||
#include "inflection/dialog/SpeakableString.hpp" | ||
#include "inflection/lang/features/LanguageGrammarFeatures.hpp" | ||
#include "inflection/util/ULocale.hpp" | ||
|
||
#include <unicode/locid.h> | ||
#include <unicode/messageformat2.h> | ||
#include <unicode/messageformat2_function_registry.h> | ||
#include <unicode/messageformat2_formattable.h> | ||
|
||
using U_ICU_NAMESPACE::Locale; | ||
using U_ICU_NAMESPACE::UnicodeString; | ||
using U_ICU_NAMESPACE::message2::Formatter; | ||
using U_ICU_NAMESPACE::message2::Formattable; | ||
using U_ICU_NAMESPACE::message2::FormattedValue; | ||
using U_ICU_NAMESPACE::message2::FormattedPlaceholder; | ||
using U_ICU_NAMESPACE::message2::FormatterFactory; | ||
using U_ICU_NAMESPACE::message2::FunctionOptions; | ||
using U_ICU_NAMESPACE::message2::FunctionOptionsMap; | ||
using U_ICU_NAMESPACE::message2::MessageArguments; | ||
using U_ICU_NAMESPACE::message2::MessageFormatter; | ||
using U_ICU_NAMESPACE::message2::MFFunctionRegistry; | ||
using U_ICU_NAMESPACE::message2::Selector; | ||
using U_ICU_NAMESPACE::message2::SelectorFactory; | ||
|
||
namespace inflection::message2 { | ||
|
||
class InflectionFormatterFactory : public FormatterFactory { | ||
public: | ||
Formatter* createFormatter(const Locale&, UErrorCode&) override; | ||
}; | ||
|
||
class InflectionSelectorFactory : public SelectorFactory { | ||
public: | ||
Selector* createSelector(const Locale&, UErrorCode&) const override; | ||
}; | ||
|
||
icu::message2::FormatterFactory* MF2Factory::CreateFormatterFactory() { | ||
return new InflectionFormatterFactory(); | ||
} | ||
|
||
icu::message2::SelectorFactory* MF2Factory::CreateSelectorFactory() { | ||
return new InflectionSelectorFactory(); | ||
} | ||
|
||
const inflection::dialog::SemanticFeatureModel* GetSemanticFeatureModel( | ||
const Locale& locale) { | ||
return ::inflection::dialog::LocalizedCommonConceptFactoryProvider | ||
::getDefaultCommonConceptFactoryProvider() | ||
->getCommonConceptFactory( | ||
inflection::util::ULocale(locale.getName())) | ||
->getSemanticFeatureModel(); | ||
} | ||
|
||
class InflectionFormatter : public Formatter { | ||
public: | ||
FormattedPlaceholder format(FormattedPlaceholder&&, FunctionOptions&& opts, | ||
UErrorCode& errorCode) const override; | ||
InflectionFormatter(const inflection::dialog::SemanticFeatureModel* model) | ||
: model(model) { | ||
} | ||
private: | ||
const ::inflection::dialog::SemanticFeatureModel* model; | ||
}; | ||
|
||
Formatter* InflectionFormatterFactory::createFormatter( | ||
const Locale& locale, UErrorCode& errorCode) { | ||
if (U_FAILURE(errorCode)) { return nullptr; } | ||
|
||
Formatter* result = new InflectionFormatter(GetSemanticFeatureModel(locale)); | ||
if (result == nullptr) { | ||
errorCode = U_MEMORY_ALLOCATION_ERROR; | ||
} | ||
return result; | ||
} | ||
|
||
FormattedPlaceholder InflectionFormatter::format( | ||
FormattedPlaceholder&& arg, FunctionOptions&& options, | ||
UErrorCode& errorCode) const { | ||
if (U_FAILURE(errorCode)) { return {}; } | ||
|
||
// Argument must be present | ||
if (!arg.canFormat()) { | ||
errorCode = U_MF_FORMATTING_ERROR; | ||
return FormattedPlaceholder("inflection"); | ||
} | ||
|
||
// Assumes the argument is not-yet-formatted | ||
const Formattable& toFormat = arg.asFormattable(); | ||
UnicodeString result; | ||
|
||
switch (toFormat.getType()) { | ||
case UFMT_STRING: { | ||
inflection::dialog::SpeakableString input( | ||
toFormat.getString(errorCode)); | ||
inflection::dialog::InflectableStringConcept stringConcept( | ||
model, input); | ||
for (const auto& [key, value] : options.getOptions()) { | ||
auto constraint = model->getFeature(key); | ||
if (constraint != nullptr) { | ||
stringConcept.putConstraint(*constraint, | ||
value.getString(errorCode)); | ||
} | ||
} | ||
result += stringConcept.toSpeakableString()->getPrint(); | ||
break; | ||
} | ||
default: { | ||
result += toFormat.getString(errorCode); | ||
break; | ||
} | ||
} | ||
|
||
return FormattedPlaceholder(arg, FormattedValue(std::move(result))); | ||
} | ||
|
||
class InflectionSelector : public Selector { | ||
public: | ||
void selectKey(FormattedPlaceholder &&arg, FunctionOptions &&options, | ||
const UnicodeString *keys, int32_t keysLen, | ||
UnicodeString *prefs, int32_t &prefsLen, | ||
UErrorCode &status) const override; | ||
|
||
InflectionSelector(const inflection::dialog::SemanticFeatureModel* model) | ||
: model(model) { | ||
} | ||
|
||
private: | ||
const ::inflection::dialog::SemanticFeatureModel* model; | ||
}; | ||
|
||
Selector* InflectionSelectorFactory::createSelector( | ||
const Locale& locale, UErrorCode& errorCode) const { | ||
if (U_FAILURE(errorCode)) { return nullptr; } | ||
|
||
Selector* result = new InflectionSelector(GetSemanticFeatureModel(locale)); | ||
if (result == nullptr) { | ||
errorCode = U_MEMORY_ALLOCATION_ERROR; | ||
} | ||
return result; | ||
} | ||
|
||
void InflectionSelector::selectKey( | ||
FormattedPlaceholder &&arg, FunctionOptions &&options, | ||
const UnicodeString *keys, int32_t keysLen, | ||
UnicodeString *prefs, int32_t &prefsLen, UErrorCode &errorCode) const { | ||
if (U_FAILURE(errorCode)) { return; } | ||
// Argument must be present | ||
if (!arg.canFormat()) { | ||
errorCode = U_MF_SELECTOR_ERROR; | ||
return; | ||
} | ||
|
||
// Assumes the argument is not-yet-formatted | ||
const Formattable& toFormat = arg.asFormattable(); | ||
prefsLen = 0; | ||
auto opt = options.getOptions(); | ||
if (toFormat.getType() == UFMT_STRING) { | ||
inflection::dialog::SpeakableString input( | ||
toFormat.getString(errorCode)); | ||
inflection::dialog::InflectableStringConcept stringConcept( | ||
model, input); | ||
if (!opt.contains(u"select")) { | ||
errorCode = U_MF_SELECTOR_ERROR; | ||
return; | ||
} | ||
for (const auto& [key, value] : options.getOptions()) { | ||
auto constraint = model->getFeature(key); | ||
if (constraint != nullptr) { | ||
stringConcept.putConstraint(*constraint, | ||
value.getString(errorCode)); | ||
} | ||
} | ||
auto value = model->getFeature(opt.at(u"select").getString(errorCode)); | ||
UnicodeString feature; | ||
if (value != nullptr) { | ||
auto result = stringConcept.getFeatureValue(*value); | ||
if (result != nullptr) { | ||
feature = result->getPrint(); | ||
} | ||
} | ||
for (int i = 0; i < keysLen; i++) { | ||
if (feature == keys[i]) { | ||
prefs[prefsLen++] = keys[i]; | ||
} | ||
} | ||
} | ||
return; | ||
} | ||
|
||
} // namespace inflection::message2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2025 Unicode Incorporated and others. All rights reserved. | ||
*/ | ||
#pragma once | ||
|
||
#include <inflection/api.h> | ||
// INFLECTION_CLASS_API | ||
|
||
#include <unicode/uversion.h> | ||
|
||
namespace U_ICU_NAMESPACE::message2 { | ||
class FormatterFactory; | ||
class SelectorFactory; | ||
} // namespace U_ICU_NAMESPACE::message2 | ||
|
||
namespace inflection::message2 { | ||
|
||
/** | ||
* The MF2Factory provide factory method to create custom formatter and selector | ||
* factory to work with icu::message2 library. | ||
* The intend usage is to creat icu::message2::FormatterFactory | ||
* or icu::message2::SelectorFactory and use it with | ||
* icu::message2::MFFunctionRegistry::Builder | ||
* to construct a custom function registry to build MessageFormatter | ||
* with MessageFormatter::Builder. | ||
* | ||
* For example: | ||
* | ||
* auto customRegistry = icu::message2::MFFunctionRegistry::Builder(errorCode) | ||
* .adoptFormatter(FunctionName("inflection"), | ||
* MF2Factory::CreateFormatterFactory(), errorCode) | ||
* .adoptSelector(FunctionName("inflection"), | ||
* MF2Factory::CreateSelectorFactory(), errorCode) | ||
* .build(); | ||
* | ||
* UParseError pe; | ||
* auto mf1 = icu::message2::MessageFormatter::Builder(errorCode) | ||
* .setFunctionRegistry(customRegistry) | ||
* .setLocale(Locale::forLanguageTag("es-MX", errorCode)) | ||
* .setPattern("Location is {$name :inflection hello=world \ | ||
* definiteness=definite number=plural \ | ||
* gender=feminine}", | ||
* pe, errorCode) | ||
* .build(errorCode); | ||
* | ||
* auto mf2 = icu::message2::MessageFormatter::Builder(errorCode) | ||
* .setFunctionRegistry(customRegistry) | ||
* .setLocale(Locale::forLanguageTag("es-MX", errorCode)) | ||
* .setPattern(".local $gender = {$name :inflection \ | ||
* feature=gender} \ | ||
* .local $number = {$name :inflection \ | ||
* feature=number} \ | ||
* .match $gender $number \ | ||
* feminine singular {{Feminine Singular {$name}}}\ | ||
* masculine singular {{Masculine Singular {$name}}}\ | ||
* * * {{other {$name} }}\n", | ||
* pe, errorCode) | ||
* .build(errorCode); | ||
*/ | ||
class INFLECTION_CLASS_API MF2Factory { | ||
public: | ||
/** | ||
* Create an implementation of icu::message2::FormatterFactory*, based on the | ||
* infleciton library, which can be passed to | ||
* icu::messsage2::MFFunctionRegistry::Builder::adoptFormatter | ||
* to register a custom formatter factory. | ||
*/ | ||
static icu::message2::FormatterFactory* CreateFormatterFactory(); | ||
|
||
/** | ||
* Create an implementation of icu::message2::SelectorFactory*, based on the | ||
* infleciton library, which can be passed to | ||
* icu::messsage2::MFFunctionRegistry::Builder::adoptSelector | ||
* to register a custom selector factory. | ||
*/ | ||
static icu::message2::SelectorFactory* CreateSelectorFactory(); | ||
}; | ||
|
||
} // namespace inflection::message2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking about this in more detail, what is the value in these factory methods? It's not caching the factory. There are no complicated data structures. The caller has to delete the factory classes.
Should
InflectionFormatterFactory
andInflectionSelectorFactory
be exposed and allow people to stack allocate them?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value is to hide unnecessary details of which exact concrete class implement the FormatterFactory and SelectorFactory so we do not need to expose unnecessary class.
No, because they need to passed into
MFFunctionRegistry::Builder::adoptFormatter and adoptSelector and own by the Builder. Notice in the test code, no code free them. The code free them is inside MFFunctionRegistry::Builder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went to ICU TC and ask them about C API (Richard is also there). The answer is currently they are waiting for the C++ and Java API to be proven stable first. Then they will consider adding a C API later.