Advertisement
sylviapsh

Extract Sentences Containing Certain Word

Jan 29th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6.   class ExtractSentencesContainingCertainWord
  7. {
  8.     //Write a program that extracts from a given text all sentences containing given word.
  9.         //Example: The word is "in". The text is:
  10.     //We are living in a yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days.
  11.     //The expected result is:
  12.     //We are living in a yellow submarine.
  13.     //We will move out of it in 5 days.
  14.     //Consider that the sentences are separated by "." and the words – by non-letter symbols.
  15.  
  16.   static List<int> FindSentencesWithWord(string[] sentences, string word)//Returns a list with the indices of the sentences that contain the searched word
  17.   {
  18.     List<int> sentencesToOutput = new List<int>();
  19.  
  20.     for (int i = 0; i < sentences.Length; i++)
  21.     {
  22.       if (Regex.Matches(sentences[i], "\\b" + word + "\\b",RegexOptions.IgnoreCase).Count > 0)
  23.       {
  24.         sentencesToOutput.Add(i);
  25.       }
  26.     }
  27.     return sentencesToOutput;
  28.   }
  29.  
  30.   static void Main()
  31.   {
  32.     string text = "We are living in a yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days.";
  33.     string word = "in";
  34.     List<int> indecesOfwordInText = new List<int>();
  35.  
  36.     string[] sentencesArray;
  37.     sentencesArray = text.Split('.');
  38.  
  39.     indecesOfwordInText = FindSentencesWithWord(sentencesArray, word);//Get the sentences that contain the word
  40.  
  41.     //Print on the console the found sentences
  42.     for (int i = 0; i < indecesOfwordInText.Count; i++)
  43.     {
  44.       Console.WriteLine((sentencesArray[(indecesOfwordInText[i])] + ".").Trim()); //Add the dot and trim the emty spaces of each sentence that we are printing
  45.     }
  46.    
  47.   }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement