tags:

views:

46

answers:

1

Hi,

I'm trying to get x number of unique words on each side of a word in a string in C#. for example, the method

GetUniqueWords("she sells seashells, by the seashore. the shells she sells, are surely seashells.", "seashore", 3) 

(The first parameter is the sentence string. The second parameter is the word that will be used to get words on its two sides. The third parameter is the number of words to check)

will return a list of string with values:

seashells
by
the
shells
she

Thanks in advance.

+2  A: 

Not pretty, but working for your sample :-)

    private static IEnumerable<string> GetUniqueWords(string phrase, string word, int amount)
    {
        //Clean up the string and get the words
        string[] words = Regex.Split(phrase.Replace(".","").Replace(",",""),@"\s+");

        //Find the first occurrence of the desired word (spec allows)
        int index = Array.IndexOf(words,word);

        //We don't wrap around the edges if the word is the first, 
        //we won't look before if last, we won't look after, again, spec allows
        int min = (index - amount < 0) ? 0 : index - amount;
        int max = (index + amount > words.Count() - 1) ? words.Count() - 1 : index + amount;

        //Add all the words to a list except the supplied one
        List<string> rv = new List<string>();
        for (int i = min; i <= max; i++)
        {
            if (i == index) continue;
            rv.Add(words[i]);
        }

        //Unique-ify the resulting list
        return rv.Distinct();
    }
}
Vinko Vrsalovic
magic, thank you!!!
StarCub