nltk 3.2.1__zip → 3.3__zip
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {nltk-3.2.1 → nltk-3.3}/LICENSE.txt +1 -1
- {nltk-3.2.1/nltk.egg-info → nltk-3.3}/PKG-INFO +13 -5
- nltk-3.3/README.md +49 -0
- nltk-3.3/nltk/VERSION +1 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/__init__.py +3 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/app/__init__.py +2 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/app/chartparser_app.py +92 -92
- {nltk-3.2.1 → nltk-3.3}/nltk/app/chunkparser_app.py +11 -13
- {nltk-3.2.1 → nltk-3.3}/nltk/app/collocations_app.py +8 -11
- {nltk-3.2.1 → nltk-3.3}/nltk/app/concordance_app.py +9 -11
- {nltk-3.2.1 → nltk-3.3}/nltk/app/nemo_app.py +18 -17
- {nltk-3.2.1 → nltk-3.3}/nltk/app/rdparser_app.py +10 -12
- {nltk-3.2.1 → nltk-3.3}/nltk/app/srparser_app.py +16 -16
- {nltk-3.2.1 → nltk-3.3}/nltk/app/wordfreq_app.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/app/wordnet_app.py +11 -11
- {nltk-3.2.1 → nltk-3.3}/nltk/book.py +2 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/ccg/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/ccg/api.py +67 -47
- {nltk-3.2.1 → nltk-3.3}/nltk/ccg/chart.py +7 -5
- {nltk-3.2.1 → nltk-3.3}/nltk/ccg/combinator.py +42 -11
- {nltk-3.2.1 → nltk-3.3}/nltk/ccg/lexicon.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/ccg/logic.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/chat/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/chat/eliza.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/chat/iesha.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/chat/rude.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/chat/suntsu.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/chat/util.py +12 -10
- {nltk-3.2.1 → nltk-3.3}/nltk/chat/zen.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/chunk/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/chunk/api.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/chunk/named_entity.py +13 -13
- {nltk-3.2.1 → nltk-3.3}/nltk/chunk/regexp.py +9 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/chunk/util.py +12 -12
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/api.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/decisiontree.py +18 -18
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/maxent.py +26 -8
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/megam.py +5 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/naivebayes.py +29 -26
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/rte_classify.py +58 -89
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/scikitlearn.py +4 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/senna.py +24 -22
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/svm.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/tadm.py +4 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/textcat.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/util.py +13 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/weka.py +7 -6
- {nltk-3.2.1 → nltk-3.3}/nltk/cluster/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/cluster/api.py +9 -5
- {nltk-3.2.1 → nltk-3.3}/nltk/cluster/em.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/cluster/gaac.py +3 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/cluster/kmeans.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/cluster/util.py +35 -20
- nltk-3.3/nltk/collections.py +640 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/collocations.py +2 -2
- nltk-3.3/nltk/compat.py +365 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/__init__.py +37 -16
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/europarl_raw.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/__init__.py +4 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/aligned.py +4 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/api.py +12 -10
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/bnc.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/bracket_parse.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/categorized_sents.py +4 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/chasen.py +5 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/childes.py +8 -9
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/chunked.py +4 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/cmudict.py +4 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/comparative_sents.py +4 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/conll.py +19 -12
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/crubadan.py +2 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/dependency.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/framenet.py +1085 -119
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/ieer.py +4 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/indian.py +4 -5
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/ipipan.py +6 -5
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/knbc.py +2 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/lin.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/mte.py +5 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/nkjp.py +4 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/nombank.py +8 -8
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/nps_chat.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/opinion_lexicon.py +3 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/panlex_lite.py +1 -1
- nltk-3.3/nltk/corpus/reader/pl196x.py +292 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/plaintext.py +3 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/ppattach.py +4 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/propbank.py +8 -10
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/pros_cons.py +5 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/reviews.py +3 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/rte.py +5 -7
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/semcor.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/senseval.py +4 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/sentiwordnet.py +3 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/sinica_treebank.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/string_category.py +4 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/switchboard.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/tagged.py +4 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/timit.py +10 -8
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/toolbox.py +4 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/twitter.py +4 -6
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/util.py +7 -3
- nltk-3.3/nltk/corpus/reader/verbnet.py +595 -0
- nltk-3.3/nltk/corpus/reader/wordlist.py +134 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/wordnet.py +357 -285
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/xmldocs.py +10 -5
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/ycoe.py +25 -24
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/util.py +22 -5
- {nltk-3.2.1 → nltk-3.3}/nltk/data.py +65 -29
- {nltk-3.2.1 → nltk-3.3}/nltk/decorators.py +5 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/downloader.py +59 -55
- {nltk-3.2.1 → nltk-3.3}/nltk/draw/__init__.py +4 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/draw/cfg.py +5 -8
- {nltk-3.2.1 → nltk-3.3}/nltk/draw/dispersion.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/draw/table.py +4 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/draw/tree.py +2 -5
- {nltk-3.2.1 → nltk-3.3}/nltk/draw/util.py +17 -17
- {nltk-3.2.1 → nltk-3.3}/nltk/featstruct.py +290 -173
- {nltk-3.2.1 → nltk-3.3}/nltk/grammar.py +87 -44
- {nltk-3.2.1 → nltk-3.3}/nltk/help.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/inference/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/inference/api.py +27 -19
- {nltk-3.2.1 → nltk-3.3}/nltk/inference/discourse.py +20 -16
- {nltk-3.2.1 → nltk-3.3}/nltk/inference/nonmonotonic.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/inference/prover9.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/inference/resolution.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/inference/tableau.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/internals.py +44 -44
- {nltk-3.2.1 → nltk-3.3}/nltk/jsontags.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/metrics/__init__.py +2 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/metrics/agreement.py +29 -9
- nltk-3.3/nltk/metrics/aline.py +607 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/metrics/association.py +8 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/metrics/confusionmatrix.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/metrics/distance.py +12 -7
- {nltk-3.2.1 → nltk-3.3}/nltk/metrics/paice.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/metrics/scores.py +7 -7
- {nltk-3.2.1 → nltk-3.3}/nltk/metrics/segmentation.py +4 -5
- {nltk-3.2.1 → nltk-3.3}/nltk/metrics/spearman.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/misc/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/misc/chomsky.py +4 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/misc/minimalset.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/misc/sort.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/misc/wordfinder.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/__init__.py +2 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/api.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/bllip.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/chart.py +8 -7
- nltk-3.3/nltk/parse/corenlp.py +780 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/dependencygraph.py +4 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/earleychart.py +4 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/evaluate.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/featurechart.py +6 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/generate.py +15 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/malt.py +2 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/nonprojectivedependencyparser.py +3 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/pchart.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/projectivedependencyparser.py +6 -5
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/recursivedescent.py +2 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/shiftreduce.py +2 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/stanford.py +53 -28
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/transitionparser.py +11 -9
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/util.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/parse/viterbi.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/probability.py +75 -20
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/boxer.py +15 -15
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/chat80.py +6 -6
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/cooper_storage.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/drt.py +9 -8
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/drt_glue_demo.py +5 -9
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/evaluate.py +9 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/glue.py +8 -6
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/hole.py +4 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/lfg.py +4 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/linearlogic.py +4 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/logic.py +14 -14
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/relextract.py +11 -18
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/skolemize.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/sem/util.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/sentiment/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/sentiment/sentiment_analyzer.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/sentiment/util.py +14 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/sentiment/vader.py +41 -42
- {nltk-3.2.1 → nltk-3.3}/nltk/stem/__init__.py +1 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/stem/api.py +7 -4
- nltk-3.3/nltk/stem/arlstem.py +369 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/stem/isri.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/stem/lancaster.py +39 -12
- nltk-3.3/nltk/stem/porter.py +710 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/stem/regexp.py +2 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/stem/rslp.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/stem/snowball.py +537 -13
- {nltk-3.2.1 → nltk-3.3}/nltk/stem/util.py +11 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/stem/wordnet.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/__init__.py +32 -7
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/api.py +13 -8
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/brill.py +4 -6
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/crf.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/hmm.py +11 -11
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/hunpos.py +4 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/mapping.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/perceptron.py +25 -10
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/senna.py +25 -23
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/sequential.py +19 -15
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/stanford.py +59 -36
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/tnt.py +11 -20
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/util.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/tbl/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/tbl/demo.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/tbl/erroranalysis.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/tbl/feature.py +5 -7
- {nltk-3.2.1 → nltk-3.3}/nltk/tbl/rule.py +6 -6
- {nltk-3.2.1 → nltk-3.3}/nltk/tbl/template.py +18 -23
- {nltk-3.2.1 → nltk-3.3}/nltk/test/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/all.py +7 -6
- {nltk-3.2.1 → nltk-3.3}/nltk/test/bnc.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/ccg.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/ccg_semantics.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/chat80.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/childes_fixt.py +1 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/chunk.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/classify.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/classify_fixt.py +2 -1
- nltk-3.3/nltk/test/collections.doctest +20 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/collocations.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/compat.doctest +0 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/test/compat_fixt.py +1 -0
- nltk-3.3/nltk/test/concordance.doctest +55 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/corpus.doctest +38 -49
- {nltk-3.2.1 → nltk-3.3}/nltk/test/crubadan.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/data.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/dependency.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/discourse.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/discourse_fixt.py +1 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/doctest_nose_plugin.py +8 -5
- {nltk-3.2.1 → nltk-3.3}/nltk/test/drt.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/featgram.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/featstruct.doctest +2 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/test/framenet.doctest +82 -33
- {nltk-3.2.1 → nltk-3.3}/nltk/test/generate.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/gensim.doctest +6 -7
- {nltk-3.2.1 → nltk-3.3}/nltk/test/gensim_fixt.py +1 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/gluesemantics.doctest +13 -15
- {nltk-3.2.1 → nltk-3.3}/nltk/test/gluesemantics_malt.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/gluesemantics_malt_fixt.py +1 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/grammar.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/grammartestsuites.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/index.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/inference.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/inference_fixt.py +1 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/internals.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/japanese.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/logic.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/metrics.doctest +3 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/test/misc.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/nonmonotonic.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/nonmonotonic_fixt.py +1 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/parse.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/portuguese_en.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/probability.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/probability_fixt.py +2 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/propbank.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/relextract.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/resolution.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/runtests.py +35 -13
- {nltk-3.2.1 → nltk-3.3}/nltk/test/semantics.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/sentiment.doctest +3 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/test/sentiwordnet.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/simple.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/stem.doctest +2 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/test/tag.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/tokenize.doctest +109 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/test/toolbox.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/translate.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/tree.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/treeprettyprinter.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/treetransforms.doctest +1 -1
- nltk-3.3/nltk/test/unit/test_aline.py +52 -0
- nltk-3.3/nltk/test/unit/test_chunk.py +56 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_classify.py +20 -18
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_collocations.py +16 -7
- nltk-3.3/nltk/test/unit/test_concordance.py +98 -0
- nltk-3.3/nltk/test/unit/test_corenlp.py +1000 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_corpora.py +18 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_json2csv_corpus.py +4 -3
- nltk-3.3/nltk/test/unit/test_rte_classify.py +86 -0
- nltk-3.3/nltk/test/unit/test_senna.py +74 -0
- nltk-3.3/nltk/test/unit/test_stem.py +121 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_tgrep.py +4 -2
- nltk-3.3/nltk/test/unit/test_tokenize.py +168 -0
- nltk-3.3/nltk/test/unit/test_wordnet.py +134 -0
- nltk-3.3/nltk/test/unit/translate/test_bleu.py +244 -0
- nltk-3.3/nltk/test/unit/translate/test_gdfa.py +57 -0
- nltk-3.3/nltk/test/unit/translate/test_nist.py +37 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/translate/test_stack_decoder.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/util.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/wordnet.doctest +17 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/test/wordnet_fixt.py +1 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/wordnet_lch.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/test/wsd.doctest +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/text.py +92 -39
- {nltk-3.2.1 → nltk-3.3}/nltk/tgrep.py +8 -7
- {nltk-3.2.1 → nltk-3.3}/nltk/tokenize/__init__.py +37 -15
- {nltk-3.2.1 → nltk-3.3}/nltk/tokenize/api.py +3 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/tokenize/casual.py +10 -9
- {nltk-3.2.1 → nltk-3.3}/nltk/tokenize/mwe.py +2 -2
- nltk-3.3/nltk/tokenize/nist.py +173 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/tokenize/punkt.py +186 -167
- {nltk-3.2.1 → nltk-3.3}/nltk/tokenize/regexp.py +13 -7
- nltk-3.3/nltk/tokenize/repp.py +151 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/tokenize/sexpr.py +4 -6
- {nltk-3.2.1 → nltk-3.3}/nltk/tokenize/simple.py +7 -5
- {nltk-3.2.1 → nltk-3.3}/nltk/tokenize/stanford.py +18 -14
- nltk-3.3/nltk/tokenize/stanford_segmenter.py +246 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/tokenize/texttiling.py +43 -45
- nltk-3.3/nltk/tokenize/toktok.py +155 -0
- nltk-3.3/nltk/tokenize/treebank.py +363 -0
- nltk-3.3/nltk/tokenize/util.py +261 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/toolbox.py +4 -2
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/api.py +50 -40
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/bleu_score.py +221 -156
- nltk-3.3/nltk/translate/chrf_score.py +137 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/gale_church.py +32 -19
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/gdfa.py +14 -10
- nltk-3.3/nltk/translate/gleu_score.py +193 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/ibm1.py +17 -8
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/ibm2.py +4 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/ibm4.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/ibm5.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/ibm_model.py +4 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/metrics.py +1 -1
- nltk-3.3/nltk/translate/nist_score.py +178 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/phrase_based.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/ribes_score.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/stack_decoder.py +5 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/tree.py +10 -8
- {nltk-3.2.1 → nltk-3.3}/nltk/treeprettyprinter.py +6 -6
- {nltk-3.2.1 → nltk-3.3}/nltk/twitter/__init__.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/twitter/api.py +8 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/twitter/common.py +9 -3
- {nltk-3.2.1 → nltk-3.3}/nltk/twitter/twitter_demo.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/twitter/twitterclient.py +1 -1
- {nltk-3.2.1 → nltk-3.3}/nltk/twitter/util.py +5 -4
- {nltk-3.2.1 → nltk-3.3}/nltk/util.py +41 -654
- {nltk-3.2.1 → nltk-3.3}/nltk/wsd.py +2 -2
- {nltk-3.2.1 → nltk-3.3/nltk.egg-info}/PKG-INFO +13 -5
- {nltk-3.2.1 → nltk-3.3}/nltk.egg-info/SOURCES.txt +23 -1
- nltk-3.3/nltk.egg-info/requires.txt +31 -0
- {nltk-3.2.1 → nltk-3.3}/setup.cfg +0 -1
- {nltk-3.2.1 → nltk-3.3}/setup.py +32 -5
- nltk-3.2.1/nltk/VERSION +0 -1
- nltk-3.2.1/nltk/compat.py +0 -706
- nltk-3.2.1/nltk/corpus/reader/pl196x.py +0 -278
- nltk-3.2.1/nltk/corpus/reader/verbnet.py +0 -408
- nltk-3.2.1/nltk/corpus/reader/wordlist.py +0 -37
- nltk-3.2.1/nltk/six.py +0 -868
- nltk-3.2.1/nltk/stem/porter.py +0 -692
- nltk-3.2.1/nltk/test/unit/test_stem.py +0 -36
- nltk-3.2.1/nltk/test/unit/test_tokenize.py +0 -23
- nltk-3.2.1/nltk/test/unit/translate/test_bleu.py +0 -138
- nltk-3.2.1/nltk/tokenize/stanford_segmenter.py +0 -157
- nltk-3.2.1/nltk/tokenize/treebank.py +0 -124
- nltk-3.2.1/nltk/tokenize/util.py +0 -90
- {nltk-3.2.1 → nltk-3.3}/INSTALL.txt +0 -0
- {nltk-3.2.1 → nltk-3.3}/MANIFEST.in +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/classify/positivenaivebayes.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/corpus/reader/udhr.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/inference/mace.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/lazyimport.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/misc/babelfish.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/tag/brill_trainer.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/tbl/api.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/bleu.doctest +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/childes.doctest +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/corpus_fixt.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/paice.doctest +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/portuguese_en_fixt.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/segmentation_fixt.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/semantics_fixt.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/translate_fixt.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/__init__.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_2x_compat.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_corpus_views.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_hmm.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_naivebayes.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_seekable_unicode_stream_reader.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_tag.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/test_twitter_auth.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/translate/__init__.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/translate/test_ibm1.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/translate/test_ibm2.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/translate/test_ibm3.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/translate/test_ibm4.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/translate/test_ibm5.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/translate/test_ibm_model.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/test/unit/utils.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/translate/ibm3.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk/treetransforms.py +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk.egg-info/dependency_links.txt +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk.egg-info/not-zip-safe +0 -0
- {nltk-3.2.1 → nltk-3.3}/nltk.egg-info/top_level.txt +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
2
|
# Natural Language Toolkit: BLEU Score
|
|
3
3
|
#
|
|
4
|
-
# Copyright (C) 2001-
|
|
4
|
+
# Copyright (C) 2001-2018 NLTK Project
|
|
5
5
|
# Authors: Chin Yee Lee, Hengfeng Li, Ruxin Hou, Calvin Tanujaya Lim
|
|
6
|
-
# Contributors: Dmitrijs Milajevs, Liling Tan
|
|
6
|
+
# Contributors: Björn Mattsson, Dmitrijs Milajevs, Liling Tan
|
|
7
7
|
# URL: <http://nltk.org/>
|
|
8
8
|
# For license information, see LICENSE.TXT
|
|
9
9
|
|
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
from __future__ import division
|
|
12
12
|
|
|
13
13
|
import math
|
|
14
|
+
import sys
|
|
14
15
|
import fractions
|
|
16
|
+
import warnings
|
|
15
17
|
from collections import Counter
|
|
16
18
|
|
|
17
19
|
from nltk.util import ngrams
|
|
@@ -21,14 +23,14 @@ try:
|
|
|
21
23
|
from fractions import Fraction
|
|
22
24
|
except TypeError:
|
|
23
25
|
from nltk.compat import Fraction
|
|
24
|
-
|
|
26
|
+
|
|
25
27
|
|
|
26
28
|
def sentence_bleu(references, hypothesis, weights=(0.25, 0.25, 0.25, 0.25),
|
|
27
|
-
smoothing_function=None):
|
|
29
|
+
smoothing_function=None, auto_reweigh=False):
|
|
28
30
|
"""
|
|
29
31
|
Calculate BLEU score (Bilingual Evaluation Understudy) from
|
|
30
32
|
Papineni, Kishore, Salim Roukos, Todd Ward, and Wei-Jing Zhu. 2002.
|
|
31
|
-
"BLEU: a method for automatic evaluation of machine translation."
|
|
33
|
+
"BLEU: a method for automatic evaluation of machine translation."
|
|
32
34
|
In Proceedings of ACL. http://www.aclweb.org/anthology/P02-1040.pdf
|
|
33
35
|
|
|
34
36
|
>>> hypothesis1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',
|
|
@@ -55,41 +57,61 @@ def sentence_bleu(references, hypothesis, weights=(0.25, 0.25, 0.25, 0.25),
|
|
|
55
57
|
>>> sentence_bleu([reference1, reference2, reference3], hypothesis1) # doctest: +ELLIPSIS
|
|
56
58
|
0.5045...
|
|
57
59
|
|
|
58
|
-
|
|
59
|
-
0.
|
|
60
|
+
If there is no ngrams overlap for any order of n-grams, BLEU returns the
|
|
61
|
+
value 0. This is because the precision for the order of n-grams without
|
|
62
|
+
overlap is 0, and the geometric mean in the final BLEU score computation
|
|
63
|
+
multiplies the 0 with the precision of other n-grams. This results in 0
|
|
64
|
+
(independently of the precision of the othe n-gram orders). The following
|
|
65
|
+
example has zero 3-gram and 4-gram overlaps:
|
|
66
|
+
|
|
67
|
+
>>> round(sentence_bleu([reference1, reference2, reference3], hypothesis2),4) # doctest: +ELLIPSIS
|
|
68
|
+
0.0
|
|
69
|
+
|
|
70
|
+
To avoid this harsh behaviour when no ngram overlaps are found a smoothing
|
|
71
|
+
function can be used.
|
|
72
|
+
|
|
73
|
+
>>> chencherry = SmoothingFunction()
|
|
74
|
+
>>> sentence_bleu([reference1, reference2, reference3], hypothesis2,
|
|
75
|
+
... smoothing_function=chencherry.method1) # doctest: +ELLIPSIS
|
|
76
|
+
0.0370...
|
|
60
77
|
|
|
61
|
-
The default BLEU calculates a score for up to
|
|
62
|
-
weights. To evaluate your translations with
|
|
63
|
-
use customized weights. E.g. when accounting
|
|
64
|
-
weights:
|
|
78
|
+
The default BLEU calculates a score for up to 4-grams using uniform
|
|
79
|
+
weights (this is called BLEU-4). To evaluate your translations with
|
|
80
|
+
higher/lower order ngrams, use customized weights. E.g. when accounting
|
|
81
|
+
for up to 5-grams with uniform weights (this is called BLEU-5) use:
|
|
82
|
+
|
|
83
|
+
>>> weights = (1./5., 1./5., 1./5., 1./5., 1./5.)
|
|
84
|
+
>>> sentence_bleu([reference1, reference2, reference3], hypothesis1, weights) # doctest: +ELLIPSIS
|
|
85
|
+
0.3920...
|
|
65
86
|
|
|
66
|
-
>>> weights = (0.1666, 0.1666, 0.1666, 0.1666, 0.1666)
|
|
67
|
-
>>> sentence_bleu([reference1, reference2, reference3], hypothesis1, weights)
|
|
68
|
-
0.45838627164939455
|
|
69
|
-
|
|
70
87
|
:param references: reference sentences
|
|
71
88
|
:type references: list(list(str))
|
|
72
89
|
:param hypothesis: a hypothesis sentence
|
|
73
90
|
:type hypothesis: list(str)
|
|
74
91
|
:param weights: weights for unigrams, bigrams, trigrams and so on
|
|
75
92
|
:type weights: list(float)
|
|
93
|
+
:param smoothing_function:
|
|
94
|
+
:type smoothing_function: SmoothingFunction
|
|
95
|
+
:param auto_reweigh: Option to re-normalize the weights uniformly.
|
|
96
|
+
:type auto_reweigh: bool
|
|
76
97
|
:return: The sentence-level BLEU score.
|
|
77
98
|
:rtype: float
|
|
78
99
|
"""
|
|
79
|
-
return corpus_bleu([references], [hypothesis],
|
|
100
|
+
return corpus_bleu([references], [hypothesis],
|
|
101
|
+
weights, smoothing_function, auto_reweigh)
|
|
80
102
|
|
|
81
103
|
|
|
82
104
|
def corpus_bleu(list_of_references, hypotheses, weights=(0.25, 0.25, 0.25, 0.25),
|
|
83
|
-
smoothing_function=None):
|
|
105
|
+
smoothing_function=None, auto_reweigh=False):
|
|
84
106
|
"""
|
|
85
|
-
Calculate a single corpus-level BLEU score (aka. system-level BLEU) for all
|
|
86
|
-
the hypotheses and their respective references.
|
|
107
|
+
Calculate a single corpus-level BLEU score (aka. system-level BLEU) for all
|
|
108
|
+
the hypotheses and their respective references.
|
|
87
109
|
|
|
88
|
-
Instead of averaging the sentence level BLEU scores (i.e. marco-average
|
|
89
|
-
precision), the original BLEU metric (Papineni et al. 2002) accounts for
|
|
110
|
+
Instead of averaging the sentence level BLEU scores (i.e. marco-average
|
|
111
|
+
precision), the original BLEU metric (Papineni et al. 2002) accounts for
|
|
90
112
|
the micro-average precision (i.e. summing the numerators and denominators
|
|
91
113
|
for each hypothesis-reference(s) pairs before the division).
|
|
92
|
-
|
|
114
|
+
|
|
93
115
|
>>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',
|
|
94
116
|
... 'ensures', 'that', 'the', 'military', 'always',
|
|
95
117
|
... 'obeys', 'the', 'commands', 'of', 'the', 'party']
|
|
@@ -102,82 +124,93 @@ def corpus_bleu(list_of_references, hypotheses, weights=(0.25, 0.25, 0.25, 0.25)
|
|
|
102
124
|
>>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',
|
|
103
125
|
... 'army', 'always', 'to', 'heed', 'the', 'directions',
|
|
104
126
|
... 'of', 'the', 'party']
|
|
105
|
-
|
|
106
|
-
>>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',
|
|
127
|
+
|
|
128
|
+
>>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',
|
|
107
129
|
... 'interested', 'in', 'world', 'history']
|
|
108
|
-
>>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',
|
|
130
|
+
>>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',
|
|
109
131
|
... 'because', 'he', 'read', 'the', 'book']
|
|
110
|
-
|
|
132
|
+
|
|
111
133
|
>>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]
|
|
112
134
|
>>> hypotheses = [hyp1, hyp2]
|
|
113
135
|
>>> corpus_bleu(list_of_references, hypotheses) # doctest: +ELLIPSIS
|
|
114
136
|
0.5920...
|
|
115
|
-
|
|
116
|
-
The example below show that corpus_bleu() is different from averaging
|
|
117
|
-
sentence_bleu() for hypotheses
|
|
118
|
-
|
|
137
|
+
|
|
138
|
+
The example below show that corpus_bleu() is different from averaging
|
|
139
|
+
sentence_bleu() for hypotheses
|
|
140
|
+
|
|
119
141
|
>>> score1 = sentence_bleu([ref1a, ref1b, ref1c], hyp1)
|
|
120
142
|
>>> score2 = sentence_bleu([ref2a], hyp2)
|
|
121
143
|
>>> (score1 + score2) / 2 # doctest: +ELLIPSIS
|
|
122
144
|
0.6223...
|
|
123
|
-
|
|
124
|
-
:param
|
|
125
|
-
:type
|
|
145
|
+
|
|
146
|
+
:param list_of_references: a corpus of lists of reference sentences, w.r.t. hypotheses
|
|
147
|
+
:type list_of_references: list(list(list(str)))
|
|
126
148
|
:param hypotheses: a list of hypothesis sentences
|
|
127
149
|
:type hypotheses: list(list(str))
|
|
128
150
|
:param weights: weights for unigrams, bigrams, trigrams and so on
|
|
129
151
|
:type weights: list(float)
|
|
152
|
+
:param smoothing_function:
|
|
153
|
+
:type smoothing_function: SmoothingFunction
|
|
154
|
+
:param auto_reweigh: Option to re-normalize the weights uniformly.
|
|
155
|
+
:type auto_reweigh: bool
|
|
130
156
|
:return: The corpus-level BLEU score.
|
|
131
157
|
:rtype: float
|
|
132
158
|
"""
|
|
133
159
|
# Before proceeding to compute BLEU, perform sanity checks.
|
|
134
160
|
|
|
135
|
-
p_numerators = Counter()
|
|
136
|
-
p_denominators = Counter()
|
|
161
|
+
p_numerators = Counter() # Key = ngram order, and value = no. of ngram matches.
|
|
162
|
+
p_denominators = Counter() # Key = ngram order, and value = no. of ngram in ref.
|
|
137
163
|
hyp_lengths, ref_lengths = 0, 0
|
|
138
|
-
|
|
139
|
-
assert len(list_of_references) == len(hypotheses), "The number of hypotheses and their reference(s) should be the
|
|
140
|
-
|
|
164
|
+
|
|
165
|
+
assert len(list_of_references) == len(hypotheses), "The number of hypotheses and their reference(s) should be the " \
|
|
166
|
+
"same "
|
|
167
|
+
|
|
141
168
|
# Iterate through each hypothesis and their corresponding references.
|
|
142
169
|
for references, hypothesis in zip(list_of_references, hypotheses):
|
|
143
170
|
# For each order of ngram, calculate the numerator and
|
|
144
171
|
# denominator for the corpus-level modified precision.
|
|
145
|
-
for i, _ in enumerate(weights, start=1):
|
|
172
|
+
for i, _ in enumerate(weights, start=1):
|
|
146
173
|
p_i = modified_precision(references, hypothesis, i)
|
|
147
174
|
p_numerators[i] += p_i.numerator
|
|
148
175
|
p_denominators[i] += p_i.denominator
|
|
149
|
-
|
|
176
|
+
|
|
150
177
|
# Calculate the hypothesis length and the closest reference length.
|
|
151
178
|
# Adds them to the corpus-level hypothesis and reference counts.
|
|
152
|
-
hyp_len =
|
|
179
|
+
hyp_len = len(hypothesis)
|
|
153
180
|
hyp_lengths += hyp_len
|
|
154
181
|
ref_lengths += closest_ref_length(references, hyp_len)
|
|
155
|
-
|
|
182
|
+
|
|
156
183
|
# Calculate corpus-level brevity penalty.
|
|
157
184
|
bp = brevity_penalty(ref_lengths, hyp_lengths)
|
|
158
|
-
|
|
185
|
+
|
|
186
|
+
# Uniformly re-weighting based on maximum hypothesis lengths if largest
|
|
187
|
+
# order of n-grams < 4 and weights is set at default.
|
|
188
|
+
if auto_reweigh:
|
|
189
|
+
if hyp_lengths < 4 and weights == (0.25, 0.25, 0.25, 0.25):
|
|
190
|
+
weights = (1 / hyp_lengths,) * hyp_lengths
|
|
191
|
+
|
|
159
192
|
# Collects the various precision values for the different ngram orders.
|
|
160
|
-
p_n = [Fraction(p_numerators[i], p_denominators[i], _normalize=False)
|
|
193
|
+
p_n = [Fraction(p_numerators[i], p_denominators[i], _normalize=False)
|
|
161
194
|
for i, _ in enumerate(weights, start=1)]
|
|
162
|
-
|
|
163
|
-
# Returns 0 if there's no matching n-grams
|
|
195
|
+
|
|
196
|
+
# Returns 0 if there's no matching n-grams
|
|
164
197
|
# We only need to check for p_numerators[1] == 0, since if there's
|
|
165
198
|
# no unigrams, there won't be any higher order ngrams.
|
|
166
199
|
if p_numerators[1] == 0:
|
|
167
200
|
return 0
|
|
168
|
-
|
|
201
|
+
|
|
202
|
+
# If there's no smoothing, set use method0 from SmoothinFunction class.
|
|
203
|
+
if not smoothing_function:
|
|
204
|
+
smoothing_function = SmoothingFunction().method0
|
|
169
205
|
# Smoothen the modified precision.
|
|
170
|
-
# Note:
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
s
|
|
178
|
-
if p_i.numerator != 0)
|
|
179
|
-
|
|
180
|
-
return bp * math.exp(math.fsum(s))
|
|
206
|
+
# Note: smoothing_function() may convert values into floats;
|
|
207
|
+
# it tries to retain the Fraction object as much as the
|
|
208
|
+
# smoothing method allows.
|
|
209
|
+
p_n = smoothing_function(p_n, references=references, hypothesis=hypothesis,
|
|
210
|
+
hyp_len=hyp_len)
|
|
211
|
+
s = (w_i * math.log(p_i) for w_i, p_i in zip(weights, p_n))
|
|
212
|
+
s = bp * math.exp(math.fsum(s))
|
|
213
|
+
return s
|
|
181
214
|
|
|
182
215
|
|
|
183
216
|
def modified_precision(references, hypothesis, n):
|
|
@@ -186,28 +219,28 @@ def modified_precision(references, hypothesis, n):
|
|
|
186
219
|
|
|
187
220
|
The normal precision method may lead to some wrong translations with
|
|
188
221
|
high-precision, e.g., the translation, in which a word of reference
|
|
189
|
-
repeats several times, has very high precision.
|
|
222
|
+
repeats several times, has very high precision.
|
|
223
|
+
|
|
224
|
+
This function only returns the Fraction object that contains the numerator
|
|
225
|
+
and denominator necessary to calculate the corpus-level precision.
|
|
226
|
+
To calculate the modified precision for a single pair of hypothesis and
|
|
227
|
+
references, cast the Fraction object into a float.
|
|
190
228
|
|
|
191
|
-
This function only returns the Fraction object that contains the numerator
|
|
192
|
-
and denominator necessary to calculate the corpus-level precision.
|
|
193
|
-
To calculate the modified precision for a single pair of hypothesis and
|
|
194
|
-
references, cast the Fraction object into a float.
|
|
195
|
-
|
|
196
229
|
The famous "the the the ... " example shows that you can get BLEU precision
|
|
197
230
|
by duplicating high frequency words.
|
|
198
|
-
|
|
231
|
+
|
|
199
232
|
>>> reference1 = 'the cat is on the mat'.split()
|
|
200
233
|
>>> reference2 = 'there is a cat on the mat'.split()
|
|
201
234
|
>>> hypothesis1 = 'the the the the the the the'.split()
|
|
202
235
|
>>> references = [reference1, reference2]
|
|
203
236
|
>>> float(modified_precision(references, hypothesis1, n=1)) # doctest: +ELLIPSIS
|
|
204
237
|
0.2857...
|
|
205
|
-
|
|
206
|
-
In the modified n-gram precision, a reference word will be considered
|
|
238
|
+
|
|
239
|
+
In the modified n-gram precision, a reference word will be considered
|
|
207
240
|
exhausted after a matching hypothesis word is identified, e.g.
|
|
208
|
-
|
|
241
|
+
|
|
209
242
|
>>> reference1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',
|
|
210
|
-
... 'ensures', 'that', 'the', 'military', 'will',
|
|
243
|
+
... 'ensures', 'that', 'the', 'military', 'will',
|
|
211
244
|
... 'forever', 'heed', 'Party', 'commands']
|
|
212
245
|
>>> reference2 = ['It', 'is', 'the', 'guiding', 'principle', 'which',
|
|
213
246
|
... 'guarantees', 'the', 'military', 'forces', 'always',
|
|
@@ -222,26 +255,26 @@ def modified_precision(references, hypothesis, n):
|
|
|
222
255
|
1.0
|
|
223
256
|
>>> float(modified_precision(references, hypothesis, n=2))
|
|
224
257
|
1.0
|
|
225
|
-
|
|
258
|
+
|
|
226
259
|
An example of a normal machine translation hypothesis:
|
|
227
|
-
|
|
260
|
+
|
|
228
261
|
>>> hypothesis1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',
|
|
229
262
|
... 'ensures', 'that', 'the', 'military', 'always',
|
|
230
263
|
... 'obeys', 'the', 'commands', 'of', 'the', 'party']
|
|
231
|
-
|
|
264
|
+
|
|
232
265
|
>>> hypothesis2 = ['It', 'is', 'to', 'insure', 'the', 'troops',
|
|
233
266
|
... 'forever', 'hearing', 'the', 'activity', 'guidebook',
|
|
234
267
|
... 'that', 'party', 'direct']
|
|
235
|
-
|
|
268
|
+
|
|
236
269
|
>>> reference1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',
|
|
237
|
-
... 'ensures', 'that', 'the', 'military', 'will',
|
|
270
|
+
... 'ensures', 'that', 'the', 'military', 'will',
|
|
238
271
|
... 'forever', 'heed', 'Party', 'commands']
|
|
239
|
-
|
|
272
|
+
|
|
240
273
|
>>> reference2 = ['It', 'is', 'the', 'guiding', 'principle', 'which',
|
|
241
274
|
... 'guarantees', 'the', 'military', 'forces', 'always',
|
|
242
275
|
... 'being', 'under', 'the', 'command', 'of', 'the',
|
|
243
276
|
... 'Party']
|
|
244
|
-
|
|
277
|
+
|
|
245
278
|
>>> reference3 = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',
|
|
246
279
|
... 'army', 'always', 'to', 'heed', 'the', 'directions',
|
|
247
280
|
... 'of', 'the', 'party']
|
|
@@ -254,8 +287,8 @@ def modified_precision(references, hypothesis, n):
|
|
|
254
287
|
0.5882352941176471
|
|
255
288
|
>>> float(modified_precision(references, hypothesis2, n=2)) # doctest: +ELLIPSIS
|
|
256
289
|
0.07692...
|
|
257
|
-
|
|
258
|
-
|
|
290
|
+
|
|
291
|
+
|
|
259
292
|
:param references: A list of reference translations.
|
|
260
293
|
:type references: list(list(str))
|
|
261
294
|
:param hypothesis: A hypothesis translation.
|
|
@@ -265,44 +298,46 @@ def modified_precision(references, hypothesis, n):
|
|
|
265
298
|
:return: BLEU's modified precision for the nth order ngram.
|
|
266
299
|
:rtype: Fraction
|
|
267
300
|
"""
|
|
268
|
-
# Extracts all ngrams in hypothesis
|
|
269
|
-
|
|
270
|
-
|
|
301
|
+
# Extracts all ngrams in hypothesis
|
|
302
|
+
# Set an empty Counter if hypothesis is empty.
|
|
303
|
+
counts = Counter(ngrams(hypothesis, n)) if len(hypothesis) >= n else Counter()
|
|
271
304
|
# Extract a union of references' counts.
|
|
272
|
-
|
|
305
|
+
# max_counts = reduce(or_, [Counter(ngrams(ref, n)) for ref in references])
|
|
273
306
|
max_counts = {}
|
|
274
307
|
for reference in references:
|
|
275
|
-
reference_counts = Counter(ngrams(reference, n))
|
|
308
|
+
reference_counts = Counter(ngrams(reference, n)) if len(reference) >= n else Counter()
|
|
276
309
|
for ngram in counts:
|
|
277
|
-
max_counts[ngram] = max(max_counts.get(ngram, 0),
|
|
310
|
+
max_counts[ngram] = max(max_counts.get(ngram, 0),
|
|
278
311
|
reference_counts[ngram])
|
|
279
|
-
|
|
312
|
+
|
|
280
313
|
# Assigns the intersection between hypothesis and references' counts.
|
|
281
|
-
clipped_counts = {ngram: min(count, max_counts[ngram])
|
|
314
|
+
clipped_counts = {ngram: min(count, max_counts[ngram])
|
|
282
315
|
for ngram, count in counts.items()}
|
|
283
316
|
|
|
284
317
|
numerator = sum(clipped_counts.values())
|
|
285
|
-
denominator
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
318
|
+
# Ensures that denominator is minimum 1 to avoid ZeroDivisionError.
|
|
319
|
+
# Usually this happens when the ngram order is > len(reference).
|
|
320
|
+
denominator = max(1, sum(counts.values()))
|
|
321
|
+
|
|
322
|
+
return Fraction(numerator, denominator, _normalize=False)
|
|
323
|
+
|
|
289
324
|
|
|
290
325
|
def closest_ref_length(references, hyp_len):
|
|
291
326
|
"""
|
|
292
|
-
This function finds the reference that is the closest length to the
|
|
293
|
-
hypothesis. The closest reference length is referred to as *r* variable
|
|
327
|
+
This function finds the reference that is the closest length to the
|
|
328
|
+
hypothesis. The closest reference length is referred to as *r* variable
|
|
294
329
|
from the brevity penalty formula in Papineni et. al. (2002)
|
|
295
|
-
|
|
330
|
+
|
|
296
331
|
:param references: A list of reference translations.
|
|
297
332
|
:type references: list(list(str))
|
|
298
|
-
:param
|
|
299
|
-
:type
|
|
333
|
+
:param hyp_len: The length of the hypothesis.
|
|
334
|
+
:type hyp_len: int
|
|
300
335
|
:return: The length of the reference that's closest to the hypothesis.
|
|
301
|
-
:rtype: int
|
|
336
|
+
:rtype: int
|
|
302
337
|
"""
|
|
303
338
|
ref_lens = (len(reference) for reference in references)
|
|
304
|
-
closest_ref_len = min(ref_lens, key=lambda ref_len:
|
|
305
|
-
|
|
339
|
+
closest_ref_len = min(ref_lens, key=lambda ref_len:
|
|
340
|
+
(abs(ref_len - hyp_len), ref_len))
|
|
306
341
|
return closest_ref_len
|
|
307
342
|
|
|
308
343
|
|
|
@@ -379,43 +414,47 @@ def brevity_penalty(closest_ref_len, hyp_len):
|
|
|
379
414
|
>>> closest_ref_len = closest_ref_length(references, hyp_len)
|
|
380
415
|
>>> brevity_penalty(closest_ref_len, hyp_len)
|
|
381
416
|
1.0
|
|
382
|
-
|
|
383
|
-
:param hyp_len: The length of the hypothesis for a single sentence OR the
|
|
417
|
+
|
|
418
|
+
:param hyp_len: The length of the hypothesis for a single sentence OR the
|
|
384
419
|
sum of all the hypotheses' lengths for a corpus
|
|
385
420
|
:type hyp_len: int
|
|
386
|
-
:param closest_ref_len: The length of the closest reference for a single
|
|
421
|
+
:param closest_ref_len: The length of the closest reference for a single
|
|
387
422
|
hypothesis OR the sum of all the closest references for every hypotheses.
|
|
388
|
-
:type
|
|
423
|
+
:type closest_ref_len: int
|
|
389
424
|
:return: BLEU's brevity penalty.
|
|
390
425
|
:rtype: float
|
|
391
426
|
"""
|
|
392
427
|
if hyp_len > closest_ref_len:
|
|
393
428
|
return 1
|
|
429
|
+
# If hypothesis is empty, brevity penalty = 0 should result in BLEU = 0.0
|
|
430
|
+
elif hyp_len == 0:
|
|
431
|
+
return 0
|
|
394
432
|
else:
|
|
395
433
|
return math.exp(1 - closest_ref_len / hyp_len)
|
|
396
434
|
|
|
397
435
|
|
|
398
436
|
class SmoothingFunction:
|
|
399
437
|
"""
|
|
400
|
-
This is an implementation of the smoothing techniques
|
|
401
|
-
for segment-level BLEU scores that was presented in
|
|
402
|
-
Boxing Chen and Collin Cherry (2014) A Systematic Comparison of
|
|
403
|
-
Smoothing Techniques for Sentence-Level BLEU. In WMT14.
|
|
438
|
+
This is an implementation of the smoothing techniques
|
|
439
|
+
for segment-level BLEU scores that was presented in
|
|
440
|
+
Boxing Chen and Collin Cherry (2014) A Systematic Comparison of
|
|
441
|
+
Smoothing Techniques for Sentence-Level BLEU. In WMT14.
|
|
404
442
|
http://acl2014.org/acl2014/W14-33/pdf/W14-3346.pdf
|
|
405
443
|
"""
|
|
444
|
+
|
|
406
445
|
def __init__(self, epsilon=0.1, alpha=5, k=5):
|
|
407
446
|
"""
|
|
408
447
|
This will initialize the parameters required for the various smoothing
|
|
409
448
|
techniques, the default values are set to the numbers used in the
|
|
410
449
|
experiments from Chen and Cherry (2014).
|
|
411
450
|
|
|
412
|
-
>>> hypothesis1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', 'ensures',
|
|
413
|
-
... 'that', 'the', 'military', 'always', 'obeys', 'the',
|
|
451
|
+
>>> hypothesis1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', 'ensures',
|
|
452
|
+
... 'that', 'the', 'military', 'always', 'obeys', 'the',
|
|
414
453
|
... 'commands', 'of', 'the', 'party']
|
|
415
|
-
>>> reference1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', 'ensures',
|
|
416
|
-
... 'that', 'the', 'military', 'will', 'forever', 'heed',
|
|
454
|
+
>>> reference1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', 'ensures',
|
|
455
|
+
... 'that', 'the', 'military', 'will', 'forever', 'heed',
|
|
417
456
|
... 'Party', 'commands']
|
|
418
|
-
|
|
457
|
+
|
|
419
458
|
>>> chencherry = SmoothingFunction()
|
|
420
459
|
>>> print (sentence_bleu([reference1], hypothesis1)) # doctest: +ELLIPSIS
|
|
421
460
|
0.4118...
|
|
@@ -432,7 +471,7 @@ class SmoothingFunction:
|
|
|
432
471
|
>>> print (sentence_bleu([reference1], hypothesis1, smoothing_function=chencherry.method5)) # doctest: +ELLIPSIS
|
|
433
472
|
0.4905...
|
|
434
473
|
>>> print (sentence_bleu([reference1], hypothesis1, smoothing_function=chencherry.method6)) # doctest: +ELLIPSIS
|
|
435
|
-
0.
|
|
474
|
+
0.4135...
|
|
436
475
|
>>> print (sentence_bleu([reference1], hypothesis1, smoothing_function=chencherry.method7)) # doctest: +ELLIPSIS
|
|
437
476
|
0.4905...
|
|
438
477
|
|
|
@@ -446,31 +485,50 @@ class SmoothingFunction:
|
|
|
446
485
|
self.epsilon = epsilon
|
|
447
486
|
self.alpha = alpha
|
|
448
487
|
self.k = k
|
|
449
|
-
|
|
488
|
+
|
|
450
489
|
def method0(self, p_n, *args, **kwargs):
|
|
451
|
-
"""
|
|
452
|
-
|
|
453
|
-
|
|
490
|
+
"""
|
|
491
|
+
No smoothing.
|
|
492
|
+
"""
|
|
493
|
+
p_n_new = []
|
|
494
|
+
for i, p_i in enumerate(p_n):
|
|
495
|
+
if p_i.numerator != 0:
|
|
496
|
+
p_n_new.append(p_i)
|
|
497
|
+
else:
|
|
498
|
+
_msg = str("\nThe hypothesis contains 0 counts of {}-gram overlaps.\n"
|
|
499
|
+
"Therefore the BLEU score evaluates to 0, independently of\n"
|
|
500
|
+
"how many N-gram overlaps of lower order it contains.\n"
|
|
501
|
+
"Consider using lower n-gram order or use "
|
|
502
|
+
"SmoothingFunction()").format(i+1)
|
|
503
|
+
warnings.warn(_msg)
|
|
504
|
+
# When numerator==0 where denonminator==0 or !=0, the result
|
|
505
|
+
# for the precision score should be equal to 0 or undefined.
|
|
506
|
+
# Due to BLEU geometric mean computation in logarithm space,
|
|
507
|
+
# we we need to take the return sys.float_info.min such that
|
|
508
|
+
# math.log(sys.float_info.min) returns a 0 precision score.
|
|
509
|
+
p_n_new.append(sys.float_info.min)
|
|
510
|
+
return p_n_new
|
|
511
|
+
|
|
454
512
|
def method1(self, p_n, *args, **kwargs):
|
|
455
|
-
"""
|
|
513
|
+
"""
|
|
456
514
|
Smoothing method 1: Add *epsilon* counts to precision with 0 counts.
|
|
457
|
-
"""
|
|
458
|
-
return [(p_i.numerator + self.epsilon)/ p_i.denominator
|
|
515
|
+
"""
|
|
516
|
+
return [(p_i.numerator + self.epsilon) / p_i.denominator
|
|
459
517
|
if p_i.numerator == 0 else p_i for p_i in p_n]
|
|
460
|
-
|
|
518
|
+
|
|
461
519
|
def method2(self, p_n, *args, **kwargs):
|
|
462
520
|
"""
|
|
463
|
-
Smoothing method 2: Add 1 to both numerator and denominator from
|
|
464
|
-
Chin-Yew Lin and Franz Josef Och (2004) Automatic evaluation of
|
|
465
|
-
machine translation quality using longest common subsequence and
|
|
521
|
+
Smoothing method 2: Add 1 to both numerator and denominator from
|
|
522
|
+
Chin-Yew Lin and Franz Josef Och (2004) Automatic evaluation of
|
|
523
|
+
machine translation quality using longest common subsequence and
|
|
466
524
|
skip-bigram statistics. In ACL04.
|
|
467
525
|
"""
|
|
468
526
|
return [Fraction(p_i.numerator + 1, p_i.denominator + 1, _normalize=False) for p_i in p_n]
|
|
469
|
-
|
|
527
|
+
|
|
470
528
|
def method3(self, p_n, *args, **kwargs):
|
|
471
529
|
"""
|
|
472
|
-
Smoothing method 3: NIST geometric sequence smoothing
|
|
473
|
-
The smoothing is computed by taking 1 / ( 2^k ), instead of 0, for each
|
|
530
|
+
Smoothing method 3: NIST geometric sequence smoothing
|
|
531
|
+
The smoothing is computed by taking 1 / ( 2^k ), instead of 0, for each
|
|
474
532
|
precision score whose matching n-gram count is null.
|
|
475
533
|
k is 1 for the first 'n' value for which the n-gram match count is null/
|
|
476
534
|
For example, if the text contains:
|
|
@@ -482,34 +540,32 @@ class SmoothingFunction:
|
|
|
482
540
|
- n=3 => prec_count = 1/2 (no trigram, taking 'smoothed' value of 1 / ( 2^k ), with k=1)
|
|
483
541
|
- n=4 => prec_count = 1/4 (no fourgram, taking 'smoothed' value of 1 / ( 2^k ), with k=2)
|
|
484
542
|
"""
|
|
485
|
-
incvnt = 1
|
|
543
|
+
incvnt = 1 # From the mteval-v13a.pl, it's referred to as k.
|
|
486
544
|
for i, p_i in enumerate(p_n):
|
|
487
545
|
if p_i.numerator == 0:
|
|
488
|
-
p_n[i] = 1 / (2**incvnt * p_i.denominator)
|
|
489
|
-
incvnt+=1
|
|
546
|
+
p_n[i] = 1 / (2 ** incvnt * p_i.denominator)
|
|
547
|
+
incvnt += 1
|
|
490
548
|
return p_n
|
|
491
|
-
|
|
492
|
-
def method4(self, p_n, references, hypothesis, hyp_len):
|
|
549
|
+
|
|
550
|
+
def method4(self, p_n, references, hypothesis, hyp_len, *args, **kwargs):
|
|
493
551
|
"""
|
|
494
|
-
Smoothing method 4:
|
|
495
|
-
Shorter translations may have inflated precision values due to having
|
|
552
|
+
Smoothing method 4:
|
|
553
|
+
Shorter translations may have inflated precision values due to having
|
|
496
554
|
smaller denominators; therefore, we give them proportionally
|
|
497
|
-
smaller smoothed counts. Instead of scaling to 1/(2^k), Chen and Cherry
|
|
555
|
+
smaller smoothed counts. Instead of scaling to 1/(2^k), Chen and Cherry
|
|
498
556
|
suggests dividing by 1/ln(len(T)), where T is the length of the translation.
|
|
499
557
|
"""
|
|
500
|
-
incvnt = 1
|
|
501
558
|
for i, p_i in enumerate(p_n):
|
|
502
559
|
if p_i.numerator == 0 and hyp_len != 0:
|
|
503
|
-
|
|
504
|
-
incvnt
|
|
560
|
+
incvnt = i + 1 * self.k / math.log(hyp_len) # Note that this K is different from the K from NIST.
|
|
561
|
+
p_n[i] = 1 / incvnt
|
|
505
562
|
return p_n
|
|
506
563
|
|
|
507
|
-
|
|
508
|
-
def method5(self, p_n, references, hypothesis, hyp_len):
|
|
564
|
+
def method5(self, p_n, references, hypothesis, hyp_len, *args, **kwargs):
|
|
509
565
|
"""
|
|
510
566
|
Smoothing method 5:
|
|
511
|
-
The matched counts for similar values of n should be similar. To a
|
|
512
|
-
calculate the n-gram matched count, it averages the n−1, n and n+1 gram
|
|
567
|
+
The matched counts for similar values of n should be similar. To a
|
|
568
|
+
calculate the n-gram matched count, it averages the n−1, n and n+1 gram
|
|
513
569
|
matched counts.
|
|
514
570
|
"""
|
|
515
571
|
m = {}
|
|
@@ -517,32 +573,41 @@ class SmoothingFunction:
|
|
|
517
573
|
p_n_plus1 = p_n + [modified_precision(references, hypothesis, 5)]
|
|
518
574
|
m[-1] = p_n[0] + 1
|
|
519
575
|
for i, p_i in enumerate(p_n):
|
|
520
|
-
p_n[i] = (m[i-1] + p_i + p_n_plus1[i+1]) / 3
|
|
521
|
-
m[i] = p_n[i]
|
|
576
|
+
p_n[i] = (m[i - 1] + p_i + p_n_plus1[i + 1]) / 3
|
|
577
|
+
m[i] = p_n[i]
|
|
522
578
|
return p_n
|
|
523
|
-
|
|
524
|
-
def method6(self, p_n, references, hypothesis, hyp_len):
|
|
579
|
+
|
|
580
|
+
def method6(self, p_n, references, hypothesis, hyp_len, *args, **kwargs):
|
|
525
581
|
"""
|
|
526
582
|
Smoothing method 6:
|
|
527
|
-
Interpolates the maximum likelihood estimate of the precision *p_n* with
|
|
528
|
-
a prior estimate *pi0*. The prior is estimated by assuming that the ratio
|
|
529
|
-
between pn and pn−1 will be the same as that between pn−1 and pn−2
|
|
583
|
+
Interpolates the maximum likelihood estimate of the precision *p_n* with
|
|
584
|
+
a prior estimate *pi0*. The prior is estimated by assuming that the ratio
|
|
585
|
+
between pn and pn−1 will be the same as that between pn−1 and pn−2; from
|
|
586
|
+
Gao and He (2013) Training MRF-Based Phrase Translation Models using
|
|
587
|
+
Gradient Ascent. In NAACL.
|
|
530
588
|
"""
|
|
589
|
+
# This smoothing only works when p_1 and p_2 is non-zero.
|
|
590
|
+
# Raise an error with an appropriate message when the input is too short
|
|
591
|
+
# to use this smoothing technique.
|
|
592
|
+
assert p_n[2], "This smoothing method requires non-zero precision for bigrams."
|
|
531
593
|
for i, p_i in enumerate(p_n):
|
|
532
|
-
if i in [1
|
|
594
|
+
if i in [0, 1]: # Skips the first 2 orders of ngrams.
|
|
533
595
|
continue
|
|
534
596
|
else:
|
|
535
|
-
pi0 = 0 if p_n[i-2] == 0 else p_n[i-1]**2 / p_n[i-2]
|
|
597
|
+
pi0 = 0 if p_n[i - 2] == 0 else p_n[i - 1] ** 2 / p_n[i - 2]
|
|
598
|
+
# No. of ngrams in translation that matches the reference.
|
|
599
|
+
m = p_i.numerator
|
|
536
600
|
# No. of ngrams in translation.
|
|
537
|
-
l = sum(1 for _ in ngrams(hypothesis, i+1))
|
|
538
|
-
|
|
601
|
+
l = sum(1 for _ in ngrams(hypothesis, i + 1))
|
|
602
|
+
# Calculates the interpolated precision.
|
|
603
|
+
p_n[i] = (m + self.alpha * pi0) / (l + self.alpha)
|
|
539
604
|
return p_n
|
|
540
|
-
|
|
541
|
-
def method7(self, p_n, references, hypothesis, hyp_len):
|
|
605
|
+
|
|
606
|
+
def method7(self, p_n, references, hypothesis, hyp_len, *args, **kwargs):
|
|
542
607
|
"""
|
|
543
608
|
Smoothing method 6:
|
|
544
|
-
Interpolates the maximum likelihood estimate of the precision *p_n* with
|
|
545
|
-
a prior estimate *pi0*. The prior is estimated by assuming that the ratio
|
|
609
|
+
Interpolates the maximum likelihood estimate of the precision *p_n* with
|
|
610
|
+
a prior estimate *pi0*. The prior is estimated by assuming that the ratio
|
|
546
611
|
between pn and pn−1 will be the same as that between pn−1 and pn−2.
|
|
547
612
|
"""
|
|
548
613
|
p_n = self.method4(p_n, references, hypothesis, hyp_len)
|