public void FindExampleSentences()
		{
			// Make some analyses linked to the same LexSense.
			ParagraphBuilder pb = new ParagraphBuilder(m_textsDefn, m_text1, (int)Text1ParaIndex.SimpleSegmentPara);
			ParagraphAnnotatorForParagraphBuilder tapb = new ParagraphAnnotatorForParagraphBuilder(pb);
			ParagraphParser.ParseParagraph(pb.ActualParagraph);
			// Create a new lexical entry and sense.
			int clsidForm;
			string formLexEntry = "xnihimbilira";
			var morphTypeRepository = Cache.ServiceLocator.GetInstance<IMoMorphTypeRepository>();
			var rootMorphType = morphTypeRepository. GetObject(MoMorphTypeTags.kguidMorphRoot);
			ITsString tssLexEntryForm = TsStringUtils.MakeTss(formLexEntry, Cache.DefaultVernWs);
			var entryFactory = Cache.ServiceLocator.GetInstance<ILexEntryFactory>();
			ILexEntry xnihimbilira_Entry = entryFactory.Create(rootMorphType, tssLexEntryForm, "xnihimbilira.sense1", null);

			ILexSense xnihimbilira_Sense1 = xnihimbilira_Entry.SensesOS[0];
			var senseFactory = Cache.ServiceLocator.GetInstance<ILexSenseFactory>();
			ILexSense xnihimbilira_Sense2 = senseFactory.Create(xnihimbilira_Entry, null, "xnihimbilira.sense2");
			//<!--xxxpus xxxyalola xxxnihimbilira. xxxnihimbilira xxxpus xxxyalola. xxxhesyla xxxnihimbilira.-->
			ArrayList moForms = new ArrayList();
			moForms.Add("xx");
			moForms.Add(xnihimbilira_Entry.LexemeFormOA);
			// 1. Establish first analysis with Sense1
			IWfiAnalysis wfiAnalysis1 = tapb.BreakIntoMorphs(1, 0, moForms);
			tapb.SetMorphSense(1, 0, 1, xnihimbilira_Sense1);
			Assert.That(xnihimbilira_Sense1.ReferringObjects, Has.Count.EqualTo(1));
			Assert.That(xnihimbilira_Sense2.ReferringObjects, Has.Count.EqualTo(0));

			IWfiAnalysis waCba1_0 = pb.ActualParagraph.SegmentsOS[1].AnalysesRS[0] as IWfiAnalysis;
			Assert.IsNotNull(waCba1_0,
							 String.Format("Unexpected class({0}) of Analysis({1}) for cba0.",
							 pb.ActualParagraph.SegmentsOS[0].AnalysesRS[2].GetType(),
							 pb.ActualParagraph.SegmentsOS[0].AnalysesRS[2].Hvo));
			Assert.AreEqual(xnihimbilira_Sense1, waCba1_0.MorphBundlesOS[1].SenseRA);
			Assert.That(waCba1_0.ReferringObjects, Has.Count.EqualTo(1)); // one ref to the analysis (from segment 0)
			var wordform = (IWfiWordform)waCba1_0.Owner;
			Assert.That(wordform.FullConcordanceCount, Is.EqualTo(3)); // unchanged even though one is now an analysis

			// 2. Establish word gloss on the existing analysis.
			string wordGloss1;
			tapb.SetDefaultWordGloss(2, 1, wfiAnalysis1, out wordGloss1);
			Assert.That(wordform.FullConcordanceCount, Is.EqualTo(3)); // analysis and gloss and unchanged wordform all count

			var wgCba2_1 = pb.ActualParagraph.SegmentsOS[2].AnalysesRS[1] as IWfiGloss;
			Assert.IsNotNull(wgCba2_1,
							 String.Format("Unexpected class({0}) of InstanceOf({1}) for cba1.",
							 pb.ActualParagraph.SegmentsOS[2].AnalysesRS[1].GetType(), pb.ActualParagraph.SegmentsOS[2].AnalysesRS[1].Hvo));
			var waCba2_1 = wgCba2_1.Owner as IWfiAnalysis;
			Assert.AreEqual(xnihimbilira_Sense1.Hvo, waCba2_1.MorphBundlesOS[1].SenseRA.Hvo);

			// 3. establish a new analysis with Sense1.
			IWfiAnalysis wfiAnalysis2 = tapb.BreakIntoMorphs(0, 2, moForms); // xxxnihimbilira (first occurrence)
			tapb.SetMorphSense(0, 2, 1, xnihimbilira_Sense1);
			Assert.That(wordform.FullConcordanceCount, Is.EqualTo(3));

			var waCba0_2 = pb.ActualParagraph.SegmentsOS[0].AnalysesRS[2] as IWfiAnalysis;
			Assert.IsNotNull(waCba0_2,
							 String.Format("Unexpected class({0}) of InstanceOf({1}) for cba0.",
							 pb.ActualParagraph.SegmentsOS[2].AnalysesRS[1].GetType(), pb.ActualParagraph.SegmentsOS[2].AnalysesRS[1].Hvo));
			Assert.AreEqual(xnihimbilira_Sense1.Hvo, waCba0_2.MorphBundlesOS[1].SenseRA.Hvo);
			Assert.That(xnihimbilira_Sense1.ReferringObjects, Has.Count.EqualTo(2));
			Assert.That(xnihimbilira_Sense2.ReferringObjects, Has.Count.EqualTo(0));

			// 4. change an existing sense to sense2.
			tapb.SetMorphSense(0, 2, 1, xnihimbilira_Sense2);
			Assert.That(xnihimbilira_Sense1.ReferringObjects, Has.Count.EqualTo(1));
			Assert.That(xnihimbilira_Sense2.ReferringObjects, Has.Count.EqualTo(1));

			waCba0_2 = pb.ActualParagraph.SegmentsOS[0].AnalysesRS[2] as IWfiAnalysis;
			Assert.IsNotNull(waCba0_2,
							 String.Format("Unexpected class({0}) of InstanceOf({1}) for cba0.",
							 pb.ActualParagraph.SegmentsOS[2].AnalysesRS[1].GetType(), pb.ActualParagraph.SegmentsOS[2].AnalysesRS[1].Hvo));
			Assert.AreEqual(xnihimbilira_Sense2, waCba0_2.MorphBundlesOS[1].SenseRA);

			// do multiple occurrences of the same sense in the same segment.
			IWfiAnalysis wfiAnalysis3 = tapb.BreakIntoMorphs(0, 0, moForms); // break xxxpus into xx xnihimbilira (for fun).
			tapb.SetMorphSense(0, 0, 1, xnihimbilira_Sense2);
			Assert.That(xnihimbilira_Sense2.ReferringObjects, Has.Count.EqualTo(2));

			// reparse paragraph and make sure important stuff doesn't change
			tapb.ReparseParagraph();
			Assert.That(xnihimbilira_Sense1.ReferringObjects, Has.Count.EqualTo(1));
			Assert.That(xnihimbilira_Sense2.ReferringObjects, Has.Count.EqualTo(2));
		}
		public void SparseSegmentAnalyses_FreeformAnnotations_LT7318()
		{
			// Make some analyses linked to the same LexSense.
			//<!--xxxpus xxxyalola xxxnihimbilira. xxxnihimbilira xxxpus xxxyalola. xxxhesyla xxxnihimbilira.-->
			ParagraphBuilder pb = new ParagraphBuilder(m_textsDefn, m_text1, (int)Text1ParaIndex.SimpleSegmentPara);
			var segments = pb.ActualParagraph.SegmentsOS;
			Assert.AreEqual(3, segments.Count, "Unexpected number of senses.");

			// Load free form annotations for segments. shouldn't have any.
			Assert.That(segments[0].FreeTranslation.AvailableWritingSystemIds.Length, Is.EqualTo(0));
			Assert.That(segments[1].FreeTranslation.AvailableWritingSystemIds.Length, Is.EqualTo(0));
			Assert.That(segments[2].FreeTranslation.AvailableWritingSystemIds.Length, Is.EqualTo(0));

			// Try adding some freeform translations to third segment.
			segments[2].FreeTranslation.AnalysisDefaultWritingSystem =
				Cache.TsStrFactory.MakeString("Segment2: Freeform translation.", Cache.DefaultAnalWs);
			segments[2].LiteralTranslation.AnalysisDefaultWritingSystem =
				Cache.TsStrFactory.MakeString("Segment2: Literal translation.", Cache.DefaultAnalWs);

			// make sure the other segments don't have freeform annotations.
			Assert.That(segments[0].FreeTranslation.AvailableWritingSystemIds.Length, Is.EqualTo(0));
			Assert.That(segments[1].FreeTranslation.AvailableWritingSystemIds.Length, Is.EqualTo(0));

			// reparse the paragraph and make sure nothing changed.
			ParagraphAnnotatorForParagraphBuilder tapb = new ParagraphAnnotatorForParagraphBuilder(pb);
			tapb.ReparseParagraph();
			segments = (pb.ActualParagraph as IStTxtPara).SegmentsOS;

			Assert.That(segments[0].FreeTranslation.AvailableWritingSystemIds.Length, Is.EqualTo(0));
			Assert.That(segments[1].FreeTranslation.AvailableWritingSystemIds.Length, Is.EqualTo(0));
			Assert.That(segments[2].FreeTranslation.AnalysisDefaultWritingSystem.Text, Is.EqualTo("Segment2: Freeform translation."));
			Assert.That(segments[2].LiteralTranslation.AnalysisDefaultWritingSystem.Text, Is.EqualTo("Segment2: Literal translation."));
		}
		public void LT7974_Phrase_MakeAndBreak()
		{
			// 1. Make Phrases
			IList<int[]> secondaryPathsToJoinWords = new List<int[]>();
			IList<int[]> secondaryPathsToBreakPhrases = new List<int[]>();
			//Thist test proved to be incapable of being Undone, since further tests relied on state this test affected
			//the following two variables duplicate member variables to isolate changes to this test.
			XmlDocument donTouchIt = new XmlDocument();
			IText noLoToque = LoadTestText(
				Path.Combine("FDO",
				 Path.Combine("FDOTests",
				  Path.Combine("TestData", "ParagraphParserTestTexts.xml"))),
					1, donTouchIt);

			ParagraphBuilder pb = new ParagraphBuilder(donTouchIt, noLoToque, (int)Text1ParaIndex.PhraseWordforms);
			ParagraphAnnotatorForParagraphBuilder tapb = new ParagraphAnnotatorForParagraphBuilder(pb);
			// go ahead and parse the text generating real wordforms to be more like a real user situation (LT-7974)
			// and reset the concordance to simulate the situation where the user hasn't loaded ConcordanceWords yet.
			ParagraphParser.ParseText(noLoToque.ContentsOA);
			pb.ResyncExpectedAnnotationIds();

			// first do a basic phrase (without secondary phrases (guesses)), which should not delete "xxxpus" or "xxxyalola"
			// since they occur later in the text.
			// [xxxpus xxxyalola] xxxnihimbilira. xxxpus xxxyalola xxxhesyla xxxnihimbilira. xxxpus xxxyalola xxxnihimbilira
			IWfiWordform wf1 = pb.ActualParagraph.SegmentsOS[0].AnalysesRS[0].Wordform;
			IWfiWordform wf2 = pb.ActualParagraph.SegmentsOS[0].AnalysesRS[1].Wordform;
			tapb.MergeAdjacentAnnotations(0, 0, secondaryPathsToJoinWords, secondaryPathsToBreakPhrases);
			// after the merge, make sure the wordforms for xxxpus and xxxyalola are still valid
			Assert.IsTrue(wf1.IsValidObject, "expected xxxpus to still be valid");
			Assert.IsTrue(wf2.IsValidObject, "expected xxxyalola to still be valid");
			tapb.ValidateAnnotations(true);

			// now break the phrase and make sure we delete this wordform, b/c it is not occurring elsewhere
			pb.ResyncExpectedAnnotationIds();
			// \xxxpus\ \xxxyalola\ xxxnihimbilira. xxxpus xxxyalola xxxhesyla xxxnihimbilira. xxxpus xxxyalola xxxnihimbilira
			IWfiWordform wf3 = pb.ActualParagraph.SegmentsOS[0].AnalysesRS[0].Wordform;
			secondaryPathsToJoinWords.Clear();
			secondaryPathsToBreakPhrases.Clear();
			tapb.BreakPhrase(0, 0, secondaryPathsToBreakPhrases, secondaryPathsToJoinWords, null);
			Assert.IsFalse(wf3.IsValidObject, "expected 'xxxpus xxxyalola' to be deleted.");
			tapb.ValidateAnnotations(true);

			// rejoin the first phrase, parse the text, and break the first phrase
			// [xxxpus xxxyalola] xxxnihimbilira. {xxxpus xxxyalola} xxxhesyla xxxnihimbilira. {xxxpus xxxyalola} xxxnihimbilira
			secondaryPathsToJoinWords.Add(new int[2] { 1, 0 }); // {xxxpus xxxyalola} xxxhesyla
			secondaryPathsToJoinWords.Add(new int[2] { 2, 0 }); // {xxxpus xxxyalola} xxxnihimbilira
			tapb.MergeAdjacentAnnotations(0, 0, secondaryPathsToJoinWords, secondaryPathsToBreakPhrases);
			wf1 = pb.ActualParagraph.SegmentsOS[0].AnalysesRS[0].Wordform;
			tapb.ReparseParagraph();
			// just break the new phrase (and the leave existing guesses)
			// \xxxpus\ \xxxyalola\ xxxnihimbilira. {xxxpus xxxyalola} xxxhesyla xxxnihimbilira. {xxxpus xxxyalola} xxxnihimbilira
			secondaryPathsToJoinWords.Clear();
			tapb.BreakPhrase(0, 0, secondaryPathsToBreakPhrases, secondaryPathsToJoinWords, null);
			Assert.IsTrue(wf1.IsValidObject, "expected 'xxxpus xxxyalola' to still be valid");
			tapb.ValidateAnnotations(true);
			// xxxpus xxxyalola xxxnihimbilira. \xxxpus\ \xxxyalola\ xxxhesyla xxxnihimbilira. {xxxpus xxxyalola} xxxnihimbilira
			tapb.BreakPhrase(1, 0, secondaryPathsToBreakPhrases, secondaryPathsToJoinWords, null);
			Assert.IsTrue(wf1.IsValidObject, "expected 'xxxpus xxxyalola' to still be valid");
			tapb.ValidateAnnotations(true);
			// xxxpus xxxyalola xxxnihimbilira. xxxpus xxxyalola xxxhesyla xxxnihimbilira. \xxxpus\ \xxxyalola\ xxxnihimbilira
			tapb.BreakPhrase(2, 0, secondaryPathsToBreakPhrases, secondaryPathsToJoinWords, null);
			Assert.IsFalse(wf1.IsValidObject, "expected 'xxxpus xxxyalola' to be deleted");
			tapb.ValidateAnnotations(true);

			// now do two joins, the second resulting in deletion of a wordform.
			// xxxpus xxxyalola xxxnihimbilira. xxxpus [xxxyalola xxxhesyla] xxxnihimbilira. xxxpus xxxyalola xxxnihimbilira
			// xxxpus xxxyalola xxxnihimbilira. xxxpus [xxxyalola xxxhesyla xxxnihimbilira]. xxxpus xxxyalola xxxnihimbilira
			ParagraphParser.ParseText(noLoToque.ContentsOA);
			pb.ResyncExpectedAnnotationIds();
			tapb.MergeAdjacentAnnotations(1, 1, secondaryPathsToJoinWords, secondaryPathsToBreakPhrases);
			IWfiWordform wf4 = pb.ActualParagraph.SegmentsOS[1].AnalysesRS[1].Wordform;
			tapb.MergeAdjacentAnnotations(1, 1, secondaryPathsToJoinWords, secondaryPathsToBreakPhrases);
			// this merge should have deleted 'xxxyalola xxxhesyla'
			Assert.IsFalse(wf4.IsValidObject, "expected 'xxxyalola xxxhesyla' to be deleted.");
			tapb.ValidateAnnotations(true);
		}
		public void FindExampleSentences()
		{
			// Make some analyses linked to the same LexSense.
			ParagraphBuilder pb = new ParagraphBuilder(m_textsDefn, m_text1, (int)Text1ParaIndex.SimpleSegmentPara);
			ParagraphAnnotatorForParagraphBuilder tapb = new ParagraphAnnotatorForParagraphBuilder(pb);
			// Create a new lexical entry and sense.
			int clsidForm;
			string formLexEntry = "xnihimbilira";
			ITsString tssLexEntryForm = StringUtils.MakeTss(formLexEntry, Cache.DefaultVernWs);
			ILexEntry xnihimbilira_Entry = LexEntry.CreateEntry(Cache,
				MoMorphType.FindMorphType(Cache, new MoMorphTypeCollection(Cache), ref formLexEntry, out clsidForm), tssLexEntryForm,
				"xnihimbilira.sense1", null);

			ILexSense xnihimbilira_Sense1 = xnihimbilira_Entry.SensesOS[0];
			ILexSense xnihimbilira_Sense2 = LexSense.CreateSense(xnihimbilira_Entry, null, "xnihimbilira.sense2");
			//<!--xxxpus xxxyalola xxxnihimbilira. xxxnihimbilira xxxpus xxxyalola. xxxhesyla xxxnihimbilira.-->
			ArrayList moForms = new ArrayList();
			moForms.Add("xx");
			moForms.Add(xnihimbilira_Entry.LexemeFormOA);
			// 1. Establish first analysis with Sense1
			IWfiAnalysis wfiAnalysis1 = tapb.BreakIntoMorphs(1, 0, moForms);
			tapb.SetMorphSense(1, 0, 1, xnihimbilira_Sense1);
			List<int> instancesInTwfics_Sense1 = (xnihimbilira_Sense1 as LexSense).InstancesInTwfics;
			List<int> instancesInTwfics_Sense2 = (xnihimbilira_Sense2 as LexSense).InstancesInTwfics;
			Assert.AreEqual(1, instancesInTwfics_Sense1.Count,
				String.Format("Unexpected number of instances of sense '{0}'", xnihimbilira_Sense1.Gloss.AnalysisDefaultWritingSystem));
			Assert.AreEqual(0, instancesInTwfics_Sense2.Count,
				String.Format("Unexpected number of instances of sense '{0}'", xnihimbilira_Sense2.Gloss.AnalysisDefaultWritingSystem));
			StTxtPara.TwficInfo infoCba1_0 = new StTxtPara.TwficInfo(Cache, instancesInTwfics_Sense1[0]);
			Assert.AreEqual(pb.ActualParagraph.Hvo, infoCba1_0.Object.BeginObjectRAHvo);
			Assert.AreEqual(1, infoCba1_0.SegmentIndex,
				String.Format("Unexpected index of segment '{0}'", infoCba1_0.SegmentHvo));

			WfiAnalysis waCba1_0 = infoCba1_0.Object.InstanceOfRA as WfiAnalysis;
			Assert.IsNotNull(waCba1_0,
				String.Format("Unexpected class({0}) of InstanceOf({1}) for cba0.", infoCba1_0.Object.InstanceOfRA.ClassID, infoCba1_0.Object.InstanceOfRAHvo));
			Assert.AreEqual(xnihimbilira_Sense1.Hvo, waCba1_0.MorphBundlesOS[1].SenseRAHvo);

			// get the segment information for the twfics.
			List<int> segments_Sense1 = StTxtPara.TwficSegments(Cache, instancesInTwfics_Sense1);
			Assert.AreEqual(1, segments_Sense1.Count, "Unexpected number of senses for twfics.");
			Assert.AreEqual(infoCba1_0.SegmentHvo, segments_Sense1[0], "Unexpected segment hvo.");

			// 2. Establish word gloss on the existing analysis.
			string wordGloss1;
			tapb.SetDefaultWordGloss(2, 1, wfiAnalysis1, out wordGloss1);
			instancesInTwfics_Sense1 = (xnihimbilira_Sense1 as LexSense).InstancesInTwfics;
			Assert.AreEqual(2, instancesInTwfics_Sense1.Count,
				String.Format("Unexpected number of instances of sense '{0}'", xnihimbilira_Sense1.Gloss.AnalysisDefaultWritingSystem));

			infoCba1_0 = new StTxtPara.TwficInfo(Cache, instancesInTwfics_Sense1[0]);
			Assert.AreEqual(pb.ActualParagraph.Hvo, infoCba1_0.Object.BeginObjectRAHvo);
			Assert.AreEqual(1, infoCba1_0.SegmentIndex,
				String.Format("Unexpected index of segment '{0}'", infoCba1_0.SegmentHvo));

			StTxtPara.TwficInfo infoCba2_1 = new StTxtPara.TwficInfo(Cache, instancesInTwfics_Sense1[1]);
			Assert.AreEqual(pb.ActualParagraph.Hvo, infoCba2_1.Object.BeginObjectRAHvo);
			Assert.AreEqual(2, infoCba2_1.SegmentIndex,
				String.Format("Unexpected index of segment '{0}'", infoCba2_1.SegmentHvo));

			waCba1_0 = infoCba1_0.Object.InstanceOfRA as WfiAnalysis;
			Assert.IsNotNull(waCba1_0,
				String.Format("Unexpected class({0}) of InstanceOf({1}) for cba0.", infoCba1_0.Object.InstanceOfRA.ClassID, infoCba1_0.Object.InstanceOfRAHvo));
			Assert.AreEqual(xnihimbilira_Sense1.Hvo, waCba1_0.MorphBundlesOS[1].SenseRAHvo);

			WfiGloss wgCba2_1 = infoCba2_1.Object.InstanceOfRA as WfiGloss;
			Assert.IsNotNull(wgCba2_1,
				String.Format("Unexpected class({0}) of InstanceOf({1}) for cba1.", infoCba2_1.Object.InstanceOfRA.ClassID, infoCba2_1.Object.InstanceOfRAHvo));
			WfiAnalysis waCba2_1 = WfiAnalysis.CreateFromDBObject(Cache, wgCba2_1.OwnerHVO) as WfiAnalysis;
			Assert.AreEqual(xnihimbilira_Sense1.Hvo, waCba2_1.MorphBundlesOS[1].SenseRAHvo);

			segments_Sense1 = StTxtPara.TwficSegments(Cache, instancesInTwfics_Sense1);
			Assert.AreEqual(2, segments_Sense1.Count, "Unexpected number of senses for twfics.");
			Assert.AreEqual(infoCba1_0.SegmentHvo, segments_Sense1[0], "Unexpected segment hvo.");
			Assert.AreEqual(infoCba2_1.SegmentHvo, segments_Sense1[1], "Unexpected segment hvo.");

			// 3. establish a new analysis with Sense1.
			IWfiAnalysis wfiAnalysis2 = tapb.BreakIntoMorphs(0, 2, moForms); // xxxnihimbilira (first occurrence)
			tapb.SetMorphSense(0, 2, 1, xnihimbilira_Sense1);
			instancesInTwfics_Sense1 = (xnihimbilira_Sense1 as LexSense).InstancesInTwfics;
			Assert.AreEqual(3, instancesInTwfics_Sense1.Count,
				String.Format("Unexpected number of instances of sense '{0}'", xnihimbilira_Sense1.Gloss.AnalysisDefaultWritingSystem));

			StTxtPara.TwficInfo infoCba0_2 = new StTxtPara.TwficInfo(Cache, instancesInTwfics_Sense1[0]);
			Assert.AreEqual(pb.ActualParagraph.Hvo, infoCba0_2.Object.BeginObjectRAHvo);
			Assert.AreEqual(0, infoCba0_2.SegmentIndex,
				String.Format("Unexpected index of segment '{0}'", infoCba0_2.SegmentHvo));

			WfiAnalysis waCba0_2 = infoCba0_2.Object.InstanceOfRA as WfiAnalysis;
			Assert.IsNotNull(waCba0_2,
				String.Format("Unexpected class({0}) of InstanceOf({1}) for cba0.", infoCba0_2.Object.InstanceOfRA.ClassID, infoCba0_2.Object.InstanceOfRAHvo));
			Assert.AreEqual(xnihimbilira_Sense1.Hvo, waCba0_2.MorphBundlesOS[1].SenseRAHvo);

			segments_Sense1 = StTxtPara.TwficSegments(Cache, instancesInTwfics_Sense1);
			Assert.AreEqual(3, segments_Sense1.Count, "Unexpected number of senses for twfics.");
			Assert.AreEqual(infoCba0_2.SegmentHvo, segments_Sense1[0], "Unexpected segment hvo.");
			Assert.AreEqual(infoCba1_0.SegmentHvo, segments_Sense1[1], "Unexpected segment hvo.");
			Assert.AreEqual(infoCba2_1.SegmentHvo, segments_Sense1[2], "Unexpected segment hvo.");

			// 4. change an existing sense to sense2.
			tapb.SetMorphSense(0, 2, 1, xnihimbilira_Sense2);
			instancesInTwfics_Sense1 = (xnihimbilira_Sense1 as LexSense).InstancesInTwfics;
			Assert.AreEqual(2, instancesInTwfics_Sense1.Count,
				String.Format("Unexpected number of instances of sense '{0}'", xnihimbilira_Sense1.Gloss.AnalysisDefaultWritingSystem));
			instancesInTwfics_Sense2 = (xnihimbilira_Sense2 as LexSense).InstancesInTwfics;
			Assert.AreEqual(1, instancesInTwfics_Sense2.Count,
				String.Format("Unexpected number of instances of sense '{0}'", xnihimbilira_Sense2.Gloss.AnalysisDefaultWritingSystem));

			infoCba0_2 = new StTxtPara.TwficInfo(Cache, instancesInTwfics_Sense2[0]);
			Assert.AreEqual(pb.ActualParagraph.Hvo, infoCba0_2.Object.BeginObjectRAHvo);
			Assert.AreEqual(0, infoCba0_2.SegmentIndex,
				String.Format("Unexpected index of segment '{0}'", infoCba0_2.SegmentHvo));

			waCba0_2 = infoCba0_2.Object.InstanceOfRA as WfiAnalysis;
			Assert.IsNotNull(waCba0_2,
				String.Format("Unexpected class({0}) of InstanceOf({1}) for cba0.", infoCba0_2.Object.InstanceOfRA.ClassID, infoCba0_2.Object.InstanceOfRAHvo));
			Assert.AreEqual(xnihimbilira_Sense2.Hvo, waCba0_2.MorphBundlesOS[1].SenseRAHvo);

			// do multiple occurrences of the same sense in the same segment.
			IWfiAnalysis wfiAnalysis3 = tapb.BreakIntoMorphs(0, 0, moForms); // break xxxpus into xx xnihimbilira (for fun).
			tapb.SetMorphSense(0, 0, 1, xnihimbilira_Sense2);
			instancesInTwfics_Sense2 = (xnihimbilira_Sense2 as LexSense).InstancesInTwfics;
			Assert.AreEqual(2, instancesInTwfics_Sense2.Count,
				String.Format("Unexpected number of instances of sense '{0}'", xnihimbilira_Sense2.Gloss.AnalysisDefaultWritingSystem));
			StTxtPara.TwficInfo infoCba0_0 = new StTxtPara.TwficInfo(Cache, instancesInTwfics_Sense2[0]);

			// reparse paragraph to convert all segments with real analyses to real ones.
			tapb.ReparseParagraph();
			infoCba0_0.ReloadInfo();
			infoCba0_2.ReloadInfo();
			infoCba1_0.ReloadInfo();
			infoCba2_1.ReloadInfo();

			List<int> segments_Sense2 = StTxtPara.TwficSegments(Cache, instancesInTwfics_Sense2);
			Assert.AreEqual(2, segments_Sense2.Count, "Unexpected number of senses for twfics.");
			Assert.AreEqual(infoCba0_0.SegmentHvo, segments_Sense2[0], "Unexpected segment hvo.");
			Assert.AreEqual(infoCba0_2.SegmentHvo, segments_Sense2[1], "Unexpected segment hvo.");

			// Load free form annotations for segments Sense 2.
			Set<int> analWsIds = new Set<int>(Cache.LangProject.AnalysisWssRC.HvoArray);
			StTxtPara.LoadSegmentFreeformAnnotationData(Cache, new Set<int>(segments_Sense2), analWsIds);
			int tagSegFF = StTxtPara.SegmentFreeformAnnotationsFlid(Cache);
			int[] segFFs = Cache.GetVectorProperty(infoCba0_0.SegmentHvo, tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 0 should not have any freeform annotations.");
			StTxtPara.LoadSegmentFreeformAnnotationData(Cache, pb.ActualParagraph.Hvo, analWsIds);
			segFFs = Cache.GetVectorProperty(infoCba0_0.SegmentHvo, tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 0 should not have any freeform annotations.");
			segFFs = Cache.GetVectorProperty(infoCba1_0.SegmentHvo, tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 1 should not have any freeform annotations.");
			segFFs = Cache.GetVectorProperty(infoCba2_1.SegmentHvo, tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 2 should not have any freeform annotations.");

			// Try adding some freeform translations
			int segDefn_literalTranslation = Cache.GetIdFromGuid(LangProject.kguidAnnLiteralTranslation);
			int segDefn_freeTranslation = Cache.GetIdFromGuid(LangProject.kguidAnnFreeTranslation);
			BaseFreeformAdder ffAdder = new BaseFreeformAdder(Cache);
			ICmIndirectAnnotation freeTrans0 = ffAdder.AddFreeformAnnotation(infoCba0_0.SegmentHvo, segDefn_freeTranslation);
			freeTrans0.Comment.SetAlternative("Segment0: Freeform translation.", Cache.DefaultAnalWs);
			ICmIndirectAnnotation literalTrans0 = ffAdder.AddFreeformAnnotation(infoCba0_0.SegmentHvo, segDefn_literalTranslation);
			literalTrans0.Comment.SetAlternative("Segment0: Literal translation.", Cache.DefaultAnalWs);

			// see if we can load this into the cache.
			StTxtPara.LoadSegmentFreeformAnnotationData(Cache, new Set<int>(segments_Sense2), analWsIds);
			segFFs = Cache.GetVectorProperty(infoCba0_0.SegmentHvo, tagSegFF, true);
			Assert.AreEqual(2, segFFs.Length, "Segment 0 should have freeform annotations.");
			Assert.AreEqual(segFFs[0], freeTrans0.Hvo, "Segment 0 Freeform translation id.");
			Assert.AreEqual(segFFs[1], literalTrans0.Hvo, "Segment 0 Literal translation id.");

			StTxtPara.LoadSegmentFreeformAnnotationData(Cache, pb.ActualParagraph.Hvo, analWsIds);
			segFFs = Cache.GetVectorProperty(infoCba0_0.SegmentHvo, tagSegFF, true);
			Assert.AreEqual(2, segFFs.Length, "Segment 0 should have freeform annotations.");
			Assert.AreEqual(segFFs[0], freeTrans0.Hvo, "Segment 0 Freeform translation id.");
			Assert.AreEqual(segFFs[1], literalTrans0.Hvo, "Segment 0 Literal translation id.");
			segFFs = Cache.GetVectorProperty(infoCba1_0.SegmentHvo, tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 1 should not have any freeform annotations.");
			segFFs = Cache.GetVectorProperty(infoCba2_1.SegmentHvo, tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 2 should not have any freeform annotations.");
		}
		public void SparseSegmentAnalyses_FreeformAnnotations_LT7318()
		{
			// Make some analyses linked to the same LexSense.
			//<!--xxxpus xxxyalola xxxnihimbilira. xxxnihimbilira xxxpus xxxyalola. xxxhesyla xxxnihimbilira.-->
			ParagraphBuilder pb = new ParagraphBuilder(m_textsDefn, m_text1, (int)Text1ParaIndex.SimpleSegmentPara);
			List<int> segments = (pb.ActualParagraph as StTxtPara).Segments;
			Assert.AreEqual(3, segments.Count, "Unexpected number of senses.");

			// Verify that each of these segments are dummy
			Assert.IsTrue(Cache.IsDummyObject(segments[0]), "Expected dummy segment.");
			Assert.IsTrue(Cache.IsDummyObject(segments[1]), "Expected dummy segment.");
			Assert.IsTrue(Cache.IsDummyObject(segments[2]), "Expected dummy segment.");

			// Load free form annotations for segments. shouldn't have any.
			Set<int> analWsIds = new Set<int>(Cache.LangProject.AnalysisWssRC.HvoArray);
			StTxtPara.LoadSegmentFreeformAnnotationData(Cache, new Set<int>(segments), analWsIds);
			int tagSegFF = StTxtPara.SegmentFreeformAnnotationsFlid(Cache);
			int[] segFFs = Cache.GetVectorProperty(segments[0], tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 0 should not have any freeform annotations.");
			StTxtPara.LoadSegmentFreeformAnnotationData(Cache, pb.ActualParagraph.Hvo, analWsIds);
			segFFs = Cache.GetVectorProperty(segments[1], tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 1 should not have any freeform annotations.");
			segFFs = Cache.GetVectorProperty(segments[2], tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 2 should not have any freeform annotations.");

			// convert the third segment to a real, in preparation for adding the annotation.
			ICmBaseAnnotation cbaReal = CmBaseAnnotation.ConvertBaseAnnotationToReal(m_fdoCache, segments[2]);
			segments = (pb.ActualParagraph as StTxtPara).Segments;
			Assert.IsTrue(!Cache.IsDummyObject(segments[2]), "Expected real segment.");

			// Try adding some freeform translations to third segment.
			int segDefn_literalTranslation = Cache.GetIdFromGuid(LangProject.kguidAnnLiteralTranslation);
			int segDefn_freeTranslation = Cache.GetIdFromGuid(LangProject.kguidAnnFreeTranslation);
			BaseFreeformAdder ffAdder = new BaseFreeformAdder(Cache);
			ICmIndirectAnnotation freeTrans0 = ffAdder.AddFreeformAnnotation(segments[2], segDefn_freeTranslation);
			freeTrans0.Comment.SetAlternative("Segment2: Freeform translation.", Cache.DefaultAnalWs);
			ICmIndirectAnnotation literalTrans0 = ffAdder.AddFreeformAnnotation(segments[2], segDefn_literalTranslation);
			literalTrans0.Comment.SetAlternative("Segment2: Literal translation.", Cache.DefaultAnalWs);

			// see if we can load this into the cache.
			StTxtPara.LoadSegmentFreeformAnnotationData(Cache, new Set<int>(segments), analWsIds);
			segFFs = Cache.GetVectorProperty(segments[2], tagSegFF, true);
			Assert.AreEqual(2, segFFs.Length, "Segment 2 should have freeform annotations.");
			Assert.AreEqual(segFFs[0], freeTrans0.Hvo, "Segment 2 Freeform translation id.");
			Assert.AreEqual(segFFs[1], literalTrans0.Hvo, "Segment 2 Literal translation id.");
			// make sure the other segments don't have freeform annotations.
			segFFs = Cache.GetVectorProperty(segments[0], tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 0 should not have any freeform annotations.");
			StTxtPara.LoadSegmentFreeformAnnotationData(Cache, pb.ActualParagraph.Hvo, analWsIds);
			segFFs = Cache.GetVectorProperty(segments[1], tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 1 should not have any freeform annotations.");

			// reparse the paragraph and make sure nothing changed.
			ParagraphAnnotatorForParagraphBuilder tapb = new ParagraphAnnotatorForParagraphBuilder(pb);
			tapb.ReparseParagraph();
			segments = (pb.ActualParagraph as StTxtPara).Segments;

			StTxtPara.LoadSegmentFreeformAnnotationData(Cache, new Set<int>(segments), analWsIds);
			segFFs = Cache.GetVectorProperty(segments[2], tagSegFF, true);
			Assert.AreEqual(2, segFFs.Length, "Segment 2 should have freeform annotations.");
			Assert.AreEqual(segFFs[0], freeTrans0.Hvo, "Segment 2 Freeform translation id.");
			Assert.AreEqual(segFFs[1], literalTrans0.Hvo, "Segment 2 Literal translation id.");
			// make sure the other segments don't have freeform annotations.
			segFFs = Cache.GetVectorProperty(segments[0], tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 0 should not have any freeform annotations.");
			StTxtPara.LoadSegmentFreeformAnnotationData(Cache, pb.ActualParagraph.Hvo, analWsIds);
			segFFs = Cache.GetVectorProperty(segments[1], tagSegFF, true);
			Assert.AreEqual(0, segFFs.Length, "Segment 1 should not have any freeform annotations.");
		}
		public void LT7974_Phrase_MakeAndBreak()
		{
			CheckDisposed();
			// 1. Make Phrases
			IList<int[]> secondaryPathsToJoinWords = new List<int[]>();
			IList<int[]> secondaryPathsToBreakPhrases = new List<int[]>();

			ParagraphBuilder pb = new ParagraphBuilder(m_textsDefn, m_text1, (int)Text1ParaIndex.PhraseWordforms);
			ParagraphAnnotatorForParagraphBuilder tapb = new ParagraphAnnotatorForParagraphBuilder(pb);
			// go ahead and parse the text generating real wordforms to be more like a real user situation (LT-7974)
			// and reset the concordance to simulate the situation where the user hasn't loaded ConcordanceWords yet.
			(Cache.LangProject.WordformInventoryOA as WordformInventory).ResetConcordanceWordformsAndOccurrences();
			bool fDidParse;
			(m_text1.ContentsOA as StText).LastParsedTimestamp = 0;
			ParagraphParser.ParseText(m_text1.ContentsOA, new NullProgressState(), out fDidParse);
			pb.ResyncExpectedAnnotationIds();

			// first do a basic phrase (without secondary phrases (guesses)), which should not delete "xxxpus" or "xxxyalola"
			// since they occur later in the text.
			// [xxxpus xxxyalola] xxxnihimbilira. xxxpus xxxyalola xxxhesyla xxxnihimbilira. xxxpus xxxyalola xxxnihimbilira
			int hvoCba1_0 = tapb.GetSegmentForm(1, 0);	// second xxxpus
			int hvoCba1_1 = tapb.GetSegmentForm(1, 1);	// second xxxyalola
			ICmBaseAnnotation cba1_0 = new CmBaseAnnotation(Cache, hvoCba1_0);
			ICmBaseAnnotation cba1_1 = new CmBaseAnnotation(Cache, hvoCba1_1);
			IWfiWordform wf1 = cba1_0.InstanceOfRA as WfiWordform;
			IWfiWordform wf2 = cba1_0.InstanceOfRA as WfiWordform;
			tapb.MergeAdjacentAnnotations(0, 0, secondaryPathsToJoinWords, secondaryPathsToBreakPhrases);
			// after the merge, make sure the wordforms for xxxpus and xxxyalola are still valid
			Assert.IsTrue(wf1.IsValidObject(), "expected xxxpus to still be valid");
			Assert.IsTrue(wf2.IsValidObject(), "expected xxxyalola to still be valid");
			tapb.ValidateAnnotations(true);

			// now break the phrase and make sure we delete this wordform, b/c it is not occurring elsewhere
			(Cache.LangProject.WordformInventoryOA as WordformInventory).ResetConcordanceWordformsAndOccurrences();
			pb.ResyncExpectedAnnotationIds();
			// \xxxpus\ \xxxyalola\ xxxnihimbilira. xxxpus xxxyalola xxxhesyla xxxnihimbilira. xxxpus xxxyalola xxxnihimbilira
			int hvoCba0_0 = tapb.GetSegmentForm(0, 0);	// xxxpus xxxyalola
			ICmBaseAnnotation cba0_0 = new CmBaseAnnotation(Cache, hvoCba0_0);
			IWfiWordform wf3 = cba0_0.InstanceOfRA as WfiWordform;
			secondaryPathsToJoinWords.Clear();
			secondaryPathsToBreakPhrases.Clear();
			tapb.BreakPhrase(0, 0, secondaryPathsToBreakPhrases, secondaryPathsToJoinWords, null);
			Assert.IsFalse(wf3.IsValidObject(), "expected 'xxxpus xxxyalola' to be deleted.");
			tapb.ValidateAnnotations(true);

			// rejoin the first phrase, parse the text, and break the first phrase
			// [xxxpus xxxyalola] xxxnihimbilira. {xxxpus xxxyalola} xxxhesyla xxxnihimbilira. {xxxpus xxxyalola} xxxnihimbilira
			secondaryPathsToJoinWords.Add(new int[2] { 1, 0 }); // {xxxpus xxxyalola} xxxhesyla
			secondaryPathsToJoinWords.Add(new int[2] { 2, 0 }); // {xxxpus xxxyalola} xxxnihimbilira
			tapb.MergeAdjacentAnnotations(0, 0, secondaryPathsToJoinWords, secondaryPathsToBreakPhrases);
			hvoCba0_0 = tapb.GetSegmentForm(0, 0);	// xxxpus xxxyalola
			cba0_0 = new CmBaseAnnotation(Cache, hvoCba0_0);
			wf1 = cba0_0.InstanceOfRA as WfiWordform;
			tapb.ReparseParagraph();
			// just break the new phrase (and the leave existing guesses)
			// \xxxpus\ \xxxyalola\ xxxnihimbilira. {xxxpus xxxyalola} xxxhesyla xxxnihimbilira. {xxxpus xxxyalola} xxxnihimbilira
			(Cache.LangProject.WordformInventoryOA as WordformInventory).ResetConcordanceWordformsAndOccurrences();
			secondaryPathsToJoinWords.Clear();
			tapb.BreakPhrase(0, 0, secondaryPathsToBreakPhrases, secondaryPathsToJoinWords, null);
			Assert.IsTrue(wf1.IsValidObject(), "expected 'xxxpus xxxyalola' to still be valid");
			tapb.ValidateAnnotations(true);
			// xxxpus xxxyalola xxxnihimbilira. \xxxpus\ \xxxyalola\ xxxhesyla xxxnihimbilira. {xxxpus xxxyalola} xxxnihimbilira
			tapb.BreakPhrase(1, 0, secondaryPathsToBreakPhrases, secondaryPathsToJoinWords, null);
			Assert.IsTrue(wf1.IsValidObject(), "expected 'xxxpus xxxyalola' to still be valid");
			tapb.ValidateAnnotations(true);
			// xxxpus xxxyalola xxxnihimbilira. xxxpus xxxyalola xxxhesyla xxxnihimbilira. \xxxpus\ \xxxyalola\ xxxnihimbilira
			tapb.BreakPhrase(2, 0, secondaryPathsToBreakPhrases, secondaryPathsToJoinWords, null);
			Assert.IsFalse(wf1.IsValidObject(), "expected 'xxxpus xxxyalola' to be deleted");
			tapb.ValidateAnnotations(true);

			// now do two joins, the second resulting in deletion of a wordform.
			// xxxpus xxxyalola xxxnihimbilira. xxxpus [xxxyalola xxxhesyla] xxxnihimbilira. xxxpus xxxyalola xxxnihimbilira
			// xxxpus xxxyalola xxxnihimbilira. xxxpus [xxxyalola xxxhesyla xxxnihimbilira]. xxxpus xxxyalola xxxnihimbilira
			(Cache.LangProject.WordformInventoryOA as WordformInventory).ResetConcordanceWordformsAndOccurrences();
			(m_text1.ContentsOA as StText).LastParsedTimestamp = 0;
			ParagraphParser.ParseText(m_text1.ContentsOA, new NullProgressState(), out fDidParse);
			pb.ResyncExpectedAnnotationIds();
			tapb.MergeAdjacentAnnotations(1, 1, secondaryPathsToJoinWords, secondaryPathsToBreakPhrases);
			hvoCba1_1 = tapb.GetSegmentForm(1, 1);	// xxxyalola xxxhesyla
			cba1_1 = new CmBaseAnnotation(Cache, hvoCba1_1);
			IWfiWordform wf4 = cba1_1.InstanceOfRA as WfiWordform;
			tapb.MergeAdjacentAnnotations(1, 1, secondaryPathsToJoinWords, secondaryPathsToBreakPhrases);
			// this merge should have deleted 'xxxyalola xxxhesyla'
			Assert.IsFalse(wf4.IsValidObject(), "expected 'xxxyalola xxxhesyla' to be deleted.");
			tapb.ValidateAnnotations(true);
		}