<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _generate_results(self, output_dir, labels, results):
 """Creates multiple results files in `output_dir` based on `results`. For each label in `labels`, create three results file, containing those n-grams with that label that first occurred, only occurred, and last occurred in that label. """

        ngrams = {}
        for idx, label in enumerate(labels):
            now_results = results[results[constants.LABEL_FIELDNAME] == label]
            earlier_labels = labels[:idx]
            earlier_ngrams = results[results[constants.LABEL_FIELDNAME].isin(
                earlier_labels)][constants.NGRAM_FIELDNAME].values
            later_labels = labels[idx + 1:]
            later_ngrams = results[results[constants.LABEL_FIELDNAME].isin(
                later_labels)][constants.NGRAM_FIELDNAME].values
            first_ngrams = []
            only_ngrams = []
            last_ngrams = []
            for ngram in now_results[constants.NGRAM_FIELDNAME].unique():
                if ngram in earlier_ngrams:
                    if ngram not in later_ngrams:
                        last_ngrams.append(ngram)
                elif ngram in later_ngrams:
                    first_ngrams.append(ngram)
                else:
                    only_ngrams.append(ngram)
            self._save_results(output_dir, label, now_results, first_ngrams,
                               'first')
            self._save_results(output_dir, label, now_results, only_ngrams,
                               'only')
            self._save_results(output_dir, label, now_results, last_ngrams,
                               'last')
            ngrams[label] = {'first': first_ngrams, 'last': last_ngrams,
                             'only': only_ngrams}
        return ngrams