Nltk: Missing modules from nltk.metrics in Python 3.5 ??

Created on 21 Nov 2016  ·  7Comments  ·  Source: nltk/nltk

Hey, I'm trying to compute

nltk.metrics.distance.edit_distance

in v3.5, but I'm seeing "module 'nltk.translate.metrics' has no attribute 'distance'"

Can you help?
Thx!

Most helpful comment

Please use this idiom instead of nltk.metrics.distance.edit_distance:

from nltk.metrics import edit_distance

The explanation can be found on http://stackoverflow.com/questions/33398282/attributeerror-module-object-has-no-attribute-scores

>>> import inspect
>>> from nltk.metrics import edit_distance
>>> print inspect.getdoc(edit_distance)
Calculate the Levenshtein edit-distance between two strings.
The edit distance is the number of characters that need to be
substituted, inserted, or deleted, to transform s1 into s2.  For
example, transforming "rain" to "shine" requires three steps,
consisting of two substitutions and one insertion:
"rain" -> "sain" -> "shin" -> "shine".  These operations could have
been done in other orders, but at least three steps are needed.

This also optionally allows transposition edits (e.g., "ab" -> "ba"),
though this is disabled by default.

:param s1, s2: The strings to be analysed
:param transpositions: Whether to allow transposition edits
:type s1: str
:type s2: str
:type transpositions: bool
:rtype int

All 7 comments

Please use this idiom instead of nltk.metrics.distance.edit_distance:

from nltk.metrics import edit_distance

The explanation can be found on http://stackoverflow.com/questions/33398282/attributeerror-module-object-has-no-attribute-scores

>>> import inspect
>>> from nltk.metrics import edit_distance
>>> print inspect.getdoc(edit_distance)
Calculate the Levenshtein edit-distance between two strings.
The edit distance is the number of characters that need to be
substituted, inserted, or deleted, to transform s1 into s2.  For
example, transforming "rain" to "shine" requires three steps,
consisting of two substitutions and one insertion:
"rain" -> "sain" -> "shin" -> "shine".  These operations could have
been done in other orders, but at least three steps are needed.

This also optionally allows transposition edits (e.g., "ab" -> "ba"),
though this is disabled by default.

:param s1, s2: The strings to be analysed
:param transpositions: Whether to allow transposition edits
:type s1: str
:type s2: str
:type transpositions: bool
:rtype int

wonderful, thanks :)

Thanks, this works fine for me for chrf_score. But, unfortunately, it does not work for meteor_score
from nltk.translate import meteor_score

gives:

ImportError                               Traceback (most recent call last)

<ipython-input-27-807021fa1266> in <module>()
----> 1 from nltk.translate import meteor_score

ImportError: cannot import name 'meteor_score'

Anyone with a hint on how to import meteor_score from nltk?

@VincentCCL Hello!
I can't reproduce your issue: I can use from nltk.translate import meteor_score without an ImportError. This will however import a module, and not the function you seem to be after. The same goes for importing chrf_score.

As can be seen here: https://github.com/nltk/nltk/blob/220920caf5f313d67e82474415310e6b44bdc4de/nltk/translate/__init__.py#L23
This function is likely meant to be imported as
from nltk.translate import meteor
The same goes for chrf:
from nltk.translate import chrf

However, I can't promise this'll work for you, as I cannot reproduce your issue to begin with.
For context, these are my results, with nltk version 3.4.4

>>> from nltk.translate import meteor_score
>>> from nltk.translate import meteor      
>>> type(meteor_score)
<class 'module'>
>>> type(meteor)
<class 'function'>

If this fails, you might try updating your nltk version and retrying these steps.

Good luck!

I tried in on Google Colab:
here

I first do:
!pip install nltk

from nltk.translate import meteor_score

ImportError                               Traceback (most recent call last)

<ipython-input-6-807021fa1266> in <module>()
----> 1 from nltk.translate import meteor_score

ImportError: cannot import name 'meteor_score'


Same for
from nltk.translate import meteor

This should give me the most recent version of nltk, no?
thanks

@VincentCCL Ah, yes. That link is useful. It tells me that the the version of nltk that's installed is 3.2.5. (This can be seen in the first line after !pip install nltk)

Version 3.2.5 dates to 24 Sep 2017.
According to the ChangeLog:
https://github.com/nltk/nltk/blob/ca357e5cdcdb137f40c45346bb8bfea618dd863f/ChangeLog#L52-L55
The meteor score was only added in version 3.4.1, dating to 2019-04-17.

So, you cannot import this file because this file does not exist in the nltk version that you have installed.

!pip install nltk will first check whether there is already a version of nltk installed. Because there is (an older one), the newer one isn't installed. You can upgrade to the newest version by running !pip install --upgrade nltk, or you can run !pip install nltk==3.5 to install specifically version 3.5, which is currently the newest one.

Good luck!

That solves it -- thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jeryini picture jeryini  ·  5Comments

stevenbird picture stevenbird  ·  3Comments

zdog234 picture zdog234  ·  3Comments

alvations picture alvations  ·  4Comments

peterbe picture peterbe  ·  5Comments