Skip to content

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 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions inflection/src/inflection/message2/MF2Factory.cpp
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();
}
Comment on lines +45 to +51
Copy link
Member

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 and InflectionSelectorFactory be exposed and allow people to stack allocate them?

Copy link
Collaborator Author

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.

Should InflectionFormatterFactory and InflectionSelectorFactory be exposed and allow people to stack allocate them?

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.

Copy link
Collaborator Author

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.


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
79 changes: 79 additions & 0 deletions inflection/src/inflection/message2/MF2Factory.hpp
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
Loading