/// <summary> /// Идентификация всех сущностей в подпредложении /// </summary> /// <param name="subSentence">подпредложение</param> private void IdentifyEntities(SubSentence subSentence) { if (subSentence.SubTextInfo != null) { IdentifyEntities(subSentence.SubTextInfo); } else { int childIndex = 0; foreach (var unit in subSentence.Units) { while ((childIndex < subSentence.Children.Length) && (subSentence.Children[childIndex].StartPosition < unit.PositionInfo.Start)) { IdentifyEntities(subSentence.Children[childIndex]); ++childIndex; } if (unit.IsEntity) { IdentifyEntity((Entity)unit); } } for (; childIndex < subSentence.Children.Length; ++childIndex) { IdentifyEntities(subSentence.Children[childIndex]); } } }
/// <summary> /// Создание подпредложения из xml-элемента /// </summary> /// <param name="subSentence">xml-элемент</param> /// <param name="language">язык предложения</param> /// <returns>подпредложение</returns> private static SubSentence SubSentenceFromXElement(XElement subSentence, string language) { #region [.defense.] if (!subSentence.IsName(UnitTextType.SUB_SENT)) { throw new ArgumentException("element isn't SUB_SENT"); } #endregion SubSentenceType type = (SubSentenceType)Enum.Parse(typeof(SubSentenceType), subSentence.GetAttributeValue(RDF.Attribute.Type)); SubSentence result = new SubSentence(type, _positionInSentence); switch (type) { case SubSentenceType.AdjunctPhrase: case SubSentenceType.DanglingParticiple: case SubSentenceType.Default: case SubSentenceType.Introductory: case SubSentenceType.Participial: case SubSentenceType.Subordinate: SimpleSubSentenceFromXElement(result, subSentence, language); break; case SubSentenceType.ApproximatingForceps: case SubSentenceType.DirectSpeech: SubTextFromXElement(result, subSentence, language); break; #region [.defense.] default: throw new InvalidOperationException("Unknown subSentence type"); #endregion } return(result); }
/// <summary> /// Создание подтекста из xml-элемента /// </summary> /// <param name="subSentence">родительское подпредложение</param> /// <param name="subSentenceXML">xml-элемент подтекста</param> /// <param name="language">язык предложения</param> private static void SubTextFromXElement(SubSentence subSentence, XElement subSentenceXML, string language) { int position = _positionInSentence; var entities = _sentenceEntities; SubTextInfo subText = new SubTextInfo( GetSymbolFromXElement(subSentenceXML.Elements().First()), GetSymbolFromXElement(subSentenceXML.Elements().Last())); subText.Sentences = TextFromXElement(subSentenceXML).Sentences; subSentence.SubTextInfo = subText; _sentenceEntities = entities; _positionInSentence = position + GetFullValue(subSentenceXML).Length; }
/// <summary> /// Пополнение заданной коллекции заданным подпредложением и всеми его подпредложениями-потомками /// </summary> /// <param name="parentSubSentence">заданное подпредложение</param> /// <param name="result">пополняемая коллекция</param> private void GetFlattenSubSentences(SubSentence parentSubSentence, ref List <SubSentence> result) { result.Add(parentSubSentence); if (parentSubSentence.SubTextInfo == null) { var children = ((SubSentence)parentSubSentence).Children; if (children == null) { return; } foreach (var subSentence in children) { GetFlattenSubSentences(subSentence, ref result); } } }
/// <summary> /// Создание объекта подпредложения /// </summary> /// <param name="parentSentence">родительское предложение</param> /// <param name="textSentence">текстовое предложение</param> /// <param name="entities">список сущностей предложения</param> /// <param name="contentIndexes">индексы контентов в предложении</param> /// <returns>объект подпредложения</returns> private SubSentence CreateSubSentenceObject( Sentence parentSentence, TextParsing.Sentence textSentence, IEnumerable <Entity> entities, IEnumerable <int> contentIndexes) { var result = new SubSentence(SubSentenceType.Default, 0) { ParentObject = parentSentence }; var units = GetSubSentenceUnits(result, textSentence, entities); var subSentences = GetChildContentSubSentences(contentIndexes, units, parentSentence); result.SetUnits(units); result.AppendSubSentences(subSentences); return(result); }
/// <summary> /// Получение коллекции элементов подпредложения /// </summary> /// <param name="parentSubSentence">объект подпредложения</param> /// <param name="textSentence">текстовое подпредложение</param> /// <param name="entities">список сущностей</param> /// <returns>коллекция элементов предложения</returns> private List <UnitTextBase> GetSubSentenceUnits(SubSentence parentSubSentence, TextParsing.Sentence textSentence, IEnumerable <Entity> entities) { List <UnitTextBase> result = new List <UnitTextBase>(); int currentTextPos = 0; foreach (Entity entity in entities) { entity.MovePosition(textSentence.GetFullStartPosition()); if (entity.PositionInfo.Start != 0) { result.Add(SetParentSubSentence(CreateUnmarkedText(textSentence.Text, currentTextPos, entity.PositionInfo.Start), parentSubSentence)); } result.Add(SetParentSubSentence(entity, parentSubSentence)); currentTextPos = entity.PositionInfo.End; } if (currentTextPos < textSentence.Text.Length) { result.Add(SetParentSubSentence(CreateUnmarkedText(textSentence.Text, currentTextPos, textSentence.Text.Length), parentSubSentence)); } return(result); }
/// <summary> /// Создание простого подпредложения из xml-элемента /// </summary> /// <param name="subSentence">xml-элемент</param> /// <param name="language">язык предложения</param> /// <returns>простое подпредложение</returns> private static void SimpleSubSentenceFromXElement(SubSentence subSentence, XElement subSentenceXML, string language) { List <UnitTextBase> units = new List <UnitTextBase>(); List <SubSentence> subSentences = new List <SubSentence>(); foreach (XElement element in subSentenceXML.Elements()) { if (element.IsName(UnitTextType.U)) { units.Add(UnmarkedFromXElement(element)); } else if (element.IsName(UnitTextType.SUB_SENT)) { subSentences.Add(SubSentenceFromXElement(element, language)); } else if (IsEntity(element)) { units.Add(EntityFromXElement(element, language, subSentence.Type)); } #region [.defense.] else { throw new InvalidOperationException("unknown xml element"); } #endregion } if (subSentenceXML.IsContainAttribute(RDF.Attribute.Subject)) { subSentence.Subject = _sentenceEntities[subSentenceXML.GetAttributeValue(RDF.Attribute.Subject)]; } if (subSentenceXML.IsContainAttribute(RDF.Attribute.IsPassiveSubject)) { subSentence.IsPassiveSubject = bool.Parse(subSentenceXML.GetAttributeValue(RDF.Attribute.IsPassiveSubject)); } subSentence.AppendSubSentences(subSentences); subSentence.SetUnits(units); }
/// <summary> /// Задание родительского подпредложения для заданного юнита /// </summary> /// <param name="unit">юнит</param> /// <param name="parentSubSentence">родительское предложение</param> /// <returns>юнит</returns> private UnitTextBase SetParentSubSentence(UnitTextBase unit, SubSentence parentSubSentence) { unit.ParentObject = parentSubSentence; return(unit); }
/// <summary> /// Получение идентификатора заданного подпредложения /// </summary> /// <param name="subSentence">подпредложение</param> /// <returns>идентификатор подпредложения</returns> public static int GetID(SubSentence subSentence) { return(++_currentSubSentenceID); }