Skip to content

Instantly share code, notes, and snippets.

@theeluwin
Created July 26, 2017 10:28
Show Gist options
  • Select an option

  • Save theeluwin/d0a6f5a832801c1f739bc2a2cd0f219c to your computer and use it in GitHub Desktop.

Select an option

Save theeluwin/d0a6f5a832801c1f739bc2a2cd0f219c to your computer and use it in GitHub Desktop.
from collections import Counter
def F1(precision, recall):
denominator = precision + recall
if not denominator:
return 0
return 2 * precision * recall / denominator
def tokens2bigrams(tokens):
num_tokens = len(tokens)
bigrams = []
for i in range(num_tokens - 1):
bigrams.append("{} {}".format(tokens[i], tokens[i + 1]))
return Counter(bigrams)
def evaluate_one():
predictions = 'Jenson Button waves to the crowd ahead of the Bahrain Grand Prix which he failed to start Perhaps a career in the media beckons Lewis Hamilton has out-qualified and finished ahead of Nico Rosberg at every race this season Nico Rosberg has been left in the shade by Lewis Hamilton who celebrates winning his third race of the year Kimi Raikkonen secured a record seventh podium finish in Bahrain following his superb late salvo, although the Ferrari driver has never won in the Gulf Kingdom Formula One supremo Bernie Ecclestone speaks to Nico Rosberg ahead of the Bahrain Grand Prix'.lower().split()
answer = 'Button denied 100th race start for McLaren after ERS failure. Button then spent much of the Bahrain Grand Prix on Twitter delivering his verdict on the action as it unfolded Lewis Hamilton has out-qualified and finished ahead of Mercedes team-mate Nico Rosberg at every race this season Bernie Ecclestone confirms F1 will make its bow in Azerbaijan next season'.lower().split()
set_a = set(answer)
set_p = set(predictions)
intersection = len(set_a & set_p)
precision = intersection / len(set_p)
recall = intersection / len(set_a)
f1 = F1(precision, recall)
tokens_a = answer
tokens_p = predictions
unigrams_a = Counter(tokens_a)
unigrams_p = Counter(tokens_p)
bigrams_a = tokens2bigrams(tokens_a)
bigrams_p = tokens2bigrams(tokens_p)
rouge1 = sum((unigrams_a & unigrams_p).values()) / sum(unigrams_a.values())
rouge2 = sum((bigrams_a & bigrams_p).values()) / sum(bigrams_a.values())
return f1, rouge1, rouge2
print(evaluate_one())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment