Like all AIs, this is a very complex problem. You should study natural language processing to learn about some of the issues.
One very, very simplified approach may be to build a 2d-table of words, and for each pair of words the average distance (in words) that they are displayed in the text. Obviously, you need to limit the maximum distance traveled and possibly the number of words. Then, after processing a large amount of text, you will have an indicator of how often some words appear in the same context.
source share