python计算bleu代码
时间: 2023-07-23 11:04:41 浏览: 243
Sure! Here's an example code in Python for calculating BLEU (Bilingual Evaluation Understudy) score using the `nltk` library:
```python
import nltk
# Reference sentences
ref_sentences = [['The cat is on the mat'], ['There is a cat on the mat']]
# Candidate sentence
candidate_sentence = ['The cat is on the mat']
# Calculate BLEU score
bleu_score = nltk.translate.bleu_score.corpus_bleu(ref_sentences, candidate_sentence)
print("BLEU score:", bleu_score)
```
Make sure you have the `nltk` library installed before running this code. You can install it by running `pip install nltk` in your command prompt or terminal.
In this example, we have a set of reference sentences (`ref_sentences`) and a single candidate sentence (`candidate_sentence`). The `corpus_bleu` function from the `nltk.translate.bleu_score` module is used to calculate the BLEU score. The resulting BLEU score is then printed to the console.
You can modify the code according to your specific use case, such as using different reference sentences or multiple candidate sentences.
Hope this helps! Let me know if you have any more questions.
阅读全文