#!/usr/bin/env python # -*- coding: utf-8 -*- u"""Official Evaluator for WikiTableQuestions Dataset There are 3 value types 1. String (unicode) 2. Number (float) 3. Date (a struct with 3 fields: year, month, and date) Some fields (but not all) can be left unspecified. However, if only the year is specified, the date is automatically converted into a number. Target denotation = a set of items - Each item T is a raw unicode string from Mechanical Turk - If T can be converted to a number or date (via Stanford CoreNLP), the converted value (number T_N or date T_D) is precomputed Predicted denotation = a set of items - Each item P is a string, a number, or a date - If P is read from a text file, assume the following - A string that can be converted into a number (float) is converted into a number - A string of the form "yyyy-mm-dd" is converted into a date. Unspecified fields can be marked as "xx". For example, "xx-01-02" represents the date January 2nd of an unknown year. - Otherwise, it is kept as a string The predicted denotation is correct if 1. The sizes of the target denotation and the predicted denotation are equal 2. Each item in the target denotation matches an item in the predicted denotation A target item T matches a predicted item P if one of the following is true: 1. normalize(raw string of T) and normalize(string form of P) are identical. The normalize method performs the following normalizations on strings: - Remove diacritics (é → e) - Convert smart quotes (‘’´`“”) and dashes (‐‑‒–—−) into ASCII ones - Remove citations (trailing •♦†‡*#+ or [...]) - Remove details in parenthesis (trailing (...)) - Remove outermost quotation marks - Remove trailing period (.) - Convert to lowercase - Collapse multiple whitespaces and strip outermost whitespaces 2. T can be interpreted as a number T_N, P is a number, and P = T_N 3. T can be interpreted as a date T_D, P is a date, and P = T_D (exact match on all fields; e.g., xx-01-12 and 1990-01-12 do not match) """ __version__ = '1.0.2' import sys, os, re, argparse import unicodedata from codecs import open from math import isnan, isinf from abc import ABCMeta, abstractmethod ################ String Normalization ################ def normalize(x): if not isinstance(x, str): x = x.decode('utf8', errors='ignore') # Remove diacritics x = ''.join(c for c in unicodedata.normalize('NFKD', x) if unicodedata.category(c) != 'Mn') # Normalize quotes and dashes x = re.sub(r"[‘’´`]", "'", x) x = re.sub(r"[“”]", "\"", x) x = re.sub(r"[‐‑‒–—−]", "-", x) while True: old_x = x # Remove citations x = re.sub(r"((? backslash + n vertical bar (0x7C) -> backslash + p backslash (0x5C) -> backslash + backslash Args: x (str or unicode) Returns: a unicode """ return x.replace(r'\n', '\n').replace(r'\p', '|').replace('\\\\', '\\') def tsv_unescape_list(x): """Unescape a list in the TSV file. List items are joined with vertical bars (0x5C) Args: x (str or unicode) Returns: a list of unicodes """ return [tsv_unescape(y) for y in x.split('|')] def main(): pred_answer = ['ABC'] gold_answer = ['Abc'] pred_answer_val = to_value_list(pred_answer) gold_answer_val = to_value_list(gold_answer) correct = check_denotation(pred_answer_val, gold_answer_val) print(pred_answer_val) print(gold_answer_val) print(correct) if __name__ == '__main__': main()