Skip to content

Commit ec6d4a4

Browse files
authored
Inflection 77: Add C and C++ API for stating source grammemes and tests for the same. (#174)
1 parent 643b085 commit ec6d4a4

File tree

13 files changed

+206
-21
lines changed

13 files changed

+206
-21
lines changed

inflection/src/inflection/dialog/DisplayValue.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
namespace inflection::dialog {
1010

1111

12-
DisplayValue::DisplayValue(const ::std::u16string& displayString, const ::std::map<SemanticFeature, ::std::u16string>& constraintMap)
12+
DisplayValue::DisplayValue(
13+
const ::std::u16string& displayString,
14+
const ::std::map<SemanticFeature, ::std::u16string>& constraintMap
15+
)
1316
: super()
1417
, displayString(displayString)
1518
, constraintMap(constraintMap)
@@ -21,11 +24,23 @@ DisplayValue::DisplayValue(const ::std::u16string& value)
2124
{
2225
}
2326

24-
DisplayValue::DisplayValue(const SpeakableString& value, const SemanticFeature& speakFeature)
25-
: DisplayValue(value.getPrint(), {})
27+
DisplayValue::DisplayValue(
28+
const SpeakableString& value,
29+
const SemanticFeature& speakFeature
30+
)
31+
: DisplayValue(value, speakFeature, {})
32+
{
33+
}
34+
35+
DisplayValue::DisplayValue(
36+
const SpeakableString& value,
37+
const SemanticFeature& speakFeature,
38+
const ::std::map<SemanticFeature, ::std::u16string>& constraintMap
39+
)
40+
: DisplayValue(value.getPrint(), constraintMap)
2641
{
2742
if (!value.speakEqualsPrint()) {
28-
constraintMap.emplace(speakFeature, value.getSpeak());
43+
this->constraintMap.emplace(speakFeature, value.getSpeak());
2944
}
3045
}
3146

inflection/src/inflection/dialog/DisplayValue.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class INFLECTION_CLASS_API inflection::dialog::DisplayValue
7272
* @param speakFeature The speakFeature from the SemanticFeatureModel that represents the SemanticFeature for the speak information for a SpeakableString.
7373
*/
7474
DisplayValue(const SpeakableString& value, const SemanticFeature& speakFeature);
75+
/**
76+
* Construct a display value with a SpeakableString.
77+
* @param value A SpeakableString
78+
* @param speakFeature The speakFeature from the SemanticFeatureModel that represents the SemanticFeature for the speak information for a SpeakableString.
79+
* @param constraintMap The intitial constraint map.
80+
*/
81+
DisplayValue(const SpeakableString& value, const SemanticFeature& speakFeature, const ::std::map<SemanticFeature, ::std::u16string>& constraintMap);
7582
/**
7683
* The destructor
7784
*/

inflection/src/inflection/dialog/InflectableStringConcept-c.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55

66
#include <inflection/dialog/InflectableStringConcept.hpp>
77
#include <inflection/dialog/SemanticFeatureConcept.h>
8+
#include <inflection/dialog/SemanticFeature.hpp>
9+
#include <inflection/dialog/SemanticUtils.hpp>
10+
#include <inflection/dialog/SemanticFeatureModel.hpp>
811
#include <inflection/exception/ClassCastException.hpp>
912
#include <inflection/util/ULocale.hpp>
1013
#include <inflection/util/TypeConversionUtils.hpp>
1114
#include <inflection/util/Validate.hpp>
15+
#include <inflection/npc.hpp>
1216

1317
INFLECTION_CAPI IDSemanticFeatureConcept* iinf_toSemanticFeatureConcept(IDInflectableStringConcept* thisObject, UErrorCode*)
1418
{
@@ -47,6 +51,27 @@ iinf_create(const IDSemanticFeatureModel* model, const IDSpeakableString* value,
4751
return nullptr;
4852
}
4953

54+
INFLECTION_CAPI IDInflectableStringConcept*
55+
iinf_createWithDefaults(const IDSemanticFeatureModel* model, const IDSpeakableString* value,
56+
const IDDisplayValue_Constraint* defaultConstraints, int32_t defaultConstraintsLen, UErrorCode* status)
57+
{
58+
if (status != nullptr && U_SUCCESS(*status)) {
59+
try {
60+
auto defaultConstraintsMap(inflection::dialog::SemanticUtils::to_constraintMap(*npc((const inflection::dialog::SemanticFeatureModel*)model), defaultConstraints, defaultConstraintsLen));
61+
62+
return (IDInflectableStringConcept*) new ::inflection::dialog::InflectableStringConcept(
63+
(const ::inflection::dialog::SemanticFeatureModel*)model,
64+
*((const ::inflection::dialog::SpeakableString*)value),
65+
defaultConstraintsMap
66+
);
67+
}
68+
catch (const ::std::exception& e) {
69+
inflection::util::TypeConversionUtils::convert(e, status);
70+
}
71+
}
72+
return nullptr;
73+
}
74+
5075
INFLECTION_CAPI void
5176
iinf_destroy(IDInflectableStringConcept* thisObject)
5277
{

inflection/src/inflection/dialog/InflectableStringConcept.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,23 @@
1515

1616
namespace inflection::dialog {
1717

18-
InflectableStringConcept::InflectableStringConcept(const SemanticFeatureModel* model, const SpeakableString& value)
18+
19+
InflectableStringConcept::InflectableStringConcept(
20+
const SemanticFeatureModel* model,
21+
const SpeakableString& value
22+
)
23+
: InflectableStringConcept(model, value, {})
24+
{
25+
}
26+
27+
InflectableStringConcept::InflectableStringConcept(
28+
const SemanticFeatureModel* model,
29+
const SpeakableString& value,
30+
const ::std::map<SemanticFeature, ::std::u16string>& initialConstraints
31+
)
1932
: super(model)
2033
, value(value)
21-
, defaultDisplayValue(value, *npc(super::getSpeakFeature()))
34+
, defaultDisplayValue(value, *npc(super::getSpeakFeature()), initialConstraints)
2235
{
2336
}
2437

@@ -49,6 +62,7 @@ SpeakableString* InflectableStringConcept::getFeatureValue(const SemanticFeature
4962
return nullptr;
5063
}
5164

65+
5266
bool InflectableStringConcept::isExists() const
5367
{
5468
return getDisplayValue(false).has_value();
@@ -63,10 +77,7 @@ ::std::optional<DisplayValue> InflectableStringConcept::getDisplayValue(bool all
6377
{
6478
auto defaultDisplayFunction = npc(getModel())->getDefaultDisplayFunction();
6579
if (defaultDisplayFunction != nullptr && !constraints.empty()) {
66-
::std::map<SemanticFeature, ::std::u16string> constraintMap;
67-
if (!value.speakEqualsPrint()) {
68-
constraintMap.emplace(*npc(getSpeakFeature()), value.getSpeak());
69-
}
80+
::std::map<SemanticFeature, ::std::u16string> constraintMap = defaultDisplayValue.getConstraintMap();
7081
SemanticFeatureModel_DisplayData displayData({DisplayValue(value.getPrint(), constraintMap)});
7182
::std::unique_ptr<DisplayValue> returnVal(npc(defaultDisplayFunction)->getDisplayValue(displayData, constraints, allowInflectionGuess));
7283
if (returnVal != nullptr) {

inflection/src/inflection/dialog/InflectableStringConcept.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <inflection/dialog/SpeakableString.h>
77
#include <inflection/dialog/SemanticFeatureConcept.h>
88
#include <inflection/dialog/SemanticFeatureModel.h>
9-
9+
#include <inflection/dialog/DisplayValue.h>
1010
/**
1111
* An object that provides a way to format a word with additional grammatical category values or semantic features of a word for a given language.
1212
*/
@@ -34,6 +34,19 @@ INFLECTION_CAPI IDInflectableStringConcept* iinf_toInflectableStringConcept(IDSe
3434
* This is set to a failure when a failure has occurred during execution.
3535
*/
3636
INFLECTION_CAPI IDInflectableStringConcept* iinf_create(const IDSemanticFeatureModel* model, const IDSpeakableString* value, UErrorCode* status);
37+
/**
38+
* Constructs a concept given a semantic feature model and a speakable string
39+
*
40+
* @param model - The semantic feature model required to initialize the concept.
41+
* @param value - The speakable string to convert to a concept
42+
* @param defaultConstraints - The initial defaultConstraints to apply to the concept
43+
* @param defaultConstraintsLen - The number of semantic features and values provided
44+
* @param status Must be a valid pointer to an error code value,
45+
* which must not indicate a failure before the function call.
46+
* This is set to a failure when a failure has occurred during execution.
47+
*/
48+
INFLECTION_CAPI IDInflectableStringConcept* iinf_createWithDefaults(const IDSemanticFeatureModel* model, const IDSpeakableString* value,
49+
const IDDisplayValue_Constraint* defaultConstraints, int32_t defaultConstraintsLen, UErrorCode* status);
3750
/**
3851
* Destructor
3952
*/

inflection/src/inflection/dialog/InflectableStringConcept.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ class INFLECTION_CLASS_API inflection::dialog::InflectableStringConcept
6969
* @param value - The speakable string to convert to a concept
7070
*/
7171
InflectableStringConcept(const SemanticFeatureModel* model, const SpeakableString& value);
72+
73+
/**
74+
* Constructs a concept given a semantic feature model and a speakable string
75+
*
76+
* @param model - The semantic feature model required to initialize the concept.
77+
* @param value - The speakable string to convert to a concept
78+
* @param initialConstraints - The initial constraints for the map.
79+
*/
80+
InflectableStringConcept(const SemanticFeatureModel* model, const SpeakableString& value, const std::map<SemanticFeature, ::std::u16string>& initialConstraints);
7281
/**
7382
* Copy constructor
7483
*/

inflection/src/inflection/grammar/synthesis/FrGrammarSynthesizer_CountLookupFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ ::std::optional<::std::u16string> FrGrammarSynthesizer_CountLookupFunction::dete
6767
int64_t wordGrammemes = 0;
6868
dictionary.getCombinedBinaryType(&wordGrammemes, word);
6969
if ((wordGrammemes & properNounProperty) == properNounProperty) {
70-
return {{}};
70+
return std::u16string{};
7171
}
7272
if (checkInvariantNouns(word, wordGrammemes)) {
7373
return GrammemeConstants::NUMBER_SINGULAR();

inflection/src/inflection/grammar/synthesis/HiGrammarSynthesizer_HiDisplayFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ ::std::optional<::std::u16string> HiGrammarSynthesizer_HiDisplayFunction::inflec
185185

186186
::std::optional<::std::vector<::std::u16string>> HiGrammarSynthesizer_HiDisplayFunction::inflectSignificantWords(const std::vector<::std::u16string> &words, const ::std::map<SemanticFeature, ::std::u16string> &constraints, bool enableInflectionGuess) const {
187187
if (words.empty()) {
188-
return {{}};
188+
return std::vector<std::u16string>{};
189189
}
190190
const auto &dictionary = dictionaryInflector.getDictionary();
191191
int64_t adpositionIndex = -1;

inflection/src/inflection/grammar/synthesis/PtGrammarSynthesizer_PtDisplayFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ ::std::optional<::std::vector<::std::u16string>> PtGrammarSynthesizer_PtDisplayF
195195

196196
::std::optional<::std::vector<::std::u16string>> PtGrammarSynthesizer_PtDisplayFunction::inflectSignificantWords(const std::vector<::std::u16string> &words, const ::std::map<::inflection::dialog::SemanticFeature, ::std::u16string> &constraints, bool enableInflectionGuess) const {
197197
switch (words.size()) {
198-
case 0: return {{}};
198+
case 0: return std::vector<std::u16string>{};
199199
case 1: {
200200
int64_t wordType = 0;
201201
dictionary.getCombinedBinaryType(&wordType, words[0]);

inflection/test/resources/inflection/dialog/inflection/es.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,11 @@
221221
<test><source definiteness="definite">álgebra añeja</source><result>el álgebra añeja</result></test> <!-- An exception to the gender rule. -->
222222
<test><source definiteness="definite">añeja álgebra</source><result>la añeja álgebra</result></test>
223223
<test><source>área</source><result withDemAdj="esta área"/></test>
224+
225+
<!-- New Tests for stating initial grammemes -->
226+
<test><source definiteness="definite">cometa</source><initial gender="masculine"/><result number="singular" gender="masculine">el cometa</result></test>
227+
<test><source definiteness="definite">cometa</source><initial gender="feminine"/><result number="singular" gender="feminine">la cometa</result></test>
228+
<test><source definiteness="definite">QQQQ</source><initial gender="masculine" number="plural"/><result number="plural" gender="masculine">los QQQQ</result></test>
229+
<test><source definiteness="definite">QQQQ</source><initial gender="feminine" number="plural"/><result number="plural" gender="feminine">las QQQQ</result></test>
230+
224231
</inflectionTest>

0 commit comments

Comments
 (0)