A comment left by the author of the summarization algorithm I used has explained how to reduce the loops when ranking words.