util.py 474 B

123456789101112131415161718192021
  1. # -*- coding: utf-8 -*-
  2. # Natural Language Toolkit
  3. #
  4. # Copyright (C) 2001-2019 NLTK Project
  5. # Author: Ilia Kurenkov <ilia.kurenkov@gmail.com>
  6. # URL: <http://nltk.org/>
  7. # For license information, see LICENSE.TXT
  8. """Language Model Utilities"""
  9. from math import log
  10. NEG_INF = float("-inf")
  11. POS_INF = float("inf")
  12. def log_base2(score):
  13. """Convenience function for computing logarithms with base 2."""
  14. if score == 0.0:
  15. return NEG_INF
  16. return log(score, 2)