示例#1
0
        private IEnumerable <ISentenceItem> ParseSubSentence(string source)
        {
            List <ISentenceItem> wordsAndSpaces = new List <ISentenceItem>();
            int startIndex     = 0;
            int spaceOccurence = source.IndexOf(SeparatorContainer.Space(), startIndex);

            while (spaceOccurence != -1)
            {
                if (spaceOccurence != startIndex)
                {//beginning with word
                    wordsAndSpaces.Add(_sentenceItemFactory.Create(source.Substring(startIndex, spaceOccurence - startIndex)));
                    wordsAndSpaces.Add(_sentenceItemFactory.Create(SeparatorContainer.Space().ToString()));
                }
                else
                {//beginning with space
                    wordsAndSpaces.Add(_sentenceItemFactory.Create(SeparatorContainer.Space().ToString()));
                }

                startIndex     = spaceOccurence + 1;
                spaceOccurence = source.IndexOf(SeparatorContainer.Space(), startIndex);
            }
            wordsAndSpaces.Add(_sentenceItemFactory.Create(source.Substring(startIndex)));

            return(wordsAndSpaces.AsEnumerable());
        }
示例#2
0
        static void Main(string[] args)
        {
            TextReader         textReader = new StreamReader("../../test_1.txt");
            SeparatorContainer sc         = new SeparatorContainer();
            Parser             parser     = new Parser(sc);
            Text text = parser.Parse(textReader);

            // Отображать все предложения заданного текста в порядке возрастания количества слов в каждом из них.
            text.SentancesOrderedByWordCount.ForEach(s =>
            {
                Console.WriteLine(s.Chars);
                Console.WriteLine("=={0}==----------------------------------", s.WordCount);
            });

            // Во всех вопросительных предложениях текста найти и напечатать без повторений слова заданной длины.
            int length = 3;

            Console.WriteLine("In all interrogative sentences of the text print the word without repetition of a given length = {0}.", length);
            text.InterrogativeSentancesWordsDistinct(length).ForEach(w =>
            {
                Console.WriteLine(w.Chars);
            });

            //Из текста удалить все слова заданной длины, начинающиеся на согласную букву.
            text.RemoveConsonantWords(3);

            Console.WriteLine("----------------------------");

            Console.WriteLine("In all interrogative sentences of the text print the word without repetition of a given length = {0}.", length);
            text.InterrogativeSentancesWordsDistinct(length).ForEach(w =>
            {
                Console.WriteLine(w.Chars);
            });

            Console.WriteLine("----------------------------");

            //"В некотором предложении текста слова заданной длины заменить указанной подстрокой, длина которой может не совпадать с длиной слова."
            text.Content[10].Replace(6, "YOYOYOYO, YOYOYOYO");

            var c = text.Concordances;

            text.PrintConcordances();

            text.ToFile("../../out.txt");
        }
示例#3
0
        private Sentence ParseSentence(string source)
        {
            Console.WriteLine("Parsing {0} sentence in {1} paragraph", currentSentence, currentLine);

            Sentence sentence = new Sentence();

            while (source.Length > 0)
            {
                int    separatorOccurence = -1;
                string sentenceSeparator  = SeparatorContainer.WordSeparators().FirstOrDefault(
                    x =>
                {
                    separatorOccurence = source.IndexOf(x);
                    return(separatorOccurence >= 0);
                });

                if (separatorOccurence == -1)
                {//there is no separators in sentence
                    sentence.AddSentenceItemsRange(ParseSubSentence(source));
                    source = source.Remove(0);
                }
                else if (separatorOccurence == 0)
                {//separator is first in sentence
                    sentence.AddSentenceItem(_sentenceItemFactory.Create(source.Substring(0, sentenceSeparator.Length)));
                    source = source.Remove(0, sentenceSeparator.Length);
                }
                else
                {//separator inside somewhere between words
                    string subsentence = source.Substring(0, separatorOccurence);
                    sentence.AddSentenceItemsRange(ParseSubSentence(subsentence));
                    sentence.AddSentenceItem(_sentenceItemFactory.Create(source.Substring(separatorOccurence, sentenceSeparator.Length)));
                    source = source.Remove(0, separatorOccurence + sentenceSeparator.Length);
                }
            }

            currentSentence++;

            return(sentence);
        }
示例#4
0
        private Line ParseLine(string source)
        {
            Line line = new Line();

            while (source.Length > 0)
            {
                int    sentenceSeparatorOccurence = -1;
                string sentenceSeparator          = SeparatorContainer.SentenceSeparators().FirstOrDefault(
                    x =>
                {
                    sentenceSeparatorOccurence = source.IndexOf(x);
                    return(sentenceSeparatorOccurence >= 0);
                });

                line.AddSentence(ParseSentence(source.Substring(0, sentenceSeparatorOccurence + sentenceSeparator.Length)));

                source = source.Remove(0, sentenceSeparatorOccurence + sentenceSeparator.Length);
            }

            currentLine++;

            return(line);
        }
示例#5
0
 public Parser()
 {
     SeparatorContainer = new SeparatorContainer();
     WordFactory        = new WordFactory();
     PunctuationFactory = new PunctuationFactory(SeparatorContainer);
 }