AttributeError: 'the module object has no attributes' score'

I get an error when trying to use the precision function from nltk.metrics.scores . I tried many different imports, but without success.

I looked at the files in my python directories (see below), and the function is there, but just "can't touch this / that." I watched:

 /usr/local/lib/python2.7/dist-packages/nltk/metrics /usr/local/lib/python2.7/dist-packages/nltk/metrics/scores.py 

This is what my terminal shows:

 File "/home/login/projects/python-projects/test.py", line 39, in <module> precision = nltk.metrics.scores.precision(correct[CLASS_POS], predicted[CLASS_POS]) AttributeError: 'module' object has no attribute 'scores' 

In my search I came across a link that gives me two options, but I don’t know how to go to any of these

  • The obvious reason for this is that the settings.py parameter does not have the directory containing blah specified in INSTALLED_APPS .
  • Less obvious reason: you will also get this error if the directory does not contain the __init__.py file.
+7
python nltk
source share
1 answer

In short:

 from nltk import precision 

In the long

It's complicated. The problem arose because NLTK was packaged. If we look at dir(nltk.metrics) , there is nothing inside it except alignment_error_rate

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

BTW, in the NLTK version with a short release, alignment_error_rate moved to nltk.translate.metrics , see https://github.com/nltk/nltk/blob/develop/nltk/translate/metrics.py#L10 . The nltk.translate package nltk.translate bit unstable because it is still under development.

Returning to the metrics package, https://github.com/nltk/nltk/blob/develop/nltk/metrics/__init__.py , we see the following:

 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, it means that the functions from the metrics package were manually encoded and nltk.metrics.__init__.py to nltk.metrics.__init__.py . Therefore, if import stops here, dir(metrics) , he would list all the indicators imported here.

But since at a higher level in nltk.__init__.py https://github.com/nltk/nltk/blob/develop/nltk/__init__.py#L131 , the packages were imported with:

 from nltk.metrics import * 

Now all metrics metrics have been imported to the top level, which you can do:

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

But you can still access any middleware modules that are inside nltk.metrics that are not imported into nltk.metrics.__init__.py . But you must use the correct namespaces as a save function in the appropriate directory. Note that they will not appear in dir(nltk.metrics) , but they 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> 
+11
source share

All Articles