Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. from collections import Counter
  2.  
  3.  
  4. def F1(precision, recall):
  5. denominator = precision + recall
  6. if not denominator:
  7. return 0
  8. return 2 * precision * recall / denominator
  9.  
  10.  
  11. def tokens2bigrams(tokens):
  12. num_tokens = len(tokens)
  13. bigrams = []
  14. for i in range(num_tokens - 1):
  15. bigrams.append("{} {}".format(tokens[i], tokens[i + 1]))
  16. return Counter(bigrams)
  17.  
  18.  
  19. def evaluate_one():
  20. 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()
  21. 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()
  22. set_a = set(answer)
  23. set_p = set(predictions)
  24. intersection = len(set_a & set_p)
  25. precision = intersection / len(set_p)
  26. recall = intersection / len(set_a)
  27. f1 = F1(precision, recall)
  28. tokens_a = answer
  29. tokens_p = predictions
  30. unigrams_a = Counter(tokens_a)
  31. unigrams_p = Counter(tokens_p)
  32. bigrams_a = tokens2bigrams(tokens_a)
  33. bigrams_p = tokens2bigrams(tokens_p)
  34. rouge1 = sum((unigrams_a & unigrams_p).values()) / sum(unigrams_a.values())
  35. rouge2 = sum((bigrams_a & bigrams_p).values()) / sum(bigrams_a.values())
  36. return f1, rouge1, rouge2
  37.  
  38.  
  39. print(evaluate_one())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement