/// <summary> /// Import domain pronunciation. /// </summary> /// <param name="domainPron">Domain LexiconPronunciation.</param> /// <param name="domainTag">Domain tag.</param> /// <param name="first">Whether this pronunciation is the first one in domain lexicon.</param> /// <returns>Whether this LexiconPronunciation changed .</returns> public bool ImportDomainPronunciation(LexiconPronunciation domainPron, string domainTag, bool first) { Helper.ThrowIfNull(domainPron); Helper.ThrowIfNull(domainTag); if (!domainPron.OnlyContainsOneDomain(domainTag)) { throw new InvalidDataException("It is invalid to include any other domain in property level."); } if (!first) { RemovePronunciationIsFirstTags(domainTag); } bool changed = false; foreach (LexiconItemProperty domainProperty in domainPron.Properties) { if (domainProperty.Gender != null || domainProperty.Case != null || domainProperty.Number != null) { throw new InvalidDataException("domain lexicon contains old format <gender> <case> <number>. Please convert them to new format <attr> before import."); } // look for target property that domain tag will import to bool propertyImported = false; foreach (LexiconItemProperty targetProperty in _properties) { if (targetProperty.Gender != null || targetProperty.Case != null || targetProperty.Number != null) { throw new InvalidDataException("target lexicon contains old format <gender> <case> <number>. Please convert them to new format <attr> before import."); } // If main lexicon and domain lexicon have same pos in <pr>, import the domain lexicon attributes to main lexicon. if (HistoryValue.Equals(targetProperty.PartOfSpeech, domainProperty.PartOfSpeech)) { propertyImported = true; // found a proper <pr> to import domain tag. DomainItem domainItem = domainProperty.Domains[domainTag]; Helper.ThrowIfNull(domainItem); DomainItem newDomainItem = new DomainItem(domainItem.Value); if (targetProperty.ImportDomainItem(newDomainItem)) { changed = true; } targetProperty.Domains[domainTag].IsFirstPronunciation = first; // Import domain lexicon attributes to main lexicon. foreach (string attributeKey in domainProperty.AttributeSet.Keys) { if (targetProperty.AttributeSet.ContainsKey(attributeKey)) { // Union main lexicon and domain lexicon and remove duplicate. targetProperty.AttributeSet[attributeKey] = targetProperty.AttributeSet[attributeKey].Union(domainProperty.AttributeSet[attributeKey]).ToList(); } else { targetProperty.AttributeSet.Add(attributeKey, domainProperty.AttributeSet[attributeKey]); } } } } if (!propertyImported) { // not found. Copy the whole <pr> from domain lexicon LexiconItemProperty newProperty = domainProperty.Clone(); foreach (DomainItem domainItem in newProperty.Domains.Values) { domainItem.IsFirstPronunciation = first; } _properties.Add(newProperty); changed = true; } } return changed; }
/// <summary> /// Change to another specified domain. /// </summary> /// <param name="domainItem">DomainItem.</param> public void ChangeDomain(DomainItem domainItem) { if (domainItem == null) { throw new ArgumentNullException(); } _domains.Clear(); _domains.Add(domainItem.Value, domainItem.Clone()); }
/// <summary> /// Load DomainItem. /// </summary> /// <param name="parentProperty">LexiconItemProperty.</param> /// <param name="domainNode">XmlNode.</param> /// <param name="nsmgr">XmlNamespaceManager.</param> /// <param name="contentController">Object.</param> /// <param name="errorSet">ErrorSet.</param> /// <returns>DomainItem.</returns> internal static DomainItem Load(LexiconItemProperty parentProperty, XmlNode domainNode, XmlNamespaceManager nsmgr, Lexicon.ContentControler contentController, ErrorSet errorSet) { Debug.Assert(parentProperty != null && parentProperty.Parent != null && parentProperty.Parent.Parent != null && domainNode != null && contentController != null && nsmgr != null); DomainItem domainItem = new DomainItem(); XmlElement domainElem = domainNode as XmlElement; Debug.Assert(domainElem != null); string domainStatusValue = domainElem.GetAttribute("s"); if (!string.IsNullOrEmpty(domainStatusValue)) { domainItem.Status = (Lexicon.LexiconStatus)Enum.Parse(typeof(Lexicon.LexiconStatus), domainStatusValue, true); // Lexicon object is shared with lexicon reviewer tool, // We drop those items if they have "deleted" status when it is not loaded by lexicon reviewer tool if (domainItem.Status == Lexicon.LexiconStatus.Deleted && !contentController.IsHistoryCheckingMode) { domainItem = null; } } if (domainItem != null) { // Check whether pronunciation is prefered in this domain string preferedValue = domainElem.GetAttribute("p"); if (!string.IsNullOrEmpty(preferedValue)) { domainItem.IsFirstPronunciation = bool.Parse(preferedValue); } string domainValue = domainElem.GetAttribute("v"); string originalDomainValue = domainElem.GetAttribute("vo"); if (string.IsNullOrEmpty(domainValue)) { Error error = new Error(DomainError.EmptyDomain); errorSet.Add(LexiconError.DomainError, error, parentProperty.Parent.Parent.Text, parentProperty.Parent.Symbolic); domainItem = null; } else { domainItem.Value = domainValue.ToLower(); if (!string.IsNullOrEmpty(originalDomainValue) && domainItem.Status != Lexicon.LexiconStatus.Original) { domainItem.OldValue = originalDomainValue.ToLower(); } else { domainItem.OldValue = domainValue; } } } return domainItem; }
/// <summary> /// Add domain. /// </summary> /// <param name="domainItem">DomainItem.</param> /// <returns>Whether imported.</returns> public bool ImportDomainItem(DomainItem domainItem) { if (domainItem == null) { throw new ArgumentNullException(); } bool imported = false; if (_domains.Count == 0) { _domains.Add(DomainItem.GeneralDomain, new DomainItem()); } if (!_domains.ContainsKey(domainItem.Value)) { _domains.Add(domainItem.Value, domainItem.Clone()); imported = true; } return imported; }
/// <summary> /// Clone current domain. /// </summary> /// <returns>Cloned DomainItem.</returns> public DomainItem Clone() { DomainItem clonedItem = new DomainItem(); this.CopyTo(clonedItem); clonedItem.IsFirstPronunciation = IsFirstPronunciation; return clonedItem; }