Skip to content

Commit

Permalink
Fix Python 3 syntax errors
Browse files Browse the repository at this point in the history
  • Loading branch information
salaniz committed Feb 6, 2018
1 parent d8af937 commit 5c85f15
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 45 deletions.
4 changes: 2 additions & 2 deletions bleu/bleu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
#
#
# File Name : bleu.py
#
# Description : Wrapper for BLEU scorer.
Expand All @@ -8,7 +8,7 @@
# Last Modified : Thu 19 Mar 2015 09:13:28 PM PDT
# Authors : Hao Fang <[email protected]> and Tsung-Yi Lin <[email protected]>

from bleu_scorer import BleuScorer
from .bleu_scorer import BleuScorer


class Bleu:
Expand Down
33 changes: 17 additions & 16 deletions bleu/bleu_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# reserved. Do not redistribute without permission from the
# author. Not for commercial use.

# Modified by:
# Modified by:
# Hao Fang <[email protected]>
# Tsung-Yi Lin <[email protected]>

Expand Down Expand Up @@ -52,21 +52,22 @@ def cook_refs(refs, eff=None, n=4): ## lhuang: oracle will call with "average"
reflen = float(sum(reflen))/len(reflen)

## lhuang: N.B.: leave reflen computaiton to the very end!!

## lhuang: N.B.: in case of "closest", keep a list of reflens!! (bad design)

return (reflen, maxcounts)

def cook_test(test, (reflen, refmaxcounts), eff=None, n=4):
def cook_test(test, refs, eff=None, n=4):
'''Takes a test sentence and returns an object that
encapsulates everything that BLEU needs to know about it.'''

reflen, refmaxcounts = refs
testlen, counts = precook(test, n, True)

result = {}

# Calculate effective reference sentence length.

if eff == "closest":
result["reflen"] = min((abs(l-testlen), l) for l in reflen)[1]
else: ## i.e., "average" or "shortest" or None
Expand Down Expand Up @@ -108,7 +109,7 @@ def __init__(self, test=None, refs=None, n=4, special_reflen=None):

def cook_append(self, test, refs):
'''called by constructor and __iadd__ to avoid creating new instances.'''

if refs is not None:
self.crefs.append(cook_refs(refs))
if test is not None:
Expand Down Expand Up @@ -136,7 +137,7 @@ def reflen(self, option=None):

def testlen(self, option=None):
self.compute_score(option=option)
return self._testlen
return self._testlen

def retest(self, new_test):
if type(new_test) is str:
Expand All @@ -151,7 +152,7 @@ def retest(self, new_test):

def rescore(self, new_test):
''' replace test(s) with new test(s), and returns the new score.'''

return self.retest(new_test).compute_score()

def size(self):
Expand All @@ -170,7 +171,7 @@ def __iadd__(self, other):
self.crefs.extend(other.crefs)
self._score = None ## need to recompute

return self
return self

def compatible(self, other):
return isinstance(other, BleuScorer) and self.n == other.n
Expand All @@ -179,7 +180,7 @@ def single_reflen(self, option="average"):
return self._single_reflen(self.crefs[0][0], option)

def _single_reflen(self, reflens, option=None, testlen=None):

if option == "shortest":
reflen = min(reflens)
elif option == "average":
Expand All @@ -194,7 +195,7 @@ def _single_reflen(self, reflens, option=None, testlen=None):
def recompute_score(self, option=None, verbose=0):
self._score = None
return self.compute_score(option, verbose)

def compute_score(self, option=None, verbose=0):
n = self.n
small = 1e-9
Expand All @@ -212,7 +213,7 @@ def compute_score(self, option=None, verbose=0):
totalcomps = {'testlen':0, 'reflen':0, 'guess':[0]*n, 'correct':[0]*n}

# for each sentence
for comps in self.ctest:
for comps in self.ctest:
testlen = comps['testlen']
self._testlen += testlen

Expand All @@ -222,7 +223,7 @@ def compute_score(self, option=None, verbose=0):
reflen = self.special_reflen

self._reflen += reflen

for key in ['guess','correct']:
for k in xrange(n):
totalcomps[key][k] += comps[key][k]
Expand All @@ -231,15 +232,15 @@ def compute_score(self, option=None, verbose=0):
bleu = 1.
for k in xrange(n):
bleu *= (float(comps['correct'][k]) + tiny) \
/(float(comps['guess'][k]) + small)
/(float(comps['guess'][k]) + small)
bleu_list[k].append(bleu ** (1./(k+1)))
ratio = (testlen + tiny) / (reflen + small) ## N.B.: avoid zero division
if ratio < 1:
for k in xrange(n):
bleu_list[k][-1] *= math.exp(1 - 1/ratio)

if verbose > 1:
print comps, reflen
print(comps, reflen)

totalcomps['reflen'] = self._reflen
totalcomps['testlen'] = self._testlen
Expand All @@ -256,8 +257,8 @@ def compute_score(self, option=None, verbose=0):
bleus[k] *= math.exp(1 - 1/ratio)

if verbose > 0:
print totalcomps
print "ratio:", ratio
print(totalcomps)
print("ratio:", ratio)

self._score = bleus
return self._score, bleu_list
10 changes: 5 additions & 5 deletions cider/cider.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Filename: cider.py
#
# Description: Describes the class to compute the CIDEr (Consensus-Based Image Description Evaluation) Metric
# Description: Describes the class to compute the CIDEr (Consensus-Based Image Description Evaluation) Metric
# by Vedantam, Zitnick, and Parikh (http://arxiv.org/abs/1411.5726)
#
# Creation Date: Sun Feb 8 14:16:54 2015
#
# Authors: Ramakrishna Vedantam <[email protected]> and Tsung-Yi Lin <[email protected]>

from cider_scorer import CiderScorer
from .cider_scorer import CiderScorer
import pdb

class Cider:
"""
Main Class to compute the CIDEr metric
Main Class to compute the CIDEr metric
"""
def __init__(self, test=None, refs=None, n=4, sigma=6.0):
Expand All @@ -26,7 +26,7 @@ def compute_score(self, gts, res):
Main function to compute CIDEr score
:param hypo_for_image (dict) : dictionary with key <image> and value <tokenized hypothesis / candidate sentence>
ref_for_image (dict) : dictionary with key <image> and value <tokenized reference sentence>
:return: cider (float) : computed CIDEr score for the corpus
:return: cider (float) : computed CIDEr score for the corpus
"""

assert(gts.keys() == res.keys())
Expand All @@ -51,4 +51,4 @@ def compute_score(self, gts, res):
return score, scores

def method(self):
return "CIDEr"
return "CIDEr"
22 changes: 11 additions & 11 deletions eval.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
__author__ = 'tylin'
from tokenizer.ptbtokenizer import PTBTokenizer
from bleu.bleu import Bleu
from meteor.meteor import Meteor
from rouge.rouge import Rouge
from cider.cider import Cider
from .tokenizer.ptbtokenizer import PTBTokenizer
from .bleu.bleu import Bleu
from .meteor.meteor import Meteor
from .rouge.rouge import Rouge
from .cider.cider import Cider

class COCOEvalCap:
def __init__(self, coco, cocoRes):
Expand All @@ -26,15 +26,15 @@ def evaluate(self):
# =================================================
# Set up scorers
# =================================================
print 'tokenization...'
print('tokenization...')
tokenizer = PTBTokenizer()
gts = tokenizer.tokenize(gts)
res = tokenizer.tokenize(res)

# =================================================
# Set up scorers
# =================================================
print 'setting up scorers...'
print('setting up scorers...')
scorers = [
(Bleu(4), ["Bleu_1", "Bleu_2", "Bleu_3", "Bleu_4"]),
(Meteor(),"METEOR"),
Expand All @@ -46,17 +46,17 @@ def evaluate(self):
# Compute scores
# =================================================
for scorer, method in scorers:
print 'computing %s score...'%(scorer.method())
print('computing %s score...'%(scorer.method()))
score, scores = scorer.compute_score(gts, res)
if type(method) == list:
for sc, scs, m in zip(score, scores, method):
self.setEval(sc, m)
self.setImgToEvalImgs(scs, gts.keys(), m)
print "%s: %0.3f"%(m, sc)
print("%s: %0.3f"%(m, sc))
else:
self.setEval(score, method)
self.setImgToEvalImgs(scores, gts.keys(), method)
print "%s: %0.3f"%(method, score)
print("%s: %0.3f"%(method, score))
self.setEvalImgs()

def setEval(self, score, method):
Expand All @@ -70,4 +70,4 @@ def setImgToEvalImgs(self, scores, imgIds, method):
self.imgToEval[imgId][method] = score

def setEvalImgs(self):
self.evalImgs = [eval for imgId, eval in self.imgToEval.items()]
self.evalImgs = [eval for imgId, eval in self.imgToEval.items()]
6 changes: 3 additions & 3 deletions meteor/meteor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python

# Python wrapper for METEOR implementation, by Xinlei Chen
# Acknowledge Michael Denkowski for the generous discussion and help
# Acknowledge Michael Denkowski for the generous discussion and help

import os
import sys
Expand Down Expand Up @@ -63,15 +63,15 @@ def _score(self, hypothesis_str, reference_list):
self.meteor_p.stdin.write('{}\n'.format(score_line))
stats = self.meteor_p.stdout.readline().strip()
eval_line = 'EVAL ||| {}'.format(stats)
# EVAL ||| stats
# EVAL ||| stats
self.meteor_p.stdin.write('{}\n'.format(eval_line))
score = float(self.meteor_p.stdout.readline().strip())
# bug fix: there are two values returned by the jar file, one average, and one all, so do it twice
# thanks for Andrej for pointing this out
score = float(self.meteor_p.stdout.readline().strip())
self.lock.release()
return score

def __del__(self):
self.lock.acquire()
self.meteor_p.stdin.close()
Expand Down
12 changes: 6 additions & 6 deletions rouge/rouge.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
#
#
# File Name : rouge.py
#
# Description : Computes ROUGE-L metric as described by Lin and Hovey (2004)
Expand Down Expand Up @@ -49,14 +49,14 @@ def calc_score(self, candidate, refs):
:param refs: list of str : COCO reference sentences for the particular image to be evaluated
:returns score: int (ROUGE-L score for the candidate evaluated against references)
"""
assert(len(candidate)==1)
assert(len(refs)>0)
assert(len(candidate)==1)
assert(len(refs)>0)
prec = []
rec = []

# split into tokens
token_c = candidate[0].split(" ")

for reference in refs:
# split into tokens
token_r = reference.split(" ")
Expand All @@ -77,8 +77,8 @@ def calc_score(self, candidate, refs):
def compute_score(self, gts, res):
"""
Computes Rouge-L score given a set of reference and candidate sentences for the dataset
Invoked by evaluate_captions.py
:param hypo_for_image: dict : candidate / test sentences with "image name" key and "tokenized sentences" as values
Invoked by evaluate_captions.py
:param hypo_for_image: dict : candidate / test sentences with "image name" key and "tokenized sentences" as values
:param ref_for_image: dict : reference MS-COCO sentences with "image name" key and "tokenized sentences" as values
:returns: average_score: float (mean ROUGE-L score computed by averaging scores for all the images)
"""
Expand Down
4 changes: 2 additions & 2 deletions tokenizer/ptbtokenizer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
#
#
# File Name : ptbtokenizer.py
#
# Description : Do the PTB Tokenization and remove punctuations.
Expand All @@ -19,7 +19,7 @@

# punctuations to be removed from the sentences
PUNCTUATIONS = ["''", "'", "``", "`", "-LRB-", "-RRB-", "-LCB-", "-RCB-", \
".", "?", "!", ",", ":", "-", "--", "...", ";"]
".", "?", "!", ",", ":", "-", "--", "...", ";"]

class PTBTokenizer:
"""Python wrapper of Stanford PTBTokenizer"""
Expand Down

0 comments on commit 5c85f15

Please sign in to comment.