当前位置: 首页>>代码示例>>C#>>正文


C# Sentence.Last方法代码示例

本文整理汇总了C#中Sentence.Last方法的典型用法代码示例。如果您正苦于以下问题:C# Sentence.Last方法的具体用法?C# Sentence.Last怎么用?C# Sentence.Last使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Sentence的用法示例。


在下文中一共展示了Sentence.Last方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ParseSentenceString

        public static Sentence ParseSentenceString(string source, PunctuationMark endMark)
        {
            var sentence = new Sentence();

            // Get split parts of the sentence
            var splitParts = source.Split(new char[] { ' ' });

            foreach (var part in splitParts)
            {
                var sPart = part;

                // Get inner punctuation marks in the split part
                var innerPunctuationMarks = sPart.GetPunctuationMarks(DefaultPunctuationMarks.InternalPunctuationMarks);
                if (innerPunctuationMarks.Any())
                {
                    var sentenceParts = new List<ISingleTextElement>();
                    var compositeWord = new CompositeWord();

                    // If split part contains punctuation marks, split this part to single text elements
                    foreach (var mark in innerPunctuationMarks)
                    {
                        var markLenght = mark.StringValue.Length;
                        var index = sPart.IndexOfPunctuationMark(mark);

                        var leftPart = sPart.Substring(0, index);
                        if (!string.IsNullOrEmpty(leftPart))
                        {
                            var word = new Word() { StringValue = leftPart };
                            compositeWord.Add(word);
                            if (!sentenceParts.Contains(compositeWord))
                                sentenceParts.Add(compositeWord);
                        }

                        if(index == 0)
                        {
                            sentenceParts.Add(mark);
                        }
                        else if(index + markLenght < sPart.Length)
                        {
                            compositeWord.Add(mark);
                            if (!sentenceParts.Contains(compositeWord))
                                sentenceParts.Add(compositeWord);
                        }
                        else
                        {
                            sentenceParts.Add(mark);
                        }

                        sPart = sPart.Substring(index + markLenght);
                    }

                    if (!string.IsNullOrEmpty(sPart))
                    {
                        var word = new Word() { StringValue = sPart };
                        compositeWord.Add(word);
                        if (!sentenceParts.Contains(compositeWord))
                            sentenceParts.Add(compositeWord);
                    }

                    // Add single text elements to sentence
                    foreach(var sentencePart in sentenceParts)
                    {
                        if (sentencePart == sentenceParts.Last())
                            sentencePart.InnerOption = SingleTextElementInnerOption.RightSpace;
                        sentence.Add(sentencePart);
                    }
                }
                else
                {
                    var word = new Word() { StringValue = sPart, InnerOption = SingleTextElementInnerOption.RightSpace };
                    sentence.Add(word);
                }
            }

            if (endMark.HasValue)
            {
                if(sentence.Last().InnerOption == SingleTextElementInnerOption.RightSpace)
                {
                    sentence.Last().InnerOption = SingleTextElementInnerOption.None;
                }
                sentence.Add(endMark);
            }

            return sentence;
        }
开发者ID:GeorgeManiya,项目名称:EpamTraining,代码行数:85,代码来源:TextParser.cs


注:本文中的Sentence.Last方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。