Getting n number of words on either side of a particular word in a sentence : <p>I need to extract context words on either side of a particular word in a string. The particular word in this case pertains to a proper noun in the string. E.g.</p>

<p>I love the cakes Martha bakes as they are so delicious!</p>

<p>In this case Martha is a proper noun and I would like to extract say 4 words on either side of Martha to be able to classify Martha as a name, location or organization. In this case bakes is my clue that Marth is a person. I was looking at a window size of 4 but what in the cases where there are fewer than 4 words on either side of the target word or what if its the  first word of the sentence.</p>

<p>So essentially:</p>

<ol>
<li>I'd like to get 4 words on either side of the target word.</li>
<li>Put them in two separate lists called leftWords and rightWords </li>
<li>Check to see if there are fewer than 4 words then reduce the window size and get whatever number of words are available on either side.</li>
</ol>
