/// <summary> /// Validate all the category names under the category. /// </summary> /// <param name="categorySet">Set of category name.</param> /// <param name="category">Category.</param> private void ValidateCategoryName(Collection<string> categorySet, AttributeCategory category) { if (category != null) { if (categorySet.Contains(category.Name)) { this.ErrorSet.Add(LexicalAttributeSchemaError.DuplicateCategoryName, category.Name); } else { categorySet.Add(category.Name); } if (category.Values != null) { foreach (AttributeValue value in category.Values) { if (value.Categories != null) { foreach (AttributeCategory subCategory in value.Categories) { ValidateCategoryName(categorySet, subCategory); } } } } } }
/// <summary> /// Validate continued ID for each node under category node. /// </summary> /// <param name="category">Category node.</param> /// <param name="idIndex">First id.</param> /// <param name="errorFound">Whether found error.</param> private void ValidateContinuedId(AttributeCategory category, ref int idIndex, ref bool errorFound) { if (category != null) { if (category.Id != idIndex) { this.ErrorSet.Add(LexicalAttributeSchemaError.NotContinuedId, "category=" + category.Name, category.Id.ToString(CultureInfo.InvariantCulture)); errorFound = true; } else { idIndex++; if (idIndex > _maxId) { errorFound = true; } else if (category.Values != null) { foreach (AttributeValue value in category.Values) { if (value.Id != idIndex) { this.ErrorSet.Add(LexicalAttributeSchemaError.NotContinuedId, "value=" + value.Name, value.Id.ToString(CultureInfo.InvariantCulture)); errorFound = true; } else { idIndex++; if (idIndex > _maxId) { errorFound = true; } else if (value.Categories != null) { foreach (AttributeCategory subCategory in value.Categories) { ValidateContinuedId(subCategory, ref idIndex, ref errorFound); if (errorFound) { break; } } } } if (errorFound) { break; } } } } } }
/// <summary> /// Check whether exists duplicate name under the category /// Currently we only check value under POS category, as we need to ensure unique POS values. /// </summary> /// <param name="valueSet">Set of value name.</param> /// <param name="category">Category.</param> private void CheckDuplicateValueName(Collection<string> valueSet, AttributeCategory category) { if (category != null && category.Values != null) { foreach (AttributeValue value in category.Values) { if (valueSet.Contains(value.Name)) { this.ErrorSet.Add(LexicalAttributeSchemaError.DuplicatePosValueName, value.Name); } else { valueSet.Add(value.Name); } if (value.Categories != null) { foreach (AttributeCategory subCategory in value.Categories) { CheckDuplicateValueName(valueSet, subCategory); } } } } }
/// <summary> /// Validate category values. /// </summary> /// <param name="category">Categories to be validated.</param> /// <param name="existedValues">Existed values.</param> private void ValidateCategoryValues(AttributeCategory category, Dictionary<string, string> existedValues) { foreach (AttributeValue attributeValue in category.Values) { if (!existedValues.ContainsKey(attributeValue.Name)) { existedValues.Add(attributeValue.Name, category.Name); } else { this.ErrorSet.Add(new Error(LexicalAttributeSchemaError.DuplicatePosValue, attributeValue.Name, category.Name, existedValues[attributeValue.Name])); } if (attributeValue.Categories != null) { foreach (AttributeCategory subCategory in attributeValue.Categories) { ValidateCategoryValues(subCategory, existedValues); } } } }
/// <summary> /// Check POS tagging flag for each POS value whether set to true. /// </summary> /// <param name="category">Category.</param> private void CheckPosTaggingFlag(AttributeCategory category) { if (category == null) { throw new ArgumentNullException("category"); } foreach (AttributeValue value in category.Values) { if (!value.PosTagging) { if (value.Categories != null && value.Categories.Count > 0) { foreach (AttributeCategory subCategory in value.Categories) { CheckPosTaggingFlag(subCategory); } } else { this.ErrorSet.Add(LexicalAttributeSchemaError.MissPosTaggingSetting, value.Name); } } } }
/// <summary> /// Get the attribute category from the required category root and queried by its name. /// </summary> /// <param name="attributeCategory">Required category root.</param> /// <param name="name">Queied category name.</param> /// <returns>The arrribute category.</returns> private AttributeCategory GetCategory(AttributeCategory attributeCategory, string name) { if (attributeCategory == null) { throw new ArgumentNullException("attributeCategory"); } AttributeCategory category = null; if (attributeCategory.Values != null && attributeCategory.Values.Count > 0) { foreach (AttributeValue value in attributeCategory.Values) { if (value.Categories != null && value.Categories.Count > 0) { foreach (AttributeCategory subCategory in value.Categories) { category = GetCategory(subCategory, name); if (category != null) { break; } } } } } return category; }
/// <summary> /// Remove all the categories with no value under the assigned category. /// </summary> /// <param name="category">Category.</param> private void RemoveEmptyCategory(AttributeCategory category) { foreach (AttributeValue value in category.Values) { if (value != null && value.Categories != null && value.Categories.Count > 0) { Collection<AttributeCategory> removedCategories = new Collection<AttributeCategory>(); foreach (AttributeCategory subCategory in value.Categories) { if (subCategory.Values == null || subCategory.Values.Count == 0) { removedCategories.Add(subCategory); } else { RemoveEmptyCategory(subCategory); } } foreach (AttributeCategory subCategory in removedCategories) { value.Categories.Remove(subCategory); } } if (value != null && value.Categories != null && value.Categories.Count == 0) { value.UpdateCategories(null); } } }
/// <summary> /// Set continued ID for each node under category node. /// </summary> /// <param name="category">Category node.</param> /// <param name="idIndex">First id.</param> private void SetContinuedId(AttributeCategory category, ref int idIndex) { if (category != null) { category.Id = idIndex++; if (category.Id < _maxId && category.Values != null) { foreach (AttributeValue value in category.Values) { value.Id = idIndex++; if (value.Id < _maxId && value.Categories != null) { foreach (AttributeCategory subCategory in value.Categories) { SetContinuedId(subCategory, ref idIndex); if (idIndex > _maxId) { break; } } } if (idIndex > _maxId) { break; } } } } }
/// <summary> /// Generate the equation string for the value under the attribute category /// <param /> /// Example: /// GenearteString(node of "POS", "noun") /// The return value is /// POS=NOM=1 NOM_CLASS=noun=3. /// </summary> /// <param name="attributeCategory">Attribute category used for search.</param> /// <param name="value">Value.</param> /// <returns>Equation string for the value; for example: POS=NOM=1 NOM_CLASS=noun=3.</returns> private string GenerateString(AttributeCategory attributeCategory, string value) { string concatenatedString = "\t"; string generatedString = string.Empty; if (attributeCategory.Values != null) { foreach (AttributeValue attributeValue in attributeCategory.Values) { if (attributeValue.Name.Equals(value)) { generatedString = attributeCategory.Name + "=" + value + "=" + attributeValue.Id.ToString(CultureInfo.InvariantCulture); break; } else if (attributeValue.Categories != null && attributeValue.Categories.Count > 0) { string appendString = string.Empty; foreach (AttributeCategory subCategory in attributeValue.Categories) { appendString = GenerateString(subCategory, value); if (!string.IsNullOrEmpty(appendString)) { break; } } if (!string.IsNullOrEmpty(appendString)) { generatedString = attributeCategory.Name + "=" + attributeValue.Name + "=" + attributeValue.Id.ToString(CultureInfo.InvariantCulture) + concatenatedString + appendString; break; } } } } return generatedString; }
private void WriteXmlCategoryNode(XmlWriter writer, AttributeCategory category) { if (category != null) { writer.WriteStartElement("Category"); writer.WriteAttributeString("name", category.Name); writer.WriteAttributeString("ID", category.Id.ToString(CultureInfo.InvariantCulture)); writer.WriteAttributeString("Mean", category.Mean.ToString(CultureInfo.InvariantCulture)); writer.WriteAttributeString("InvStdDev", category.InvStdDev.ToString(CultureInfo.InvariantCulture)); if (category.Values != null) { foreach (AttributeValue value in category.Values) { writer.WriteStartElement("Value"); writer.WriteAttributeString("name", value.Name); writer.WriteAttributeString("ID", value.Id.ToString(CultureInfo.InvariantCulture)); if (value.PosTagging) { writer.WriteAttributeString("posTagging", value.PosTagging.ToString().ToLower(CultureInfo.InvariantCulture)); } if (value.Categories != null) { foreach (AttributeCategory subCategory in value.Categories) { WriteXmlCategoryNode(writer, subCategory); } } writer.WriteEndElement(); } } writer.WriteEndElement(); } }
/// <summary> /// Load category from xml node of category. /// </summary> /// <param name="categoryNode">Xml category node.</param> /// <returns>Attribute category.</returns> private AttributeCategory LoadXmlCategoryNode(XmlNode categoryNode) { AttributeCategory category = null; if (categoryNode.Name.Equals("Category")) { category = new AttributeCategory(); Debug.Assert(categoryNode.Attributes["name"] != null); category.Name = categoryNode.Attributes["name"].InnerText.Trim(); Debug.Assert(categoryNode.Attributes["ID"] != null); category.Id = int.Parse(categoryNode.Attributes["ID"].InnerText.Trim(), NumberFormatInfo.InvariantInfo); // Permit category load mean and invStdDev if (categoryNode.Attributes["Mean"] != null) { category.Mean = float.Parse(categoryNode.Attributes["Mean"].InnerText.Trim(), NumberFormatInfo.InvariantInfo); } else { category.Mean = 0.0f; } if (categoryNode.Attributes["InvStdDev"] != null) { category.InvStdDev = float.Parse(categoryNode.Attributes["InvStdDev"].InnerText.Trim(), NumberFormatInfo.InvariantInfo); } else { category.InvStdDev = 1.0f; } // Permit category dosen't have child if (categoryNode.HasChildNodes) { foreach (XmlNode valueNode in categoryNode.ChildNodes) { if (valueNode.NodeType == XmlNodeType.Comment) { continue; } Debug.Assert(valueNode.Name.Equals("Value")); if (valueNode.Name.Equals("Value")) { AttributeValue value = new AttributeValue(); Debug.Assert(valueNode.Attributes["name"] != null); value.Name = valueNode.Attributes["name"].InnerText.Trim(); Debug.Assert(valueNode.Attributes["ID"] != null); value.Id = int.Parse(valueNode.Attributes["ID"].InnerText.Trim(), NumberFormatInfo.InvariantInfo); if (valueNode.Attributes["posTagging"] != null) { value.PosTagging = bool.Parse(valueNode.Attributes["posTagging"].InnerText.Trim()); } if (valueNode.HasChildNodes) { value.UpdateCategories(new Collection<AttributeCategory>()); foreach (XmlNode subCategoryNode in valueNode.ChildNodes) { AttributeCategory subCategory = LoadXmlCategoryNode(subCategoryNode); value.Categories.Add(subCategory); } } category.Values.Add(value); } } } } return category; }