AttributeError: ‘module’ object has no attribute 'scores'

前端 未结 2 1492
梦谈多话
梦谈多话 2021-01-05 07:17

I am getting an error when trying to use the function precision from nltk.metrics.scores. I have tried many different imports but with no success.<

相关标签:
2条回答
  • 2021-01-05 07:53

    Replace import of nltk.metrics by this :

    from nltk.metrics import *
    

    Now call precision or scores or recall directly.

    0 讨论(0)
  • 2021-01-05 08:01

    In short:

    from nltk import precision
    

    In long:

    This is tricky. The issue occurred because of how NLTK was packaged. If we look at dir(nltk.metrics), there's nothing inside it, other than alignment_error_rate

    >>> import nltk
    >>> dir(nltk.metrics)
    ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'alignment_error_rate']
    

    BTW, in the bleeding edge version of NLTK, alignment_error_rate has been moved to nltk.translate.metrics, see https://github.com/nltk/nltk/blob/develop/nltk/translate/metrics.py#L10 . The nltk.translate package is a little unstable because it's still under-development.

    Going back to the metrics package, from https://github.com/nltk/nltk/blob/develop/nltk/metrics/__init__.py, we see this:

    from nltk.metrics.scores import          (accuracy, precision, recall, f_measure,
                                              log_likelihood, approxrand)
    from nltk.metrics.confusionmatrix import ConfusionMatrix
    from nltk.metrics.distance        import (edit_distance, binary_distance,
                                              jaccard_distance, masi_distance,
                                              interval_distance, custom_distance,
                                              presence, fractional_presence)
    from nltk.metrics.paice           import Paice
    from nltk.metrics.segmentation    import windowdiff, ghd, pk
    from nltk.metrics.agreement       import AnnotationTask
    from nltk.metrics.association     import (NgramAssocMeasures, BigramAssocMeasures,
                                              TrigramAssocMeasures, ContingencyMeasures)
    from nltk.metrics.spearman        import (spearman_correlation, ranks_from_sequence,
                                          ranks_from_scores)
    

    basically, this means that the functions from the metrics package has been manually coded and pushed up to nltk.metrics.__init__.py. So if the imports stop here, dir(metrics), would have listed all the metrics imported here.

    But because on the higher level, at nltk.__init__.py https://github.com/nltk/nltk/blob/develop/nltk/__init__.py#L131, the packages was imported using:

    from nltk.metrics import *
    

    Now all metrics score has been imported to the top level meaning you can do:

    >>> from nltk import precision
    >>> from nltk import spearman_correlation
    >>> from nltk import NgramAssocMeasures
    

    But you can still access any intermediate level modules that are within nltk.metrics that are not imported in nltk.metrics.__init__.py. But you have to use the correct namespaces as how the functions are saved in their respective directory. Note that these will not show in dir(nltk.metrics) but are valid ways to import a function:

    >>> from nltk.metrics import spearman
    >>> from nltk.metrics import paice
    >>> from nltk.metrics import scores
    <function precision at 0x7fb584a34938>
    >>> scores.precision
    >>> spearman.spearman_correlation
    <function spearman_correlation at 0x7fb5842b3230>
    >>> from nltk.metrics.scores import precision
    >>> precision
    <function precision at 0x7fb584a34938>
    
    0 讨论(0)
提交回复
热议问题