コード例 #1
0
ファイル: FdoTestHelper.cs プロジェクト: bbriggs/FieldWorks
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Creates analyses for the words and punctuation forms of the specified segment. Words
		/// will have WfiWordforms created if they don't already exist.
		/// </summary>
		/// <param name="segment">The segment.</param>
		/// <param name="paraContents">The para contents.</param>
		/// <param name="ichBeginOffset">The beginning character offset.</param>
		/// <param name="ichLimOffset">The character offset limit.</param>
		/// <param name="fCreateGlosses">if set to <c>true</c> create glosses in addition to the
		/// WfiWordforms for each surface form.</param>
		/// ------------------------------------------------------------------------------------
		public static void CreateAnalyses(ISegment segment, ITsString paraContents,
			int ichBeginOffset, int ichLimOffset, bool fCreateGlosses)
		{
			FdoCache cache = segment.Cache;
			IFdoServiceLocator servloc = cache.ServiceLocator;
			if (SegmentBreaker.HasLabelText(paraContents, ichBeginOffset, ichLimOffset))
			{
				IPunctuationForm labelPunc = servloc.GetInstance<IPunctuationFormFactory>().Create();
				segment.AnalysesRS.Add(labelPunc);
				labelPunc.Form = paraContents.GetSubstring(ichBeginOffset, ichLimOffset);
			}
			else
			{
				ParseSegBaseline(segment, ichBeginOffset, ichLimOffset, (iForm, word, iAnalysis) =>
					{
						CreateAnalysisForWord(word, segment, cache.DefaultAnalWs, fCreateGlosses);
						return true;
					},
					(sPunc, iAnalysis) => CreatePuncForm(segment, cache.TsStrFactory.MakeString(sPunc, cache.DefaultVernWs)),
					(ichOrc, iAnalysis) => CreatePuncForm(segment, paraContents.Substring(segment.BeginOffset + ichOrc, 1)));
			}
		}
コード例 #2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Creates analyses for the words and punctuation forms of the specified segment. Words
        /// will have WfiWordforms created if they don't already exist.
        /// </summary>
        /// <param name="segment">The segment.</param>
        /// <param name="paraContents">The para contents.</param>
        /// <param name="ichBeginOffset">The beginning character offset.</param>
        /// <param name="ichLimOffset">The character offset limit.</param>
        /// <param name="fCreateGlosses">if set to <c>true</c> create glosses in addition to the
        /// WfiWordforms for each surface form.</param>
        /// ------------------------------------------------------------------------------------
        public static void CreateAnalyses(ISegment segment, ITsString paraContents,
                                          int ichBeginOffset, int ichLimOffset, bool fCreateGlosses)
        {
            FdoCache           cache   = segment.Cache;
            IFdoServiceLocator servloc = cache.ServiceLocator;

            if (SegmentBreaker.HasLabelText(paraContents, ichBeginOffset, ichLimOffset))
            {
                IPunctuationForm labelPunc = servloc.GetInstance <IPunctuationFormFactory>().Create();
                segment.AnalysesRS.Add(labelPunc);
                labelPunc.Form = paraContents.GetSubstring(ichBeginOffset, ichLimOffset);
            }
            else
            {
                ParseSegBaseline(segment, ichBeginOffset, ichLimOffset, (iForm, word, iAnalysis) =>
                {
                    CreateAnalysisForWord(word, segment, cache.DefaultAnalWs, fCreateGlosses);
                    return(true);
                },
                                 (sPunc, iAnalysis) => CreatePuncForm(segment, cache.TsStrFactory.MakeString(sPunc, cache.DefaultVernWs)),
                                 (ichOrc, iAnalysis) => CreatePuncForm(segment, paraContents.Substring(segment.BeginOffset + ichOrc, 1)));
            }
        }
コード例 #3
0
ファイル: TsStringUtils.cs プロジェクト: bbriggs/FieldWorks
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Trim the leading AND/OR trailing non-word forming characters.
		/// </summary>
		/// <param name="tssInput">string that may contain non-word forming characters</param>
		/// <param name="writingSystemFactory">The ws factory used to get character properties</param>
		/// <param name="fTrimLeading">if set to <c>true</c> trim leading characters.</param>
		/// <param name="fTrimTrailing">if set to <c>true</c> trim trailing characters.</param>
		/// <returns>
		/// string with leading or trailing non-word forming characters trimmed
		/// </returns>
		/// ------------------------------------------------------------------------------------
		public static ITsString TrimNonWordFormingChars(ITsString tssInput, ILgWritingSystemFactory writingSystemFactory, bool fTrimLeading, bool fTrimTrailing)
		{
			Debug.Assert(fTrimLeading || fTrimTrailing);

			if (tssInput == null)
				return null;
			string untrimmedString = tssInput.Text;
			if (String.IsNullOrEmpty(untrimmedString))
				return tssInput;
			ILgCharacterPropertyEngine charProps = null;

			int ichMin;
			bool fFoundWordFormingChar = false;
			if (fTrimLeading)
			{
				// Trim leading non-word forming characters from string.
				ichMin = -1;
				for (int ich = 0; ich < untrimmedString.Length; ich++)
				{
					charProps = GetCharPropEngineAtOffset(tssInput, writingSystemFactory, ich);

					if (charProps != null && charProps.get_IsWordForming(untrimmedString[ich]))
					{
						// first word-forming character found
						ichMin = ich;
						fFoundWordFormingChar = true;
						break;
					}
				}
				if (ichMin == -1)
					return MakeTss("", GetWsAtOffset(tssInput, 0));
				// no word-forming characters found in the string
			}

			else
				ichMin = 0;

			int strLength;
			if (fTrimTrailing)
			{
				// Trim trailing non-word forming characters from string.
				strLength = 1;
				int iMin = (ichMin == 0) ? -1 : ichMin;
				for (int ich = untrimmedString.Length - 1; ich > iMin; ich--)
				{
					charProps = GetCharPropEngineAtOffset(tssInput, writingSystemFactory, ich);
					if (charProps != null && charProps.get_IsWordForming(untrimmedString[ich]))
					{
						// last word-forming character found
						strLength = ich - ichMin + 1;
						fFoundWordFormingChar = true;
						break;
					}
				}

				if (!fFoundWordFormingChar)
					return MakeTss("", GetWsAtOffset(tssInput, 0));
				// no word-forming characters found in the string
			}

			else
				strLength = untrimmedString.Length - ichMin;

			return tssInput.Substring(ichMin, strLength);
		}
コード例 #4
0
ファイル: TsStringUtils.cs プロジェクト: bbriggs/FieldWorks
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Split the ITsString into pieces separated by one of the strings in separator, and
		/// using the same options as String.Split().
		/// </summary>
		/// ------------------------------------------------------------------------------------
		public static List<ITsString> Split(ITsString tss, string[] separator, StringSplitOptions opt)
		{
			List<ITsString> rgtss = new List<ITsString>();
			if (tss == null || tss.Length == 0 || separator == null || separator.Length == 0)
			{
				rgtss.Add(tss);
			}

			else
			{
				int ich = 0;
				while (ich < tss.Length)
				{
					int cchMatch = 0;
					int ichEnd = tss.Text.IndexOf(separator[0], ich);
					if (ichEnd < 0)
						ichEnd = tss.Length;
					else
						cchMatch = separator[0].Length;
					for (int i = 1; i < separator.Length; ++i)
					{
						int ichEnd2 = tss.Text.IndexOf(separator[i], ich);
						if (ichEnd2 < 0)
							ichEnd2 = tss.Length;
						if (ichEnd2 < ichEnd)
						{
							ichEnd = ichEnd2;
							cchMatch = separator[i].Length;
						}
					}
					int length = ichEnd - ich;
					if (length > 0 || opt == StringSplitOptions.None)
						rgtss.Add(tss.Substring(ich, length));
					ich = ichEnd + cchMatch;
				}
			}
			return rgtss;
		}
コード例 #5
0
 public MultiLineInsertData(Selection whereToInsert, List <ITsString> stringToInsert, List <IStyle> styles)
 {
     if (stringToInsert == null || stringToInsert.Count <= 0)
     {
         return;
     }
     ParaStyles = styles;
     Selection  = whereToInsert;
     if (stringToInsert.Count == 1)
     {
         ITsString tsString = stringToInsert[0];
         int       charIndexOfNextRun;
         int       startOfUnprocessedLines      = tsString.Text.IndexOfAny(new[] { '\r', '\n' });
         int       limOfUnprocessedLines        = tsString.Text.LastIndexOfAny(new[] { '\r', '\n' }) + 1;
         int       otherStartOfUnprocessedLines = tsString.Text.IndexOf(@"\par");
         int       otherLimOfUnprocessedLines   = tsString.Text.LastIndexOf(@"\par") + 1;
         startOfUnprocessedLines =
             Math.Min(startOfUnprocessedLines != -1 ? startOfUnprocessedLines : otherStartOfUnprocessedLines,
                      otherStartOfUnprocessedLines != -1 ? otherStartOfUnprocessedLines : startOfUnprocessedLines);
         limOfUnprocessedLines  = Math.Max(limOfUnprocessedLines, otherLimOfUnprocessedLines);
         TsStrAppendToFirstPara = tsString.Substring(0, startOfUnprocessedLines);
         if (limOfUnprocessedLines == tsString.Length)
         {
             TsStrPrependToLastPara =
                 TsStrFactoryClass.Create().EmptyString(
                     tsString.Runs().Reverse().Where(run => run.Props.GetWs() > 0).First().Props.GetWs());
         }
         else
         {
             TsStrPrependToLastPara = tsString.Substring(limOfUnprocessedLines);
             if (TsStrPrependToLastPara.Text == "\r" || TsStrPrependToLastPara.Text == "\n" ||
                 TsStrPrependToLastPara.Text == @"\par")
             {
                 var bldr = TsStrPrependToLastPara.GetBldr();
                 bldr.Replace(0, TsStrPrependToLastPara.Text.Length, "", null);
                 TsStrPrependToLastPara = bldr.GetString();
             }
         }
         if (TsStrAppendToFirstPara.Text == "\r" || TsStrAppendToFirstPara.Text == "\n" || TsStrAppendToFirstPara.Text == @"\par")
         {
             var bldr = TsStrAppendToFirstPara.GetBldr();
             bldr.Replace(0, TsStrAppendToFirstPara.Text.Length, "", null);
             TsStrAppendToFirstPara = bldr.GetString();
         }
         InsertedTsStrLines = new List <ITsString>();
         while (true)
         {
             startOfUnprocessedLines = AdvancePastLineBreak(startOfUnprocessedLines, tsString.Text);
             if (startOfUnprocessedLines >= limOfUnprocessedLines)
             {
                 break;
             }
             int limIndex   = tsString.Text.IndexOfAny(new[] { '\r', '\n' }, startOfUnprocessedLines);
             int otherIndex = tsString.Text.IndexOf(@"\par", startOfUnprocessedLines);
             limIndex = Math.Min(limIndex != -1 ? limIndex : otherIndex, otherIndex != -1 ? otherIndex : limIndex);
             string nextString = tsString.Text.Substring(startOfUnprocessedLines, limIndex - startOfUnprocessedLines);
             nextString = nextString.Trim(new[] { '\r', '\n' });
             nextString = nextString.Replace(@"\par", "");
             var bldr =
                 TsStrFactoryClass.Create().MakeString(nextString, tsString.get_WritingSystemAt(startOfUnprocessedLines)).GetBldr();
             for (int runIndex = tsString.get_RunAt(startOfUnprocessedLines); runIndex < tsString.get_RunAt(limIndex); runIndex++)
             {
                 int start = tsString.get_MinOfRun(runIndex) - startOfUnprocessedLines;
                 int end   = tsString.get_LimOfRun(runIndex) - startOfUnprocessedLines;
                 bldr.Replace(start, end, bldr.Text.Substring(start, end - start), tsString.get_Properties(runIndex));
             }
             InsertedTsStrLines.Add(bldr.GetString());
             startOfUnprocessedLines = limIndex;
         }
     }
     else
     {
         TsStrAppendToFirstPara = stringToInsert.First();
         TsStrPrependToLastPara = stringToInsert.Last();
         stringToInsert.Remove(stringToInsert.First());
         stringToInsert.Remove(stringToInsert.Last());
         InsertedTsStrLines = stringToInsert;
     }
 }