Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing modules from nltk.metrics in Python 3.5 ?? #1516

Closed
jonrbates opened this issue Nov 21, 2016 · 7 comments
Closed

Missing modules from nltk.metrics in Python 3.5 ?? #1516

jonrbates opened this issue Nov 21, 2016 · 7 comments

Comments

@jonrbates
Copy link

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!

@alvations
Copy link
Contributor

alvations commented Nov 21, 2016

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

@jonrbates
Copy link
Author

wonderful, thanks :)

@VincentCCL
Copy link

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?

@tomaarsen
Copy link
Member

@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:

from nltk.translate.meteor_score import meteor_score as meteor

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!

@VincentCCL
Copy link

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

@tomaarsen
Copy link
Member

@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:

nltk/ChangeLog

Lines 52 to 55 in ca357e5

Version 3.4.1 2019-04-17
* add chomsky_normal_form for CFGs
* add meteor score

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!

@VincentCCL
Copy link

That solves it -- thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants