00001 #include "LinguisticDefinition/LingFeaturesSet.h"
00002
00003 using namespace std;
00004 using namespace LinguisticDefinition;
00005
00006
00007
00011 LingFeaturesSet::LingFeaturesSet() :
00012 d_lingDef(NULL),
00013 d_posDef(NULL),
00014 d_sharedPosDef(true) {
00015 }
00016
00020 LingFeaturesSet::~LingFeaturesSet() {
00021 {for (ElementIterator it = d_elements.begin();
00022 it != d_elements.end(); ++it) {
00023 delete *it;
00024 }}
00025 {for (ElementIterator it = d_removedElements.begin();
00026 it != d_removedElements.end(); ++it) {
00027 delete *it;
00028 }}
00029 }
00030
00034 void LingFeaturesSet::add(const LingFeatures &element) {
00035 if (d_lingDef == NULL) {
00036 d_lingDef = element.getLingDef();
00037 } else if (d_lingDef != element.getLingDef()) {
00038 d_sharedPosDef = false;
00039 }
00040
00041 if (d_sharedPosDef && d_posDef == NULL) {
00042 d_posDef = element.getPosDef();
00043 } else if (d_sharedPosDef && d_posDef != element.getPosDef()) {
00044 d_sharedPosDef = false;
00045 }
00046
00047 d_elements.insert(new LingFeatures(element));
00048 }
00049
00053 bool LingFeaturesSet::isEmpty() const {
00054 return d_elements.empty();
00055 }
00056
00060 void LingFeaturesSet::set(const string &feature) {
00061 const LingDef::Feature *featureDef = NULL;
00062 if (d_sharedPosDef && d_posDef != NULL) {
00063
00064 featureDef = d_posDef->getFeature(feature);
00065 }
00066
00067 if (featureDef != NULL) {
00068 set(*featureDef);
00069 } else {
00070
00071
00072
00073 cerr << "Warning: unable to look up '" << feature << "'" << endl;
00074 {for (ElementIterator it = d_elements.begin();
00075 it != d_elements.end(); ++it) {
00076 (*it)->set(feature);
00077 }}
00078 }
00079 }
00080
00084 void LingFeaturesSet::set(const LingDef::Feature &feature) {
00085 const LingDef::Feature *enumFeature = feature.getParentEnum();
00086
00087
00088
00089 if (enumFeature != NULL && !enumFeature->allowSeveralValues()) {
00090
00091
00092 ElementSet elementsCopy(d_elements);
00093 {for (ElementIterator it = elementsCopy.begin();
00094 it != elementsCopy.end(); ++it) {
00095 if (!(*it)->has(*enumFeature)) {
00096 LingFeatures *newFeatures = new LingFeatures(**it);
00097 newFeatures->set(feature);
00098 d_elements.insert(newFeatures);
00099 }
00100 }}
00101
00102 } else {
00103
00104 {for (ElementSet::iterator it = d_elements.begin();
00105 it != d_elements.end(); ++it) {
00106 (*it)->set(feature);
00107 }}
00108 }
00109 }
00110
00114 bool LingFeaturesSet::has(const string &feature) const {
00115 if (d_sharedPosDef && d_posDef != NULL) {
00116
00117 const LingDef::Feature *featureDef = d_posDef->getFeature(feature);
00118 if (featureDef == NULL) {
00119 return false;
00120 }
00121 return has(*featureDef);
00122 }
00123
00124
00125 cerr << "Warning: '" << feature << "' not shared pos" << endl;
00126 {for (ElementIterator it = d_elements.begin();
00127 it != d_elements.end(); ++it) {
00128 if ((*it)->has(feature)) {
00129 return true;
00130 }
00131 }}
00132 return false;
00133 }
00134
00138 bool LingFeaturesSet::has(const LingDef::Feature &feature) const {
00139 {for (ElementIterator it = d_elements.begin();
00140 it != d_elements.end(); ++it) {
00141 if ((*it)->has(feature)) {
00142 return true;
00143 }
00144 }}
00145 return false;
00146 }
00147
00151 void LingFeaturesSet::setEnum(const string &enumFeature,
00152 const string &valueFeature) {
00153 if (d_sharedPosDef && d_posDef != NULL) {
00154 const LingDef::Feature *enumFeatureDef = d_posDef->getFeature(enumFeature);
00155 if (enumFeatureDef != NULL) {
00156 const LingDef::Feature *valueFeatureDef =
00157 d_posDef->getFeature(valueFeature);
00158
00159 if (valueFeatureDef != NULL) {
00160 setEnum(*enumFeatureDef, *valueFeatureDef);
00161 }
00162 }
00163 } else {
00164
00165 }
00166 }
00167
00171 void LingFeaturesSet::setEnum(const LingDef::Feature &enumFeature,
00172 const LingDef::Feature &valueFeature) {
00173 if (valueFeature.getParentEnum() == &enumFeature) {
00174 set(valueFeature);
00175 }
00176 }
00177
00181 void LingFeaturesSet::getEnumValues(const string &enumFeature,
00182 std::set<const LingDef::Feature *> &result)
00183 const {
00184 if (d_sharedPosDef && d_posDef != NULL) {
00185 const LingDef::Feature *featureDef = d_posDef->getFeature(enumFeature);
00186 if (featureDef != NULL) {
00187 getEnumValues(*featureDef, result);
00188 }
00189 } else {
00190
00191 }
00192 }
00193
00197 void LingFeaturesSet::getEnumValues(const LingDef::Feature &enumFeature,
00198 std::set<const LingDef::Feature *> &result)
00199 const {
00200 {for (ElementIterator it = d_elements.begin();
00201 it != d_elements.end(); ++it) {
00202 (*it)->getEnumValues(enumFeature, result);
00203 }}
00204 }
00205
00209 LingFeaturesSet::ElementIterator LingFeaturesSet::elementsBegin() const {
00210 return d_elements.begin();
00211 }
00212
00216 LingFeaturesSet::ElementIterator LingFeaturesSet::elementsEnd() const {
00217 return d_elements.end();
00218 }