Prevent importing ngrams which will lead to loops
Second part of #498.
This MR extends the ngrams machinery such that if we were to try importing ngrams which would lead to loops, GGTX will now reject it:
To the best of my knowledge loops can be created only manually by importing (it shouldn't be possible via the frontend UI to create some), and ngrams extracted from documents are always flattened, this should cover all cases.
@cgenie When you have some spare cycles, would you mind reviewing? I will add the usual inline commentary.