0

In the documentation of nltk.translate I can see it clearly that there is this

nltk.translate.bleu_score.sentence_bleu

function available for use. But when I use it for my script it shows the below error:

AttributeError: 'module' object has no attribute 'sentence_bleu'

Please suggest what mistake am I doing? or anything I missed in between.

For reference below is my sample code:

import nltk 
hypothesis = ['It', 'is', 'a', 'cat', 'at', 'room']
reference = ['It', 'is', 'a', 'cat', 'inside', 'the', 'room']
BLEUscore = nltk.translate.bleu_score.sentence_bleu([reference], hypothesis)

This error occured due to the version of NLTK. This module is supported in NLTK 3.3 and above. Hava a look here

My NLTK version was 3.1

SilentFlame
  • 487
  • 5
  • 15
  • You can have a look at the documentation of nltk.translate at http://www.nltk.org/api/nltk.translate.html – SilentFlame Jun 14 '18 at 04:12
  • First, do you have version 3.3 or later of NLTK? – abarnert Jun 14 '18 at 04:12
  • 1
    I have NLTk 3.1, Oh I see. The version is the problem. @abarnert thanks for help. :) – SilentFlame Jun 14 '18 at 04:15
  • did you try doing " from nltk.translate.bleu_score import sentence_bleu" – rawwar Jun 14 '18 at 04:18
  • I don't know for _sure_ that it's the problem (you can check the 3.1 docs, although I don't know how to dig them up off the top of my head), but given that [the top-level docs don't say anything about the `translate` module beyond "Experimental features for machine translation. These interfaces are prone to change."](http://www.nltk.org/api/nltk.html#module-nltk.translate), it seems like a good bet that they changed between 3.1 and 3.3. – abarnert Jun 14 '18 at 04:19
  • 1
    @InAFlash I have tried all the possible permutations of it. It seems the version of NLTK is the issue as mentioned in above comment by abarnert. – SilentFlame Jun 14 '18 at 04:20
  • @abarnert let me have a look. :) – SilentFlame Jun 14 '18 at 04:21
  • 1
    @SilentFlame, yes i think its version problem too. i just checked and i have 3.3 and was able to import with the command i mentioned in the previous comment – rawwar Jun 14 '18 at 04:22
  • 1
    By the way, you should edit the docs link into your question, instead of only having it as a comment. – abarnert Jun 14 '18 at 04:31
  • Please refer: [https://stackoverflow.com/questions/40542523/nltk-corpus-level-bleu-vs-sentence-level-bleu-score](https://stackoverflow.com/questions/40542523/nltk-corpus-level-bleu-vs-sentence-level-bleu-score) – Mugdha Jun 14 '18 at 05:33

1 Answers1

0

The NLTK version is the problem. Install the latest version using:

pip install nltk==3.5