def write_lm(self, outfile):
        """
        Write the text-based language model to a file
        """

        # TODO(jbg): actually write the correct mean and variance

        outfile.write("%i\n" % len(self._unigram))
        outfile.write("%i\n" % len(self._vocab))
        vocab_size = len(self._vocab)
        for ii, count in sorted(self._vocab.iteritems(),
                                key=lambda key_value: key_value[1]):
            outfile.write("%s\n" % ii)
        print("Done writing vocab")

        corpus_num = 0
        for ii in sorted(self._unigram):
            corpus_num += 1
            if corpus_num % 100 == 0:
                print("Writing LM for %s (%i of %i)" %
                      (unidecode(ii), corpus_num, len(self._unigram)))

            outfile.write("%s %i\n" % (ii, self.compare(ii)))

        for ii in sorted(self._unigram):
            for jj in xrange(vocab_size):
                if jj in self._obs_counts[ii]:
                    total = self._obs_counts[ii][jj].N()
                    contexts = self._obs_counts[ii][jj].B()
                else:
                    total = 0
                    contexts = 0
                outfile.write("%i %i %i\n" % (jj, total, contexts))
                if jj in self._obs_counts[ii]:
                    for kk in self._obs_counts[ii][jj]:
                        outfile.write("%i %i\n" %
                                      (kk, self._obs_counts[ii][jj][kk]))
dataset/ETHPy150Open Pinafore/qb/clm/lm_wrapper.py/LanguageModelWriter.write_lm/original